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_BROWSER_PPAPI_PLUGIN_PROCESS_HOST_H_
6 #define CONTENT_BROWSER_PPAPI_PLUGIN_PROCESS_HOST_H_
11 #include "base/basictypes.h"
12 #include "base/files/file_path.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/process.h"
16 #include "base/string16.h"
17 #include "content/browser/renderer_host/pepper/browser_ppapi_host_impl.h"
18 #include "content/browser/renderer_host/pepper/pepper_message_filter.h"
19 #include "content/public/browser/browser_child_process_host_delegate.h"
20 #include "content/public/browser/browser_child_process_host_iterator.h"
21 #include "ipc/ipc_sender.h"
22 #include "ppapi/shared_impl/ppapi_permissions.h"
29 class BrowserChildProcessHostImpl
;
30 class ResourceContext
;
31 struct PepperPluginInfo
;
33 // Process host for PPAPI plugin and broker processes.
34 // When used for the broker, interpret all references to "plugin" with "broker".
35 class PpapiPluginProcessHost
: public BrowserChildProcessHostDelegate
,
40 // Gets the information about the renderer that's requesting the channel.
41 virtual void GetPpapiChannelInfo(base::ProcessHandle
* renderer_handle
,
42 int* renderer_id
) = 0;
44 // Called when the channel is asynchronously opened to the plugin or on
45 // error. On error, the parameters should be:
46 // base::kNullProcessHandle
47 // IPC::ChannelHandle(),
49 virtual void OnPpapiChannelOpened(
50 const IPC::ChannelHandle
& channel_handle
,
51 base::ProcessId plugin_pid
,
52 int plugin_child_id
) = 0;
54 // Returns true if the current connection is off-the-record.
55 virtual bool OffTheRecord() = 0;
61 class PluginClient
: public Client
{
63 // Returns the resource context for the renderer requesting the channel.
64 virtual ResourceContext
* GetResourceContext() = 0;
67 virtual ~PluginClient() {}
70 class BrokerClient
: public Client
{
72 virtual ~BrokerClient() {}
75 virtual ~PpapiPluginProcessHost();
77 static PpapiPluginProcessHost
* CreatePluginHost(
78 const PepperPluginInfo
& info
,
79 const base::FilePath
& profile_data_directory
,
80 net::HostResolver
* host_resolver
);
81 static PpapiPluginProcessHost
* CreateBrokerHost(
82 const PepperPluginInfo
& info
);
84 // Notification that a PP_Instance has been created and the associated
85 // renderer related data including the RenderView/Process pair for the given
86 // plugin. This is necessary so that when the plugin calls us with a
87 // PP_Instance we can find the RenderView associated with it without trusting
89 static void DidCreateOutOfProcessInstance(
90 int plugin_process_id
,
92 const PepperRendererInstanceData
& instance_data
);
94 // The opposite of DIdCreate... above.
95 static void DidDeleteOutOfProcessInstance(int plugin_process_id
,
98 // Returns the instances that match the specified process name.
99 // It can only be called on the IO thread.
100 static void FindByName(const string16
& name
,
101 std::vector
<PpapiPluginProcessHost
*>* hosts
);
103 // IPC::Sender implementation:
104 virtual bool Send(IPC::Message
* message
) OVERRIDE
;
106 // Opens a new channel to the plugin. The client will be notified when the
107 // channel is ready or if there's an error.
108 void OpenChannelToPlugin(Client
* client
);
110 const base::FilePath
& plugin_path() const { return plugin_path_
; }
111 const base::FilePath
& profile_data_directory() const {
112 return profile_data_directory_
;
115 // The client pointer must remain valid until its callback is issued.
118 class PluginNetworkObserver
;
120 // Constructors for plugin and broker process hosts, respectively.
121 // You must call Init before doing anything else.
122 PpapiPluginProcessHost(const PepperPluginInfo
& info
,
123 const base::FilePath
& profile_data_directory
,
124 net::HostResolver
* host_resolver
);
125 PpapiPluginProcessHost();
127 // Actually launches the process with the given plugin info. Returns true
128 // on success (the process was spawned).
129 bool Init(const PepperPluginInfo
& info
);
131 void RequestPluginChannel(Client
* client
);
133 virtual void OnProcessLaunched() OVERRIDE
;
135 virtual void OnProcessCrashed(int exit_code
) OVERRIDE
;
136 virtual bool OnMessageReceived(const IPC::Message
& msg
) OVERRIDE
;
137 virtual void OnChannelConnected(int32 peer_pid
) OVERRIDE
;
138 virtual void OnChannelError() OVERRIDE
;
140 void CancelRequests();
142 // IPC message handlers.
143 void OnRendererPluginChannelCreated(const IPC::ChannelHandle
& handle
);
145 // Handles most requests from the plugin. May be NULL.
146 scoped_refptr
<PepperMessageFilter
> filter_
;
148 ppapi::PpapiPermissions permissions_
;
149 scoped_ptr
<BrowserPpapiHostImpl
> host_impl_
;
151 // Observes network changes. May be NULL.
152 scoped_ptr
<PluginNetworkObserver
> network_observer_
;
154 // Channel requests that we are waiting to send to the plugin process once
155 // the channel is opened.
156 std::vector
<Client
*> pending_requests_
;
158 // Channel requests that we have already sent to the plugin process, but
159 // haven't heard back about yet.
160 std::queue
<Client
*> sent_requests_
;
162 // Path to the plugin library.
163 base::FilePath plugin_path_
;
165 // Path to the top-level plugin data directory (differs based upon profile).
166 base::FilePath profile_data_directory_
;
168 const bool is_broker_
;
170 scoped_ptr
<BrowserChildProcessHostImpl
> process_
;
172 DISALLOW_COPY_AND_ASSIGN(PpapiPluginProcessHost
);
175 class PpapiPluginProcessHostIterator
176 : public BrowserChildProcessHostTypeIterator
<
177 PpapiPluginProcessHost
> {
179 PpapiPluginProcessHostIterator()
180 : BrowserChildProcessHostTypeIterator
<
181 PpapiPluginProcessHost
>(PROCESS_TYPE_PPAPI_PLUGIN
) {}
184 class PpapiBrokerProcessHostIterator
185 : public BrowserChildProcessHostTypeIterator
<
186 PpapiPluginProcessHost
> {
188 PpapiBrokerProcessHostIterator()
189 : BrowserChildProcessHostTypeIterator
<
190 PpapiPluginProcessHost
>(PROCESS_TYPE_PPAPI_BROKER
) {}
193 } // namespace content
195 #endif // CONTENT_BROWSER_PPAPI_PLUGIN_PROCESS_HOST_H_