Home | History | Annotate | Download | only in browser
      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 
     24 namespace net {
     25 class HostResolver;
     26 }
     27 
     28 namespace content {
     29 class BrowserChildProcessHostImpl;
     30 class ResourceContext;
     31 struct PepperPluginInfo;
     32 
     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,
     36                                public IPC::Sender {
     37  public:
     38   class Client {
     39    public:
     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;
     43 
     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(),
     48     //   0
     49     virtual void OnPpapiChannelOpened(
     50         const IPC::ChannelHandle& channel_handle,
     51         base::ProcessId plugin_pid,
     52         int plugin_child_id) = 0;
     53 
     54     // Returns true if the current connection is off-the-record.
     55     virtual bool OffTheRecord() = 0;
     56 
     57    protected:
     58     virtual ~Client() {}
     59   };
     60 
     61   class PluginClient : public Client {
     62    public:
     63     // Returns the resource context for the renderer requesting the channel.
     64     virtual ResourceContext* GetResourceContext() = 0;
     65 
     66    protected:
     67     virtual ~PluginClient() {}
     68   };
     69 
     70   class BrokerClient : public Client {
     71    protected:
     72     virtual ~BrokerClient() {}
     73   };
     74 
     75   virtual ~PpapiPluginProcessHost();
     76 
     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);
     83 
     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
     88   // the plugin.
     89   static void DidCreateOutOfProcessInstance(
     90       int plugin_process_id,
     91       int32 pp_instance,
     92       const PepperRendererInstanceData& instance_data);
     93 
     94   // The opposite of DIdCreate... above.
     95   static void DidDeleteOutOfProcessInstance(int plugin_process_id,
     96                                             int32 pp_instance);
     97 
     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);
    102 
    103   // IPC::Sender implementation:
    104   virtual bool Send(IPC::Message* message) OVERRIDE;
    105 
    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);
    109 
    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_;
    115   }
    116 
    117   // The client pointer must remain valid until its callback is issued.
    118 
    119  private:
    120   class PluginNetworkObserver;
    121 
    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                          net::HostResolver* host_resolver);
    127   PpapiPluginProcessHost();
    128 
    129   // Actually launches the process with the given plugin info. Returns true
    130   // on success (the process was spawned).
    131   bool Init(const PepperPluginInfo& info);
    132 
    133   void RequestPluginChannel(Client* client);
    134 
    135   virtual void OnProcessLaunched() OVERRIDE;
    136 
    137   virtual void OnProcessCrashed(int exit_code) OVERRIDE;
    138   virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
    139   virtual void OnChannelConnected(int32 peer_pid) OVERRIDE;
    140   virtual void OnChannelError() OVERRIDE;
    141 
    142   void CancelRequests();
    143 
    144   // IPC message handlers.
    145   void OnRendererPluginChannelCreated(const IPC::ChannelHandle& handle);
    146 
    147   // Handles most requests from the plugin. May be NULL.
    148   scoped_refptr<PepperMessageFilter> filter_;
    149 
    150   ppapi::PpapiPermissions permissions_;
    151   scoped_ptr<BrowserPpapiHostImpl> host_impl_;
    152 
    153   // Observes network changes. May be NULL.
    154   scoped_ptr<PluginNetworkObserver> network_observer_;
    155 
    156   // Channel requests that we are waiting to send to the plugin process once
    157   // the channel is opened.
    158   std::vector<Client*> pending_requests_;
    159 
    160   // Channel requests that we have already sent to the plugin process, but
    161   // haven't heard back about yet.
    162   std::queue<Client*> sent_requests_;
    163 
    164   // Path to the plugin library.
    165   base::FilePath plugin_path_;
    166 
    167   // Path to the top-level plugin data directory (differs based upon profile).
    168   base::FilePath profile_data_directory_;
    169 
    170   const bool is_broker_;
    171 
    172   scoped_ptr<BrowserChildProcessHostImpl> process_;
    173 
    174   DISALLOW_COPY_AND_ASSIGN(PpapiPluginProcessHost);
    175 };
    176 
    177 class PpapiPluginProcessHostIterator
    178     : public BrowserChildProcessHostTypeIterator<
    179           PpapiPluginProcessHost> {
    180  public:
    181   PpapiPluginProcessHostIterator()
    182       : BrowserChildProcessHostTypeIterator<
    183           PpapiPluginProcessHost>(PROCESS_TYPE_PPAPI_PLUGIN) {}
    184 };
    185 
    186 class PpapiBrokerProcessHostIterator
    187     : public BrowserChildProcessHostTypeIterator<
    188           PpapiPluginProcessHost> {
    189  public:
    190   PpapiBrokerProcessHostIterator()
    191       : BrowserChildProcessHostTypeIterator<
    192           PpapiPluginProcessHost>(PROCESS_TYPE_PPAPI_BROKER) {}
    193 };
    194 
    195 }  // namespace content
    196 
    197 #endif  // CONTENT_BROWSER_PPAPI_PLUGIN_PROCESS_HOST_H_
    198 
    199