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