[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 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 "base/basictypes.h" | ||||
[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 9 | #include "base/threading/thread_local.h" // For testing purposes only. |
[email protected] | 2f59e38 | 2011-10-20 22:51:01 | [diff] [blame] | 10 | #include "ppapi/c/pp_instance.h" |
11 | #include "ppapi/c/pp_module.h" | ||||
[email protected] | ac4b54d | 2011-10-20 23:09:28 | [diff] [blame] | 12 | #include "ppapi/shared_impl/api_id.h" |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 13 | #include "ppapi/shared_impl/ppapi_shared_export.h" |
14 | |||||
[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 15 | namespace base { |
16 | class Lock; | ||||
17 | } | ||||
18 | |||||
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 19 | namespace ppapi { |
20 | |||||
[email protected] | bbc4912 | 2011-12-29 20:16:50 | [diff] [blame] | 21 | class CallbackTracker; |
[email protected] | 2f59e38 | 2011-10-20 22:51:01 | [diff] [blame] | 22 | class FunctionGroupBase; |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 23 | class ResourceTracker; |
24 | class VarTracker; | ||||
25 | |||||
26 | // Abstract base class | ||||
27 | class PPAPI_SHARED_EXPORT PpapiGlobals { | ||||
28 | public: | ||||
29 | PpapiGlobals(); | ||||
[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 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 | |||||
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 38 | virtual ~PpapiGlobals(); |
39 | |||||
40 | // Getter for the global singleton. | ||||
[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 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); | ||||
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 59 | |
[email protected] | 2f59e38 | 2011-10-20 22:51:01 | [diff] [blame] | 60 | // Retrieves the corresponding tracker. |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 61 | virtual ResourceTracker* GetResourceTracker() = 0; |
62 | virtual VarTracker* GetVarTracker() = 0; | ||||
[email protected] | bbc4912 | 2011-12-29 20:16:50 | [diff] [blame] | 63 | virtual CallbackTracker* GetCallbackTrackerForInstance( |
64 | PP_Instance instance) = 0; | ||||
[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 65 | virtual base::Lock* GetProxyLock() = 0; |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 66 | |
[email protected] | 2f59e38 | 2011-10-20 22:51:01 | [diff] [blame] | 67 | // Returns the function object corresponding to the given ID, or NULL if |
68 | // there isn't one. | ||||
[email protected] | ac4b54d | 2011-10-20 23:09:28 | [diff] [blame] | 69 | virtual FunctionGroupBase* GetFunctionAPI(PP_Instance inst, ApiID id) = 0; |
[email protected] | 2f59e38 | 2011-10-20 22:51:01 | [diff] [blame] | 70 | |
71 | // Returns the PP_Module associated with the given PP_Instance, or 0 on | ||||
72 | // failure. | ||||
73 | virtual PP_Module GetModuleForInstance(PP_Instance instance) = 0; | ||||
74 | |||||
[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 75 | virtual bool IsHostGlobals() const; |
76 | virtual bool IsPluginGlobals() const; | ||||
77 | |||||
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 78 | private: |
[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 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 | |||||
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 84 | static PpapiGlobals* ppapi_globals_; |
85 | |||||
86 | DISALLOW_COPY_AND_ASSIGN(PpapiGlobals); | ||||
87 | }; | ||||
88 | |||||
89 | } // namespace ppapi | ||||
90 | |||||
91 | #endif // PPAPI_SHARED_IMPL_PPAPI_GLOBALS_H_ |