OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ppapi/proxy/ppb_message_loop_proxy.h" | 5 #include "ppapi/proxy/ppb_message_loop_proxy.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" |
12 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
13 #include "base/location.h" | 14 #include "base/logging.h" |
14 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
15 #include "ppapi/c/pp_errors.h" | 16 #include "ppapi/c/pp_errors.h" |
16 #include "ppapi/c/ppb_message_loop.h" | 17 #include "ppapi/c/ppb_message_loop.h" |
17 #include "ppapi/proxy/plugin_dispatcher.h" | 18 #include "ppapi/proxy/plugin_dispatcher.h" |
18 #include "ppapi/proxy/plugin_globals.h" | 19 #include "ppapi/proxy/plugin_globals.h" |
19 #include "ppapi/shared_impl/proxy_lock.h" | 20 #include "ppapi/shared_impl/proxy_lock.h" |
20 #include "ppapi/thunk/enter.h" | 21 #include "ppapi/thunk/enter.h" |
21 | 22 |
22 using ppapi::thunk::PPB_MessageLoop_API; | 23 using ppapi::thunk::PPB_MessageLoop_API; |
23 | 24 |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 | 103 |
103 return PP_OK; | 104 return PP_OK; |
104 } | 105 } |
105 | 106 |
106 int32_t MessageLoopResource::Run() { | 107 int32_t MessageLoopResource::Run() { |
107 if (!IsCurrent()) | 108 if (!IsCurrent()) |
108 return PP_ERROR_WRONG_THREAD; | 109 return PP_ERROR_WRONG_THREAD; |
109 if (is_main_thread_loop_) | 110 if (is_main_thread_loop_) |
110 return PP_ERROR_INPROGRESS; | 111 return PP_ERROR_INPROGRESS; |
111 | 112 |
| 113 base::RunLoop* previous_run_loop = run_loop_; |
| 114 base::RunLoop run_loop; |
| 115 run_loop_ = &run_loop; |
| 116 |
112 nested_invocations_++; | 117 nested_invocations_++; |
113 CallWhileUnlocked( | 118 CallWhileUnlocked( |
114 base::Bind(&base::MessageLoop::Run, base::Unretained(loop_.get()))); | 119 base::Bind(&base::RunLoop::Run, base::Unretained(run_loop_))); |
115 nested_invocations_--; | 120 nested_invocations_--; |
116 | 121 |
| 122 run_loop_ = previous_run_loop; |
| 123 |
117 if (should_destroy_ && nested_invocations_ == 0) { | 124 if (should_destroy_ && nested_invocations_ == 0) { |
118 task_runner_ = NULL; | 125 task_runner_ = NULL; |
119 loop_.reset(); | 126 loop_.reset(); |
120 destroyed_ = true; | 127 destroyed_ = true; |
121 } | 128 } |
122 return PP_OK; | 129 return PP_OK; |
123 } | 130 } |
124 | 131 |
125 int32_t MessageLoopResource::PostWork(PP_CompletionCallback callback, | 132 int32_t MessageLoopResource::PostWork(PP_CompletionCallback callback, |
126 int64_t delay_ms) { | 133 int64_t delay_ms) { |
127 if (!callback.func) | 134 if (!callback.func) |
128 return PP_ERROR_BADARGUMENT; | 135 return PP_ERROR_BADARGUMENT; |
129 if (destroyed_) | 136 if (destroyed_) |
130 return PP_ERROR_FAILED; | 137 return PP_ERROR_FAILED; |
131 PostClosure(FROM_HERE, | 138 PostClosure(FROM_HERE, |
132 base::Bind(callback.func, callback.user_data, | 139 base::Bind(callback.func, callback.user_data, |
133 static_cast<int32_t>(PP_OK)), | 140 static_cast<int32_t>(PP_OK)), |
134 delay_ms); | 141 delay_ms); |
135 return PP_OK; | 142 return PP_OK; |
136 } | 143 } |
137 | 144 |
138 int32_t MessageLoopResource::PostQuit(PP_Bool should_destroy) { | 145 int32_t MessageLoopResource::PostQuit(PP_Bool should_destroy) { |
139 if (is_main_thread_loop_) | 146 if (is_main_thread_loop_) |
140 return PP_ERROR_WRONG_THREAD; | 147 return PP_ERROR_WRONG_THREAD; |
141 | 148 |
142 if (PP_ToBool(should_destroy)) | 149 if (PP_ToBool(should_destroy)) |
143 should_destroy_ = true; | 150 should_destroy_ = true; |
144 | 151 |
145 if (IsCurrent() && nested_invocations_ > 0) | 152 if (IsCurrent() && nested_invocations_ > 0) { |
146 loop_->QuitWhenIdle(); | 153 run_loop_->QuitWhenIdle(); |
147 else | 154 } else { |
148 PostClosure(FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(), 0); | 155 PostClosure(FROM_HERE, base::Bind(&MessageLoopResource::QuitRunLoopWhenIdle, |
| 156 Unretained(this)), |
| 157 0); |
| 158 } |
149 return PP_OK; | 159 return PP_OK; |
150 } | 160 } |
151 | 161 |
152 // static | 162 // static |
153 MessageLoopResource* MessageLoopResource::GetCurrent() { | 163 MessageLoopResource* MessageLoopResource::GetCurrent() { |
154 PluginGlobals* globals = PluginGlobals::Get(); | 164 PluginGlobals* globals = PluginGlobals::Get(); |
155 if (!globals->msg_loop_slot()) | 165 if (!globals->msg_loop_slot()) |
156 return NULL; | 166 return NULL; |
157 return reinterpret_cast<MessageLoopResource*>( | 167 return reinterpret_cast<MessageLoopResource*>( |
158 globals->msg_loop_slot()->Get()); | 168 globals->msg_loop_slot()->Get()); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 } | 204 } |
195 | 205 |
196 base::SingleThreadTaskRunner* MessageLoopResource::GetTaskRunner() { | 206 base::SingleThreadTaskRunner* MessageLoopResource::GetTaskRunner() { |
197 return task_runner_.get(); | 207 return task_runner_.get(); |
198 } | 208 } |
199 | 209 |
200 bool MessageLoopResource::CurrentlyHandlingBlockingMessage() { | 210 bool MessageLoopResource::CurrentlyHandlingBlockingMessage() { |
201 return currently_handling_blocking_message_; | 211 return currently_handling_blocking_message_; |
202 } | 212 } |
203 | 213 |
| 214 void MessageLoopResource::QuitRunLoopWhenIdle() { |
| 215 DCHECK(IsCurrent()); |
| 216 DCHECK(run_loop_); |
| 217 run_loop_->QuitWhenIdle(); |
| 218 } |
| 219 |
204 // static | 220 // static |
205 void MessageLoopResource::ReleaseMessageLoop(void* value) { | 221 void MessageLoopResource::ReleaseMessageLoop(void* value) { |
206 static_cast<MessageLoopResource*>(value)->DetachFromThread(); | 222 static_cast<MessageLoopResource*>(value)->DetachFromThread(); |
207 } | 223 } |
208 | 224 |
209 // ----------------------------------------------------------------------------- | 225 // ----------------------------------------------------------------------------- |
210 | 226 |
211 PP_Resource Create(PP_Instance instance) { | 227 PP_Resource Create(PP_Instance instance) { |
212 ProxyAutoLock lock; | 228 ProxyAutoLock lock; |
213 // Validate the instance. | 229 // Validate the instance. |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 PPB_MessageLoop_Proxy::~PPB_MessageLoop_Proxy() { | 293 PPB_MessageLoop_Proxy::~PPB_MessageLoop_Proxy() { |
278 } | 294 } |
279 | 295 |
280 // static | 296 // static |
281 const PPB_MessageLoop_1_0* PPB_MessageLoop_Proxy::GetInterface() { | 297 const PPB_MessageLoop_1_0* PPB_MessageLoop_Proxy::GetInterface() { |
282 return &ppb_message_loop_interface; | 298 return &ppb_message_loop_interface; |
283 } | 299 } |
284 | 300 |
285 } // namespace proxy | 301 } // namespace proxy |
286 } // namespace ppapi | 302 } // namespace ppapi |
OLD | NEW |