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;
38 class PPAPI_PROXY_EXPORT PluginGlobals : public PpapiGlobals {
41 explicit PluginGlobals(PpapiGlobals::PerThreadForTest);
42 virtual ~PluginGlobals();
44 // Getter for the global singleton. Generally, you should use
45 // PpapiGlobals::Get() when possible. Use this only when you need some
46 // plugin-specific functionality.
47 inline static PluginGlobals* Get() {
48 // Explicitly crash if this is the wrong process type, we want to get
50 CHECK(PpapiGlobals::Get()->IsPluginGlobals());
51 return static_cast<PluginGlobals*>(PpapiGlobals::Get());
54 // PpapiGlobals implementation.
55 virtual ResourceTracker* GetResourceTracker() OVERRIDE;
56 virtual VarTracker* GetVarTracker() OVERRIDE;
57 virtual CallbackTracker* GetCallbackTrackerForInstance(
58 PP_Instance instance) OVERRIDE;
59 virtual thunk::PPB_Instance_API* GetInstanceAPI(
60 PP_Instance instance) OVERRIDE;
61 virtual thunk::ResourceCreationAPI* GetResourceCreationAPI(
62 PP_Instance instance) OVERRIDE;
63 virtual PP_Module GetModuleForInstance(PP_Instance instance) OVERRIDE;
64 virtual std::string GetCmdLine() OVERRIDE;
65 virtual void PreCacheFontForFlash(const void* logfontw) OVERRIDE;
66 virtual void LogWithSource(PP_Instance instance,
68 const std::string& source,
69 const std::string& value) OVERRIDE;
70 virtual void BroadcastLogWithSource(PP_Module module,
72 const std::string& source,
73 const std::string& value) OVERRIDE;
74 virtual MessageLoopShared* GetCurrentMessageLoop() OVERRIDE;
75 base::TaskRunner* GetFileTaskRunner() OVERRIDE;
77 // Returns the channel for sending to the browser.
78 IPC::Sender* GetBrowserSender();
80 // Returns the language code of the current UI language.
81 std::string GetUILanguage();
83 // Sets the active url which is reported by breakpad.
84 void SetActiveURL(const std::string& url);
86 PP_Resource CreateBrowserFont(
87 Connection connection,
89 const PP_BrowserFont_Trusted_Description& desc,
90 const Preferences& prefs);
92 // Getters for the plugin-specific versions.
93 PluginResourceTracker* plugin_resource_tracker() {
94 return &plugin_resource_tracker_;
96 PluginVarTracker* plugin_var_tracker() {
97 return &plugin_var_tracker_;
100 // The embedder should call set_proxy_delegate during startup.
101 void set_plugin_proxy_delegate(PluginProxyDelegate* d) {
102 plugin_proxy_delegate_ = d;
105 // Returns the TLS slot that holds the message loop TLS.
107 // If we end up needing more TLS storage for more stuff, we should probably
108 // have a struct in here for the different items.
109 base::ThreadLocalStorage::Slot* msg_loop_slot() {
110 return msg_loop_slot_.get();
113 // Sets the message loop slot, takes ownership of the given heap-alloated
115 void set_msg_loop_slot(base::ThreadLocalStorage::Slot* slot) {
116 msg_loop_slot_.reset(slot);
119 // Return the special Resource that represents the MessageLoop for the main
120 // thread. This Resource is not associated with any instance, and lives as
121 // long as the plugin.
122 MessageLoopResource* loop_for_main_thread();
124 // The embedder should call this function when the name of the plugin module
125 // is known. This will be used for error logging.
126 void set_plugin_name(const std::string& name) { plugin_name_ = name; }
128 // The embedder should call this function when the command line is known.
129 void set_command_line(const std::string& c) { command_line_ = c; }
134 // PpapiGlobals overrides.
135 virtual bool IsPluginGlobals() const OVERRIDE;
137 static PluginGlobals* plugin_globals_;
139 PluginProxyDelegate* plugin_proxy_delegate_;
140 PluginResourceTracker plugin_resource_tracker_;
141 PluginVarTracker plugin_var_tracker_;
142 scoped_refptr<CallbackTracker> callback_tracker_;
144 scoped_ptr<base::ThreadLocalStorage::Slot> msg_loop_slot_;
145 // Note that loop_for_main_thread's constructor sets msg_loop_slot_, so it
146 // must be initialized after msg_loop_slot_ (hence the order here).
147 scoped_refptr<MessageLoopResource> loop_for_main_thread_;
149 // Name of the plugin used for error logging. This will be empty until
150 // set_plugin_name is called.
151 std::string plugin_name_;
153 // Command line for the plugin. This will be empty until set_command_line is
155 std::string command_line_;
157 scoped_ptr<BrowserSender> browser_sender_;
159 // Thread for performing potentially blocking file operations. It's created
160 // lazily, since it might not be needed.
161 scoped_ptr<base::Thread> file_thread_;
163 DISALLOW_COPY_AND_ASSIGN(PluginGlobals);
169 #endif // PPAPI_PROXY_PLUGIN_GLOBALS_H_