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.
5 #ifndef CONTENT_PPAPI_PLUGIN_PPAPI_THREAD_H_
6 #define CONTENT_PPAPI_PLUGIN_PPAPI_THREAD_H_
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"
28 #include "base/win/scoped_handle.h"
43 class PpapiWebKitPlatformSupportImpl;
45 class PpapiThread : public ChildThread,
46 public ppapi::proxy::PluginDispatcher::PluginDelegate,
47 public ppapi::proxy::PluginProxyDelegate {
49 PpapiThread(const CommandLine& command_line, bool is_broker);
50 virtual ~PpapiThread();
51 virtual void Shutdown() OVERRIDE;
54 // Make sure the enum list in tools/histogram/histograms.xml is updated with
55 // any change in this list.
61 // NOTE: Add new values only immediately above this line.
62 LOAD_RESULT_MAX // Boundary value for UMA_HISTOGRAM_ENUMERATION.
65 // This class finds the target PluginDispatcher for each message it receives
66 // and forwards the message.
67 class DispatcherMessageListener : public IPC::Listener {
69 explicit DispatcherMessageListener(PpapiThread* owner);
70 virtual ~DispatcherMessageListener();
72 // IPC::Listener implementation.
73 virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
78 DISALLOW_COPY_AND_ASSIGN(DispatcherMessageListener);
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;
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;
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;
112 void OnLoadPlugin(const base::FilePath& path,
113 const ppapi::PpapiPermissions& permissions);
114 void OnCreateChannel(base::ProcessId renderer_pid,
115 int renderer_child_id,
117 void OnResourceReply(
118 const ppapi::proxy::ResourceMessageReplyParams& reply_params,
119 const IPC::Message& nested_msg);
120 void OnSetNetworkState(bool online);
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,
129 IPC::ChannelHandle* handle);
131 // Sets up the name of the plugin for logging using the given path.
132 void SavePluginName(const base::FilePath& path);
134 void ReportLoadResult(const base::FilePath& path, LoadResult result);
136 // True if running in a broker process rather than a normal plugin process.
139 base::ScopedNativeLibrary library_;
141 ppapi::PpapiPermissions permissions_;
143 // Global state tracking for the proxy.
144 ppapi::proxy::PluginGlobals plugin_globals_;
146 // Storage for plugin entry points.
147 PepperPluginInfo::EntryPoints plugin_entry_points_;
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_;
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_;
161 // See Dispatcher::Delegate::GetGloballySeenInstanceIDSet.
162 std::set<PP_Instance> globally_seen_instance_ids_;
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_;
168 // The WebKitPlatformSupport implementation.
169 scoped_ptr<PpapiWebKitPlatformSupportImpl> webkit_platform_support_;
172 // Caches the handle to the peer process if this is a broker.
173 base::win::ScopedHandle peer_handle_;
176 DispatcherMessageListener dispatcher_message_listener_;
178 DISALLOW_IMPLICIT_CONSTRUCTORS(PpapiThread);
181 } // namespace content
183 #endif // CONTENT_PPAPI_PLUGIN_PPAPI_THREAD_H_