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 virtual ~PluginGlobals();
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 virtual ResourceTracker
* GetResourceTracker() override
;
61 virtual VarTracker
* GetVarTracker() override
;
62 virtual CallbackTracker
* GetCallbackTrackerForInstance(
63 PP_Instance instance
) override
;
64 virtual thunk::PPB_Instance_API
* GetInstanceAPI(
65 PP_Instance instance
) override
;
66 virtual thunk::ResourceCreationAPI
* GetResourceCreationAPI(
67 PP_Instance instance
) override
;
68 virtual PP_Module
GetModuleForInstance(PP_Instance instance
) override
;
69 virtual std::string
GetCmdLine() override
;
70 virtual void PreCacheFontForFlash(const void* logfontw
) override
;
71 virtual void LogWithSource(PP_Instance instance
,
73 const std::string
& source
,
74 const std::string
& value
) override
;
75 virtual void BroadcastLogWithSource(PP_Module module
,
77 const std::string
& source
,
78 const std::string
& value
) override
;
79 virtual MessageLoopShared
* GetCurrentMessageLoop() override
;
80 base::TaskRunner
* GetFileTaskRunner() override
;
81 virtual void MarkPluginIsActive() override
;
83 // Returns the channel for sending to the browser.
84 IPC::Sender
* GetBrowserSender();
86 base::TaskRunner
* ipc_task_runner() { return ipc_task_runner_
.get(); }
88 // Returns the language code of the current UI language.
89 std::string
GetUILanguage();
91 // Sets the active url which is reported by breakpad.
92 void SetActiveURL(const std::string
& url
);
94 PP_Resource
CreateBrowserFont(
95 Connection connection
,
97 const PP_BrowserFont_Trusted_Description
& desc
,
98 const Preferences
& prefs
);
100 // Getters for the plugin-specific versions.
101 PluginResourceTracker
* plugin_resource_tracker() {
102 return &plugin_resource_tracker_
;
104 PluginVarTracker
* plugin_var_tracker() {
105 return &plugin_var_tracker_
;
108 // The embedder should call SetPluginProxyDelegate during startup.
109 void SetPluginProxyDelegate(PluginProxyDelegate
* d
);
110 // The embedder may choose to call ResetPluginProxyDelegate during shutdown.
111 // After that point, it's unsafe to call most members of PluginGlobals,
112 // and GetBrowserSender will return NULL.
113 void ResetPluginProxyDelegate();
115 // Returns the TLS slot that holds the message loop TLS.
117 // If we end up needing more TLS storage for more stuff, we should probably
118 // have a struct in here for the different items.
119 base::ThreadLocalStorage::Slot
* msg_loop_slot() {
120 return msg_loop_slot_
.get();
123 // Sets the message loop slot, takes ownership of the given heap-alloated
125 void set_msg_loop_slot(base::ThreadLocalStorage::Slot
* slot
) {
126 msg_loop_slot_
.reset(slot
);
129 // Return the special Resource that represents the MessageLoop for the main
130 // thread. This Resource is not associated with any instance, and lives as
131 // long as the plugin.
132 MessageLoopResource
* loop_for_main_thread();
134 // The embedder should call this function when the name of the plugin module
135 // is known. This will be used for error logging.
136 void set_plugin_name(const std::string
& name
) { plugin_name_
= name
; }
138 // The embedder should call this function when the command line is known.
139 void set_command_line(const std::string
& c
) { command_line_
= c
; }
141 ResourceReplyThreadRegistrar
* resource_reply_thread_registrar() {
142 return resource_reply_thread_registrar_
.get();
145 UDPSocketFilter
* udp_socket_filter() const {
146 return udp_socket_filter_
.get();
148 // Add any necessary ResourceMessageFilters to the PluginMessageFilter so
149 // that they can receive and handle appropriate messages on the IO thread.
150 void RegisterResourceMessageFilters(
151 ppapi::proxy::PluginMessageFilter
* plugin_filter
);
153 // Interval to limit how many IPC messages are sent indicating that the plugin
154 // is active and should be kept alive. The value must be smaller than any
155 // threshold used to kill inactive plugins by the embedder host.
156 void set_keepalive_throttle_interval_milliseconds(unsigned i
);
161 // PpapiGlobals overrides.
162 virtual bool IsPluginGlobals() const override
;
164 // Locks the proxy lock and releases the throttle on keepalive IPC messages.
165 void OnReleaseKeepaliveThrottle();
167 static PluginGlobals
* plugin_globals_
;
169 PluginProxyDelegate
* plugin_proxy_delegate_
;
170 PluginResourceTracker plugin_resource_tracker_
;
171 PluginVarTracker plugin_var_tracker_
;
172 scoped_refptr
<CallbackTracker
> callback_tracker_
;
174 scoped_ptr
<base::ThreadLocalStorage::Slot
> msg_loop_slot_
;
175 // Note that loop_for_main_thread's constructor sets msg_loop_slot_, so it
176 // must be initialized after msg_loop_slot_ (hence the order here).
177 scoped_refptr
<MessageLoopResource
> loop_for_main_thread_
;
179 // Name of the plugin used for error logging. This will be empty until
180 // set_plugin_name is called.
181 std::string plugin_name_
;
183 // Command line for the plugin. This will be empty until set_command_line is
185 std::string command_line_
;
187 scoped_ptr
<BrowserSender
> browser_sender_
;
189 scoped_refptr
<base::TaskRunner
> ipc_task_runner_
;
191 // Thread for performing potentially blocking file operations. It's created
192 // lazily, since it might not be needed.
193 scoped_ptr
<base::Thread
> file_thread_
;
195 scoped_refptr
<ResourceReplyThreadRegistrar
> resource_reply_thread_registrar_
;
197 scoped_refptr
<UDPSocketFilter
> udp_socket_filter_
;
199 // Indicates activity by the plugin. Used to monitor when a plugin can be
200 // shutdown due to idleness. Current needs do not require differentiating
201 // between idle state between multiple instances, if any are active they are
202 // all considered active.
203 bool plugin_recently_active_
;
205 unsigned keepalive_throttle_interval_milliseconds_
;
207 // Member variables should appear before the WeakPtrFactory, see weak_ptr.h.
208 base::WeakPtrFactory
<PluginGlobals
> weak_factory_
;
210 DISALLOW_COPY_AND_ASSIGN(PluginGlobals
);
216 #endif // PPAPI_PROXY_PLUGIN_GLOBALS_H_