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