OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef PPAPI_SHARED_IMPL_PPAPI_GLOBALS_H_ | 5 #ifndef PPAPI_SHARED_IMPL_PPAPI_GLOBALS_H_ |
6 #define PPAPI_SHARED_IMPL_PPAPI_GLOBALS_H_ | 6 #define PPAPI_SHARED_IMPL_PPAPI_GLOBALS_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/threading/thread_local.h" // For testing purposes only. |
9 #include "ppapi/c/pp_instance.h" | 10 #include "ppapi/c/pp_instance.h" |
10 #include "ppapi/c/pp_module.h" | 11 #include "ppapi/c/pp_module.h" |
11 #include "ppapi/shared_impl/api_id.h" | 12 #include "ppapi/shared_impl/api_id.h" |
12 #include "ppapi/shared_impl/ppapi_shared_export.h" | 13 #include "ppapi/shared_impl/ppapi_shared_export.h" |
13 | 14 |
| 15 namespace base { |
| 16 class Lock; |
| 17 } |
| 18 |
14 namespace ppapi { | 19 namespace ppapi { |
15 | 20 |
16 class CallbackTracker; | 21 class CallbackTracker; |
17 class FunctionGroupBase; | 22 class FunctionGroupBase; |
18 class ResourceTracker; | 23 class ResourceTracker; |
19 class VarTracker; | 24 class VarTracker; |
20 | 25 |
21 // Abstract base class | 26 // Abstract base class |
22 class PPAPI_SHARED_EXPORT PpapiGlobals { | 27 class PPAPI_SHARED_EXPORT PpapiGlobals { |
23 public: | 28 public: |
24 PpapiGlobals(); | 29 PpapiGlobals(); |
| 30 |
| 31 // This constructor is to be used only for making a PpapiGlobal for testing |
| 32 // purposes. This avoids setting the global static ppapi_globals_. For unit |
| 33 // tests that use this feature, the "test" PpapiGlobals should be constructed |
| 34 // using this method. See SetPpapiGlobalsOnThreadForTest for more information. |
| 35 struct ForTest {}; |
| 36 PpapiGlobals(ForTest); |
| 37 |
25 virtual ~PpapiGlobals(); | 38 virtual ~PpapiGlobals(); |
26 | 39 |
27 // Getter for the global singleton. | 40 // Getter for the global singleton. |
28 inline static PpapiGlobals* Get() { return ppapi_globals_; } | 41 inline static PpapiGlobals* Get() { |
| 42 if (ppapi_globals_) |
| 43 return ppapi_globals_; |
| 44 // In unit tests, the following might be valid (see |
| 45 // SetPpapiGlobalsOnThreadForTest). Normally, this will just return NULL. |
| 46 return GetThreadLocalPointer(); |
| 47 } |
| 48 |
| 49 // This allows us to set a given PpapiGlobals object as the PpapiGlobals for |
| 50 // a given thread. After setting the PpapiGlobals for a thread, Get() will |
| 51 // return that PpapiGlobals when Get() is called on that thread. Other threads |
| 52 // are unaffected. This allows us to have tests which use >1 PpapiGlobals in |
| 53 // the same process, e.g. for having 1 thread emulate the "host" and 1 thread |
| 54 // emulate the "plugin". |
| 55 // |
| 56 // PpapiGlobals object must have been constructed using the "ForTest" |
| 57 // parameter. |
| 58 static void SetPpapiGlobalsOnThreadForTest(PpapiGlobals* ptr); |
29 | 59 |
30 // Retrieves the corresponding tracker. | 60 // Retrieves the corresponding tracker. |
31 virtual ResourceTracker* GetResourceTracker() = 0; | 61 virtual ResourceTracker* GetResourceTracker() = 0; |
32 virtual VarTracker* GetVarTracker() = 0; | 62 virtual VarTracker* GetVarTracker() = 0; |
33 virtual CallbackTracker* GetCallbackTrackerForInstance( | 63 virtual CallbackTracker* GetCallbackTrackerForInstance( |
34 PP_Instance instance) = 0; | 64 PP_Instance instance) = 0; |
| 65 virtual base::Lock* GetProxyLock() = 0; |
35 | 66 |
36 // Returns the function object corresponding to the given ID, or NULL if | 67 // Returns the function object corresponding to the given ID, or NULL if |
37 // there isn't one. | 68 // there isn't one. |
38 virtual FunctionGroupBase* GetFunctionAPI(PP_Instance inst, ApiID id) = 0; | 69 virtual FunctionGroupBase* GetFunctionAPI(PP_Instance inst, ApiID id) = 0; |
39 | 70 |
40 // Returns the PP_Module associated with the given PP_Instance, or 0 on | 71 // Returns the PP_Module associated with the given PP_Instance, or 0 on |
41 // failure. | 72 // failure. |
42 virtual PP_Module GetModuleForInstance(PP_Instance instance) = 0; | 73 virtual PP_Module GetModuleForInstance(PP_Instance instance) = 0; |
43 | 74 |
| 75 virtual bool IsHostGlobals() const; |
| 76 virtual bool IsPluginGlobals() const; |
| 77 |
44 private: | 78 private: |
| 79 // Return the thread-local pointer which is used only for unit testing. It |
| 80 // should always be NULL when running in production. It allows separate |
| 81 // threads to have distinct "globals". |
| 82 static PpapiGlobals* GetThreadLocalPointer(); |
| 83 |
45 static PpapiGlobals* ppapi_globals_; | 84 static PpapiGlobals* ppapi_globals_; |
46 | 85 |
47 DISALLOW_COPY_AND_ASSIGN(PpapiGlobals); | 86 DISALLOW_COPY_AND_ASSIGN(PpapiGlobals); |
48 }; | 87 }; |
49 | 88 |
50 } // namespace ppapi | 89 } // namespace ppapi |
51 | 90 |
52 #endif // PPAPI_SHARED_IMPL_PPAPI_GLOBALS_H_ | 91 #endif // PPAPI_SHARED_IMPL_PPAPI_GLOBALS_H_ |
OLD | NEW |