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_RENDERER_HOST_PEPPER_BROWSER_PPAPI_HOST_IMPL_H_
6#define CONTENT_BROWSER_RENDERER_HOST_PEPPER_BROWSER_PPAPI_HOST_IMPL_H_
7
8#include <map>
9#include <string>
10
11#include "base/basictypes.h"
12#include "base/compiler_specific.h"
13#include "base/files/file_path.h"
14#include "base/memory/ref_counted.h"
15#include "base/memory/weak_ptr.h"
16#include "content/browser/renderer_host/pepper/content_browser_pepper_host_factory.h"
17#include "content/browser/renderer_host/pepper/ssl_context_helper.h"
18#include "content/common/content_export.h"
19#include "content/common/pepper_renderer_instance_data.h"
20#include "content/public/browser/browser_ppapi_host.h"
21#include "content/public/common/process_type.h"
22#include "ipc/message_filter.h"
23#include "ppapi/host/ppapi_host.h"
24
25#if !defined(ENABLE_PLUGINS)
26#error "Plugins should be enabled"
27#endif
28
29namespace content {
30
31class CONTENT_EXPORT BrowserPpapiHostImpl : public BrowserPpapiHost {
32 public:
33  // The creator is responsible for calling set_plugin_process_handle as soon
34  // as it is known (we start the process asynchronously so it won't be known
35  // when this object is created).
36  // |external_plugin| signfies that this is a proxy created for an embedder's
37  // plugin, i.e. using BrowserPpapiHost::CreateExternalPluginProcess.
38  BrowserPpapiHostImpl(IPC::Sender* sender,
39                       const ppapi::PpapiPermissions& permissions,
40                       const std::string& plugin_name,
41                       const base::FilePath& plugin_path,
42                       const base::FilePath& profile_data_directory,
43                       bool in_process,
44                       bool external_plugin);
45  virtual ~BrowserPpapiHostImpl();
46
47  // BrowserPpapiHost.
48  virtual ppapi::host::PpapiHost* GetPpapiHost() OVERRIDE;
49  virtual base::ProcessHandle GetPluginProcessHandle() const OVERRIDE;
50  virtual bool IsValidInstance(PP_Instance instance) const OVERRIDE;
51  virtual bool GetRenderFrameIDsForInstance(PP_Instance instance,
52                                            int* render_process_id,
53                                            int* render_frame_id) const
54      OVERRIDE;
55  virtual const std::string& GetPluginName() OVERRIDE;
56  virtual const base::FilePath& GetPluginPath() OVERRIDE;
57  virtual const base::FilePath& GetProfileDataDirectory() OVERRIDE;
58  virtual GURL GetDocumentURLForInstance(PP_Instance instance) OVERRIDE;
59  virtual GURL GetPluginURLForInstance(PP_Instance instance) OVERRIDE;
60  virtual void SetOnKeepaliveCallback(
61      const BrowserPpapiHost::OnKeepaliveCallback& callback) OVERRIDE;
62
63  void set_plugin_process_handle(base::ProcessHandle handle) {
64    plugin_process_handle_ = handle;
65  }
66
67  bool external_plugin() const { return external_plugin_; }
68
69  // These two functions are notifications that an instance has been created
70  // or destroyed. They allow us to maintain a mapping of PP_Instance to data
71  // associated with the instance including view IDs in the browser process.
72  void AddInstance(PP_Instance instance,
73                   const PepperRendererInstanceData& instance_data);
74  void DeleteInstance(PP_Instance instance);
75
76  scoped_refptr<IPC::MessageFilter> message_filter() {
77    return message_filter_;
78  }
79
80  const scoped_refptr<SSLContextHelper>& ssl_context_helper() const {
81    return ssl_context_helper_;
82  }
83
84 private:
85  friend class BrowserPpapiHostTest;
86
87  // Implementing MessageFilter on BrowserPpapiHostImpl makes it ref-counted,
88  // preventing us from returning these to embedders without holding a
89  // reference. To avoid that, define a message filter object.
90  class HostMessageFilter : public IPC::MessageFilter {
91   public:
92    HostMessageFilter(ppapi::host::PpapiHost* ppapi_host,
93                      BrowserPpapiHostImpl* browser_ppapi_host_impl);
94
95    // IPC::MessageFilter.
96    virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
97
98    void OnHostDestroyed();
99
100   private:
101    virtual ~HostMessageFilter();
102
103    void OnKeepalive();
104    void OnHostMsgLogInterfaceUsage(int hash) const;
105
106    // Non owning pointers cleared in OnHostDestroyed()
107    ppapi::host::PpapiHost* ppapi_host_;
108    BrowserPpapiHostImpl* browser_ppapi_host_impl_;
109  };
110
111  // Reports plugin activity to the callback set with SetOnKeepaliveCallback.
112  void OnKeepalive();
113
114  scoped_ptr<ppapi::host::PpapiHost> ppapi_host_;
115  base::ProcessHandle plugin_process_handle_;
116  std::string plugin_name_;
117  base::FilePath plugin_path_;
118  base::FilePath profile_data_directory_;
119
120  // If true, this refers to a plugin running in the renderer process.
121  bool in_process_;
122
123  // If true, this is an external plugin, i.e. created by the embedder using
124  // BrowserPpapiHost::CreateExternalPluginProcess.
125  bool external_plugin_;
126
127  scoped_refptr<SSLContextHelper> ssl_context_helper_;
128
129  // Tracks all PP_Instances in this plugin and associated renderer-related
130  // data.
131  typedef std::map<PP_Instance, PepperRendererInstanceData> InstanceMap;
132  InstanceMap instance_map_;
133
134  scoped_refptr<HostMessageFilter> message_filter_;
135
136  BrowserPpapiHost::OnKeepaliveCallback on_keepalive_callback_;
137
138  DISALLOW_COPY_AND_ASSIGN(BrowserPpapiHostImpl);
139};
140
141}  // namespace content
142
143#endif  // CONTENT_BROWSER_RENDERER_HOST_PEPPER_BROWSER_PPAPI_HOST_IMPL_H_
144