blob: c770c6981e18646b4a4a189a9e9cfaf4a95fa022 [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
[email protected]a9b16dd2012-01-31 05:00:268#include <string>
9
[email protected]adeb8322012-07-14 00:05:1110#include "base/memory/ref_counted.h"
[email protected]73097562012-01-12 19:38:5511#include "base/threading/thread_local.h" // For testing purposes only.
[email protected]2f59e382011-10-20 22:51:0112#include "ppapi/c/pp_instance.h"
13#include "ppapi/c/pp_module.h"
[email protected]598816ad2012-12-13 01:34:3214#include "ppapi/c/ppb_console.h"
[email protected]ac4b54d2011-10-20 23:09:2815#include "ppapi/shared_impl/api_id.h"
[email protected]794d83cd2011-10-20 19:09:2016#include "ppapi/shared_impl/ppapi_shared_export.h"
17
[email protected]73097562012-01-12 19:38:5518namespace base {
skyostil23490d42015-06-12 12:54:2619class SingleThreadTaskRunner;
[email protected]f2d7c042013-07-11 04:37:5820class TaskRunner;
[email protected]73097562012-01-12 19:38:5521}
22
[email protected]794d83cd2011-10-20 19:09:2023namespace ppapi {
24
[email protected]bbc49122011-12-29 20:16:5025class CallbackTracker;
[email protected]77c34172012-11-08 18:55:1626class MessageLoopShared;
[email protected]794d83cd2011-10-20 19:09:2027class ResourceTracker;
28class VarTracker;
29
[email protected]4f2006122012-04-30 05:13:1730namespace thunk {
31class PPB_Instance_API;
32class ResourceCreationAPI;
33}
34
[email protected]794d83cd2011-10-20 19:09:2035// Abstract base class
36class PPAPI_SHARED_EXPORT PpapiGlobals {
37 public:
[email protected]77c34172012-11-08 18:55:1638 // Must be created on the main thread.
[email protected]794d83cd2011-10-20 19:09:2039 PpapiGlobals();
[email protected]73097562012-01-12 19:38:5540
41 // This constructor is to be used only for making a PpapiGlobal for testing
42 // purposes. This avoids setting the global static ppapi_globals_. For unit
43 // tests that use this feature, the "test" PpapiGlobals should be constructed
44 // using this method. See SetPpapiGlobalsOnThreadForTest for more information.
[email protected]7ef6b79b2013-01-17 02:38:2445 struct PerThreadForTest {};
46 explicit PpapiGlobals(PerThreadForTest);
[email protected]73097562012-01-12 19:38:5547
Peter Boström3d5b3cb2021-09-23 21:35:4548 PpapiGlobals(const PpapiGlobals&) = delete;
49 PpapiGlobals& operator=(const PpapiGlobals&) = delete;
50
[email protected]794d83cd2011-10-20 19:09:2051 virtual ~PpapiGlobals();
52
53 // Getter for the global singleton.
[email protected]f12e596a2013-05-21 01:42:1054 static PpapiGlobals* Get();
[email protected]73097562012-01-12 19:38:5555
56 // This allows us to set a given PpapiGlobals object as the PpapiGlobals for
57 // a given thread. After setting the PpapiGlobals for a thread, Get() will
58 // return that PpapiGlobals when Get() is called on that thread. Other threads
59 // are unaffected. This allows us to have tests which use >1 PpapiGlobals in
60 // the same process, e.g. for having 1 thread emulate the "host" and 1 thread
61 // emulate the "plugin".
62 //
[email protected]7ef6b79b2013-01-17 02:38:2463 // PpapiGlobals object must have been constructed using the "PerThreadForTest"
[email protected]73097562012-01-12 19:38:5564 // parameter.
65 static void SetPpapiGlobalsOnThreadForTest(PpapiGlobals* ptr);
[email protected]794d83cd2011-10-20 19:09:2066
[email protected]2f59e382011-10-20 22:51:0167 // Retrieves the corresponding tracker.
[email protected]794d83cd2011-10-20 19:09:2068 virtual ResourceTracker* GetResourceTracker() = 0;
69 virtual VarTracker* GetVarTracker() = 0;
[email protected]bbc49122011-12-29 20:16:5070 virtual CallbackTracker* GetCallbackTrackerForInstance(
71 PP_Instance instance) = 0;
[email protected]77c34172012-11-08 18:55:1672
[email protected]a9b16dd2012-01-31 05:00:2673 // 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,
[email protected]598816ad2012-12-13 01:34:3276 PP_LogLevel level,
[email protected]a9b16dd2012-01-31 05:00:2677 const std::string& source,
78 const std::string& value) = 0;
79
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.
85 //
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,
[email protected]598816ad2012-12-13 01:34:3289 PP_LogLevel level,
[email protected]a9b16dd2012-01-31 05:00:2690 const std::string& source,
91 const std::string& value) = 0;
92
[email protected]4f2006122012-04-30 05:13:1793 // 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;
[email protected]2f59e382011-10-20 22:51:0198
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;
102
skyostil23490d42015-06-12 12:54:26103 // Returns the base::SingleThreadTaskRunner for the main thread. This is set
104 // in the constructor, so PpapiGlobals must be created on the main thread.
105 base::SingleThreadTaskRunner* GetMainThreadMessageLoop();
[email protected]aed96532012-06-23 14:27:42106
[email protected]4e7a8c32013-04-23 04:09:59107 // 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();
111
[email protected]77c34172012-11-08 18:55:16112 // 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;
116
[email protected]f2d7c042013-07-11 04:37:58117 // Returns a task runner for file operations that may block.
118 // TODO(bbudge) Move this to PluginGlobals when we no longer support
119 // in-process plugins.
[email protected]3b5c68582013-10-01 06:20:44120 virtual base::TaskRunner* GetFileTaskRunner() = 0;
[email protected]f2d7c042013-07-11 04:37:58121
[email protected]73097562012-01-12 19:38:55122 virtual bool IsHostGlobals() const;
123 virtual bool IsPluginGlobals() const;
124
[email protected]794d83cd2011-10-20 19:09:20125 private:
[email protected]73097562012-01-12 19:38:55126 // Return the thread-local pointer which is used only for unit testing. It
127 // should always be NULL when running in production. It allows separate
128 // threads to have distinct "globals".
129 static PpapiGlobals* GetThreadLocalPointer();
130
skyostil23490d42015-06-12 12:54:26131 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
[email protected]794d83cd2011-10-20 19:09:20132};
133
134} // namespace ppapi
135
136#endif // PPAPI_SHARED_IMPL_PPAPI_GLOBALS_H_