[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 | |
[email protected] | a9b16dd | 2012-01-31 05:00:26 | [diff] [blame] | 8 | #include <string> |
| 9 | |
[email protected] | adeb832 | 2012-07-14 00:05:11 | [diff] [blame] | 10 | #include "base/memory/ref_counted.h" |
[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 11 | #include "base/threading/thread_local.h" // For testing purposes only. |
[email protected] | 2f59e38 | 2011-10-20 22:51:01 | [diff] [blame] | 12 | #include "ppapi/c/pp_instance.h" |
| 13 | #include "ppapi/c/pp_module.h" |
[email protected] | 598816ad | 2012-12-13 01:34:32 | [diff] [blame] | 14 | #include "ppapi/c/ppb_console.h" |
[email protected] | ac4b54d | 2011-10-20 23:09:28 | [diff] [blame] | 15 | #include "ppapi/shared_impl/api_id.h" |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 16 | #include "ppapi/shared_impl/ppapi_shared_export.h" |
| 17 | |
[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 18 | namespace base { |
skyostil | 23490d4 | 2015-06-12 12:54:26 | [diff] [blame] | 19 | class SingleThreadTaskRunner; |
[email protected] | f2d7c04 | 2013-07-11 04:37:58 | [diff] [blame] | 20 | class TaskRunner; |
[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 21 | } |
| 22 | |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 23 | namespace ppapi { |
| 24 | |
[email protected] | bbc4912 | 2011-12-29 20:16:50 | [diff] [blame] | 25 | class CallbackTracker; |
[email protected] | 77c3417 | 2012-11-08 18:55:16 | [diff] [blame] | 26 | class MessageLoopShared; |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 27 | class ResourceTracker; |
| 28 | class VarTracker; |
| 29 | |
[email protected] | 4f200612 | 2012-04-30 05:13:17 | [diff] [blame] | 30 | namespace thunk { |
| 31 | class PPB_Instance_API; |
| 32 | class ResourceCreationAPI; |
| 33 | } |
| 34 | |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 35 | // Abstract base class |
| 36 | class PPAPI_SHARED_EXPORT PpapiGlobals { |
| 37 | public: |
[email protected] | 77c3417 | 2012-11-08 18:55:16 | [diff] [blame] | 38 | // Must be created on the main thread. |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 39 | PpapiGlobals(); |
[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 40 | |
| 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] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 45 | struct PerThreadForTest {}; |
| 46 | explicit PpapiGlobals(PerThreadForTest); |
[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 47 | |
Peter Boström | 3d5b3cb | 2021-09-23 21:35:45 | [diff] [blame] | 48 | PpapiGlobals(const PpapiGlobals&) = delete; |
| 49 | PpapiGlobals& operator=(const PpapiGlobals&) = delete; |
| 50 | |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 51 | virtual ~PpapiGlobals(); |
| 52 | |
| 53 | // Getter for the global singleton. |
[email protected] | f12e596a | 2013-05-21 01:42:10 | [diff] [blame] | 54 | static PpapiGlobals* Get(); |
[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 55 | |
| 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] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 63 | // PpapiGlobals object must have been constructed using the "PerThreadForTest" |
[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 64 | // parameter. |
| 65 | static void SetPpapiGlobalsOnThreadForTest(PpapiGlobals* ptr); |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 66 | |
[email protected] | 2f59e38 | 2011-10-20 22:51:01 | [diff] [blame] | 67 | // Retrieves the corresponding tracker. |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 68 | virtual ResourceTracker* GetResourceTracker() = 0; |
| 69 | virtual VarTracker* GetVarTracker() = 0; |
[email protected] | bbc4912 | 2011-12-29 20:16:50 | [diff] [blame] | 70 | virtual CallbackTracker* GetCallbackTrackerForInstance( |
| 71 | PP_Instance instance) = 0; |
[email protected] | 77c3417 | 2012-11-08 18:55:16 | [diff] [blame] | 72 | |
[email protected] | a9b16dd | 2012-01-31 05:00:26 | [diff] [blame] | 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, |
[email protected] | 598816ad | 2012-12-13 01:34:32 | [diff] [blame] | 76 | PP_LogLevel level, |
[email protected] | a9b16dd | 2012-01-31 05:00:26 | [diff] [blame] | 77 | 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] | 598816ad | 2012-12-13 01:34:32 | [diff] [blame] | 89 | PP_LogLevel level, |
[email protected] | a9b16dd | 2012-01-31 05:00:26 | [diff] [blame] | 90 | const std::string& source, |
| 91 | const std::string& value) = 0; |
| 92 | |
[email protected] | 4f200612 | 2012-04-30 05:13:17 | [diff] [blame] | 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; |
[email protected] | 2f59e38 | 2011-10-20 22:51:01 | [diff] [blame] | 98 | |
| 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 | |
skyostil | 23490d4 | 2015-06-12 12:54:26 | [diff] [blame] | 103 | // 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] | aed9653 | 2012-06-23 14:27:42 | [diff] [blame] | 106 | |
[email protected] | 4e7a8c3 | 2013-04-23 04:09:59 | [diff] [blame] | 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(); |
| 111 | |
[email protected] | 77c3417 | 2012-11-08 18:55:16 | [diff] [blame] | 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; |
| 116 | |
[email protected] | f2d7c04 | 2013-07-11 04:37:58 | [diff] [blame] | 117 | // 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] | 3b5c6858 | 2013-10-01 06:20:44 | [diff] [blame] | 120 | virtual base::TaskRunner* GetFileTaskRunner() = 0; |
[email protected] | f2d7c04 | 2013-07-11 04:37:58 | [diff] [blame] | 121 | |
[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 122 | virtual bool IsHostGlobals() const; |
| 123 | virtual bool IsPluginGlobals() const; |
| 124 | |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 125 | private: |
[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 126 | // 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 | |
skyostil | 23490d4 | 2015-06-12 12:54:26 | [diff] [blame] | 131 | scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | } // namespace ppapi |
| 135 | |
| 136 | #endif // PPAPI_SHARED_IMPL_PPAPI_GLOBALS_H_ |