blob: 1106a75b5616febf09446d485fed84faf442cc18 [file] [log] [blame]
[email protected]73097562012-01-12 19:38:551// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]794d83cd2011-10-20 19:09:202// 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]e34e8092012-11-27 23:01:267#include "ipc/ipc_message.h"
8#include "ipc/ipc_sender.h"
[email protected]2f59e382011-10-20 22:51:019#include "ppapi/proxy/plugin_dispatcher.h"
[email protected]a085aed72012-04-24 05:32:0410#include "ppapi/proxy/plugin_proxy_delegate.h"
[email protected]6de743a2012-08-30 20:03:2211#include "ppapi/proxy/ppb_message_loop_proxy.h"
[email protected]e34e8092012-11-27 23:01:2612#include "ppapi/shared_impl/proxy_lock.h"
[email protected]a9b16dd2012-01-31 05:00:2613#include "ppapi/thunk/enter.h"
[email protected]2f59e382011-10-20 22:51:0114
[email protected]794d83cd2011-10-20 19:09:2015namespace ppapi {
16namespace proxy {
17
[email protected]e34e8092012-11-27 23:01:2618// It performs necessary locking/unlocking of the proxy lock, and forwards all
19// messages to the underlying sender.
20class 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]794d83cd2011-10-20 19:09:2047PluginGlobals* PluginGlobals::plugin_globals_ = NULL;
48
[email protected]6fc87e02011-12-20 19:18:4549PluginGlobals::PluginGlobals()
50 : ppapi::PpapiGlobals(),
[email protected]bbc49122011-12-29 20:16:5051 plugin_proxy_delegate_(NULL),
[email protected]6de743a2012-08-30 20:03:2252 callback_tracker_(new CallbackTracker),
53 loop_for_main_thread_(
54 new MessageLoopResource(MessageLoopResource::ForMainThread())) {
[email protected]2ba8d10b2012-11-20 03:40:5755#if defined(ENABLE_PEPPER_THREADING)
56 enable_threading_ = true;
57#else
58 enable_threading_ = false;
59#endif
60
[email protected]794d83cd2011-10-20 19:09:2061 DCHECK(!plugin_globals_);
62 plugin_globals_ = this;
63}
64
[email protected]73097562012-01-12 19:38:5565PluginGlobals::PluginGlobals(ForTest for_test)
66 : ppapi::PpapiGlobals(for_test),
67 plugin_proxy_delegate_(NULL),
68 callback_tracker_(new CallbackTracker) {
[email protected]2ba8d10b2012-11-20 03:40:5769#if defined(ENABLE_PEPPER_THREADING)
70 enable_threading_ = true;
71#else
72 enable_threading_ = false;
73#endif
[email protected]73097562012-01-12 19:38:5574 DCHECK(!plugin_globals_);
75}
76
[email protected]794d83cd2011-10-20 19:09:2077PluginGlobals::~PluginGlobals() {
[email protected]73097562012-01-12 19:38:5578 DCHECK(plugin_globals_ == this || !plugin_globals_);
[email protected]794d83cd2011-10-20 19:09:2079 plugin_globals_ = NULL;
80}
81
82ResourceTracker* PluginGlobals::GetResourceTracker() {
83 return &plugin_resource_tracker_;
84}
85
86VarTracker* PluginGlobals::GetVarTracker() {
87 return &plugin_var_tracker_;
88}
89
[email protected]bbc49122011-12-29 20:16:5090CallbackTracker* 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]4f2006122012-04-30 05:13:1797thunk::PPB_Instance_API* PluginGlobals::GetInstanceAPI(PP_Instance instance) {
98 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
[email protected]2f59e382011-10-20 22:51:0199 if (dispatcher)
[email protected]4f2006122012-04-30 05:13:17100 return dispatcher->GetInstanceAPI();
101 return NULL;
102}
103
104thunk::ResourceCreationAPI* PluginGlobals::GetResourceCreationAPI(
105 PP_Instance instance) {
106 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
107 if (dispatcher)
108 return dispatcher->GetResourceCreationAPI();
[email protected]2f59e382011-10-20 22:51:01109 return NULL;
110}
111
112PP_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]a085aed72012-04-24 05:32:04117std::string PluginGlobals::GetCmdLine() {
118 return command_line_;
119}
120
121void PluginGlobals::PreCacheFontForFlash(const void* logfontw) {
[email protected]e34e8092012-11-27 23:01:26122 ProxyAutoUnlock unlock;
[email protected]a085aed72012-04-24 05:32:04123 plugin_proxy_delegate_->PreCacheFont(logfontw);
124}
125
[email protected]73097562012-01-12 19:38:55126base::Lock* PluginGlobals::GetProxyLock() {
[email protected]2ba8d10b2012-11-20 03:40:57127 if (enable_threading_)
128 return &proxy_lock_;
[email protected]73097562012-01-12 19:38:55129 return NULL;
[email protected]73097562012-01-12 19:38:55130}
131
[email protected]a9b16dd2012-01-31 05:00:26132void PluginGlobals::LogWithSource(PP_Instance instance,
[email protected]598816ad2012-12-13 01:34:32133 PP_LogLevel level,
[email protected]a9b16dd2012-01-31 05:00:26134 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
140void PluginGlobals::BroadcastLogWithSource(PP_Module /* module */,
[email protected]598816ad2012-12-13 01:34:32141 PP_LogLevel level,
[email protected]a9b16dd2012-01-31 05:00:26142 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]77c34172012-11-08 18:55:16150MessageLoopShared* PluginGlobals::GetCurrentMessageLoop() {
151 return MessageLoopResource::GetCurrent();
152}
153
[email protected]e34e8092012-11-27 23:01:26154IPC::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
163std::string PluginGlobals::GetUILanguage() {
164 return plugin_proxy_delegate_->GetUILanguage();
165}
166
167void PluginGlobals::SetActiveURL(const std::string& url) {
168 plugin_proxy_delegate_->SetActiveURL(url);
169}
170
[email protected]6de743a2012-08-30 20:03:22171MessageLoopResource* PluginGlobals::loop_for_main_thread() {
172 return loop_for_main_thread_.get();
173}
174
[email protected]73097562012-01-12 19:38:55175bool PluginGlobals::IsPluginGlobals() const {
176 return true;
177}
178
[email protected]794d83cd2011-10-20 19:09:20179} // namespace proxy
180} // namespace ppapi