Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / ppapi_plugin_process_host.h
blobe4c58a9b33bf9af05d8330774cce748a1b1f3770
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_
8 #include <queue>
9 #include <vector>
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"
24 namespace content {
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,
32 public IPC::Sender {
33 public:
34 class Client {
35 public:
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(),
44 // 0
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;
53 protected:
54 virtual ~Client() {}
57 class PluginClient : public Client {
58 public:
59 // Returns the resource context for the renderer requesting the channel.
60 virtual ResourceContext* GetResourceContext() = 0;
62 protected:
63 ~PluginClient() override {}
66 class BrokerClient : public Client {
67 protected:
68 ~BrokerClient() override {}
71 ~PpapiPluginProcessHost() override;
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
83 // the plugin.
84 static void DidCreateOutOfProcessInstance(
85 int plugin_process_id,
86 int32 pp_instance,
87 const PepperRendererInstanceData& instance_data);
89 // The opposite of DIdCreate... above.
90 static void DidDeleteOutOfProcessInstance(int plugin_process_id,
91 int32 pp_instance);
93 // Notification that a Plugin instance has been throttled or unthrottled.
94 static void OnPluginInstanceThrottleStateChange(int plugin_process_id,
95 int32 pp_instance,
96 bool is_throttled);
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 base::string16& name,
101 std::vector<PpapiPluginProcessHost*>* hosts);
103 // IPC::Sender implementation:
104 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 BrowserPpapiHostImpl* host_impl() { return host_impl_.get(); }
111 const BrowserChildProcessHostImpl* process() { return process_.get(); }
112 const base::FilePath& plugin_path() const { return plugin_path_; }
113 const base::FilePath& profile_data_directory() const {
114 return profile_data_directory_;
117 // The client pointer must remain valid until its callback is issued.
119 private:
120 class PluginNetworkObserver;
122 // Constructors for plugin and broker process hosts, respectively.
123 // You must call Init before doing anything else.
124 PpapiPluginProcessHost(const PepperPluginInfo& info,
125 const base::FilePath& profile_data_directory);
126 PpapiPluginProcessHost();
128 // Actually launches the process with the given plugin info. Returns true
129 // on success (the process was spawned).
130 bool Init(const PepperPluginInfo& info);
132 void RequestPluginChannel(Client* client);
134 void OnProcessLaunched() override;
136 void OnProcessCrashed(int exit_code) override;
137 bool OnMessageReceived(const IPC::Message& msg) override;
138 void OnChannelConnected(int32 peer_pid) override;
139 void OnChannelError() override;
141 void CancelRequests();
143 // IPC message handlers.
144 void OnRendererPluginChannelCreated(const IPC::ChannelHandle& handle);
146 // Handles most requests from the plugin. May be NULL.
147 scoped_refptr<PepperMessageFilter> filter_;
149 ppapi::PpapiPermissions permissions_;
150 scoped_ptr<BrowserPpapiHostImpl> host_impl_;
152 // Observes network changes. May be NULL.
153 scoped_ptr<PluginNetworkObserver> network_observer_;
155 // Channel requests that we are waiting to send to the plugin process once
156 // the channel is opened.
157 std::vector<Client*> pending_requests_;
159 // Channel requests that we have already sent to the plugin process, but
160 // haven't heard back about yet.
161 std::queue<Client*> sent_requests_;
163 // Path to the plugin library.
164 base::FilePath plugin_path_;
166 // Path to the top-level plugin data directory (differs based upon profile).
167 base::FilePath profile_data_directory_;
169 const bool is_broker_;
171 scoped_ptr<BrowserChildProcessHostImpl> process_;
173 DISALLOW_COPY_AND_ASSIGN(PpapiPluginProcessHost);
176 class PpapiPluginProcessHostIterator
177 : public BrowserChildProcessHostTypeIterator<
178 PpapiPluginProcessHost> {
179 public:
180 PpapiPluginProcessHostIterator()
181 : BrowserChildProcessHostTypeIterator<
182 PpapiPluginProcessHost>(PROCESS_TYPE_PPAPI_PLUGIN) {}
185 class PpapiBrokerProcessHostIterator
186 : public BrowserChildProcessHostTypeIterator<
187 PpapiPluginProcessHost> {
188 public:
189 PpapiBrokerProcessHostIterator()
190 : BrowserChildProcessHostTypeIterator<
191 PpapiPluginProcessHost>(PROCESS_TYPE_PPAPI_BROKER) {}
194 } // namespace content
196 #endif // CONTENT_BROWSER_PPAPI_PLUGIN_PROCESS_HOST_H_