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 #include "content/ppapi_plugin/broker_process_dispatcher.h"
8 #include "base/bind_helpers.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "content/child/child_process.h"
12 #include "ppapi/c/pp_bool.h"
13 #include "ppapi/c/private/ppp_flash_browser_operations.h"
14 #include "ppapi/proxy/ppapi_messages.h"
19 // How long we wait before releasing the broker process.
20 const int kBrokerReleaseTimeSeconds
= 30;
22 std::string
ConvertPluginDataPath(const base::FilePath
& plugin_data_path
) {
23 // The string is always 8-bit, convert on Windows.
25 return WideToUTF8(plugin_data_path
.value());
27 return plugin_data_path
.value();
31 struct GetPermissionSettingsContext
{
32 GetPermissionSettingsContext(
33 const base::WeakPtr
<BrokerProcessDispatcher
> in_dispatcher
,
35 : dispatcher(in_dispatcher
),
36 request_id(in_request_id
) {
39 base::WeakPtr
<BrokerProcessDispatcher
> dispatcher
;
43 void GetPermissionSettingsCallback(
46 PP_Flash_BrowserOperations_Permission default_permission
,
48 const PP_Flash_BrowserOperations_SiteSetting sites
[]) {
49 scoped_ptr
<GetPermissionSettingsContext
> context(
50 reinterpret_cast<GetPermissionSettingsContext
*>(user_data
));
52 if (!context
->dispatcher
.get())
55 ppapi::FlashSiteSettings site_vector
;
57 site_vector
.reserve(site_count
);
58 for (uint32_t i
= 0; i
< site_count
; ++i
) {
63 site_vector
.push_back(
64 ppapi::FlashSiteSetting(sites
[i
].site
, sites
[i
].permission
));
70 context
->dispatcher
->OnGetPermissionSettingsCompleted(
71 context
->request_id
, PP_ToBool(success
), default_permission
, site_vector
);
76 BrokerProcessDispatcher::BrokerProcessDispatcher(
77 PP_GetInterface_Func get_plugin_interface
,
78 PP_ConnectInstance_Func connect_instance
)
79 : ppapi::proxy::BrokerSideDispatcher(connect_instance
),
80 get_plugin_interface_(get_plugin_interface
),
81 flash_browser_operations_1_3_(NULL
),
82 flash_browser_operations_1_2_(NULL
),
83 flash_browser_operations_1_0_(NULL
) {
84 ChildProcess::current()->AddRefProcess();
86 if (get_plugin_interface
) {
87 flash_browser_operations_1_0_
=
88 static_cast<const PPP_Flash_BrowserOperations_1_0
*>(
89 get_plugin_interface_(PPP_FLASH_BROWSEROPERATIONS_INTERFACE_1_0
));
91 flash_browser_operations_1_2_
=
92 static_cast<const PPP_Flash_BrowserOperations_1_2
*>(
93 get_plugin_interface_(PPP_FLASH_BROWSEROPERATIONS_INTERFACE_1_2
));
95 flash_browser_operations_1_3_
=
96 static_cast<const PPP_Flash_BrowserOperations_1_3
*>(
97 get_plugin_interface_(PPP_FLASH_BROWSEROPERATIONS_INTERFACE_1_3
));
101 BrokerProcessDispatcher::~BrokerProcessDispatcher() {
102 DVLOG(1) << "BrokerProcessDispatcher::~BrokerProcessDispatcher()";
103 // Don't free the process right away. This timer allows the child process
104 // to be re-used if the user rapidly goes to a new page that requires this
105 // plugin. This is the case for common plugins where they may be used on a
106 // source and destination page of a navigation. We don't want to tear down
107 // and re-start processes each time in these cases.
108 base::MessageLoop::current()->PostDelayedTask(
110 base::Bind(&ChildProcess::ReleaseProcess
,
111 base::Unretained(ChildProcess::current())),
112 base::TimeDelta::FromSeconds(kBrokerReleaseTimeSeconds
));
115 bool BrokerProcessDispatcher::OnMessageReceived(const IPC::Message
& msg
) {
116 IPC_BEGIN_MESSAGE_MAP(BrokerProcessDispatcher
, msg
)
117 IPC_MESSAGE_HANDLER(PpapiMsg_GetSitesWithData
, OnGetSitesWithData
)
118 IPC_MESSAGE_HANDLER(PpapiMsg_ClearSiteData
, OnClearSiteData
)
119 IPC_MESSAGE_HANDLER(PpapiMsg_DeauthorizeContentLicenses
,
120 OnDeauthorizeContentLicenses
)
121 IPC_MESSAGE_HANDLER(PpapiMsg_GetPermissionSettings
, OnGetPermissionSettings
)
122 IPC_MESSAGE_HANDLER(PpapiMsg_SetDefaultPermission
, OnSetDefaultPermission
)
123 IPC_MESSAGE_HANDLER(PpapiMsg_SetSitePermission
, OnSetSitePermission
)
124 IPC_MESSAGE_UNHANDLED(return BrokerSideDispatcher::OnMessageReceived(msg
))
125 IPC_END_MESSAGE_MAP()
129 void BrokerProcessDispatcher::OnGetPermissionSettingsCompleted(
132 PP_Flash_BrowserOperations_Permission default_permission
,
133 const ppapi::FlashSiteSettings
& sites
) {
134 Send(new PpapiHostMsg_GetPermissionSettingsResult(
135 request_id
, success
, default_permission
, sites
));
138 void BrokerProcessDispatcher::OnGetSitesWithData(
140 const base::FilePath
& plugin_data_path
) {
141 std::vector
<std::string
> sites
;
142 GetSitesWithData(plugin_data_path
, &sites
);
143 Send(new PpapiHostMsg_GetSitesWithDataResult(request_id
, sites
));
146 void BrokerProcessDispatcher::OnClearSiteData(
148 const base::FilePath
& plugin_data_path
,
149 const std::string
& site
,
152 Send(new PpapiHostMsg_ClearSiteDataResult(
153 request_id
, ClearSiteData(plugin_data_path
, site
, flags
, max_age
)));
156 void BrokerProcessDispatcher::OnDeauthorizeContentLicenses(
158 const base::FilePath
& plugin_data_path
) {
159 Send(new PpapiHostMsg_DeauthorizeContentLicensesResult(
160 request_id
, DeauthorizeContentLicenses(plugin_data_path
)));
163 void BrokerProcessDispatcher::OnGetPermissionSettings(
165 const base::FilePath
& plugin_data_path
,
166 PP_Flash_BrowserOperations_SettingType setting_type
) {
167 if (flash_browser_operations_1_3_
) {
168 std::string data_str
= ConvertPluginDataPath(plugin_data_path
);
169 // The GetPermissionSettingsContext object will be deleted in
170 // GetPermissionSettingsCallback().
171 flash_browser_operations_1_3_
->GetPermissionSettings(
172 data_str
.c_str(), setting_type
, &GetPermissionSettingsCallback
,
173 new GetPermissionSettingsContext(AsWeakPtr(), request_id
));
177 if (flash_browser_operations_1_2_
) {
178 std::string data_str
= ConvertPluginDataPath(plugin_data_path
);
179 // The GetPermissionSettingsContext object will be deleted in
180 // GetPermissionSettingsCallback().
181 flash_browser_operations_1_2_
->GetPermissionSettings(
182 data_str
.c_str(), setting_type
, &GetPermissionSettingsCallback
,
183 new GetPermissionSettingsContext(AsWeakPtr(), request_id
));
187 OnGetPermissionSettingsCompleted(
188 request_id
, false, PP_FLASH_BROWSEROPERATIONS_PERMISSION_DEFAULT
,
189 ppapi::FlashSiteSettings());
193 void BrokerProcessDispatcher::OnSetDefaultPermission(
195 const base::FilePath
& plugin_data_path
,
196 PP_Flash_BrowserOperations_SettingType setting_type
,
197 PP_Flash_BrowserOperations_Permission permission
,
198 bool clear_site_specific
) {
199 Send(new PpapiHostMsg_SetDefaultPermissionResult(
201 SetDefaultPermission(plugin_data_path
, setting_type
, permission
,
202 clear_site_specific
)));
205 void BrokerProcessDispatcher::OnSetSitePermission(
207 const base::FilePath
& plugin_data_path
,
208 PP_Flash_BrowserOperations_SettingType setting_type
,
209 const ppapi::FlashSiteSettings
& sites
) {
210 Send(new PpapiHostMsg_SetSitePermissionResult(
211 request_id
, SetSitePermission(plugin_data_path
, setting_type
, sites
)));
214 void BrokerProcessDispatcher::GetSitesWithData(
215 const base::FilePath
& plugin_data_path
,
216 std::vector
<std::string
>* site_vector
) {
217 std::string data_str
= ConvertPluginDataPath(plugin_data_path
);
218 if (flash_browser_operations_1_3_
) {
220 flash_browser_operations_1_3_
->GetSitesWithData(data_str
.c_str(), &sites
);
224 for (size_t i
= 0; sites
[i
]; ++i
)
225 site_vector
->push_back(sites
[i
]);
227 flash_browser_operations_1_3_
->FreeSiteList(sites
);
231 bool BrokerProcessDispatcher::ClearSiteData(
232 const base::FilePath
& plugin_data_path
,
233 const std::string
& site
,
236 std::string data_str
= ConvertPluginDataPath(plugin_data_path
);
237 if (flash_browser_operations_1_3_
) {
238 flash_browser_operations_1_3_
->ClearSiteData(
239 data_str
.c_str(), site
.empty() ? NULL
: site
.c_str(), flags
, max_age
);
243 // TODO(viettrungluu): Remove this (and the 1.0 interface) sometime after M21
245 if (flash_browser_operations_1_2_
) {
246 flash_browser_operations_1_2_
->ClearSiteData(
247 data_str
.c_str(), site
.empty() ? NULL
: site
.c_str(), flags
, max_age
);
251 if (flash_browser_operations_1_0_
) {
252 flash_browser_operations_1_0_
->ClearSiteData(
253 data_str
.c_str(), site
.empty() ? NULL
: site
.c_str(), flags
, max_age
);
260 bool BrokerProcessDispatcher::DeauthorizeContentLicenses(
261 const base::FilePath
& plugin_data_path
) {
262 if (flash_browser_operations_1_3_
) {
263 std::string data_str
= ConvertPluginDataPath(plugin_data_path
);
264 return PP_ToBool(flash_browser_operations_1_3_
->DeauthorizeContentLicenses(
268 if (flash_browser_operations_1_2_
) {
269 std::string data_str
= ConvertPluginDataPath(plugin_data_path
);
270 return PP_ToBool(flash_browser_operations_1_2_
->DeauthorizeContentLicenses(
277 bool BrokerProcessDispatcher::SetDefaultPermission(
278 const base::FilePath
& plugin_data_path
,
279 PP_Flash_BrowserOperations_SettingType setting_type
,
280 PP_Flash_BrowserOperations_Permission permission
,
281 bool clear_site_specific
) {
282 if (flash_browser_operations_1_3_
) {
283 std::string data_str
= ConvertPluginDataPath(plugin_data_path
);
284 return PP_ToBool(flash_browser_operations_1_3_
->SetDefaultPermission(
285 data_str
.c_str(), setting_type
, permission
,
286 PP_FromBool(clear_site_specific
)));
289 if (flash_browser_operations_1_2_
) {
290 std::string data_str
= ConvertPluginDataPath(plugin_data_path
);
291 return PP_ToBool(flash_browser_operations_1_2_
->SetDefaultPermission(
292 data_str
.c_str(), setting_type
, permission
,
293 PP_FromBool(clear_site_specific
)));
299 bool BrokerProcessDispatcher::SetSitePermission(
300 const base::FilePath
& plugin_data_path
,
301 PP_Flash_BrowserOperations_SettingType setting_type
,
302 const ppapi::FlashSiteSettings
& sites
) {
306 std::string data_str
= ConvertPluginDataPath(plugin_data_path
);
307 scoped_ptr
<PP_Flash_BrowserOperations_SiteSetting
[]> site_array(
308 new PP_Flash_BrowserOperations_SiteSetting
[sites
.size()]);
310 for (size_t i
= 0; i
< sites
.size(); ++i
) {
311 site_array
[i
].site
= sites
[i
].site
.c_str();
312 site_array
[i
].permission
= sites
[i
].permission
;
315 if (flash_browser_operations_1_3_
) {
316 PP_Bool result
= flash_browser_operations_1_3_
->SetSitePermission(
317 data_str
.c_str(), setting_type
, sites
.size(), site_array
.get());
319 return PP_ToBool(result
);
322 if (flash_browser_operations_1_2_
) {
323 PP_Bool result
= flash_browser_operations_1_2_
->SetSitePermission(
324 data_str
.c_str(), setting_type
, sites
.size(), site_array
.get());
326 return PP_ToBool(result
);
332 } // namespace content