Home | History | Annotate | Download | only in shared_impl
      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.
      4 
      5 #ifndef PPAPI_SHARED_IMPL_PPAPI_GLOBALS_H_
      6 #define PPAPI_SHARED_IMPL_PPAPI_GLOBALS_H_
      7 
      8 #include <string>
      9 
     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"
     18 
     19 namespace base {
     20 class Lock;
     21 class MessageLoopProxy;
     22 class TaskRunner;
     23 }
     24 
     25 namespace ppapi {
     26 
     27 class CallbackTracker;
     28 class MessageLoopShared;
     29 class ResourceTracker;
     30 class VarTracker;
     31 
     32 namespace thunk {
     33 class PPB_Instance_API;
     34 class ResourceCreationAPI;
     35 }
     36 
     37 // Abstract base class
     38 class PPAPI_SHARED_EXPORT PpapiGlobals {
     39  public:
     40   // Must be created on the main thread.
     41   PpapiGlobals();
     42 
     43   // This constructor is to be used only for making a PpapiGlobal for testing
     44   // purposes. This avoids setting the global static ppapi_globals_. For unit
     45   // tests that use this feature, the "test" PpapiGlobals should be constructed
     46   // using this method. See SetPpapiGlobalsOnThreadForTest for more information.
     47   struct PerThreadForTest {};
     48   explicit PpapiGlobals(PerThreadForTest);
     49 
     50   virtual ~PpapiGlobals();
     51 
     52   // Getter for the global singleton.
     53   static PpapiGlobals* Get();
     54 
     55   // This allows us to set a given PpapiGlobals object as the PpapiGlobals for
     56   // a given thread. After setting the PpapiGlobals for a thread, Get() will
     57   // return that PpapiGlobals when Get() is called on that thread. Other threads
     58   // are unaffected. This allows us to have tests which use >1 PpapiGlobals in
     59   // the same process, e.g. for having 1 thread emulate the "host" and 1 thread
     60   // emulate the "plugin".
     61   //
     62   // PpapiGlobals object must have been constructed using the "PerThreadForTest"
     63   // parameter.
     64   static void SetPpapiGlobalsOnThreadForTest(PpapiGlobals* ptr);
     65 
     66   // Retrieves the corresponding tracker.
     67   virtual ResourceTracker* GetResourceTracker() = 0;
     68   virtual VarTracker* GetVarTracker() = 0;
     69   virtual CallbackTracker* GetCallbackTrackerForInstance(
     70       PP_Instance instance) = 0;
     71 
     72   virtual base::Lock* GetProxyLock() = 0;
     73 
     74   // Logs the given string to the JS console. If "source" is empty, the name of
     75   // the current module will be used, if it can be determined.
     76   virtual void LogWithSource(PP_Instance instance,
     77                              PP_LogLevel level,
     78                              const std::string& source,
     79                              const std::string& value) = 0;
     80 
     81   // Like LogWithSource but broadcasts the log to all instances of the given
     82   // module. The module may be 0 to specify that all consoles possibly
     83   // associated with the calling code should be notified. This allows us to
     84   // log errors for things like bad resource IDs where we may not have an
     85   // associated instance.
     86   //
     87   // Note that in the plugin process, the module parameter is ignored since
     88   // there is only one possible one.
     89   virtual void BroadcastLogWithSource(PP_Module module,
     90                                       PP_LogLevel level,
     91                                       const std::string& source,
     92                                       const std::string& value) = 0;
     93 
     94   // Returns the given API object associated with the given instance, or NULL
     95   // if the instance is invalid.
     96   virtual thunk::PPB_Instance_API* GetInstanceAPI(PP_Instance instance) = 0;
     97   virtual thunk::ResourceCreationAPI* GetResourceCreationAPI(
     98       PP_Instance instance) = 0;
     99 
    100   // Returns the PP_Module associated with the given PP_Instance, or 0 on
    101   // failure.
    102   virtual PP_Module GetModuleForInstance(PP_Instance instance) = 0;
    103 
    104   // Returns the base::MessageLoopProxy for the main thread. This is set in the
    105   // constructor, so PpapiGlobals must be created on the main thread.
    106   base::MessageLoopProxy* GetMainThreadMessageLoop();
    107 
    108   // In tests, the PpapiGlobals object persists across tests but the MLP pointer
    109   // it hangs on will go stale and the next PPAPI test will crash because of
    110   // thread checks. This resets the pointer to be the current MLP object.
    111   void ResetMainThreadMessageLoopForTesting();
    112 
    113   // Return the MessageLoopShared of the current thread, if any. This will
    114   // always return NULL on the host side, where PPB_MessageLoop is not
    115   // supported.
    116   virtual MessageLoopShared* GetCurrentMessageLoop() = 0;
    117 
    118   // Returns a task runner for file operations that may block.
    119   // TODO(bbudge) Move this to PluginGlobals when we no longer support
    120   // in-process plugins.
    121   virtual base::TaskRunner* GetFileTaskRunner(PP_Instance instance) = 0;
    122 
    123   // Returns the command line for the process.
    124   virtual std::string GetCmdLine() = 0;
    125 
    126   // Preloads the font on Windows, does nothing on other platforms.
    127   // TODO(brettw) remove this by passing the instance into the API so we don't
    128   // have to have it on the globals.
    129   virtual void PreCacheFontForFlash(const void* logfontw) = 0;
    130 
    131   virtual bool IsHostGlobals() const;
    132   virtual bool IsPluginGlobals() const;
    133 
    134  private:
    135   // Return the thread-local pointer which is used only for unit testing. It
    136   // should always be NULL when running in production. It allows separate
    137   // threads to have distinct "globals".
    138   static PpapiGlobals* GetThreadLocalPointer();
    139 
    140   scoped_refptr<base::MessageLoopProxy> main_loop_proxy_;
    141 
    142   DISALLOW_COPY_AND_ASSIGN(PpapiGlobals);
    143 };
    144 
    145 }  // namespace ppapi
    146 
    147 #endif  // PPAPI_SHARED_IMPL_PPAPI_GLOBALS_H_
    148