- add sources.
[platform/framework/web/crosswalk.git] / src / content / ppapi_plugin / ppapi_thread.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 CONTENT_PPAPI_PLUGIN_PPAPI_THREAD_H_
6 #define CONTENT_PPAPI_PLUGIN_PPAPI_THREAD_H_
7
8 #include <map>
9 #include <string>
10
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/process/process.h"
15 #include "base/scoped_native_library.h"
16 #include "build/build_config.h"
17 #include "content/child/child_thread.h"
18 #include "content/public/common/pepper_plugin_info.h"
19 #include "ipc/ipc_listener.h"
20 #include "ppapi/c/pp_module.h"
21 #include "ppapi/c/trusted/ppp_broker.h"
22 #include "ppapi/proxy/connection.h"
23 #include "ppapi/proxy/plugin_dispatcher.h"
24 #include "ppapi/proxy/plugin_globals.h"
25 #include "ppapi/proxy/plugin_proxy_delegate.h"
26
27 #if defined(OS_WIN)
28 #include "base/win/scoped_handle.h"
29 #endif
30
31 class CommandLine;
32
33 namespace base {
34 class FilePath;
35 }
36
37 namespace IPC {
38 struct ChannelHandle;
39 }
40
41 namespace content {
42
43 class PpapiWebKitPlatformSupportImpl;
44
45 class PpapiThread : public ChildThread,
46                     public ppapi::proxy::PluginDispatcher::PluginDelegate,
47                     public ppapi::proxy::PluginProxyDelegate {
48  public:
49   PpapiThread(const CommandLine& command_line, bool is_broker);
50   virtual ~PpapiThread();
51   virtual void Shutdown() OVERRIDE;
52
53  private:
54   // Make sure the enum list in tools/histogram/histograms.xml is updated with
55   // any change in this list.
56   enum LoadResult {
57     LOAD_SUCCESS,
58     LOAD_FAILED,
59     ENTRY_POINT_MISSING,
60     INIT_FAILED,
61     // NOTE: Add new values only immediately above this line.
62     LOAD_RESULT_MAX  // Boundary value for UMA_HISTOGRAM_ENUMERATION.
63   };
64
65   // This class finds the target PluginDispatcher for each message it receives
66   // and forwards the message.
67   class DispatcherMessageListener : public IPC::Listener {
68    public:
69     explicit DispatcherMessageListener(PpapiThread* owner);
70     virtual ~DispatcherMessageListener();
71
72     // IPC::Listener implementation.
73     virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
74
75    private:
76     PpapiThread* owner_;
77
78     DISALLOW_COPY_AND_ASSIGN(DispatcherMessageListener);
79   };
80
81   // ChildThread overrides.
82   virtual bool Send(IPC::Message* msg) OVERRIDE;
83   virtual bool OnControlMessageReceived(const IPC::Message& msg) OVERRIDE;
84   virtual void OnChannelConnected(int32 peer_pid) OVERRIDE;
85
86   // PluginDispatcher::PluginDelegate implementation.
87   virtual std::set<PP_Instance>* GetGloballySeenInstanceIDSet() OVERRIDE;
88   virtual base::MessageLoopProxy* GetIPCMessageLoop() OVERRIDE;
89   virtual base::WaitableEvent* GetShutdownEvent() OVERRIDE;
90   virtual IPC::PlatformFileForTransit ShareHandleWithRemote(
91       base::PlatformFile handle,
92       base::ProcessId peer_pid,
93       bool should_close_source) OVERRIDE;
94   virtual uint32 Register(
95       ppapi::proxy::PluginDispatcher* plugin_dispatcher) OVERRIDE;
96   virtual void Unregister(uint32 plugin_dispatcher_id) OVERRIDE;
97
98   // PluginProxyDelegate.
99   // SendToBrowser() is intended to be safe to use on another thread so
100   // long as the main PpapiThread outlives it.
101   virtual IPC::Sender* GetBrowserSender() OVERRIDE;
102   virtual std::string GetUILanguage() OVERRIDE;
103   virtual void PreCacheFont(const void* logfontw) OVERRIDE;
104   virtual void SetActiveURL(const std::string& url) OVERRIDE;
105   virtual PP_Resource CreateBrowserFont(
106       ppapi::proxy::Connection connection,
107       PP_Instance instance,
108       const PP_BrowserFont_Trusted_Description& desc,
109       const ppapi::Preferences& prefs) OVERRIDE;
110
111   // Message handlers.
112   void OnLoadPlugin(const base::FilePath& path,
113                     const ppapi::PpapiPermissions& permissions);
114   void OnCreateChannel(base::ProcessId renderer_pid,
115                        int renderer_child_id,
116                        bool incognito);
117   void OnResourceReply(
118       const ppapi::proxy::ResourceMessageReplyParams& reply_params,
119       const IPC::Message& nested_msg);
120   void OnSetNetworkState(bool online);
121   void OnCrash();
122   void OnHang();
123
124   // Sets up the channel to the given renderer. On success, returns true and
125   // fills the given ChannelHandle with the information from the new channel.
126   bool SetupRendererChannel(base::ProcessId renderer_pid,
127                             int renderer_child_id,
128                             bool incognito,
129                             IPC::ChannelHandle* handle);
130
131   // Sets up the name of the plugin for logging using the given path.
132   void SavePluginName(const base::FilePath& path);
133
134   void ReportLoadResult(const base::FilePath& path, LoadResult result);
135
136   // True if running in a broker process rather than a normal plugin process.
137   bool is_broker_;
138
139   base::ScopedNativeLibrary library_;
140
141   ppapi::PpapiPermissions permissions_;
142
143   // Global state tracking for the proxy.
144   ppapi::proxy::PluginGlobals plugin_globals_;
145
146   // Storage for plugin entry points.
147   PepperPluginInfo::EntryPoints plugin_entry_points_;
148
149   // Callback to call when a new instance connects to the broker.
150   // Used only when is_broker_.
151   PP_ConnectInstance_Func connect_instance_func_;
152
153   // Local concept of the module ID. Some functions take this. It's necessary
154   // for the in-process PPAPI to handle this properly, but for proxied it's
155   // unnecessary. The proxy talking to multiple renderers means that each
156   // renderer has a different idea of what the module ID is for this plugin.
157   // To force people to "do the right thing" we generate a random module ID
158   // and pass it around as necessary.
159   PP_Module local_pp_module_;
160
161   // See Dispatcher::Delegate::GetGloballySeenInstanceIDSet.
162   std::set<PP_Instance> globally_seen_instance_ids_;
163
164   // The PluginDispatcher instances contained in the map are not owned by it.
165   std::map<uint32, ppapi::proxy::PluginDispatcher*> plugin_dispatchers_;
166   uint32 next_plugin_dispatcher_id_;
167
168   // The WebKitPlatformSupport implementation.
169   scoped_ptr<PpapiWebKitPlatformSupportImpl> webkit_platform_support_;
170
171 #if defined(OS_WIN)
172   // Caches the handle to the peer process if this is a broker.
173   base::win::ScopedHandle peer_handle_;
174 #endif
175
176   DispatcherMessageListener dispatcher_message_listener_;
177
178   DISALLOW_IMPLICIT_CONSTRUCTORS(PpapiThread);
179 };
180
181 }  // namespace content
182
183 #endif  // CONTENT_PPAPI_PLUGIN_PPAPI_THREAD_H_