Save errno for logging before potentially overwriting it.
[chromium-blink-merge.git] / ppapi / shared_impl / ppapi_globals.h
blob4c094c16396e4177f00d3fa56719cc6eb75d3e6b
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_
8 #include <string>
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"
19 namespace base {
20 class Lock;
21 class MessageLoopProxy;
24 namespace ppapi {
26 class CallbackTracker;
27 class MessageLoopShared;
28 class ResourceTracker;
29 class VarTracker;
31 namespace thunk {
32 class PPB_Instance_API;
33 class ResourceCreationAPI;
36 // Abstract base class
37 class PPAPI_SHARED_EXPORT PpapiGlobals {
38 public:
39 // Must be created on the main thread.
40 PpapiGlobals();
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"
62 // parameter.
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 virtual base::Lock* GetProxyLock() = 0;
73 // Logs the given string to the JS console. If "source" is empty, the name of
74 // the current module will be used, if it can be determined.
75 virtual void LogWithSource(PP_Instance instance,
76 PP_LogLevel level,
77 const std::string& source,
78 const std::string& value) = 0;
80 // Like LogWithSource but broadcasts the log to all instances of the given
81 // module. The module may be 0 to specify that all consoles possibly
82 // associated with the calling code should be notified. This allows us to
83 // log errors for things like bad resource IDs where we may not have an
84 // associated instance.
86 // Note that in the plugin process, the module parameter is ignored since
87 // there is only one possible one.
88 virtual void BroadcastLogWithSource(PP_Module module,
89 PP_LogLevel level,
90 const std::string& source,
91 const std::string& value) = 0;
93 // Returns the given API object associated with the given instance, or NULL
94 // if the instance is invalid.
95 virtual thunk::PPB_Instance_API* GetInstanceAPI(PP_Instance instance) = 0;
96 virtual thunk::ResourceCreationAPI* GetResourceCreationAPI(
97 PP_Instance instance) = 0;
99 // Returns the PP_Module associated with the given PP_Instance, or 0 on
100 // failure.
101 virtual PP_Module GetModuleForInstance(PP_Instance instance) = 0;
103 // Returns the base::MessageLoopProxy for the main thread. This is set in the
104 // constructor, so PpapiGlobals must be created on the main thread.
105 base::MessageLoopProxy* GetMainThreadMessageLoop();
107 // In tests, the PpapiGlobals object persists across tests but the MLP pointer
108 // it hangs on will go stale and the next PPAPI test will crash because of
109 // thread checks. This resets the pointer to be the current MLP object.
110 void ResetMainThreadMessageLoopForTesting();
112 // Return the MessageLoopShared of the current thread, if any. This will
113 // always return NULL on the host side, where PPB_MessageLoop is not
114 // supported.
115 virtual MessageLoopShared* GetCurrentMessageLoop() = 0;
117 // Returns the command line for the process.
118 virtual std::string GetCmdLine() = 0;
120 // Preloads the font on Windows, does nothing on other platforms.
121 // TODO(brettw) remove this by passing the instance into the API so we don't
122 // have to have it on the globals.
123 virtual void PreCacheFontForFlash(const void* logfontw) = 0;
125 virtual bool IsHostGlobals() const;
126 virtual bool IsPluginGlobals() const;
128 private:
129 // Return the thread-local pointer which is used only for unit testing. It
130 // should always be NULL when running in production. It allows separate
131 // threads to have distinct "globals".
132 static PpapiGlobals* GetThreadLocalPointer();
134 scoped_refptr<base::MessageLoopProxy> main_loop_proxy_;
136 DISALLOW_COPY_AND_ASSIGN(PpapiGlobals);
139 } // namespace ppapi
141 #endif // PPAPI_SHARED_IMPL_PPAPI_GLOBALS_H_