15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CONTENT_BROWSER_RENDERER_HOST_PEPPER_BROWSER_PPAPI_HOST_IMPL_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CONTENT_BROWSER_RENDERER_HOST_PEPPER_BROWSER_PPAPI_HOST_IMPL_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <map>
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <string>
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/compiler_specific.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/file_path.h"
142385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch#include "base/memory/ref_counted.h"
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/memory/weak_ptr.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/browser/renderer_host/pepper/content_browser_pepper_host_factory.h"
17424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "content/browser/renderer_host/pepper/ssl_context_helper.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/common/content_export.h"
19424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "content/common/pepper_renderer_instance_data.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/browser_ppapi_host.h"
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/common/process_type.h"
225c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "ipc/message_filter.h"
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ppapi/host/ppapi_host.h"
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#if !defined(ENABLE_PLUGINS)
261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#error "Plugins should be enabled"
271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#endif
281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace content {
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class CONTENT_EXPORT BrowserPpapiHostImpl : public BrowserPpapiHost {
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The creator is responsible for calling set_plugin_process_handle as soon
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // as it is known (we start the process asynchronously so it won't be known
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // when this object is created).
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |external_plugin| signfies that this is a proxy created for an embedder's
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // plugin, i.e. using BrowserPpapiHost::CreateExternalPluginProcess.
38a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  BrowserPpapiHostImpl(IPC::Sender* sender,
39a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                       const ppapi::PpapiPermissions& permissions,
40a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                       const std::string& plugin_name,
41a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                       const base::FilePath& plugin_path,
42a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                       const base::FilePath& profile_data_directory,
43a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                       bool in_process,
44a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                       bool external_plugin);
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~BrowserPpapiHostImpl();
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // BrowserPpapiHost.
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ppapi::host::PpapiHost* GetPpapiHost() OVERRIDE;
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual base::ProcessHandle GetPluginProcessHandle() const OVERRIDE;
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool IsValidInstance(PP_Instance instance) const OVERRIDE;
51a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  virtual bool GetRenderFrameIDsForInstance(PP_Instance instance,
52a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                                            int* render_process_id,
53a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                                            int* render_frame_id) const
54a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      OVERRIDE;
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual const std::string& GetPluginName() OVERRIDE;
567d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual const base::FilePath& GetPluginPath() OVERRIDE;
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual const base::FilePath& GetProfileDataDirectory() OVERRIDE;
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual GURL GetDocumentURLForInstance(PP_Instance instance) OVERRIDE;
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual GURL GetPluginURLForInstance(PP_Instance instance) OVERRIDE;
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void SetOnKeepaliveCallback(
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const BrowserPpapiHost::OnKeepaliveCallback& callback) OVERRIDE;
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void set_plugin_process_handle(base::ProcessHandle handle) {
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    plugin_process_handle_ = handle;
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  bool external_plugin() const { return external_plugin_; }
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // These two functions are notifications that an instance has been created
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // or destroyed. They allow us to maintain a mapping of PP_Instance to data
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // associated with the instance including view IDs in the browser process.
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void AddInstance(PP_Instance instance,
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   const PepperRendererInstanceData& instance_data);
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void DeleteInstance(PP_Instance instance);
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
765c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  scoped_refptr<IPC::MessageFilter> message_filter() {
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return message_filter_;
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
80424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  const scoped_refptr<SSLContextHelper>& ssl_context_helper() const {
81424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    return ssl_context_helper_;
82424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
83424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend class BrowserPpapiHostTest;
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Implementing MessageFilter on BrowserPpapiHostImpl makes it ref-counted,
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // preventing us from returning these to embedders without holding a
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // reference. To avoid that, define a message filter object.
905c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  class HostMessageFilter : public IPC::MessageFilter {
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   public:
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    HostMessageFilter(ppapi::host::PpapiHost* ppapi_host,
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                      BrowserPpapiHostImpl* browser_ppapi_host_impl);
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
955c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    // IPC::MessageFilter.
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    void OnHostDestroyed();
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   private:
1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    virtual ~HostMessageFilter();
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    void OnKeepalive();
1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    void OnHostMsgLogInterfaceUsage(int hash) const;
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Non owning pointers cleared in OnHostDestroyed()
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ppapi::host::PpapiHost* ppapi_host_;
1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    BrowserPpapiHostImpl* browser_ppapi_host_impl_;
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Reports plugin activity to the callback set with SetOnKeepaliveCallback.
1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void OnKeepalive();
1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<ppapi::host::PpapiHost> ppapi_host_;
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::ProcessHandle plugin_process_handle_;
1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::string plugin_name_;
1177d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  base::FilePath plugin_path_;
1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath profile_data_directory_;
1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // If true, this refers to a plugin running in the renderer process.
1211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  bool in_process_;
1221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If true, this is an external plugin, i.e. created by the embedder using
1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // BrowserPpapiHost::CreateExternalPluginProcess.
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool external_plugin_;
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
127424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  scoped_refptr<SSLContextHelper> ssl_context_helper_;
128424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Tracks all PP_Instances in this plugin and associated renderer-related
1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // data.
1312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  typedef std::map<PP_Instance, PepperRendererInstanceData> InstanceMap;
1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  InstanceMap instance_map_;
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<HostMessageFilter> message_filter_;
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  BrowserPpapiHost::OnKeepaliveCallback on_keepalive_callback_;
1375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(BrowserPpapiHostImpl);
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace content
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // CONTENT_BROWSER_RENDERER_HOST_PEPPER_BROWSER_PPAPI_HOST_IMPL_H_
144