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_BROWSER_PPAPI_PLUGIN_PROCESS_HOST_H_
6#define CONTENT_BROWSER_PPAPI_PLUGIN_PROCESS_HOST_H_
7
8#include <queue>
9#include <vector>
10
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"
23
24namespace content {
25class BrowserChildProcessHostImpl;
26class ResourceContext;
27struct PepperPluginInfo;
28
29// Process host for PPAPI plugin and broker processes.
30// When used for the broker, interpret all references to "plugin" with "broker".
31class 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;
39
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;
49
50    // Returns true if the current connection is off-the-record.
51    virtual bool OffTheRecord() = 0;
52
53   protected:
54    virtual ~Client() {}
55  };
56
57  class PluginClient : public Client {
58   public:
59    // Returns the resource context for the renderer requesting the channel.
60    virtual ResourceContext* GetResourceContext() = 0;
61
62   protected:
63    virtual ~PluginClient() {}
64  };
65
66  class BrokerClient : public Client {
67   protected:
68    virtual ~BrokerClient() {}
69  };
70
71  virtual ~PpapiPluginProcessHost();
72
73  static PpapiPluginProcessHost* CreatePluginHost(
74      const PepperPluginInfo& info,
75      const base::FilePath& profile_data_directory);
76  static PpapiPluginProcessHost* CreateBrokerHost(
77      const PepperPluginInfo& info);
78
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);
88
89  // The opposite of DIdCreate... above.
90  static void DidDeleteOutOfProcessInstance(int plugin_process_id,
91                                            int32 pp_instance);
92
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);
97
98  // IPC::Sender implementation:
99  virtual bool Send(IPC::Message* message) OVERRIDE;
100
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);
104
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_;
110  }
111
112  // The client pointer must remain valid until its callback is issued.
113
114 private:
115  class PluginNetworkObserver;
116
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();
122
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);
126
127  void RequestPluginChannel(Client* client);
128
129  virtual void OnProcessLaunched() OVERRIDE;
130
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;
135
136  void CancelRequests();
137
138  // IPC message handlers.
139  void OnRendererPluginChannelCreated(const IPC::ChannelHandle& handle);
140
141  // Handles most requests from the plugin. May be NULL.
142  scoped_refptr<PepperMessageFilter> filter_;
143
144  ppapi::PpapiPermissions permissions_;
145  scoped_ptr<BrowserPpapiHostImpl> host_impl_;
146
147  // Observes network changes. May be NULL.
148  scoped_ptr<PluginNetworkObserver> network_observer_;
149
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_;
153
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_;
157
158  // Path to the plugin library.
159  base::FilePath plugin_path_;
160
161  // Path to the top-level plugin data directory (differs based upon profile).
162  base::FilePath profile_data_directory_;
163
164  const bool is_broker_;
165
166  scoped_ptr<BrowserChildProcessHostImpl> process_;
167
168  DISALLOW_COPY_AND_ASSIGN(PpapiPluginProcessHost);
169};
170
171class PpapiPluginProcessHostIterator
172    : public BrowserChildProcessHostTypeIterator<
173          PpapiPluginProcessHost> {
174 public:
175  PpapiPluginProcessHostIterator()
176      : BrowserChildProcessHostTypeIterator<
177          PpapiPluginProcessHost>(PROCESS_TYPE_PPAPI_PLUGIN) {}
178};
179
180class PpapiBrokerProcessHostIterator
181    : public BrowserChildProcessHostTypeIterator<
182          PpapiPluginProcessHost> {
183 public:
184  PpapiBrokerProcessHostIterator()
185      : BrowserChildProcessHostTypeIterator<
186          PpapiPluginProcessHost>(PROCESS_TYPE_PPAPI_BROKER) {}
187};
188
189}  // namespace content
190
191#endif  // CONTENT_BROWSER_PPAPI_PLUGIN_PROCESS_HOST_H_
192
193