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_SHARED_IMPL_PPAPI_GLOBALS_H_
6 #define PPAPI_SHARED_IMPL_PPAPI_GLOBALS_H_
10 #include "base/basictypes.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/threading/thread_local.h" // For testing purposes only.
13 #include "ppapi/c/pp_instance.h"
14 #include "ppapi/c/pp_module.h"
15 #include "ppapi/c/ppb_console.h"
16 #include "ppapi/shared_impl/api_id.h"
17 #include "ppapi/shared_impl/ppapi_shared_export.h"
20 class SingleThreadTaskRunner
;
26 class CallbackTracker
;
27 class MessageLoopShared
;
28 class ResourceTracker
;
32 class PPB_Instance_API
;
33 class ResourceCreationAPI
;
36 // Abstract base class
37 class PPAPI_SHARED_EXPORT PpapiGlobals
{
39 // Must be created on the main thread.
42 // This constructor is to be used only for making a PpapiGlobal for testing
43 // purposes. This avoids setting the global static ppapi_globals_. For unit
44 // tests that use this feature, the "test" PpapiGlobals should be constructed
45 // using this method. See SetPpapiGlobalsOnThreadForTest for more information.
46 struct PerThreadForTest
{};
47 explicit PpapiGlobals(PerThreadForTest
);
49 virtual ~PpapiGlobals();
51 // Getter for the global singleton.
52 static PpapiGlobals
* Get();
54 // This allows us to set a given PpapiGlobals object as the PpapiGlobals for
55 // a given thread. After setting the PpapiGlobals for a thread, Get() will
56 // return that PpapiGlobals when Get() is called on that thread. Other threads
57 // are unaffected. This allows us to have tests which use >1 PpapiGlobals in
58 // the same process, e.g. for having 1 thread emulate the "host" and 1 thread
59 // emulate the "plugin".
61 // PpapiGlobals object must have been constructed using the "PerThreadForTest"
63 static void SetPpapiGlobalsOnThreadForTest(PpapiGlobals
* ptr
);
65 // Retrieves the corresponding tracker.
66 virtual ResourceTracker
* GetResourceTracker() = 0;
67 virtual VarTracker
* GetVarTracker() = 0;
68 virtual CallbackTracker
* GetCallbackTrackerForInstance(
69 PP_Instance instance
) = 0;
71 // Logs the given string to the JS console. If "source" is empty, the name of
72 // the current module will be used, if it can be determined.
73 virtual void LogWithSource(PP_Instance instance
,
75 const std::string
& source
,
76 const std::string
& value
) = 0;
78 // Like LogWithSource but broadcasts the log to all instances of the given
79 // module. The module may be 0 to specify that all consoles possibly
80 // associated with the calling code should be notified. This allows us to
81 // log errors for things like bad resource IDs where we may not have an
82 // associated instance.
84 // Note that in the plugin process, the module parameter is ignored since
85 // there is only one possible one.
86 virtual void BroadcastLogWithSource(PP_Module module
,
88 const std::string
& source
,
89 const std::string
& value
) = 0;
91 // Returns the given API object associated with the given instance, or NULL
92 // if the instance is invalid.
93 virtual thunk::PPB_Instance_API
* GetInstanceAPI(PP_Instance instance
) = 0;
94 virtual thunk::ResourceCreationAPI
* GetResourceCreationAPI(
95 PP_Instance instance
) = 0;
97 // Returns the PP_Module associated with the given PP_Instance, or 0 on
99 virtual PP_Module
GetModuleForInstance(PP_Instance instance
) = 0;
101 // Returns the base::SingleThreadTaskRunner for the main thread. This is set
102 // in the constructor, so PpapiGlobals must be created on the main thread.
103 base::SingleThreadTaskRunner
* GetMainThreadMessageLoop();
105 // In tests, the PpapiGlobals object persists across tests but the MLP pointer
106 // it hangs on will go stale and the next PPAPI test will crash because of
107 // thread checks. This resets the pointer to be the current MLP object.
108 void ResetMainThreadMessageLoopForTesting();
110 // Return the MessageLoopShared of the current thread, if any. This will
111 // always return NULL on the host side, where PPB_MessageLoop is not
113 virtual MessageLoopShared
* GetCurrentMessageLoop() = 0;
115 // Returns a task runner for file operations that may block.
116 // TODO(bbudge) Move this to PluginGlobals when we no longer support
117 // in-process plugins.
118 virtual base::TaskRunner
* GetFileTaskRunner() = 0;
120 // Returns the command line for the process.
121 virtual std::string
GetCmdLine() = 0;
123 // Preloads the font on Windows, does nothing on other platforms.
124 // TODO(brettw) remove this by passing the instance into the API so we don't
125 // have to have it on the globals.
126 virtual void PreCacheFontForFlash(const void* logfontw
) = 0;
128 virtual bool IsHostGlobals() const;
129 virtual bool IsPluginGlobals() const;
131 // Records that the plugin is active. The plugin reports that it is active to
132 // containers that monitor and shutdown idle content such as background apps.
133 // This method only has an effect on the plugin process, calls from the
134 // renderer process will have no effect.
135 virtual void MarkPluginIsActive();
138 // Return the thread-local pointer which is used only for unit testing. It
139 // should always be NULL when running in production. It allows separate
140 // threads to have distinct "globals".
141 static PpapiGlobals
* GetThreadLocalPointer();
143 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner_
;
145 DISALLOW_COPY_AND_ASSIGN(PpapiGlobals
);
150 #endif // PPAPI_SHARED_IMPL_PPAPI_GLOBALS_H_