blob: 9c15539ee92e80ec75c6187b3c843e20602accd4 [file] [log] [blame]
[email protected]15b12242012-01-26 19:28:471// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef PPAPI_PROXY_PPB_MESSAGE_LOOP_PROXY_H_
6#define PPAPI_PROXY_PPB_MESSAGE_LOOP_PROXY_H_
7
avie029c4132015-12-23 06:45:228#include <stdint.h>
9
dchengced92242016-04-07 00:00:1210#include <memory>
11
[email protected]6de743a2012-08-30 20:03:2212#include "base/bind.h"
avie029c4132015-12-23 06:45:2213#include "base/macros.h"
[email protected]6de743a2012-08-30 20:03:2214#include "base/memory/ref_counted.h"
[email protected]a76295972013-07-18 00:42:3215#include "base/message_loop/message_loop.h"
fdoray2df4a9e2016-07-18 23:47:1616#include "base/run_loop.h"
skyostil23490d42015-06-12 12:54:2617#include "base/single_thread_task_runner.h"
[email protected]15b12242012-01-26 19:28:4718#include "ppapi/proxy/interface_proxy.h"
[email protected]7ef6b79b2013-01-17 02:38:2419#include "ppapi/proxy/ppapi_proxy_export.h"
[email protected]77c34172012-11-08 18:55:1620#include "ppapi/shared_impl/ppb_message_loop_shared.h"
[email protected]6de743a2012-08-30 20:03:2221#include "ppapi/thunk/ppb_message_loop_api.h"
[email protected]15b12242012-01-26 19:28:4722
[email protected]d4ab94712012-11-15 21:01:2323struct PPB_MessageLoop_1_0;
[email protected]15b12242012-01-26 19:28:4724
25namespace ppapi {
26namespace proxy {
27
[email protected]7ef6b79b2013-01-17 02:38:2428class PPAPI_PROXY_EXPORT MessageLoopResource : public MessageLoopShared {
[email protected]6de743a2012-08-30 20:03:2229 public:
30 explicit MessageLoopResource(PP_Instance instance);
31 // Construct the one MessageLoopResource for the main thread. This must be
32 // invoked on the main thread.
[email protected]77c34172012-11-08 18:55:1633 explicit MessageLoopResource(ForMainThread);
nicke4784432015-04-23 14:01:4834 ~MessageLoopResource() override;
[email protected]6de743a2012-08-30 20:03:2235
36 // Resource overrides.
nicke4784432015-04-23 14:01:4837 thunk::PPB_MessageLoop_API* AsPPB_MessageLoop_API() override;
[email protected]6de743a2012-08-30 20:03:2238
39 // PPB_MessageLoop_API implementation.
nicke4784432015-04-23 14:01:4840 int32_t AttachToCurrentThread() override;
41 int32_t Run() override;
42 int32_t PostWork(PP_CompletionCallback callback, int64_t delay_ms) override;
43 int32_t PostQuit(PP_Bool should_destroy) override;
[email protected]6de743a2012-08-30 20:03:2244
[email protected]77c34172012-11-08 18:55:1645 static MessageLoopResource* GetCurrent();
[email protected]6de743a2012-08-30 20:03:2246 void DetachFromThread();
47 bool is_main_thread_loop() const {
48 return is_main_thread_loop_;
49 }
50
skyostil23490d42015-06-12 12:54:2651 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner() {
52 return task_runner_;
[email protected]e87640bd2014-06-18 16:44:0053 }
54
dmichael44b967542014-09-24 15:41:1455 void set_currently_handling_blocking_message(bool handling_blocking_message) {
56 currently_handling_blocking_message_ = handling_blocking_message;
57 }
58
[email protected]6de743a2012-08-30 20:03:2259 private:
60 struct TaskInfo {
61 tracked_objects::Location from_here;
62 base::Closure closure;
avie029c4132015-12-23 06:45:2263 int64_t delay_ms;
[email protected]6de743a2012-08-30 20:03:2264 };
65
66 // Returns true if the object is associated with the current thread.
67 bool IsCurrent() const;
68
dmichael44b967542014-09-24 15:41:1469 // MessageLoopShared implementation.
70 //
[email protected]6de743a2012-08-30 20:03:2271 // Handles posting to the message loop if there is one, or the pending queue
72 // if there isn't.
73 // NOTE: The given closure will be run *WITHOUT* acquiring the Proxy lock.
74 // This only makes sense for user code and completely thread-safe
75 // proxy operations (e.g., MessageLoop::QuitClosure).
nicke4784432015-04-23 14:01:4876 void PostClosure(const tracked_objects::Location& from_here,
77 const base::Closure& closure,
avie029c4132015-12-23 06:45:2278 int64_t delay_ms) override;
skyostil23490d42015-06-12 12:54:2679 base::SingleThreadTaskRunner* GetTaskRunner() override;
nicke4784432015-04-23 14:01:4880 bool CurrentlyHandlingBlockingMessage() override;
[email protected]511c58e2013-12-12 12:25:3381
fdoray2df4a9e2016-07-18 23:47:1682 // Quits |run_loop_|. Must be called from the thread that runs the RunLoop.
83 void QuitRunLoopWhenIdle();
84
[email protected]6de743a2012-08-30 20:03:2285 // TLS destructor function.
86 static void ReleaseMessageLoop(void* value);
87
88 // Created when we attach to the current thread, since MessageLoop assumes
89 // that it's created on the thread it will run on. NULL for the main thread
90 // loop, since that's owned by somebody else. This is needed for Run and Quit.
skyostil23490d42015-06-12 12:54:2691 // Any time we post tasks, we should post them using task_runner_.
dchengced92242016-04-07 00:00:1292 std::unique_ptr<base::MessageLoop> loop_;
skyostil23490d42015-06-12 12:54:2693 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
[email protected]6de743a2012-08-30 20:03:2294
fdoray2df4a9e2016-07-18 23:47:1695 // RunLoop currently on the stack.
96 base::RunLoop* run_loop_ = nullptr;
97
[email protected]6de743a2012-08-30 20:03:2298 // Number of invocations of Run currently on the stack.
99 int nested_invocations_;
100
101 // Set to true when the message loop is destroyed to prevent forther
102 // posting of work.
103 bool destroyed_;
104
105 // Set to true if all message loop invocations should exit and that the
106 // loop should be destroyed once it reaches the outermost Run invocation.
107 bool should_destroy_;
108
109 bool is_main_thread_loop_;
110
dmichael44b967542014-09-24 15:41:14111 bool currently_handling_blocking_message_;
112
[email protected]6de743a2012-08-30 20:03:22113 // Since we allow tasks to be posted before the message loop is actually
114 // created (when it's associated with a thread), we keep tasks posted here
115 // until that happens. Once the loop_ is created, this is unused.
116 std::vector<TaskInfo> pending_tasks_;
117
118 DISALLOW_COPY_AND_ASSIGN(MessageLoopResource);
119};
120
[email protected]15b12242012-01-26 19:28:47121class PPB_MessageLoop_Proxy : public InterfaceProxy {
122 public:
[email protected]6de743a2012-08-30 20:03:22123 explicit PPB_MessageLoop_Proxy(Dispatcher* dispatcher);
Lei Zhang94f57fe32017-08-30 23:58:24124 ~PPB_MessageLoop_Proxy() override;
[email protected]15b12242012-01-26 19:28:47125
[email protected]d4ab94712012-11-15 21:01:23126 static const PPB_MessageLoop_1_0* GetInterface();
[email protected]15b12242012-01-26 19:28:47127
128 private:
129 DISALLOW_COPY_AND_ASSIGN(PPB_MessageLoop_Proxy);
130};
131
132} // namespace proxy
133} // namespace ppapi
134
135#endif // PPAPI_PROXY_PPB_MESSAGE_LOOP_PROXY_H_