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/process.h"
16 #include "base/strings/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"
25 class BrowserChildProcessHostImpl
;
26 class ResourceContext
;
27 struct PepperPluginInfo
;
29 // Process host for PPAPI plugin and broker processes.
30 // When used for the broker, interpret all references to "plugin" with "broker".
31 class PpapiPluginProcessHost
: public BrowserChildProcessHostDelegate
,
36 // Gets the information about the renderer that's requesting the channel.
37 virtual void GetPpapiChannelInfo(base::ProcessHandle
* renderer_handle
,
38 int* renderer_id
) = 0;
40 // Called when the channel is asynchronously opened to the plugin or on
41 // error. On error, the parameters should be:
42 // base::kNullProcessHandle
43 // IPC::ChannelHandle(),
45 virtual void OnPpapiChannelOpened(
46 const IPC::ChannelHandle
& channel_handle
,
47 base::ProcessId plugin_pid
,
48 int plugin_child_id
) = 0;
50 // Returns true if the current connection is off-the-record.
51 virtual bool OffTheRecord() = 0;
57 class PluginClient
: public Client
{
59 // Returns the resource context for the renderer requesting the channel.
60 virtual ResourceContext
* GetResourceContext() = 0;
63 virtual ~PluginClient() {}
66 class BrokerClient
: public Client
{
68 virtual ~BrokerClient() {}
71 virtual ~PpapiPluginProcessHost();
73 static PpapiPluginProcessHost
* CreatePluginHost(
74 const PepperPluginInfo
& info
,
75 const base::FilePath
& profile_data_directory
);
76 static PpapiPluginProcessHost
* CreateBrokerHost(
77 const PepperPluginInfo
& info
);
79 // Notification that a PP_Instance has been created and the associated
80 // renderer related data including the RenderView/Process pair for the given
81 // plugin. This is necessary so that when the plugin calls us with a
82 // PP_Instance we can find the RenderView associated with it without trusting
84 static void DidCreateOutOfProcessInstance(
85 int plugin_process_id
,
87 const PepperRendererInstanceData
& instance_data
);
89 // The opposite of DIdCreate... above.
90 static void DidDeleteOutOfProcessInstance(int plugin_process_id
,
93 // Returns the instances that match the specified process name.
94 // It can only be called on the IO thread.
95 static void FindByName(const base::string16
& name
,
96 std::vector
<PpapiPluginProcessHost
*>* hosts
);
98 // IPC::Sender implementation:
99 virtual bool Send(IPC::Message
* message
) OVERRIDE
;
101 // Opens a new channel to the plugin. The client will be notified when the
102 // channel is ready or if there's an error.
103 void OpenChannelToPlugin(Client
* client
);
105 BrowserPpapiHostImpl
* host_impl() { return host_impl_
.get(); }
106 const BrowserChildProcessHostImpl
* process() { return process_
.get(); }
107 const base::FilePath
& plugin_path() const { return plugin_path_
; }
108 const base::FilePath
& profile_data_directory() const {
109 return profile_data_directory_
;
112 // The client pointer must remain valid until its callback is issued.
115 class PluginNetworkObserver
;
117 // Constructors for plugin and broker process hosts, respectively.
118 // You must call Init before doing anything else.
119 PpapiPluginProcessHost(const PepperPluginInfo
& info
,
120 const base::FilePath
& profile_data_directory
);
121 PpapiPluginProcessHost();
123 // Actually launches the process with the given plugin info. Returns true
124 // on success (the process was spawned).
125 bool Init(const PepperPluginInfo
& info
);
127 void RequestPluginChannel(Client
* client
);
129 virtual void OnProcessLaunched() OVERRIDE
;
131 virtual void OnProcessCrashed(int exit_code
) OVERRIDE
;
132 virtual bool OnMessageReceived(const IPC::Message
& msg
) OVERRIDE
;
133 virtual void OnChannelConnected(int32 peer_pid
) OVERRIDE
;
134 virtual void OnChannelError() OVERRIDE
;
136 void CancelRequests();
138 // IPC message handlers.
139 void OnRendererPluginChannelCreated(const IPC::ChannelHandle
& handle
);
141 // Handles most requests from the plugin. May be NULL.
142 scoped_refptr
<PepperMessageFilter
> filter_
;
144 ppapi::PpapiPermissions permissions_
;
145 scoped_ptr
<BrowserPpapiHostImpl
> host_impl_
;
147 // Observes network changes. May be NULL.
148 scoped_ptr
<PluginNetworkObserver
> network_observer_
;
150 // Channel requests that we are waiting to send to the plugin process once
151 // the channel is opened.
152 std::vector
<Client
*> pending_requests_
;
154 // Channel requests that we have already sent to the plugin process, but
155 // haven't heard back about yet.
156 std::queue
<Client
*> sent_requests_
;
158 // Path to the plugin library.
159 base::FilePath plugin_path_
;
161 // Path to the top-level plugin data directory (differs based upon profile).
162 base::FilePath profile_data_directory_
;
164 const bool is_broker_
;
166 scoped_ptr
<BrowserChildProcessHostImpl
> process_
;
168 DISALLOW_COPY_AND_ASSIGN(PpapiPluginProcessHost
);
171 class PpapiPluginProcessHostIterator
172 : public BrowserChildProcessHostTypeIterator
<
173 PpapiPluginProcessHost
> {
175 PpapiPluginProcessHostIterator()
176 : BrowserChildProcessHostTypeIterator
<
177 PpapiPluginProcessHost
>(PROCESS_TYPE_PPAPI_PLUGIN
) {}
180 class PpapiBrokerProcessHostIterator
181 : public BrowserChildProcessHostTypeIterator
<
182 PpapiPluginProcessHost
> {
184 PpapiBrokerProcessHostIterator()
185 : BrowserChildProcessHostTypeIterator
<
186 PpapiPluginProcessHost
>(PROCESS_TYPE_PPAPI_BROKER
) {}
189 } // namespace content
191 #endif // CONTENT_BROWSER_PPAPI_PLUGIN_PROCESS_HOST_H_