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 #ifndef PPAPI_PROXY_PLUGIN_GLOBALS_H_
6 #define PPAPI_PROXY_PLUGIN_GLOBALS_H_
10 #include "base/compiler_specific.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/threading/thread_local_storage.h"
14 #include "ppapi/proxy/connection.h"
15 #include "ppapi/proxy/plugin_resource_tracker.h"
16 #include "ppapi/proxy/plugin_var_tracker.h"
17 #include "ppapi/proxy/ppapi_proxy_export.h"
18 #include "ppapi/shared_impl/callback_tracker.h"
19 #include "ppapi/shared_impl/ppapi_globals.h"
28 struct PP_BrowserFont_Trusted_Description
;
36 class MessageLoopResource
;
37 class PluginMessageFilter
;
38 class PluginProxyDelegate
;
39 class ResourceReplyThreadRegistrar
;
40 class UDPSocketFilter
;
42 class PPAPI_PROXY_EXPORT PluginGlobals
: public PpapiGlobals
{
44 explicit PluginGlobals(const scoped_refptr
<base::TaskRunner
>& task_runner
);
45 PluginGlobals(PpapiGlobals::PerThreadForTest
,
46 const scoped_refptr
<base::TaskRunner
>& task_runner
);
47 ~PluginGlobals() override
;
49 // Getter for the global singleton. Generally, you should use
50 // PpapiGlobals::Get() when possible. Use this only when you need some
51 // plugin-specific functionality.
52 inline static PluginGlobals
* Get() {
53 // Explicitly crash if this is the wrong process type, we want to get
55 CHECK(PpapiGlobals::Get()->IsPluginGlobals());
56 return static_cast<PluginGlobals
*>(PpapiGlobals::Get());
59 // PpapiGlobals implementation.
60 ResourceTracker
* GetResourceTracker() override
;
61 VarTracker
* GetVarTracker() override
;
62 CallbackTracker
* GetCallbackTrackerForInstance(PP_Instance instance
) override
;
63 thunk::PPB_Instance_API
* GetInstanceAPI(PP_Instance instance
) override
;
64 thunk::ResourceCreationAPI
* GetResourceCreationAPI(
65 PP_Instance instance
) override
;
66 PP_Module
GetModuleForInstance(PP_Instance instance
) override
;
67 std::string
GetCmdLine() override
;
68 void PreCacheFontForFlash(const void* logfontw
) override
;
69 void LogWithSource(PP_Instance instance
,
71 const std::string
& source
,
72 const std::string
& value
) override
;
73 void BroadcastLogWithSource(PP_Module module
,
75 const std::string
& source
,
76 const std::string
& value
) override
;
77 MessageLoopShared
* GetCurrentMessageLoop() override
;
78 base::TaskRunner
* GetFileTaskRunner() override
;
79 void MarkPluginIsActive() override
;
81 // Returns the channel for sending to the browser.
82 IPC::Sender
* GetBrowserSender();
84 base::TaskRunner
* ipc_task_runner() { return ipc_task_runner_
.get(); }
86 // Returns the language code of the current UI language.
87 std::string
GetUILanguage();
89 // Sets the active url which is reported by breakpad.
90 void SetActiveURL(const std::string
& url
);
92 PP_Resource
CreateBrowserFont(
93 Connection connection
,
95 const PP_BrowserFont_Trusted_Description
& desc
,
96 const Preferences
& prefs
);
98 // Getters for the plugin-specific versions.
99 PluginResourceTracker
* plugin_resource_tracker() {
100 return &plugin_resource_tracker_
;
102 PluginVarTracker
* plugin_var_tracker() {
103 return &plugin_var_tracker_
;
106 // The embedder should call SetPluginProxyDelegate during startup.
107 void SetPluginProxyDelegate(PluginProxyDelegate
* d
);
108 // The embedder may choose to call ResetPluginProxyDelegate during shutdown.
109 // After that point, it's unsafe to call most members of PluginGlobals,
110 // and GetBrowserSender will return NULL.
111 void ResetPluginProxyDelegate();
113 // Returns the TLS slot that holds the message loop TLS.
115 // If we end up needing more TLS storage for more stuff, we should probably
116 // have a struct in here for the different items.
117 base::ThreadLocalStorage::Slot
* msg_loop_slot() {
118 return msg_loop_slot_
.get();
121 // Sets the message loop slot, takes ownership of the given heap-alloated
123 void set_msg_loop_slot(base::ThreadLocalStorage::Slot
* slot
) {
124 msg_loop_slot_
.reset(slot
);
127 // Return the special Resource that represents the MessageLoop for the main
128 // thread. This Resource is not associated with any instance, and lives as
129 // long as the plugin.
130 MessageLoopResource
* loop_for_main_thread();
132 // The embedder should call this function when the name of the plugin module
133 // is known. This will be used for error logging.
134 void set_plugin_name(const std::string
& name
) { plugin_name_
= name
; }
136 // The embedder should call this function when the command line is known.
137 void set_command_line(const std::string
& c
) { command_line_
= c
; }
139 ResourceReplyThreadRegistrar
* resource_reply_thread_registrar() {
140 return resource_reply_thread_registrar_
.get();
143 UDPSocketFilter
* udp_socket_filter() const {
144 return udp_socket_filter_
.get();
146 // Add any necessary ResourceMessageFilters to the PluginMessageFilter so
147 // that they can receive and handle appropriate messages on the IO thread.
148 void RegisterResourceMessageFilters(
149 ppapi::proxy::PluginMessageFilter
* plugin_filter
);
151 // Interval to limit how many IPC messages are sent indicating that the plugin
152 // is active and should be kept alive. The value must be smaller than any
153 // threshold used to kill inactive plugins by the embedder host.
154 void set_keepalive_throttle_interval_milliseconds(unsigned i
);
159 // PpapiGlobals overrides.
160 bool IsPluginGlobals() const override
;
162 // Locks the proxy lock and releases the throttle on keepalive IPC messages.
163 void OnReleaseKeepaliveThrottle();
165 static PluginGlobals
* plugin_globals_
;
167 PluginProxyDelegate
* plugin_proxy_delegate_
;
168 PluginResourceTracker plugin_resource_tracker_
;
169 PluginVarTracker plugin_var_tracker_
;
170 scoped_refptr
<CallbackTracker
> callback_tracker_
;
172 scoped_ptr
<base::ThreadLocalStorage::Slot
> msg_loop_slot_
;
173 // Note that loop_for_main_thread's constructor sets msg_loop_slot_, so it
174 // must be initialized after msg_loop_slot_ (hence the order here).
175 scoped_refptr
<MessageLoopResource
> loop_for_main_thread_
;
177 // Name of the plugin used for error logging. This will be empty until
178 // set_plugin_name is called.
179 std::string plugin_name_
;
181 // Command line for the plugin. This will be empty until set_command_line is
183 std::string command_line_
;
185 scoped_ptr
<BrowserSender
> browser_sender_
;
187 scoped_refptr
<base::TaskRunner
> ipc_task_runner_
;
189 // Thread for performing potentially blocking file operations. It's created
190 // lazily, since it might not be needed.
191 scoped_ptr
<base::Thread
> file_thread_
;
193 scoped_refptr
<ResourceReplyThreadRegistrar
> resource_reply_thread_registrar_
;
195 scoped_refptr
<UDPSocketFilter
> udp_socket_filter_
;
197 // Indicates activity by the plugin. Used to monitor when a plugin can be
198 // shutdown due to idleness. Current needs do not require differentiating
199 // between idle state between multiple instances, if any are active they are
200 // all considered active.
201 bool plugin_recently_active_
;
203 unsigned keepalive_throttle_interval_milliseconds_
;
205 // Member variables should appear before the WeakPtrFactory, see weak_ptr.h.
206 base::WeakPtrFactory
<PluginGlobals
> weak_factory_
;
208 DISALLOW_COPY_AND_ASSIGN(PluginGlobals
);
214 #endif // PPAPI_PROXY_PLUGIN_GLOBALS_H_