[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "ppapi/proxy/plugin_globals.h" |
| 6 | |
[email protected] | e34e809 | 2012-11-27 23:01:26 | [diff] [blame] | 7 | #include "ipc/ipc_message.h" |
| 8 | #include "ipc/ipc_sender.h" |
[email protected] | 2f59e38 | 2011-10-20 22:51:01 | [diff] [blame] | 9 | #include "ppapi/proxy/plugin_dispatcher.h" |
[email protected] | a085aed7 | 2012-04-24 05:32:04 | [diff] [blame] | 10 | #include "ppapi/proxy/plugin_proxy_delegate.h" |
[email protected] | 6de743a | 2012-08-30 20:03:22 | [diff] [blame] | 11 | #include "ppapi/proxy/ppb_message_loop_proxy.h" |
[email protected] | e34e809 | 2012-11-27 23:01:26 | [diff] [blame] | 12 | #include "ppapi/shared_impl/proxy_lock.h" |
[email protected] | a9b16dd | 2012-01-31 05:00:26 | [diff] [blame] | 13 | #include "ppapi/thunk/enter.h" |
[email protected] | 2f59e38 | 2011-10-20 22:51:01 | [diff] [blame] | 14 | |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 15 | namespace ppapi { |
| 16 | namespace proxy { |
| 17 | |
[email protected] | e34e809 | 2012-11-27 23:01:26 | [diff] [blame] | 18 | // It performs necessary locking/unlocking of the proxy lock, and forwards all |
| 19 | // messages to the underlying sender. |
| 20 | class PluginGlobals::BrowserSender : public IPC::Sender { |
| 21 | public: |
| 22 | // |underlying_sender| must outlive this object. |
| 23 | explicit BrowserSender(IPC::Sender* underlying_sender) |
| 24 | : underlying_sender_(underlying_sender) { |
| 25 | } |
| 26 | |
| 27 | virtual ~BrowserSender() {} |
| 28 | |
| 29 | // IPC::Sender implementation. |
| 30 | virtual bool Send(IPC::Message* msg) OVERRIDE { |
| 31 | if (msg->is_sync()) { |
| 32 | // Synchronous messages might be re-entrant, so we need to drop the lock. |
| 33 | ProxyAutoUnlock unlock; |
| 34 | return underlying_sender_->Send(msg); |
| 35 | } |
| 36 | |
| 37 | return underlying_sender_->Send(msg); |
| 38 | } |
| 39 | |
| 40 | private: |
| 41 | // Non-owning pointer. |
| 42 | IPC::Sender* underlying_sender_; |
| 43 | |
| 44 | DISALLOW_COPY_AND_ASSIGN(BrowserSender); |
| 45 | }; |
| 46 | |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 47 | PluginGlobals* PluginGlobals::plugin_globals_ = NULL; |
| 48 | |
[email protected] | 6fc87e0 | 2011-12-20 19:18:45 | [diff] [blame] | 49 | PluginGlobals::PluginGlobals() |
| 50 | : ppapi::PpapiGlobals(), |
[email protected] | bbc4912 | 2011-12-29 20:16:50 | [diff] [blame] | 51 | plugin_proxy_delegate_(NULL), |
[email protected] | 6de743a | 2012-08-30 20:03:22 | [diff] [blame] | 52 | callback_tracker_(new CallbackTracker), |
| 53 | loop_for_main_thread_( |
| 54 | new MessageLoopResource(MessageLoopResource::ForMainThread())) { |
[email protected] | 2ba8d10b | 2012-11-20 03:40:57 | [diff] [blame] | 55 | #if defined(ENABLE_PEPPER_THREADING) |
| 56 | enable_threading_ = true; |
| 57 | #else |
| 58 | enable_threading_ = false; |
| 59 | #endif |
| 60 | |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 61 | DCHECK(!plugin_globals_); |
| 62 | plugin_globals_ = this; |
| 63 | } |
| 64 | |
[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 65 | PluginGlobals::PluginGlobals(ForTest for_test) |
| 66 | : ppapi::PpapiGlobals(for_test), |
| 67 | plugin_proxy_delegate_(NULL), |
| 68 | callback_tracker_(new CallbackTracker) { |
[email protected] | 2ba8d10b | 2012-11-20 03:40:57 | [diff] [blame] | 69 | #if defined(ENABLE_PEPPER_THREADING) |
| 70 | enable_threading_ = true; |
| 71 | #else |
| 72 | enable_threading_ = false; |
| 73 | #endif |
[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 74 | DCHECK(!plugin_globals_); |
| 75 | } |
| 76 | |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 77 | PluginGlobals::~PluginGlobals() { |
[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 78 | DCHECK(plugin_globals_ == this || !plugin_globals_); |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 79 | plugin_globals_ = NULL; |
| 80 | } |
| 81 | |
| 82 | ResourceTracker* PluginGlobals::GetResourceTracker() { |
| 83 | return &plugin_resource_tracker_; |
| 84 | } |
| 85 | |
| 86 | VarTracker* PluginGlobals::GetVarTracker() { |
| 87 | return &plugin_var_tracker_; |
| 88 | } |
| 89 | |
[email protected] | bbc4912 | 2011-12-29 20:16:50 | [diff] [blame] | 90 | CallbackTracker* PluginGlobals::GetCallbackTrackerForInstance( |
| 91 | PP_Instance instance) { |
| 92 | // In the plugin process, the callback tracker is always the same, regardless |
| 93 | // of the instance. |
| 94 | return callback_tracker_.get(); |
| 95 | } |
| 96 | |
[email protected] | 4f200612 | 2012-04-30 05:13:17 | [diff] [blame] | 97 | thunk::PPB_Instance_API* PluginGlobals::GetInstanceAPI(PP_Instance instance) { |
| 98 | PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); |
[email protected] | 2f59e38 | 2011-10-20 22:51:01 | [diff] [blame] | 99 | if (dispatcher) |
[email protected] | 4f200612 | 2012-04-30 05:13:17 | [diff] [blame] | 100 | return dispatcher->GetInstanceAPI(); |
| 101 | return NULL; |
| 102 | } |
| 103 | |
| 104 | thunk::ResourceCreationAPI* PluginGlobals::GetResourceCreationAPI( |
| 105 | PP_Instance instance) { |
| 106 | PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); |
| 107 | if (dispatcher) |
| 108 | return dispatcher->GetResourceCreationAPI(); |
[email protected] | 2f59e38 | 2011-10-20 22:51:01 | [diff] [blame] | 109 | return NULL; |
| 110 | } |
| 111 | |
| 112 | PP_Module PluginGlobals::GetModuleForInstance(PP_Instance instance) { |
| 113 | // Currently proxied plugins don't use the PP_Module for anything useful. |
| 114 | return 0; |
| 115 | } |
| 116 | |
[email protected] | a085aed7 | 2012-04-24 05:32:04 | [diff] [blame] | 117 | std::string PluginGlobals::GetCmdLine() { |
| 118 | return command_line_; |
| 119 | } |
| 120 | |
| 121 | void PluginGlobals::PreCacheFontForFlash(const void* logfontw) { |
[email protected] | e34e809 | 2012-11-27 23:01:26 | [diff] [blame] | 122 | ProxyAutoUnlock unlock; |
[email protected] | a085aed7 | 2012-04-24 05:32:04 | [diff] [blame] | 123 | plugin_proxy_delegate_->PreCacheFont(logfontw); |
| 124 | } |
| 125 | |
[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 126 | base::Lock* PluginGlobals::GetProxyLock() { |
[email protected] | 2ba8d10b | 2012-11-20 03:40:57 | [diff] [blame] | 127 | if (enable_threading_) |
| 128 | return &proxy_lock_; |
[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 129 | return NULL; |
[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 130 | } |
| 131 | |
[email protected] | a9b16dd | 2012-01-31 05:00:26 | [diff] [blame] | 132 | void PluginGlobals::LogWithSource(PP_Instance instance, |
[email protected] | 598816ad | 2012-12-13 01:34:32 | [diff] [blame^] | 133 | PP_LogLevel level, |
[email protected] | a9b16dd | 2012-01-31 05:00:26 | [diff] [blame] | 134 | const std::string& source, |
| 135 | const std::string& value) { |
| 136 | const std::string& fixed_up_source = source.empty() ? plugin_name_ : source; |
| 137 | PluginDispatcher::LogWithSource(instance, level, fixed_up_source, value); |
| 138 | } |
| 139 | |
| 140 | void PluginGlobals::BroadcastLogWithSource(PP_Module /* module */, |
[email protected] | 598816ad | 2012-12-13 01:34:32 | [diff] [blame^] | 141 | PP_LogLevel level, |
[email protected] | a9b16dd | 2012-01-31 05:00:26 | [diff] [blame] | 142 | const std::string& source, |
| 143 | const std::string& value) { |
| 144 | // Since we have only one module in a plugin process, broadcast is always |
| 145 | // the same as "send to everybody" which is what the dispatcher implements |
| 146 | // for the "instance = 0" case. |
| 147 | LogWithSource(0, level, source, value); |
| 148 | } |
| 149 | |
[email protected] | 77c3417 | 2012-11-08 18:55:16 | [diff] [blame] | 150 | MessageLoopShared* PluginGlobals::GetCurrentMessageLoop() { |
| 151 | return MessageLoopResource::GetCurrent(); |
| 152 | } |
| 153 | |
[email protected] | e34e809 | 2012-11-27 23:01:26 | [diff] [blame] | 154 | IPC::Sender* PluginGlobals::GetBrowserSender() { |
| 155 | if (!browser_sender_.get()) { |
| 156 | browser_sender_.reset( |
| 157 | new BrowserSender(plugin_proxy_delegate_->GetBrowserSender())); |
| 158 | } |
| 159 | |
| 160 | return browser_sender_.get(); |
| 161 | } |
| 162 | |
| 163 | std::string PluginGlobals::GetUILanguage() { |
| 164 | return plugin_proxy_delegate_->GetUILanguage(); |
| 165 | } |
| 166 | |
| 167 | void PluginGlobals::SetActiveURL(const std::string& url) { |
| 168 | plugin_proxy_delegate_->SetActiveURL(url); |
| 169 | } |
| 170 | |
[email protected] | 6de743a | 2012-08-30 20:03:22 | [diff] [blame] | 171 | MessageLoopResource* PluginGlobals::loop_for_main_thread() { |
| 172 | return loop_for_main_thread_.get(); |
| 173 | } |
| 174 | |
[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 175 | bool PluginGlobals::IsPluginGlobals() const { |
| 176 | return true; |
| 177 | } |
| 178 | |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 179 | } // namespace proxy |
| 180 | } // namespace ppapi |