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/plugin_globals.h" | 5 #include "ppapi/proxy/plugin_globals.h" |
6 | 6 |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
8 #include "base/task_runner.h" | 8 #include "base/task_runner.h" |
9 #include "base/threading/thread.h" | 9 #include "base/threading/thread.h" |
10 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 | 57 |
58 PluginGlobals::PluginGlobals( | 58 PluginGlobals::PluginGlobals( |
59 const scoped_refptr<base::TaskRunner>& ipc_task_runner) | 59 const scoped_refptr<base::TaskRunner>& ipc_task_runner) |
60 : ppapi::PpapiGlobals(), | 60 : ppapi::PpapiGlobals(), |
61 plugin_proxy_delegate_(NULL), | 61 plugin_proxy_delegate_(NULL), |
62 callback_tracker_(new CallbackTracker), | 62 callback_tracker_(new CallbackTracker), |
63 ipc_task_runner_(ipc_task_runner), | 63 ipc_task_runner_(ipc_task_runner), |
64 resource_reply_thread_registrar_( | 64 resource_reply_thread_registrar_( |
65 new ResourceReplyThreadRegistrar(GetMainThreadMessageLoop())), | 65 new ResourceReplyThreadRegistrar(GetMainThreadMessageLoop())), |
66 udp_socket_filter_(new UDPSocketFilter()), | 66 udp_socket_filter_(new UDPSocketFilter()), |
67 plugin_recently_active_(false), | |
68 keepalive_throttle_interval_milliseconds_( | |
69 ppapi::kKeepaliveThrottleIntervalDefaultMilliseconds), | |
70 weak_factory_(this) { | 67 weak_factory_(this) { |
71 DCHECK(!plugin_globals_); | 68 DCHECK(!plugin_globals_); |
72 plugin_globals_ = this; | 69 plugin_globals_ = this; |
73 | 70 |
74 // ResourceTracker asserts that we have the lock when we add new resources, | 71 // ResourceTracker asserts that we have the lock when we add new resources, |
75 // so we lock when creating the MessageLoopResource even though there is no | 72 // so we lock when creating the MessageLoopResource even though there is no |
76 // chance of race conditions. | 73 // chance of race conditions. |
77 ProxyAutoLock lock; | 74 ProxyAutoLock lock; |
78 loop_for_main_thread_ = | 75 loop_for_main_thread_ = |
79 new MessageLoopResource(MessageLoopResource::ForMainThread()); | 76 new MessageLoopResource(MessageLoopResource::ForMainThread()); |
80 } | 77 } |
81 | 78 |
82 PluginGlobals::PluginGlobals( | 79 PluginGlobals::PluginGlobals( |
83 PerThreadForTest per_thread_for_test, | 80 PerThreadForTest per_thread_for_test, |
84 const scoped_refptr<base::TaskRunner>& ipc_task_runner) | 81 const scoped_refptr<base::TaskRunner>& ipc_task_runner) |
85 : ppapi::PpapiGlobals(per_thread_for_test), | 82 : ppapi::PpapiGlobals(per_thread_for_test), |
86 plugin_proxy_delegate_(NULL), | 83 plugin_proxy_delegate_(NULL), |
87 callback_tracker_(new CallbackTracker), | 84 callback_tracker_(new CallbackTracker), |
88 ipc_task_runner_(ipc_task_runner), | 85 ipc_task_runner_(ipc_task_runner), |
89 resource_reply_thread_registrar_( | 86 resource_reply_thread_registrar_( |
90 new ResourceReplyThreadRegistrar(GetMainThreadMessageLoop())), | 87 new ResourceReplyThreadRegistrar(GetMainThreadMessageLoop())), |
91 plugin_recently_active_(false), | |
92 keepalive_throttle_interval_milliseconds_( | |
93 kKeepaliveThrottleIntervalDefaultMilliseconds), | |
94 weak_factory_(this) { | 88 weak_factory_(this) { |
95 DCHECK(!plugin_globals_); | 89 DCHECK(!plugin_globals_); |
96 } | 90 } |
97 | 91 |
98 PluginGlobals::~PluginGlobals() { | 92 PluginGlobals::~PluginGlobals() { |
99 DCHECK(plugin_globals_ == this || !plugin_globals_); | 93 DCHECK(plugin_globals_ == this || !plugin_globals_); |
100 { | 94 { |
101 ProxyAutoLock lock; | 95 ProxyAutoLock lock; |
102 // Release the main-thread message loop. We should have the last reference | 96 // Release the main-thread message loop. We should have the last reference |
103 // count, so this will delete the MessageLoop resource. We do this before | 97 // count, so this will delete the MessageLoop resource. We do this before |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 base::TaskRunner* PluginGlobals::GetFileTaskRunner() { | 172 base::TaskRunner* PluginGlobals::GetFileTaskRunner() { |
179 if (!file_thread_.get()) { | 173 if (!file_thread_.get()) { |
180 file_thread_.reset(new base::Thread("Plugin::File")); | 174 file_thread_.reset(new base::Thread("Plugin::File")); |
181 base::Thread::Options options; | 175 base::Thread::Options options; |
182 options.message_loop_type = base::MessageLoop::TYPE_IO; | 176 options.message_loop_type = base::MessageLoop::TYPE_IO; |
183 file_thread_->StartWithOptions(options); | 177 file_thread_->StartWithOptions(options); |
184 } | 178 } |
185 return file_thread_->task_runner().get(); | 179 return file_thread_->task_runner().get(); |
186 } | 180 } |
187 | 181 |
188 void PluginGlobals::MarkPluginIsActive() { | |
189 if (!plugin_recently_active_) { | |
190 plugin_recently_active_ = true; | |
191 if (!GetBrowserSender() || !base::ThreadTaskRunnerHandle::IsSet()) | |
192 return; | |
193 GetBrowserSender()->Send(new PpapiHostMsg_Keepalive()); | |
194 DCHECK(keepalive_throttle_interval_milliseconds_); | |
195 GetMainThreadMessageLoop()->PostDelayedTask( | |
196 FROM_HERE, | |
197 RunWhileLocked(base::Bind(&PluginGlobals::OnReleaseKeepaliveThrottle, | |
198 weak_factory_.GetWeakPtr())), | |
199 base::TimeDelta::FromMilliseconds( | |
200 keepalive_throttle_interval_milliseconds_)); | |
201 } | |
202 } | |
203 | |
204 IPC::Sender* PluginGlobals::GetBrowserSender() { | 182 IPC::Sender* PluginGlobals::GetBrowserSender() { |
205 // CAUTION: This function is called without the ProxyLock. See also | 183 // CAUTION: This function is called without the ProxyLock. See also |
206 // InterfaceList::GetInterfaceForPPB. | 184 // InterfaceList::GetInterfaceForPPB. |
207 // | 185 // |
208 // See also SetPluginProxyDelegate. That initializes browser_sender_ before | 186 // See also SetPluginProxyDelegate. That initializes browser_sender_ before |
209 // the plugin can start threads, and it may be cleared after the | 187 // the plugin can start threads, and it may be cleared after the |
210 // plugin has torn down threads. So this pointer is expected to remain valid | 188 // plugin has torn down threads. So this pointer is expected to remain valid |
211 // during the lifetime of the plugin. | 189 // during the lifetime of the plugin. |
212 return browser_sender_.get(); | 190 return browser_sender_.get(); |
213 } | 191 } |
(...skipping 30 matching lines...) Expand all Loading... |
244 | 222 |
245 MessageLoopResource* PluginGlobals::loop_for_main_thread() { | 223 MessageLoopResource* PluginGlobals::loop_for_main_thread() { |
246 return loop_for_main_thread_.get(); | 224 return loop_for_main_thread_.get(); |
247 } | 225 } |
248 | 226 |
249 void PluginGlobals::RegisterResourceMessageFilters( | 227 void PluginGlobals::RegisterResourceMessageFilters( |
250 ppapi::proxy::PluginMessageFilter* plugin_filter) { | 228 ppapi::proxy::PluginMessageFilter* plugin_filter) { |
251 plugin_filter->AddResourceMessageFilter(udp_socket_filter_.get()); | 229 plugin_filter->AddResourceMessageFilter(udp_socket_filter_.get()); |
252 } | 230 } |
253 | 231 |
254 void PluginGlobals::set_keepalive_throttle_interval_milliseconds(unsigned i) { | |
255 keepalive_throttle_interval_milliseconds_ = i; | |
256 } | |
257 | |
258 bool PluginGlobals::IsPluginGlobals() const { | 232 bool PluginGlobals::IsPluginGlobals() const { |
259 return true; | 233 return true; |
260 } | 234 } |
261 | 235 |
262 void PluginGlobals::OnReleaseKeepaliveThrottle() { | |
263 ppapi::ProxyLock::AssertAcquiredDebugOnly(); | |
264 plugin_recently_active_ = false; | |
265 } | |
266 | |
267 } // namespace proxy | 236 } // namespace proxy |
268 } // namespace ppapi | 237 } // namespace ppapi |
OLD | NEW |