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 CONTENT_RENDERER_PEPPER_HOST_GLOBALS_H_
6#define CONTENT_RENDERER_PEPPER_HOST_GLOBALS_H_
7
8#include "base/compiler_specific.h"
9#include "content/renderer/pepper/host_var_tracker.h"
10#include "ppapi/shared_impl/callback_tracker.h"
11#include "ppapi/shared_impl/ppapi_globals.h"
12#include "ppapi/shared_impl/resource_tracker.h"
13#include "ppapi/shared_impl/var_tracker.h"
14
15namespace content {
16
17class PepperPluginInstanceImpl;
18class PluginModule;
19
20class HostGlobals : public ppapi::PpapiGlobals {
21 public:
22  HostGlobals();
23  explicit HostGlobals(ppapi::PpapiGlobals::PerThreadForTest);
24  virtual ~HostGlobals();
25
26  // Getter for the global singleton. Generally, you should use
27  // PpapiGlobals::Get() when possible. Use this only when you need some
28  // host-specific functionality.
29  inline static HostGlobals* Get() {
30    DCHECK(PpapiGlobals::Get()->IsHostGlobals());
31    return static_cast<HostGlobals*>(PpapiGlobals::Get());
32  }
33
34  // PpapiGlobals implementation.
35  virtual ppapi::ResourceTracker* GetResourceTracker() OVERRIDE;
36  virtual ppapi::VarTracker* GetVarTracker() OVERRIDE;
37  virtual ppapi::CallbackTracker* GetCallbackTrackerForInstance(
38      PP_Instance instance) OVERRIDE;
39  virtual ppapi::thunk::PPB_Instance_API* GetInstanceAPI(
40      PP_Instance instance) OVERRIDE;
41  virtual ppapi::thunk::ResourceCreationAPI* GetResourceCreationAPI(
42      PP_Instance instance) OVERRIDE;
43  virtual PP_Module GetModuleForInstance(PP_Instance instance) OVERRIDE;
44  virtual std::string GetCmdLine() OVERRIDE;
45  virtual void PreCacheFontForFlash(const void* logfontw) OVERRIDE;
46  virtual base::Lock* GetProxyLock() OVERRIDE;
47  virtual void LogWithSource(PP_Instance instance,
48                             PP_LogLevel level,
49                             const std::string& source,
50                             const std::string& value) OVERRIDE;
51  virtual void BroadcastLogWithSource(PP_Module module,
52                                      PP_LogLevel level,
53                                      const std::string& source,
54                                      const std::string& value) OVERRIDE;
55  virtual ppapi::MessageLoopShared* GetCurrentMessageLoop() OVERRIDE;
56  virtual base::TaskRunner* GetFileTaskRunner(PP_Instance instance) OVERRIDE;
57
58  HostVarTracker* host_var_tracker() {
59    return &host_var_tracker_;
60  }
61
62  // PP_Modules ----------------------------------------------------------------
63
64  // Adds a new plugin module to the list of tracked module, and returns a new
65  // module handle to identify it.
66  PP_Module AddModule(PluginModule* module);
67
68  // Called when a plugin modulde was deleted and should no longer be tracked.
69  // The given handle should be one generated by AddModule.
70  void ModuleDeleted(PP_Module module);
71
72  // Returns a pointer to the plugin modulde object associated with the given
73  // modulde handle. The return value will be NULL if the handle is invalid.
74  PluginModule* GetModule(PP_Module module);
75
76  // PP_Instances --------------------------------------------------------------
77
78  // Adds a new plugin instance to the list of tracked instances, and returns a
79  // new instance handle to identify it.
80  PP_Instance AddInstance(PepperPluginInstanceImpl* instance);
81
82  // Called when a plugin instance was deleted and should no longer be tracked.
83  // The given handle should be one generated by AddInstance.
84  void InstanceDeleted(PP_Instance instance);
85
86  void InstanceCrashed(PP_Instance instance);
87
88  // Returns a pointer to the plugin instance object associated with the given
89  // instance handle. The return value will be NULL if the handle is invalid or
90  // if the instance has crashed.
91  PepperPluginInstanceImpl* GetInstance(PP_Instance instance);
92
93 private:
94  // PpapiGlobals overrides.
95  virtual bool IsHostGlobals() const OVERRIDE;
96
97  static HostGlobals* host_globals_;
98
99  ppapi::ResourceTracker resource_tracker_;
100  HostVarTracker host_var_tracker_;
101
102  // Tracks all live instances and their associated object.
103  typedef std::map<PP_Instance, PepperPluginInstanceImpl*> InstanceMap;
104  InstanceMap instance_map_;
105
106  // Tracks all live modules. The pointers are non-owning, the PluginModule
107  // destructor will notify us when the module is deleted.
108  typedef std::map<PP_Module, PluginModule*> ModuleMap;
109  ModuleMap module_map_;
110
111  DISALLOW_COPY_AND_ASSIGN(HostGlobals);
112};
113
114}  // namespace content
115
116#endif   // CONTENT_RENDERER_PEPPER_HOST_GLOBALS_H_
117