[Android] Offer all TouchMoves to Javascript if TouchStart is consumed
[chromium-blink-merge.git] / ppapi / proxy / ppb_message_loop_proxy.cc
blob7e2cdfa65593cbe51b7ba28a146fa84400c6c7fd
1 // 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.
5 #include "ppapi/proxy/ppb_message_loop_proxy.h"
7 #include <vector>
9 #include "base/bind.h"
10 #include "base/compiler_specific.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/message_loop/message_loop_proxy.h"
13 #include "ppapi/c/pp_errors.h"
14 #include "ppapi/c/ppb_message_loop.h"
15 #include "ppapi/proxy/plugin_dispatcher.h"
16 #include "ppapi/proxy/plugin_globals.h"
17 #include "ppapi/shared_impl/proxy_lock.h"
18 #include "ppapi/thunk/enter.h"
20 using ppapi::thunk::PPB_MessageLoop_API;
22 namespace ppapi {
23 namespace proxy {
25 namespace {
26 typedef thunk::EnterResource<PPB_MessageLoop_API> EnterMessageLoop;
29 MessageLoopResource::MessageLoopResource(PP_Instance instance)
30 : MessageLoopShared(instance),
31 nested_invocations_(0),
32 destroyed_(false),
33 should_destroy_(false),
34 is_main_thread_loop_(false) {
37 MessageLoopResource::MessageLoopResource(ForMainThread for_main_thread)
38 : MessageLoopShared(for_main_thread),
39 nested_invocations_(0),
40 destroyed_(false),
41 should_destroy_(false),
42 is_main_thread_loop_(true) {
43 // We attach the main thread immediately. We can't use AttachToCurrentThread,
44 // because the MessageLoop already exists.
46 // This must be called only once, so the slot must be empty.
47 CHECK(!PluginGlobals::Get()->msg_loop_slot());
48 // We don't add a reference for TLS here, so we don't release it. Instead,
49 // this loop is owned by PluginGlobals. Contrast with AttachToCurrentThread
50 // where we register ReleaseMessageLoop with TLS and call AddRef.
51 base::ThreadLocalStorage::Slot* slot = new base::ThreadLocalStorage::Slot();
52 PluginGlobals::Get()->set_msg_loop_slot(slot);
54 slot->Set(this);
56 loop_proxy_ = base::MessageLoopProxy::current();
60 MessageLoopResource::~MessageLoopResource() {
63 PPB_MessageLoop_API* MessageLoopResource::AsPPB_MessageLoop_API() {
64 return this;
67 int32_t MessageLoopResource::AttachToCurrentThread() {
68 if (is_main_thread_loop_)
69 return PP_ERROR_INPROGRESS;
71 PluginGlobals* globals = PluginGlobals::Get();
73 base::ThreadLocalStorage::Slot* slot = globals->msg_loop_slot();
74 if (!slot) {
75 slot = new base::ThreadLocalStorage::Slot(&ReleaseMessageLoop);
76 globals->set_msg_loop_slot(slot);
77 } else {
78 if (slot->Get())
79 return PP_ERROR_INPROGRESS;
81 // TODO(dmichael) check that the current thread can support a message loop.
83 // Take a ref to the MessageLoop on behalf of the TLS. Note that this is an
84 // internal ref and not a plugin ref so the plugin can't accidentally
85 // release it. This is released by ReleaseMessageLoop().
86 AddRef();
87 slot->Set(this);
89 loop_.reset(new base::MessageLoop(base::MessageLoop::TYPE_DEFAULT));
90 loop_proxy_ = base::MessageLoopProxy::current();
92 // Post all pending work to the message loop.
93 for (size_t i = 0; i < pending_tasks_.size(); i++) {
94 const TaskInfo& info = pending_tasks_[i];
95 PostClosure(info.from_here, info.closure, info.delay_ms);
97 pending_tasks_.clear();
99 return PP_OK;
102 int32_t MessageLoopResource::Run() {
103 if (!IsCurrent())
104 return PP_ERROR_WRONG_THREAD;
105 if (is_main_thread_loop_)
106 return PP_ERROR_INPROGRESS;
108 nested_invocations_++;
109 CallWhileUnlocked(
110 base::Bind(&base::MessageLoop::Run, base::Unretained(loop_.get())));
111 nested_invocations_--;
113 if (should_destroy_ && nested_invocations_ == 0) {
114 loop_proxy_ = NULL;
115 loop_.reset();
116 destroyed_ = true;
118 return PP_OK;
121 int32_t MessageLoopResource::PostWork(PP_CompletionCallback callback,
122 int64_t delay_ms) {
123 if (!callback.func)
124 return PP_ERROR_BADARGUMENT;
125 if (destroyed_)
126 return PP_ERROR_FAILED;
127 PostClosure(FROM_HERE,
128 base::Bind(callback.func, callback.user_data,
129 static_cast<int32_t>(PP_OK)),
130 delay_ms);
131 return PP_OK;
134 int32_t MessageLoopResource::PostQuit(PP_Bool should_destroy) {
135 if (is_main_thread_loop_)
136 return PP_ERROR_WRONG_THREAD;
138 if (PP_ToBool(should_destroy))
139 should_destroy_ = true;
141 if (IsCurrent() && nested_invocations_ > 0)
142 loop_->Quit();
143 else
144 PostClosure(FROM_HERE, base::MessageLoop::QuitClosure(), 0);
145 return PP_OK;
148 // static
149 MessageLoopResource* MessageLoopResource::GetCurrent() {
150 PluginGlobals* globals = PluginGlobals::Get();
151 if (!globals->msg_loop_slot())
152 return NULL;
153 return reinterpret_cast<MessageLoopResource*>(
154 globals->msg_loop_slot()->Get());
157 void MessageLoopResource::DetachFromThread() {
158 // Note that the message loop must be destroyed on the thread it was created
159 // on.
160 loop_proxy_ = NULL;
161 loop_.reset();
163 // Cancel out the AddRef in AttachToCurrentThread().
164 Release();
165 // DANGER: may delete this.
168 bool MessageLoopResource::IsCurrent() const {
169 PluginGlobals* globals = PluginGlobals::Get();
170 if (!globals->msg_loop_slot())
171 return false; // Can't be current if there's nothing in the slot.
172 return static_cast<const void*>(globals->msg_loop_slot()->Get()) ==
173 static_cast<const void*>(this);
176 void MessageLoopResource::PostClosure(
177 const tracked_objects::Location& from_here,
178 const base::Closure& closure,
179 int64 delay_ms) {
180 if (loop_proxy_.get()) {
181 loop_proxy_->PostDelayedTask(
182 from_here, closure, base::TimeDelta::FromMilliseconds(delay_ms));
183 } else {
184 TaskInfo info;
185 info.from_here = FROM_HERE;
186 info.closure = closure;
187 info.delay_ms = delay_ms;
188 pending_tasks_.push_back(info);
192 // static
193 void MessageLoopResource::ReleaseMessageLoop(void* value) {
194 static_cast<MessageLoopResource*>(value)->DetachFromThread();
197 // -----------------------------------------------------------------------------
199 PP_Resource Create(PP_Instance instance) {
200 ProxyAutoLock lock;
201 // Validate the instance.
202 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
203 if (!dispatcher)
204 return 0;
205 return (new MessageLoopResource(instance))->GetReference();
208 PP_Resource GetForMainThread() {
209 ProxyAutoLock lock;
210 return PluginGlobals::Get()->loop_for_main_thread()->GetReference();
213 PP_Resource GetCurrent() {
214 ProxyAutoLock lock;
215 Resource* resource = MessageLoopResource::GetCurrent();
216 if (resource)
217 return resource->GetReference();
218 return 0;
221 int32_t AttachToCurrentThread(PP_Resource message_loop) {
222 EnterMessageLoop enter(message_loop, true);
223 if (enter.succeeded())
224 return enter.object()->AttachToCurrentThread();
225 return PP_ERROR_BADRESOURCE;
228 int32_t Run(PP_Resource message_loop) {
229 EnterMessageLoop enter(message_loop, true);
230 if (enter.succeeded())
231 return enter.object()->Run();
232 return PP_ERROR_BADRESOURCE;
235 int32_t PostWork(PP_Resource message_loop,
236 PP_CompletionCallback callback,
237 int64_t delay_ms) {
238 EnterMessageLoop enter(message_loop, true);
239 if (enter.succeeded())
240 return enter.object()->PostWork(callback, delay_ms);
241 return PP_ERROR_BADRESOURCE;
244 int32_t PostQuit(PP_Resource message_loop, PP_Bool should_destroy) {
245 EnterMessageLoop enter(message_loop, true);
246 if (enter.succeeded())
247 return enter.object()->PostQuit(should_destroy);
248 return PP_ERROR_BADRESOURCE;
251 const PPB_MessageLoop_1_0 ppb_message_loop_interface = {
252 &Create,
253 &GetForMainThread,
254 &GetCurrent,
255 &AttachToCurrentThread,
256 &Run,
257 &PostWork,
258 &PostQuit
261 PPB_MessageLoop_Proxy::PPB_MessageLoop_Proxy(Dispatcher* dispatcher)
262 : InterfaceProxy(dispatcher) {
265 PPB_MessageLoop_Proxy::~PPB_MessageLoop_Proxy() {
268 // static
269 const PPB_MessageLoop_1_0* PPB_MessageLoop_Proxy::GetInterface() {
270 return &ppb_message_loop_interface;
273 } // namespace proxy
274 } // namespace ppapi