blob: 737c3a287fd3d12e4eb52c171222ed0b5ba6b788 [file] [log] [blame]
[email protected]73097562012-01-12 19:38:551// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]794d83cd2011-10-20 19:09:202// 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]73097562012-01-12 19:38:559#include "base/threading/thread_local.h" // For testing purposes only.
[email protected]2f59e382011-10-20 22:51:0110#include "ppapi/c/pp_instance.h"
11#include "ppapi/c/pp_module.h"
[email protected]ac4b54d2011-10-20 23:09:2812#include "ppapi/shared_impl/api_id.h"
[email protected]794d83cd2011-10-20 19:09:2013#include "ppapi/shared_impl/ppapi_shared_export.h"
14
[email protected]73097562012-01-12 19:38:5515namespace base {
16class Lock;
17}
18
[email protected]794d83cd2011-10-20 19:09:2019namespace ppapi {
20
[email protected]bbc49122011-12-29 20:16:5021class CallbackTracker;
[email protected]2f59e382011-10-20 22:51:0122class FunctionGroupBase;
[email protected]794d83cd2011-10-20 19:09:2023class ResourceTracker;
24class VarTracker;
25
26// Abstract base class
27class PPAPI_SHARED_EXPORT PpapiGlobals {
28 public:
29 PpapiGlobals();
[email protected]73097562012-01-12 19:38:5530
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]794d83cd2011-10-20 19:09:2038 virtual ~PpapiGlobals();
39
40 // Getter for the global singleton.
[email protected]73097562012-01-12 19:38:5541 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]794d83cd2011-10-20 19:09:2059
[email protected]2f59e382011-10-20 22:51:0160 // Retrieves the corresponding tracker.
[email protected]794d83cd2011-10-20 19:09:2061 virtual ResourceTracker* GetResourceTracker() = 0;
62 virtual VarTracker* GetVarTracker() = 0;
[email protected]bbc49122011-12-29 20:16:5063 virtual CallbackTracker* GetCallbackTrackerForInstance(
64 PP_Instance instance) = 0;
[email protected]73097562012-01-12 19:38:5565 virtual base::Lock* GetProxyLock() = 0;
[email protected]794d83cd2011-10-20 19:09:2066
[email protected]2f59e382011-10-20 22:51:0167 // Returns the function object corresponding to the given ID, or NULL if
68 // there isn't one.
[email protected]ac4b54d2011-10-20 23:09:2869 virtual FunctionGroupBase* GetFunctionAPI(PP_Instance inst, ApiID id) = 0;
[email protected]2f59e382011-10-20 22:51:0170
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]73097562012-01-12 19:38:5575 virtual bool IsHostGlobals() const;
76 virtual bool IsPluginGlobals() const;
77
[email protected]794d83cd2011-10-20 19:09:2078 private:
[email protected]73097562012-01-12 19:38:5579 // 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]794d83cd2011-10-20 19:09:2084 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_