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/scoped_ptr.h"
12 #include "base/threading/thread_local_storage.h"
13 #include "ppapi/proxy/connection.h"
14 #include "ppapi/proxy/plugin_resource_tracker.h"
15 #include "ppapi/proxy/plugin_var_tracker.h"
16 #include "ppapi/proxy/ppapi_proxy_export.h"
17 #include "ppapi/shared_impl/callback_tracker.h"
18 #include "ppapi/shared_impl/ppapi_globals.h"
27 struct PP_BrowserFont_Trusted_Description
;
35 class MessageLoopResource
;
36 class PluginProxyDelegate
;
37 class ResourceReplyThreadRegistrar
;
39 class PPAPI_PROXY_EXPORT PluginGlobals
: public PpapiGlobals
{
42 explicit PluginGlobals(PpapiGlobals::PerThreadForTest
);
43 virtual ~PluginGlobals();
45 // Getter for the global singleton. Generally, you should use
46 // PpapiGlobals::Get() when possible. Use this only when you need some
47 // plugin-specific functionality.
48 inline static PluginGlobals
* Get() {
49 // Explicitly crash if this is the wrong process type, we want to get
51 CHECK(PpapiGlobals::Get()->IsPluginGlobals());
52 return static_cast<PluginGlobals
*>(PpapiGlobals::Get());
55 // PpapiGlobals implementation.
56 virtual ResourceTracker
* GetResourceTracker() override
;
57 virtual VarTracker
* GetVarTracker() override
;
58 virtual CallbackTracker
* GetCallbackTrackerForInstance(
59 PP_Instance instance
) override
;
60 virtual thunk::PPB_Instance_API
* GetInstanceAPI(
61 PP_Instance instance
) override
;
62 virtual thunk::ResourceCreationAPI
* GetResourceCreationAPI(
63 PP_Instance instance
) override
;
64 virtual PP_Module
GetModuleForInstance(PP_Instance instance
) override
;
65 virtual std::string
GetCmdLine() override
;
66 virtual void PreCacheFontForFlash(const void* logfontw
) override
;
67 virtual void LogWithSource(PP_Instance instance
,
69 const std::string
& source
,
70 const std::string
& value
) override
;
71 virtual void BroadcastLogWithSource(PP_Module module
,
73 const std::string
& source
,
74 const std::string
& value
) override
;
75 virtual MessageLoopShared
* GetCurrentMessageLoop() override
;
76 base::TaskRunner
* GetFileTaskRunner() override
;
77 virtual void MarkPluginIsActive() override
;
79 // Returns the channel for sending to the browser.
80 IPC::Sender
* GetBrowserSender();
82 // Returns the language code of the current UI language.
83 std::string
GetUILanguage();
85 // Sets the active url which is reported by breakpad.
86 void SetActiveURL(const std::string
& url
);
88 PP_Resource
CreateBrowserFont(
89 Connection connection
,
91 const PP_BrowserFont_Trusted_Description
& desc
,
92 const Preferences
& prefs
);
94 // Getters for the plugin-specific versions.
95 PluginResourceTracker
* plugin_resource_tracker() {
96 return &plugin_resource_tracker_
;
98 PluginVarTracker
* plugin_var_tracker() {
99 return &plugin_var_tracker_
;
102 // The embedder should call SetPluginProxyDelegate during startup.
103 void SetPluginProxyDelegate(PluginProxyDelegate
* d
);
104 // The embedder may choose to call ResetPluginProxyDelegate during shutdown.
105 // After that point, it's unsafe to call most members of PluginGlobals,
106 // and GetBrowserSender will return NULL.
107 void ResetPluginProxyDelegate();
109 // Returns the TLS slot that holds the message loop TLS.
111 // If we end up needing more TLS storage for more stuff, we should probably
112 // have a struct in here for the different items.
113 base::ThreadLocalStorage::Slot
* msg_loop_slot() {
114 return msg_loop_slot_
.get();
117 // Sets the message loop slot, takes ownership of the given heap-alloated
119 void set_msg_loop_slot(base::ThreadLocalStorage::Slot
* slot
) {
120 msg_loop_slot_
.reset(slot
);
123 // Return the special Resource that represents the MessageLoop for the main
124 // thread. This Resource is not associated with any instance, and lives as
125 // long as the plugin.
126 MessageLoopResource
* loop_for_main_thread();
128 // The embedder should call this function when the name of the plugin module
129 // is known. This will be used for error logging.
130 void set_plugin_name(const std::string
& name
) { plugin_name_
= name
; }
132 // The embedder should call this function when the command line is known.
133 void set_command_line(const std::string
& c
) { command_line_
= c
; }
135 ResourceReplyThreadRegistrar
* resource_reply_thread_registrar() {
136 return resource_reply_thread_registrar_
.get();
139 // Interval to limit how many IPC messages are sent indicating that the plugin
140 // is active and should be kept alive. The value must be smaller than any
141 // threshold used to kill inactive plugins by the embedder host.
142 void set_keepalive_throttle_interval_milliseconds(unsigned i
);
147 // PpapiGlobals overrides.
148 virtual bool IsPluginGlobals() const override
;
150 // Locks the proxy lock and releases the throttle on keepalive IPC messages.
151 void OnReleaseKeepaliveThrottle();
153 static PluginGlobals
* plugin_globals_
;
155 PluginProxyDelegate
* plugin_proxy_delegate_
;
156 PluginResourceTracker plugin_resource_tracker_
;
157 PluginVarTracker plugin_var_tracker_
;
158 scoped_refptr
<CallbackTracker
> callback_tracker_
;
160 scoped_ptr
<base::ThreadLocalStorage::Slot
> msg_loop_slot_
;
161 // Note that loop_for_main_thread's constructor sets msg_loop_slot_, so it
162 // must be initialized after msg_loop_slot_ (hence the order here).
163 scoped_refptr
<MessageLoopResource
> loop_for_main_thread_
;
165 // Name of the plugin used for error logging. This will be empty until
166 // set_plugin_name is called.
167 std::string plugin_name_
;
169 // Command line for the plugin. This will be empty until set_command_line is
171 std::string command_line_
;
173 scoped_ptr
<BrowserSender
> browser_sender_
;
175 // Thread for performing potentially blocking file operations. It's created
176 // lazily, since it might not be needed.
177 scoped_ptr
<base::Thread
> file_thread_
;
179 scoped_refptr
<ResourceReplyThreadRegistrar
> resource_reply_thread_registrar_
;
181 // Indicates activity by the plugin. Used to monitor when a plugin can be
182 // shutdown due to idleness. Current needs do not require differentiating
183 // between idle state between multiple instances, if any are active they are
184 // all considered active.
185 bool plugin_recently_active_
;
187 unsigned keepalive_throttle_interval_milliseconds_
;
189 // Member variables should appear before the WeakPtrFactory, see weak_ptr.h.
190 base::WeakPtrFactory
<PluginGlobals
> weak_factory_
;
192 DISALLOW_COPY_AND_ASSIGN(PluginGlobals
);
198 #endif // PPAPI_PROXY_PLUGIN_GLOBALS_H_