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;
43 // The creator is responsible for calling set_plugin_process as soon as it is
44 // known (we start the process asynchronously so it won't be known when this
45 // object is created).
46 // |external_plugin| signfies that this is a proxy created for an embedder's
47 // plugin, i.e. using BrowserPpapiHost::CreateExternalPluginProcess.
48 BrowserPpapiHostImpl(IPC::Sender
* sender
,
49 const ppapi::PpapiPermissions
& permissions
,
50 const std::string
& plugin_name
,
51 const base::FilePath
& plugin_path
,
52 const base::FilePath
& profile_data_directory
,
54 bool external_plugin
);
55 ~BrowserPpapiHostImpl() override
;
58 ppapi::host::PpapiHost
* GetPpapiHost() override
;
59 const base::Process
& GetPluginProcess() const override
;
60 bool IsValidInstance(PP_Instance instance
) const override
;
61 bool GetRenderFrameIDsForInstance(PP_Instance instance
,
62 int* render_process_id
,
63 int* render_frame_id
) const override
;
64 const std::string
& GetPluginName() override
;
65 const base::FilePath
& GetPluginPath() override
;
66 const base::FilePath
& GetProfileDataDirectory() override
;
67 GURL
GetDocumentURLForInstance(PP_Instance instance
) override
;
68 GURL
GetPluginURLForInstance(PP_Instance instance
) override
;
69 void SetOnKeepaliveCallback(
70 const BrowserPpapiHost::OnKeepaliveCallback
& callback
) override
;
72 void set_plugin_process(base::Process process
) {
73 plugin_process_
= process
.Pass();
76 bool external_plugin() const { return external_plugin_
; }
78 // These two functions are notifications that an instance has been created
79 // or destroyed. They allow us to maintain a mapping of PP_Instance to data
80 // associated with the instance including view IDs in the browser process.
81 void AddInstance(PP_Instance instance
,
82 const PepperRendererInstanceData
& renderer_instance_data
);
83 void DeleteInstance(PP_Instance instance
);
85 void AddInstanceObserver(PP_Instance instance
, InstanceObserver
* observer
);
86 void RemoveInstanceObserver(PP_Instance instance
, InstanceObserver
* observer
);
88 void OnThrottleStateChanged(PP_Instance instance
, bool is_throttled
);
89 bool IsThrottled(PP_Instance instance
) const;
91 scoped_refptr
<IPC::MessageFilter
> message_filter() {
92 return message_filter_
;
95 const scoped_refptr
<SSLContextHelper
>& ssl_context_helper() const {
96 return ssl_context_helper_
;
100 friend class BrowserPpapiHostTest
;
102 // Implementing MessageFilter on BrowserPpapiHostImpl makes it ref-counted,
103 // preventing us from returning these to embedders without holding a
104 // reference. To avoid that, define a message filter object.
105 class HostMessageFilter
: public IPC::MessageFilter
{
107 HostMessageFilter(ppapi::host::PpapiHost
* ppapi_host
,
108 BrowserPpapiHostImpl
* browser_ppapi_host_impl
);
110 // IPC::MessageFilter.
111 bool OnMessageReceived(const IPC::Message
& msg
) override
;
113 void OnHostDestroyed();
116 ~HostMessageFilter() override
;
119 void OnHostMsgLogInterfaceUsage(int hash
) const;
121 // Non owning pointers cleared in OnHostDestroyed()
122 ppapi::host::PpapiHost
* ppapi_host_
;
123 BrowserPpapiHostImpl
* browser_ppapi_host_impl_
;
126 struct InstanceData
{
127 InstanceData(const PepperRendererInstanceData
& renderer_data
);
130 PepperRendererInstanceData renderer_data
;
133 ObserverList
<InstanceObserver
> observer_list
;
136 // Reports plugin activity to the callback set with SetOnKeepaliveCallback.
139 scoped_ptr
<ppapi::host::PpapiHost
> ppapi_host_
;
140 base::Process plugin_process_
;
141 std::string plugin_name_
;
142 base::FilePath plugin_path_
;
143 base::FilePath profile_data_directory_
;
145 // If true, this refers to a plugin running in the renderer process.
148 // If true, this is an external plugin, i.e. created by the embedder using
149 // BrowserPpapiHost::CreateExternalPluginProcess.
150 bool external_plugin_
;
152 scoped_refptr
<SSLContextHelper
> ssl_context_helper_
;
154 // Tracks all PP_Instances in this plugin and associated data.
155 base::ScopedPtrHashMap
<PP_Instance
, InstanceData
> instance_map_
;
157 scoped_refptr
<HostMessageFilter
> message_filter_
;
159 BrowserPpapiHost::OnKeepaliveCallback on_keepalive_callback_
;
161 DISALLOW_COPY_AND_ASSIGN(BrowserPpapiHostImpl
);
164 } // namespace content
166 #endif // CONTENT_BROWSER_RENDERER_HOST_PEPPER_BROWSER_PPAPI_HOST_IMPL_H_