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/task_runner.h" | 7 #include "base/task_runner.h" |
8 #include "base/threading/thread.h" | 8 #include "base/threading/thread.h" |
9 #include "ipc/ipc_message.h" | 9 #include "ipc/ipc_message.h" |
10 #include "ipc/ipc_sender.h" | 10 #include "ipc/ipc_sender.h" |
11 #include "ppapi/proxy/plugin_dispatcher.h" | 11 #include "ppapi/proxy/plugin_dispatcher.h" |
12 #include "ppapi/proxy/plugin_proxy_delegate.h" | 12 #include "ppapi/proxy/plugin_proxy_delegate.h" |
| 13 #include "ppapi/proxy/ppapi_messages.h" |
13 #include "ppapi/proxy/ppb_message_loop_proxy.h" | 14 #include "ppapi/proxy/ppb_message_loop_proxy.h" |
14 #include "ppapi/proxy/resource_reply_thread_registrar.h" | 15 #include "ppapi/proxy/resource_reply_thread_registrar.h" |
15 #include "ppapi/shared_impl/proxy_lock.h" | 16 #include "ppapi/shared_impl/proxy_lock.h" |
16 #include "ppapi/thunk/enter.h" | 17 #include "ppapi/thunk/enter.h" |
17 | 18 |
| 19 namespace { |
| 20 |
| 21 const int kKeepaliveThrottleIntervalDefault = 5000; |
| 22 |
| 23 } // namespace |
| 24 |
18 namespace ppapi { | 25 namespace ppapi { |
19 namespace proxy { | 26 namespace proxy { |
20 | 27 |
21 // It performs necessary locking/unlocking of the proxy lock, and forwards all | 28 // It performs necessary locking/unlocking of the proxy lock, and forwards all |
22 // messages to the underlying sender. | 29 // messages to the underlying sender. |
23 class PluginGlobals::BrowserSender : public IPC::Sender { | 30 class PluginGlobals::BrowserSender : public IPC::Sender { |
24 public: | 31 public: |
25 // |underlying_sender| must outlive this object. | 32 // |underlying_sender| must outlive this object. |
26 explicit BrowserSender(IPC::Sender* underlying_sender) | 33 explicit BrowserSender(IPC::Sender* underlying_sender) |
27 : underlying_sender_(underlying_sender) { | 34 : underlying_sender_(underlying_sender) { |
(...skipping 19 matching lines...) Expand all Loading... |
47 DISALLOW_COPY_AND_ASSIGN(BrowserSender); | 54 DISALLOW_COPY_AND_ASSIGN(BrowserSender); |
48 }; | 55 }; |
49 | 56 |
50 PluginGlobals* PluginGlobals::plugin_globals_ = NULL; | 57 PluginGlobals* PluginGlobals::plugin_globals_ = NULL; |
51 | 58 |
52 PluginGlobals::PluginGlobals() | 59 PluginGlobals::PluginGlobals() |
53 : ppapi::PpapiGlobals(), | 60 : ppapi::PpapiGlobals(), |
54 plugin_proxy_delegate_(NULL), | 61 plugin_proxy_delegate_(NULL), |
55 callback_tracker_(new CallbackTracker), | 62 callback_tracker_(new CallbackTracker), |
56 resource_reply_thread_registrar_( | 63 resource_reply_thread_registrar_( |
57 new ResourceReplyThreadRegistrar(GetMainThreadMessageLoop())) { | 64 new ResourceReplyThreadRegistrar(GetMainThreadMessageLoop())), |
| 65 plugin_recently_active_(false), |
| 66 keepalive_throttle_interval_milliseconds_( |
| 67 kKeepaliveThrottleIntervalDefault), |
| 68 weak_factory_(this) { |
58 DCHECK(!plugin_globals_); | 69 DCHECK(!plugin_globals_); |
59 plugin_globals_ = this; | 70 plugin_globals_ = this; |
60 | 71 |
61 // ResourceTracker asserts that we have the lock when we add new resources, | 72 // ResourceTracker asserts that we have the lock when we add new resources, |
62 // so we lock when creating the MessageLoopResource even though there is no | 73 // so we lock when creating the MessageLoopResource even though there is no |
63 // chance of race conditions. | 74 // chance of race conditions. |
64 ProxyAutoLock lock; | 75 ProxyAutoLock lock; |
65 loop_for_main_thread_ = | 76 loop_for_main_thread_ = |
66 new MessageLoopResource(MessageLoopResource::ForMainThread()); | 77 new MessageLoopResource(MessageLoopResource::ForMainThread()); |
67 } | 78 } |
68 | 79 |
69 PluginGlobals::PluginGlobals(PerThreadForTest per_thread_for_test) | 80 PluginGlobals::PluginGlobals(PerThreadForTest per_thread_for_test) |
70 : ppapi::PpapiGlobals(per_thread_for_test), | 81 : ppapi::PpapiGlobals(per_thread_for_test), |
71 plugin_proxy_delegate_(NULL), | 82 plugin_proxy_delegate_(NULL), |
72 callback_tracker_(new CallbackTracker), | 83 callback_tracker_(new CallbackTracker), |
73 resource_reply_thread_registrar_( | 84 resource_reply_thread_registrar_( |
74 new ResourceReplyThreadRegistrar(GetMainThreadMessageLoop())) { | 85 new ResourceReplyThreadRegistrar(GetMainThreadMessageLoop())), |
| 86 plugin_recently_active_(false), |
| 87 keepalive_throttle_interval_milliseconds_( |
| 88 kKeepaliveThrottleIntervalDefault), |
| 89 weak_factory_(this) { |
75 DCHECK(!plugin_globals_); | 90 DCHECK(!plugin_globals_); |
76 } | 91 } |
77 | 92 |
78 PluginGlobals::~PluginGlobals() { | 93 PluginGlobals::~PluginGlobals() { |
79 DCHECK(plugin_globals_ == this || !plugin_globals_); | 94 DCHECK(plugin_globals_ == this || !plugin_globals_); |
80 { | 95 { |
81 ProxyAutoLock lock; | 96 ProxyAutoLock lock; |
82 // Release the main-thread message loop. We should have the last reference | 97 // Release the main-thread message loop. We should have the last reference |
83 // count, so this will delete the MessageLoop resource. We do this before | 98 // count, so this will delete the MessageLoop resource. We do this before |
84 // we clear plugin_globals_, because the Resource destructor tries to access | 99 // we clear plugin_globals_, because the Resource destructor tries to access |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 base::TaskRunner* PluginGlobals::GetFileTaskRunner() { | 173 base::TaskRunner* PluginGlobals::GetFileTaskRunner() { |
159 if (!file_thread_.get()) { | 174 if (!file_thread_.get()) { |
160 file_thread_.reset(new base::Thread("Plugin::File")); | 175 file_thread_.reset(new base::Thread("Plugin::File")); |
161 base::Thread::Options options; | 176 base::Thread::Options options; |
162 options.message_loop_type = base::MessageLoop::TYPE_IO; | 177 options.message_loop_type = base::MessageLoop::TYPE_IO; |
163 file_thread_->StartWithOptions(options); | 178 file_thread_->StartWithOptions(options); |
164 } | 179 } |
165 return file_thread_->message_loop_proxy(); | 180 return file_thread_->message_loop_proxy(); |
166 } | 181 } |
167 | 182 |
| 183 void PluginGlobals::MarkPluginIsActive() { |
| 184 if (!plugin_recently_active_) { |
| 185 plugin_recently_active_ = true; |
| 186 if (!GetBrowserSender() || !base::MessageLoop::current()) |
| 187 return; |
| 188 GetBrowserSender()->Send(new PpapiHostMsg_Keepalive()); |
| 189 |
| 190 GetMainThreadMessageLoop()->PostDelayedTask(FROM_HERE, |
| 191 RunWhileLocked(base::Bind(&PluginGlobals::OnReleaseKeepaliveThrottle, |
| 192 weak_factory_.GetWeakPtr())), |
| 193 base::TimeDelta::FromMilliseconds( |
| 194 keepalive_throttle_interval_milliseconds())); |
| 195 } |
| 196 } |
| 197 |
168 IPC::Sender* PluginGlobals::GetBrowserSender() { | 198 IPC::Sender* PluginGlobals::GetBrowserSender() { |
169 if (!browser_sender_.get()) { | 199 if (!browser_sender_.get()) { |
170 browser_sender_.reset( | 200 browser_sender_.reset( |
171 new BrowserSender(plugin_proxy_delegate_->GetBrowserSender())); | 201 new BrowserSender(plugin_proxy_delegate_->GetBrowserSender())); |
172 } | 202 } |
173 | 203 |
174 return browser_sender_.get(); | 204 return browser_sender_.get(); |
175 } | 205 } |
176 | 206 |
177 std::string PluginGlobals::GetUILanguage() { | 207 std::string PluginGlobals::GetUILanguage() { |
(...skipping 10 matching lines...) Expand all Loading... |
188 const PP_BrowserFont_Trusted_Description& desc, | 218 const PP_BrowserFont_Trusted_Description& desc, |
189 const ppapi::Preferences& prefs) { | 219 const ppapi::Preferences& prefs) { |
190 return plugin_proxy_delegate_->CreateBrowserFont( | 220 return plugin_proxy_delegate_->CreateBrowserFont( |
191 connection, instance, desc, prefs); | 221 connection, instance, desc, prefs); |
192 } | 222 } |
193 | 223 |
194 MessageLoopResource* PluginGlobals::loop_for_main_thread() { | 224 MessageLoopResource* PluginGlobals::loop_for_main_thread() { |
195 return loop_for_main_thread_.get(); | 225 return loop_for_main_thread_.get(); |
196 } | 226 } |
197 | 227 |
| 228 int PluginGlobals::keepalive_throttle_interval_milliseconds() const { |
| 229 return keepalive_throttle_interval_milliseconds_; |
| 230 } |
| 231 |
| 232 void PluginGlobals::set_keepalive_throttle_interval_milliseconds(int i) { |
| 233 keepalive_throttle_interval_milliseconds_ = i; |
| 234 } |
| 235 |
198 bool PluginGlobals::IsPluginGlobals() const { | 236 bool PluginGlobals::IsPluginGlobals() const { |
199 return true; | 237 return true; |
200 } | 238 } |
201 | 239 |
| 240 void PluginGlobals::OnReleaseKeepaliveThrottle() { |
| 241 ppapi::ProxyLock::AssertAcquiredDebugOnly(); |
| 242 plugin_recently_active_ = false; |
| 243 } |
| 244 |
202 } // namespace proxy | 245 } // namespace proxy |
203 } // namespace ppapi | 246 } // namespace ppapi |
OLD | NEW |