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