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.
5 #ifndef CONTENT_BROWSER_RENDERER_HOST_PEPPER_BROWSER_PPAPI_HOST_IMPL_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_PEPPER_BROWSER_PPAPI_HOST_IMPL_H_
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/containers/scoped_ptr_hash_map.h"
14 #include "base/files/file_path.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/observer_list.h"
18 #include "base/process/process.h"
19 #include "content/browser/renderer_host/pepper/content_browser_pepper_host_factory.h"
20 #include "content/browser/renderer_host/pepper/ssl_context_helper.h"
21 #include "content/common/content_export.h"
22 #include "content/common/pepper_renderer_instance_data.h"
23 #include "content/public/browser/browser_ppapi_host.h"
24 #include "content/public/common/process_type.h"
25 #include "ipc/message_filter.h"
26 #include "ppapi/host/ppapi_host.h"
28 #if !defined(ENABLE_PLUGINS)
29 #error "Plugins should be enabled"
34 class CONTENT_EXPORT BrowserPpapiHostImpl
: public BrowserPpapiHost
{
36 class InstanceObserver
{
38 // Called when the plugin instance is throttled or unthrottled because of
39 // the Plugin Power Saver feature. Invoked on the IO thread.
40 virtual void OnThrottleStateChanged(bool is_throttled
) = 0;
42 // Called right before the instance is destroyed.
43 virtual void OnHostDestroyed() = 0;
46 // The creator is responsible for calling set_plugin_process as soon as it is
47 // known (we start the process asynchronously so it won't be known when this
48 // object is created).
49 // |external_plugin| signfies that this is a proxy created for an embedder's
50 // plugin, i.e. using BrowserPpapiHost::CreateExternalPluginProcess.
51 BrowserPpapiHostImpl(IPC::Sender
* sender
,
52 const ppapi::PpapiPermissions
& permissions
,
53 const std::string
& plugin_name
,
54 const base::FilePath
& plugin_path
,
55 const base::FilePath
& profile_data_directory
,
57 bool external_plugin
);
58 ~BrowserPpapiHostImpl() override
;
61 ppapi::host::PpapiHost
* GetPpapiHost() override
;
62 const base::Process
& GetPluginProcess() const override
;
63 bool IsValidInstance(PP_Instance instance
) const override
;
64 bool GetRenderFrameIDsForInstance(PP_Instance instance
,
65 int* render_process_id
,
66 int* render_frame_id
) const override
;
67 const std::string
& GetPluginName() override
;
68 const base::FilePath
& GetPluginPath() override
;
69 const base::FilePath
& GetProfileDataDirectory() override
;
70 GURL
GetDocumentURLForInstance(PP_Instance instance
) override
;
71 GURL
GetPluginURLForInstance(PP_Instance instance
) override
;
72 void SetOnKeepaliveCallback(
73 const BrowserPpapiHost::OnKeepaliveCallback
& callback
) override
;
75 void set_plugin_process(base::Process process
) {
76 plugin_process_
= process
.Pass();
79 bool external_plugin() const { return external_plugin_
; }
81 // These two functions are notifications that an instance has been created
82 // or destroyed. They allow us to maintain a mapping of PP_Instance to data
83 // associated with the instance including view IDs in the browser process.
84 void AddInstance(PP_Instance instance
,
85 const PepperRendererInstanceData
& renderer_instance_data
);
86 void DeleteInstance(PP_Instance instance
);
88 void AddInstanceObserver(PP_Instance instance
, InstanceObserver
* observer
);
89 void RemoveInstanceObserver(PP_Instance instance
, InstanceObserver
* observer
);
91 void OnThrottleStateChanged(PP_Instance instance
, bool is_throttled
);
92 bool IsThrottled(PP_Instance instance
) const;
94 scoped_refptr
<IPC::MessageFilter
> message_filter() {
95 return message_filter_
;
98 const scoped_refptr
<SSLContextHelper
>& ssl_context_helper() const {
99 return ssl_context_helper_
;
103 friend class BrowserPpapiHostTest
;
105 // Implementing MessageFilter on BrowserPpapiHostImpl makes it ref-counted,
106 // preventing us from returning these to embedders without holding a
107 // reference. To avoid that, define a message filter object.
108 class HostMessageFilter
: public IPC::MessageFilter
{
110 HostMessageFilter(ppapi::host::PpapiHost
* ppapi_host
,
111 BrowserPpapiHostImpl
* browser_ppapi_host_impl
);
113 // IPC::MessageFilter.
114 bool OnMessageReceived(const IPC::Message
& msg
) override
;
116 void OnHostDestroyed();
119 ~HostMessageFilter() override
;
122 void OnHostMsgLogInterfaceUsage(int hash
) const;
124 // Non owning pointers cleared in OnHostDestroyed()
125 ppapi::host::PpapiHost
* ppapi_host_
;
126 BrowserPpapiHostImpl
* browser_ppapi_host_impl_
;
129 struct InstanceData
{
130 InstanceData(const PepperRendererInstanceData
& renderer_data
);
133 PepperRendererInstanceData renderer_data
;
136 ObserverList
<InstanceObserver
> observer_list
;
139 // Reports plugin activity to the callback set with SetOnKeepaliveCallback.
142 scoped_ptr
<ppapi::host::PpapiHost
> ppapi_host_
;
143 base::Process plugin_process_
;
144 std::string plugin_name_
;
145 base::FilePath plugin_path_
;
146 base::FilePath profile_data_directory_
;
148 // If true, this refers to a plugin running in the renderer process.
151 // If true, this is an external plugin, i.e. created by the embedder using
152 // BrowserPpapiHost::CreateExternalPluginProcess.
153 bool external_plugin_
;
155 scoped_refptr
<SSLContextHelper
> ssl_context_helper_
;
157 // Tracks all PP_Instances in this plugin and associated data.
158 base::ScopedPtrHashMap
<PP_Instance
, InstanceData
> instance_map_
;
160 scoped_refptr
<HostMessageFilter
> message_filter_
;
162 BrowserPpapiHost::OnKeepaliveCallback on_keepalive_callback_
;
164 DISALLOW_COPY_AND_ASSIGN(BrowserPpapiHostImpl
);
167 } // namespace content
169 #endif // CONTENT_BROWSER_RENDERER_HOST_PEPPER_BROWSER_PPAPI_HOST_IMPL_H_