plugin_globals.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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_PROXY_PLUGIN_GLOBALS_H_
6#define PPAPI_PROXY_PLUGIN_GLOBALS_H_
7
8#include <string>
9
10#include "base/compiler_specific.h"
11#include "base/memory/scoped_ptr.h"
12#include "base/synchronization/lock.h"
13#include "base/threading/thread_local_storage.h"
14#include "ppapi/proxy/plugin_resource_tracker.h"
15#include "ppapi/proxy/plugin_var_tracker.h"
16#include "ppapi/proxy/ppapi_proxy_export.h"
17#include "ppapi/shared_impl/callback_tracker.h"
18#include "ppapi/shared_impl/ppapi_globals.h"
19
20namespace IPC {
21class Sender;
22}
23
24namespace ppapi {
25namespace proxy {
26
27class MessageLoopResource;
28class PluginProxyDelegate;
29
30class PPAPI_PROXY_EXPORT PluginGlobals : public PpapiGlobals {
31 public:
32  PluginGlobals();
33  explicit PluginGlobals(PpapiGlobals::PerThreadForTest);
34  virtual ~PluginGlobals();
35
36  // Getter for the global singleton. Generally, you should use
37  // PpapiGlobals::Get() when possible. Use this only when you need some
38  // plugin-specific functionality.
39  inline static PluginGlobals* Get() {
40    // Explicitly crash if this is the wrong process type, we want to get
41    // crash reports.
42    CHECK(PpapiGlobals::Get()->IsPluginGlobals());
43    return static_cast<PluginGlobals*>(PpapiGlobals::Get());
44  }
45
46  // PpapiGlobals implementation.
47  virtual ResourceTracker* GetResourceTracker() OVERRIDE;
48  virtual VarTracker* GetVarTracker() OVERRIDE;
49  virtual CallbackTracker* GetCallbackTrackerForInstance(
50      PP_Instance instance) OVERRIDE;
51  virtual thunk::PPB_Instance_API* GetInstanceAPI(
52      PP_Instance instance) OVERRIDE;
53  virtual thunk::ResourceCreationAPI* GetResourceCreationAPI(
54      PP_Instance instance) OVERRIDE;
55  virtual PP_Module GetModuleForInstance(PP_Instance instance) OVERRIDE;
56  virtual std::string GetCmdLine() OVERRIDE;
57  virtual void PreCacheFontForFlash(const void* logfontw) OVERRIDE;
58  virtual base::Lock* GetProxyLock() OVERRIDE;
59  virtual void LogWithSource(PP_Instance instance,
60                             PP_LogLevel level,
61                             const std::string& source,
62                             const std::string& value) OVERRIDE;
63  virtual void BroadcastLogWithSource(PP_Module module,
64                                      PP_LogLevel level,
65                                      const std::string& source,
66                                      const std::string& value) OVERRIDE;
67  virtual MessageLoopShared* GetCurrentMessageLoop() OVERRIDE;
68
69  // Returns the channel for sending to the browser.
70  IPC::Sender* GetBrowserSender();
71
72  // Returns the language code of the current UI language.
73  std::string GetUILanguage();
74
75  // Sets the active url which is reported by breakpad.
76  void SetActiveURL(const std::string& url);
77
78  // Getters for the plugin-specific versions.
79  PluginResourceTracker* plugin_resource_tracker() {
80    return &plugin_resource_tracker_;
81  }
82  PluginVarTracker* plugin_var_tracker() {
83    return &plugin_var_tracker_;
84  }
85
86  // The embedder should call set_proxy_delegate during startup.
87  void set_plugin_proxy_delegate(PluginProxyDelegate* d) {
88    plugin_proxy_delegate_ = d;
89  }
90
91  // Returns the TLS slot that holds the message loop TLS.
92  //
93  // If we end up needing more TLS storage for more stuff, we should probably
94  // have a struct in here for the different items.
95  base::ThreadLocalStorage::Slot* msg_loop_slot() {
96    return msg_loop_slot_.get();
97  }
98
99  // Sets the message loop slot, takes ownership of the given heap-alloated
100  // pointer.
101  void set_msg_loop_slot(base::ThreadLocalStorage::Slot* slot) {
102    msg_loop_slot_.reset(slot);
103  }
104
105  // Return the special Resource that represents the MessageLoop for the main
106  // thread. This Resource is not associated with any instance, and lives as
107  // long as the plugin.
108  MessageLoopResource* loop_for_main_thread();
109
110  // The embedder should call this function when the name of the plugin module
111  // is known. This will be used for error logging.
112  void set_plugin_name(const std::string& name) { plugin_name_ = name; }
113
114  // The embedder should call this function when the command line is known.
115  void set_command_line(const std::string& c) { command_line_ = c; }
116
117  // Sets whether threadsafety is supported. Defaults to whether the
118  // ENABLE_PEPPER_THREADING build flag is set.
119  void set_enable_threading(bool enable) { enable_threading_ = enable; }
120
121 private:
122  class BrowserSender;
123
124  // PpapiGlobals overrides.
125  virtual bool IsPluginGlobals() const OVERRIDE;
126
127  static PluginGlobals* plugin_globals_;
128
129  PluginProxyDelegate* plugin_proxy_delegate_;
130  PluginResourceTracker plugin_resource_tracker_;
131  PluginVarTracker plugin_var_tracker_;
132  scoped_refptr<CallbackTracker> callback_tracker_;
133
134  bool enable_threading_;  // Indicates whether we'll use the lock.
135  base::Lock proxy_lock_;
136
137  scoped_ptr<base::ThreadLocalStorage::Slot> msg_loop_slot_;
138  // Note that loop_for_main_thread's constructor sets msg_loop_slot_, so it
139  // must be initialized after msg_loop_slot_ (hence the order here).
140  scoped_refptr<MessageLoopResource> loop_for_main_thread_;
141
142  // Name of the plugin used for error logging. This will be empty until
143  // set_plugin_name is called.
144  std::string plugin_name_;
145
146  // Command line for the plugin. This will be empty until set_command_line is
147  // called.
148  std::string command_line_;
149
150  scoped_ptr<BrowserSender> browser_sender_;
151
152  DISALLOW_COPY_AND_ASSIGN(PluginGlobals);
153};
154
155}  // namespace proxy
156}  // namespace ppapi
157
158#endif   // PPAPI_PROXY_PLUGIN_GLOBALS_H_
159