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
3 // found in the LICENSE file.
5 #include "ppapi/proxy/plugin_globals.h"
7 #include "ipc/ipc_message.h"
8 #include "ipc/ipc_sender.h"
9 #include "ppapi/proxy/plugin_dispatcher.h"
10 #include "ppapi/proxy/plugin_proxy_delegate.h"
11 #include "ppapi/proxy/ppb_message_loop_proxy.h"
12 #include "ppapi/shared_impl/proxy_lock.h"
13 #include "ppapi/thunk/enter.h"
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
{
22 // |underlying_sender| must outlive this object.
23 explicit BrowserSender(IPC::Sender
* underlying_sender
)
24 : underlying_sender_(underlying_sender
) {
27 virtual ~BrowserSender() {}
29 // IPC::Sender implementation.
30 virtual bool Send(IPC::Message
* msg
) OVERRIDE
{
32 // Synchronous messages might be re-entrant, so we need to drop the lock.
33 ProxyAutoUnlock unlock
;
34 return underlying_sender_
->Send(msg
);
37 return underlying_sender_
->Send(msg
);
41 // Non-owning pointer.
42 IPC::Sender
* underlying_sender_
;
44 DISALLOW_COPY_AND_ASSIGN(BrowserSender
);
47 PluginGlobals
* PluginGlobals::plugin_globals_
= NULL
;
49 PluginGlobals::PluginGlobals()
50 : ppapi::PpapiGlobals(),
51 plugin_proxy_delegate_(NULL
),
52 callback_tracker_(new CallbackTracker
),
53 loop_for_main_thread_(
54 new MessageLoopResource(MessageLoopResource::ForMainThread())) {
55 #if defined(ENABLE_PEPPER_THREADING)
56 enable_threading_
= true;
58 enable_threading_
= false;
61 DCHECK(!plugin_globals_
);
62 plugin_globals_
= this;
65 PluginGlobals::PluginGlobals(ForTest for_test
)
66 : ppapi::PpapiGlobals(for_test
),
67 plugin_proxy_delegate_(NULL
),
68 callback_tracker_(new CallbackTracker
) {
69 #if defined(ENABLE_PEPPER_THREADING)
70 enable_threading_
= true;
72 enable_threading_
= false;
74 DCHECK(!plugin_globals_
);
77 PluginGlobals::~PluginGlobals() {
78 DCHECK(plugin_globals_
== this || !plugin_globals_
);
79 plugin_globals_
= NULL
;
82 ResourceTracker
* PluginGlobals::GetResourceTracker() {
83 return &plugin_resource_tracker_
;
86 VarTracker
* PluginGlobals::GetVarTracker() {
87 return &plugin_var_tracker_
;
90 CallbackTracker
* PluginGlobals::GetCallbackTrackerForInstance(
91 PP_Instance instance
) {
92 // In the plugin process, the callback tracker is always the same, regardless
94 return callback_tracker_
.get();
97 thunk::PPB_Instance_API
* PluginGlobals::GetInstanceAPI(PP_Instance instance
) {
98 PluginDispatcher
* dispatcher
= PluginDispatcher::GetForInstance(instance
);
100 return dispatcher
->GetInstanceAPI();
104 thunk::ResourceCreationAPI
* PluginGlobals::GetResourceCreationAPI(
105 PP_Instance instance
) {
106 PluginDispatcher
* dispatcher
= PluginDispatcher::GetForInstance(instance
);
108 return dispatcher
->GetResourceCreationAPI();
112 PP_Module
PluginGlobals::GetModuleForInstance(PP_Instance instance
) {
113 // Currently proxied plugins don't use the PP_Module for anything useful.
117 std::string
PluginGlobals::GetCmdLine() {
118 return command_line_
;
121 void PluginGlobals::PreCacheFontForFlash(const void* logfontw
) {
122 ProxyAutoUnlock unlock
;
123 plugin_proxy_delegate_
->PreCacheFont(logfontw
);
126 base::Lock
* PluginGlobals::GetProxyLock() {
127 if (enable_threading_
)
132 void PluginGlobals::LogWithSource(PP_Instance instance
,
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
);
140 void PluginGlobals::BroadcastLogWithSource(PP_Module
/* module */,
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
);
150 MessageLoopShared
* PluginGlobals::GetCurrentMessageLoop() {
151 return MessageLoopResource::GetCurrent();
154 IPC::Sender
* PluginGlobals::GetBrowserSender() {
155 if (!browser_sender_
.get()) {
156 browser_sender_
.reset(
157 new BrowserSender(plugin_proxy_delegate_
->GetBrowserSender()));
160 return browser_sender_
.get();
163 std::string
PluginGlobals::GetUILanguage() {
164 return plugin_proxy_delegate_
->GetUILanguage();
167 void PluginGlobals::SetActiveURL(const std::string
& url
) {
168 plugin_proxy_delegate_
->SetActiveURL(url
);
171 MessageLoopResource
* PluginGlobals::loop_for_main_thread() {
172 return loop_for_main_thread_
.get();
175 bool PluginGlobals::IsPluginGlobals() const {