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. 4 5 #ifndef PPAPI_PROXY_PLUGIN_GLOBALS_H_ 6 #define PPAPI_PROXY_PLUGIN_GLOBALS_H_ 7 8 #include <string> 9 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" 19 20 namespace base { 21 class Thread; 22 } 23 namespace IPC { 24 class Sender; 25 } 26 27 struct PP_BrowserFont_Trusted_Description; 28 29 namespace ppapi { 30 31 struct Preferences; 32 33 namespace proxy { 34 35 class MessageLoopResource; 36 class PluginProxyDelegate; 37 class ResourceReplyThreadRegistrar; 38 39 class PPAPI_PROXY_EXPORT PluginGlobals : public PpapiGlobals { 40 public: 41 PluginGlobals(); 42 explicit PluginGlobals(PpapiGlobals::PerThreadForTest); 43 virtual ~PluginGlobals(); 44 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 50 // crash reports. 51 CHECK(PpapiGlobals::Get()->IsPluginGlobals()); 52 return static_cast<PluginGlobals*>(PpapiGlobals::Get()); 53 } 54 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, 68 PP_LogLevel level, 69 const std::string& source, 70 const std::string& value) OVERRIDE; 71 virtual void BroadcastLogWithSource(PP_Module module, 72 PP_LogLevel level, 73 const std::string& source, 74 const std::string& value) OVERRIDE; 75 virtual MessageLoopShared* GetCurrentMessageLoop() OVERRIDE; 76 base::TaskRunner* GetFileTaskRunner() OVERRIDE; 77 78 // Returns the channel for sending to the browser. 79 IPC::Sender* GetBrowserSender(); 80 81 // Returns the language code of the current UI language. 82 std::string GetUILanguage(); 83 84 // Sets the active url which is reported by breakpad. 85 void SetActiveURL(const std::string& url); 86 87 PP_Resource CreateBrowserFont( 88 Connection connection, 89 PP_Instance instance, 90 const PP_BrowserFont_Trusted_Description& desc, 91 const Preferences& prefs); 92 93 // Getters for the plugin-specific versions. 94 PluginResourceTracker* plugin_resource_tracker() { 95 return &plugin_resource_tracker_; 96 } 97 PluginVarTracker* plugin_var_tracker() { 98 return &plugin_var_tracker_; 99 } 100 101 // The embedder should call set_proxy_delegate during startup. 102 void set_plugin_proxy_delegate(PluginProxyDelegate* d) { 103 plugin_proxy_delegate_ = d; 104 } 105 106 // Returns the TLS slot that holds the message loop TLS. 107 // 108 // If we end up needing more TLS storage for more stuff, we should probably 109 // have a struct in here for the different items. 110 base::ThreadLocalStorage::Slot* msg_loop_slot() { 111 return msg_loop_slot_.get(); 112 } 113 114 // Sets the message loop slot, takes ownership of the given heap-alloated 115 // pointer. 116 void set_msg_loop_slot(base::ThreadLocalStorage::Slot* slot) { 117 msg_loop_slot_.reset(slot); 118 } 119 120 // Return the special Resource that represents the MessageLoop for the main 121 // thread. This Resource is not associated with any instance, and lives as 122 // long as the plugin. 123 MessageLoopResource* loop_for_main_thread(); 124 125 // The embedder should call this function when the name of the plugin module 126 // is known. This will be used for error logging. 127 void set_plugin_name(const std::string& name) { plugin_name_ = name; } 128 129 // The embedder should call this function when the command line is known. 130 void set_command_line(const std::string& c) { command_line_ = c; } 131 132 ResourceReplyThreadRegistrar* resource_reply_thread_registrar() { 133 return resource_reply_thread_registrar_.get(); 134 } 135 136 private: 137 class BrowserSender; 138 139 // PpapiGlobals overrides. 140 virtual bool IsPluginGlobals() const OVERRIDE; 141 142 static PluginGlobals* plugin_globals_; 143 144 PluginProxyDelegate* plugin_proxy_delegate_; 145 PluginResourceTracker plugin_resource_tracker_; 146 PluginVarTracker plugin_var_tracker_; 147 scoped_refptr<CallbackTracker> callback_tracker_; 148 149 scoped_ptr<base::ThreadLocalStorage::Slot> msg_loop_slot_; 150 // Note that loop_for_main_thread's constructor sets msg_loop_slot_, so it 151 // must be initialized after msg_loop_slot_ (hence the order here). 152 scoped_refptr<MessageLoopResource> loop_for_main_thread_; 153 154 // Name of the plugin used for error logging. This will be empty until 155 // set_plugin_name is called. 156 std::string plugin_name_; 157 158 // Command line for the plugin. This will be empty until set_command_line is 159 // called. 160 std::string command_line_; 161 162 scoped_ptr<BrowserSender> browser_sender_; 163 164 // Thread for performing potentially blocking file operations. It's created 165 // lazily, since it might not be needed. 166 scoped_ptr<base::Thread> file_thread_; 167 168 scoped_refptr<ResourceReplyThreadRegistrar> resource_reply_thread_registrar_; 169 170 DISALLOW_COPY_AND_ASSIGN(PluginGlobals); 171 }; 172 173 } // namespace proxy 174 } // namespace ppapi 175 176 #endif // PPAPI_PROXY_PLUGIN_GLOBALS_H_ 177