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_SHARED_IMPL_PPAPI_GLOBALS_H_ 6 #define PPAPI_SHARED_IMPL_PPAPI_GLOBALS_H_ 7 8 #include <map> 9 #include <string> 10 #include <vector> 11 12 #include "base/basictypes.h" 13 #include "base/memory/ref_counted.h" 14 #include "base/threading/thread_local.h" // For testing purposes only. 15 #include "ppapi/c/pp_instance.h" 16 #include "ppapi/c/pp_module.h" 17 #include "ppapi/c/ppb_console.h" 18 #include "ppapi/shared_impl/api_id.h" 19 #include "ppapi/shared_impl/ppapi_shared_export.h" 20 #include "ui/events/latency_info.h" 21 22 namespace base { 23 class MessageLoopProxy; 24 class TaskRunner; 25 } 26 27 namespace ppapi { 28 29 class CallbackTracker; 30 class MessageLoopShared; 31 class ResourceTracker; 32 class VarTracker; 33 34 namespace thunk { 35 class PPB_Instance_API; 36 class ResourceCreationAPI; 37 } 38 39 // Abstract base class 40 class PPAPI_SHARED_EXPORT PpapiGlobals { 41 public: 42 // Must be created on the main thread. 43 PpapiGlobals(); 44 45 // This constructor is to be used only for making a PpapiGlobal for testing 46 // purposes. This avoids setting the global static ppapi_globals_. For unit 47 // tests that use this feature, the "test" PpapiGlobals should be constructed 48 // using this method. See SetPpapiGlobalsOnThreadForTest for more information. 49 struct PerThreadForTest {}; 50 explicit PpapiGlobals(PerThreadForTest); 51 52 virtual ~PpapiGlobals(); 53 54 // Getter for the global singleton. 55 static PpapiGlobals* Get(); 56 57 // This allows us to set a given PpapiGlobals object as the PpapiGlobals for 58 // a given thread. After setting the PpapiGlobals for a thread, Get() will 59 // return that PpapiGlobals when Get() is called on that thread. Other threads 60 // are unaffected. This allows us to have tests which use >1 PpapiGlobals in 61 // the same process, e.g. for having 1 thread emulate the "host" and 1 thread 62 // emulate the "plugin". 63 // 64 // PpapiGlobals object must have been constructed using the "PerThreadForTest" 65 // parameter. 66 static void SetPpapiGlobalsOnThreadForTest(PpapiGlobals* ptr); 67 68 // Retrieves the corresponding tracker. 69 virtual ResourceTracker* GetResourceTracker() = 0; 70 virtual VarTracker* GetVarTracker() = 0; 71 virtual CallbackTracker* GetCallbackTrackerForInstance( 72 PP_Instance instance) = 0; 73 74 // Logs the given string to the JS console. If "source" is empty, the name of 75 // the current module will be used, if it can be determined. 76 virtual void LogWithSource(PP_Instance instance, 77 PP_LogLevel level, 78 const std::string& source, 79 const std::string& value) = 0; 80 81 // Like LogWithSource but broadcasts the log to all instances of the given 82 // module. The module may be 0 to specify that all consoles possibly 83 // associated with the calling code should be notified. This allows us to 84 // log errors for things like bad resource IDs where we may not have an 85 // associated instance. 86 // 87 // Note that in the plugin process, the module parameter is ignored since 88 // there is only one possible one. 89 virtual void BroadcastLogWithSource(PP_Module module, 90 PP_LogLevel level, 91 const std::string& source, 92 const std::string& value) = 0; 93 94 // Returns the given API object associated with the given instance, or NULL 95 // if the instance is invalid. 96 virtual thunk::PPB_Instance_API* GetInstanceAPI(PP_Instance instance) = 0; 97 virtual thunk::ResourceCreationAPI* GetResourceCreationAPI( 98 PP_Instance instance) = 0; 99 100 // Returns the PP_Module associated with the given PP_Instance, or 0 on 101 // failure. 102 virtual PP_Module GetModuleForInstance(PP_Instance instance) = 0; 103 104 // Returns the base::MessageLoopProxy for the main thread. This is set in the 105 // constructor, so PpapiGlobals must be created on the main thread. 106 base::MessageLoopProxy* GetMainThreadMessageLoop(); 107 108 // In tests, the PpapiGlobals object persists across tests but the MLP pointer 109 // it hangs on will go stale and the next PPAPI test will crash because of 110 // thread checks. This resets the pointer to be the current MLP object. 111 void ResetMainThreadMessageLoopForTesting(); 112 113 // Return the MessageLoopShared of the current thread, if any. This will 114 // always return NULL on the host side, where PPB_MessageLoop is not 115 // supported. 116 virtual MessageLoopShared* GetCurrentMessageLoop() = 0; 117 118 // Returns a task runner for file operations that may block. 119 // TODO(bbudge) Move this to PluginGlobals when we no longer support 120 // in-process plugins. 121 virtual base::TaskRunner* GetFileTaskRunner() = 0; 122 123 // Returns the command line for the process. 124 virtual std::string GetCmdLine() = 0; 125 126 // Preloads the font on Windows, does nothing on other platforms. 127 // TODO(brettw) remove this by passing the instance into the API so we don't 128 // have to have it on the globals. 129 virtual void PreCacheFontForFlash(const void* logfontw) = 0; 130 131 virtual bool IsHostGlobals() const; 132 virtual bool IsPluginGlobals() const; 133 134 // Records that the plugin is active. The plugin reports that it is active to 135 // containers that monitor and shutdown idle content such as background apps. 136 // This method only has an effect on the plugin process, calls from the 137 // renderer process will have no effect. 138 virtual void MarkPluginIsActive(); 139 140 // Caches an input event's |latency_info| for the plugin |instance|. 141 void AddLatencyInfo(const ui::LatencyInfo& latency_info, 142 PP_Instance instance); 143 // Transfers plugin |instance|'s latency info into |latency_info|. 144 void TransferLatencyInfoTo(std::vector<ui::LatencyInfo>* latency_info, 145 PP_Instance instance); 146 147 private: 148 // Return the thread-local pointer which is used only for unit testing. It 149 // should always be NULL when running in production. It allows separate 150 // threads to have distinct "globals". 151 static PpapiGlobals* GetThreadLocalPointer(); 152 153 scoped_refptr<base::MessageLoopProxy> main_loop_proxy_; 154 155 // If an input event is believed to have caused rendering damage, its latency 156 // info is cached in |latency_info_for_frame_| indexed by instance. These 157 // latency info will be passed back to renderer with the next plugin frame. 158 std::map<PP_Instance, std::vector<ui::LatencyInfo> > latency_info_for_frame_; 159 160 DISALLOW_COPY_AND_ASSIGN(PpapiGlobals); 161 }; 162 163 } // namespace ppapi 164 165 #endif // PPAPI_SHARED_IMPL_PPAPI_GLOBALS_H_ 166