OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/ppapi_plugin/broker_process_dispatcher.h" | 5 #include "content/ppapi_plugin/broker_process_dispatcher.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
11 #include "content/common/child_process.h" | 10 #include "content/common/child_process.h" |
12 #include "ppapi/c/pp_bool.h" | 11 #include "ppapi/c/pp_bool.h" |
13 #include "ppapi/c/private/ppp_flash_browser_operations.h" | 12 #include "ppapi/c/private/ppp_flash_browser_operations.h" |
14 #include "ppapi/proxy/ppapi_messages.h" | 13 #include "ppapi/proxy/ppapi_messages.h" |
15 #include "ppapi/shared_impl/ppapi_globals.h" | |
16 #include "ppapi/shared_impl/var.h" | |
17 #include "ppapi/shared_impl/var_tracker.h" | |
18 | 14 |
19 namespace { | 15 namespace { |
20 | 16 |
21 // How long we wait before releasing the broker process. | 17 // How long we wait before releasing the broker process. |
22 const int kBrokerReleaseTimeSeconds = 30; | 18 const int kBrokerReleaseTimeSeconds = 30; |
23 | 19 |
24 std::string ConvertPluginDataPath(const FilePath& plugin_data_path) { | 20 std::string ConvertPluginDataPath(const FilePath& plugin_data_path) { |
25 // The string is always 8-bit, convert on Windows. | 21 // The string is always 8-bit, convert on Windows. |
26 #if defined(OS_WIN) | 22 #if defined(OS_WIN) |
27 return WideToUTF8(plugin_data_path.value()); | 23 return WideToUTF8(plugin_data_path.value()); |
28 #else | 24 #else |
29 return plugin_data_path.value(); | 25 return plugin_data_path.value(); |
30 #endif | 26 #endif |
31 } | 27 } |
32 | 28 |
33 struct GetPermissionSettingsContext { | |
34 GetPermissionSettingsContext( | |
35 const base::WeakPtr<BrokerProcessDispatcher> in_dispatcher, | |
36 uint32 in_request_id) | |
37 : dispatcher(in_dispatcher), | |
38 request_id(in_request_id) { | |
39 } | |
40 | |
41 base::WeakPtr<BrokerProcessDispatcher> dispatcher; | |
42 uint32 request_id; | |
43 }; | |
44 | |
45 void GetPermissionSettingsCallback( | |
46 void* user_data, | |
47 PP_Bool success, | |
48 PP_Flash_BrowserOperations_Permission default_permission, | |
49 uint32_t site_count, | |
50 const PP_Flash_BrowserOperations_SiteSetting sites[]) { | |
51 scoped_ptr<GetPermissionSettingsContext> context( | |
52 reinterpret_cast<GetPermissionSettingsContext*>(user_data)); | |
53 | |
54 if (!context->dispatcher) | |
55 return; | |
56 | |
57 ppapi::FlashSiteSettings site_vector; | |
58 if (success) { | |
59 site_vector.reserve(site_count); | |
60 for (uint32_t i = 0; i < site_count; ++i) { | |
61 ppapi::StringVar* string_var = ppapi::StringVar::FromPPVar(sites[i].site); | |
62 if (!string_var) { | |
63 success = PP_FALSE; | |
64 break; | |
65 } | |
66 site_vector.push_back( | |
67 ppapi::FlashSiteSetting(string_var->value(), sites[i].permission)); | |
68 } | |
69 | |
70 if (!success) | |
71 site_vector.clear(); | |
72 } | |
73 context->dispatcher->OnGetPermissionSettingsCompleted( | |
74 context->request_id, PP_ToBool(success), default_permission, site_vector); | |
75 } | |
76 | |
77 } // namespace | 29 } // namespace |
78 | 30 |
79 BrokerProcessDispatcher::BrokerProcessDispatcher( | 31 BrokerProcessDispatcher::BrokerProcessDispatcher( |
80 PP_GetInterface_Func get_plugin_interface, | 32 PP_GetInterface_Func get_plugin_interface, |
81 PP_ConnectInstance_Func connect_instance) | 33 PP_ConnectInstance_Func connect_instance) |
82 : ppapi::proxy::BrokerSideDispatcher(connect_instance), | 34 : ppapi::proxy::BrokerSideDispatcher(connect_instance), |
83 get_plugin_interface_(get_plugin_interface), | 35 get_plugin_interface_(get_plugin_interface) { |
84 flash_browser_operations_1_1_(NULL), | |
85 flash_browser_operations_1_0_(NULL) { | |
86 ChildProcess::current()->AddRefProcess(); | 36 ChildProcess::current()->AddRefProcess(); |
87 | |
88 if (get_plugin_interface) { | |
89 flash_browser_operations_1_0_ = | |
90 static_cast<const PPP_Flash_BrowserOperations_1_0*>( | |
91 get_plugin_interface_(PPP_FLASH_BROWSEROPERATIONS_INTERFACE_1_0)); | |
92 | |
93 flash_browser_operations_1_1_ = | |
94 static_cast<const PPP_Flash_BrowserOperations_1_1*>( | |
95 get_plugin_interface_(PPP_FLASH_BROWSEROPERATIONS_INTERFACE_1_1)); | |
96 } | |
97 } | 37 } |
98 | 38 |
99 BrokerProcessDispatcher::~BrokerProcessDispatcher() { | 39 BrokerProcessDispatcher::~BrokerProcessDispatcher() { |
100 DVLOG(1) << "BrokerProcessDispatcher::~BrokerProcessDispatcher()"; | 40 DVLOG(1) << "BrokerProcessDispatcher::~BrokerProcessDispatcher()"; |
101 // Don't free the process right away. This timer allows the child process | 41 // Don't free the process right away. This timer allows the child process |
102 // to be re-used if the user rapidly goes to a new page that requires this | 42 // to be re-used if the user rapidly goes to a new page that requires this |
103 // plugin. This is the case for common plugins where they may be used on a | 43 // plugin. This is the case for common plugins where they may be used on a |
104 // source and destination page of a navigation. We don't want to tear down | 44 // source and destination page of a navigation. We don't want to tear down |
105 // and re-start processes each time in these cases. | 45 // and re-start processes each time in these cases. |
106 MessageLoop::current()->PostDelayedTask( | 46 MessageLoop::current()->PostDelayedTask( |
107 FROM_HERE, | 47 FROM_HERE, |
108 base::Bind(&ChildProcess::ReleaseProcess, | 48 base::Bind(&ChildProcess::ReleaseProcess, |
109 base::Unretained(ChildProcess::current())), | 49 base::Unretained(ChildProcess::current())), |
110 base::TimeDelta::FromSeconds(kBrokerReleaseTimeSeconds)); | 50 base::TimeDelta::FromSeconds(kBrokerReleaseTimeSeconds)); |
111 } | 51 } |
112 | 52 |
113 bool BrokerProcessDispatcher::OnMessageReceived(const IPC::Message& msg) { | 53 bool BrokerProcessDispatcher::OnMessageReceived(const IPC::Message& msg) { |
114 IPC_BEGIN_MESSAGE_MAP(BrokerProcessDispatcher, msg) | 54 IPC_BEGIN_MESSAGE_MAP(BrokerProcessDispatcher, msg) |
115 IPC_MESSAGE_HANDLER(PpapiMsg_ClearSiteData, OnMsgClearSiteData) | 55 IPC_MESSAGE_HANDLER(PpapiMsg_ClearSiteData, OnMsgClearSiteData) |
116 IPC_MESSAGE_HANDLER(PpapiMsg_DeauthorizeContentLicenses, | 56 IPC_MESSAGE_HANDLER(PpapiMsg_DeauthorizeContentLicenses, |
117 OnMsgDeauthorizeContentLicenses) | 57 OnMsgDeauthorizeContentLicenses) |
118 IPC_MESSAGE_HANDLER(PpapiMsg_GetPermissionSettings, | |
119 OnMsgGetPermissionSettings) | |
120 IPC_MESSAGE_HANDLER(PpapiMsg_SetDefaultPermission, | |
121 OnMsgSetDefaultPermission) | |
122 IPC_MESSAGE_HANDLER(PpapiMsg_SetSitePermission, OnMsgSetSitePermission) | |
123 IPC_MESSAGE_UNHANDLED(return BrokerSideDispatcher::OnMessageReceived(msg)) | 58 IPC_MESSAGE_UNHANDLED(return BrokerSideDispatcher::OnMessageReceived(msg)) |
124 IPC_END_MESSAGE_MAP() | 59 IPC_END_MESSAGE_MAP() |
125 return true; | 60 return true; |
126 } | 61 } |
127 | 62 |
128 void BrokerProcessDispatcher::OnGetPermissionSettingsCompleted( | |
129 uint32 request_id, | |
130 bool success, | |
131 PP_Flash_BrowserOperations_Permission default_permission, | |
132 const ppapi::FlashSiteSettings& sites) { | |
133 Send(new PpapiHostMsg_GetPermissionSettingsResult( | |
134 request_id, success, default_permission, sites)); | |
135 } | |
136 | |
137 void BrokerProcessDispatcher::OnMsgClearSiteData( | 63 void BrokerProcessDispatcher::OnMsgClearSiteData( |
138 const FilePath& plugin_data_path, | 64 const FilePath& plugin_data_path, |
139 const std::string& site, | 65 const std::string& site, |
140 uint64 flags, | 66 uint64 flags, |
141 uint64 max_age) { | 67 uint64 max_age) { |
142 Send(new PpapiHostMsg_ClearSiteDataResult( | 68 Send(new PpapiHostMsg_ClearSiteDataResult( |
143 ClearSiteData(plugin_data_path, site, flags, max_age))); | 69 ClearSiteData(plugin_data_path, site, flags, max_age))); |
144 } | 70 } |
145 | 71 |
146 void BrokerProcessDispatcher::OnMsgDeauthorizeContentLicenses( | 72 void BrokerProcessDispatcher::OnMsgDeauthorizeContentLicenses( |
147 uint32 request_id, | 73 uint32 request_id, |
148 const FilePath& plugin_data_path) { | 74 const FilePath& plugin_data_path) { |
149 Send(new PpapiHostMsg_DeauthorizeContentLicensesResult( | 75 Send(new PpapiHostMsg_DeauthorizeContentLicensesResult( |
150 request_id, DeauthorizeContentLicenses(plugin_data_path))); | 76 request_id, DeauthorizeContentLicenses(plugin_data_path))); |
151 } | 77 } |
152 | 78 |
153 void BrokerProcessDispatcher::OnMsgGetPermissionSettings( | |
154 uint32 request_id, | |
155 const FilePath& plugin_data_path, | |
156 PP_Flash_BrowserOperations_SettingType setting_type) { | |
157 if (!flash_browser_operations_1_1_) { | |
158 OnGetPermissionSettingsCompleted( | |
159 request_id, false, PP_FLASH_BROWSEROPERATIONS_PERMISSION_DEFAULT, | |
160 ppapi::FlashSiteSettings()); | |
161 return; | |
162 } | |
163 | |
164 std::string data_str = ConvertPluginDataPath(plugin_data_path); | |
165 // The GetPermissionSettingsContext object will be deleted in | |
166 // GetPermissionSettingsCallback(). | |
167 flash_browser_operations_1_1_->GetPermissionSettings( | |
168 data_str.c_str(), setting_type, &GetPermissionSettingsCallback, | |
169 new GetPermissionSettingsContext(AsWeakPtr(), request_id)); | |
170 } | |
171 | |
172 void BrokerProcessDispatcher::OnMsgSetDefaultPermission( | |
173 uint32 request_id, | |
174 const FilePath& plugin_data_path, | |
175 PP_Flash_BrowserOperations_SettingType setting_type, | |
176 PP_Flash_BrowserOperations_Permission permission, | |
177 bool clear_site_specific) { | |
178 Send(new PpapiHostMsg_SetDefaultPermissionResult( | |
179 request_id, | |
180 SetDefaultPermission(plugin_data_path, setting_type, permission, | |
181 clear_site_specific))); | |
182 } | |
183 | |
184 void BrokerProcessDispatcher::OnMsgSetSitePermission( | |
185 uint32 request_id, | |
186 const FilePath& plugin_data_path, | |
187 PP_Flash_BrowserOperations_SettingType setting_type, | |
188 const ppapi::FlashSiteSettings& sites) { | |
189 Send(new PpapiHostMsg_SetSitePermissionResult( | |
190 request_id, SetSitePermission(plugin_data_path, setting_type, sites))); | |
191 } | |
192 | |
193 bool BrokerProcessDispatcher::ClearSiteData(const FilePath& plugin_data_path, | 79 bool BrokerProcessDispatcher::ClearSiteData(const FilePath& plugin_data_path, |
194 const std::string& site, | 80 const std::string& site, |
195 uint64 flags, | 81 uint64 flags, |
196 uint64 max_age) { | 82 uint64 max_age) { |
197 std::string data_str = ConvertPluginDataPath(plugin_data_path); | 83 if (!get_plugin_interface_) |
198 if (flash_browser_operations_1_1_) { | 84 return false; |
199 flash_browser_operations_1_1_->ClearSiteData( | 85 |
200 data_str.c_str(), site.empty() ? NULL : site.c_str(), flags, max_age); | 86 const PPP_Flash_BrowserOperations_1_1* browser_interface = |
| 87 static_cast<const PPP_Flash_BrowserOperations_1_1*>( |
| 88 get_plugin_interface_(PPP_FLASH_BROWSEROPERATIONS_INTERFACE_1_1)); |
| 89 if (browser_interface) { |
| 90 std::string data_str = ConvertPluginDataPath(plugin_data_path); |
| 91 browser_interface->ClearSiteData(data_str.c_str(), |
| 92 site.empty() ? NULL : site.c_str(), |
| 93 flags, max_age); |
201 return true; | 94 return true; |
202 } | 95 } |
203 | 96 |
204 // TODO(viettrungluu): Remove this (and the 1.0 interface) sometime after M21 | 97 // TODO(viettrungluu): Remove this (and the 1.0 interface) sometime after M21 |
205 // goes to Stable. | 98 // goes to Stable. |
206 if (flash_browser_operations_1_0_) { | 99 const PPP_Flash_BrowserOperations_1_0* browser_interface_1_0 = |
207 flash_browser_operations_1_0_->ClearSiteData( | 100 static_cast<const PPP_Flash_BrowserOperations_1_0*>( |
208 data_str.c_str(), site.empty() ? NULL : site.c_str(), flags, max_age); | 101 get_plugin_interface_(PPP_FLASH_BROWSEROPERATIONS_INTERFACE_1_0)); |
| 102 if (browser_interface_1_0) { |
| 103 std::string data_str = ConvertPluginDataPath(plugin_data_path); |
| 104 browser_interface_1_0->ClearSiteData(data_str.c_str(), |
| 105 site.empty() ? NULL : site.c_str(), |
| 106 flags, max_age); |
209 return true; | 107 return true; |
210 } | 108 } |
211 | 109 |
212 return false; | 110 return false; |
213 } | 111 } |
214 | 112 |
215 bool BrokerProcessDispatcher::DeauthorizeContentLicenses( | 113 bool BrokerProcessDispatcher::DeauthorizeContentLicenses( |
216 const FilePath& plugin_data_path) { | 114 const FilePath& plugin_data_path) { |
217 if (!flash_browser_operations_1_1_) | 115 if (!get_plugin_interface_) |
| 116 return false; |
| 117 const PPP_Flash_BrowserOperations_1_1* browser_interface = |
| 118 static_cast<const PPP_Flash_BrowserOperations_1_1*>( |
| 119 get_plugin_interface_(PPP_FLASH_BROWSEROPERATIONS_INTERFACE_1_1)); |
| 120 if (!browser_interface) |
218 return false; | 121 return false; |
219 | 122 |
220 std::string data_str = ConvertPluginDataPath(plugin_data_path); | 123 std::string data_str = ConvertPluginDataPath(plugin_data_path); |
221 return PP_ToBool(flash_browser_operations_1_1_->DeauthorizeContentLicenses( | 124 return PP_ToBool(browser_interface->DeauthorizeContentLicenses( |
222 data_str.c_str())); | 125 data_str.c_str())); |
223 } | 126 } |
224 | 127 |
225 bool BrokerProcessDispatcher::SetDefaultPermission( | |
226 const FilePath& plugin_data_path, | |
227 PP_Flash_BrowserOperations_SettingType setting_type, | |
228 PP_Flash_BrowserOperations_Permission permission, | |
229 bool clear_site_specific) { | |
230 if (!flash_browser_operations_1_1_) | |
231 return false; | |
232 | |
233 std::string data_str = ConvertPluginDataPath(plugin_data_path); | |
234 return PP_ToBool(flash_browser_operations_1_1_->SetDefaultPermission( | |
235 data_str.c_str(), setting_type, permission, | |
236 PP_FromBool(clear_site_specific))); | |
237 } | |
238 | |
239 bool BrokerProcessDispatcher::SetSitePermission( | |
240 const FilePath& plugin_data_path, | |
241 PP_Flash_BrowserOperations_SettingType setting_type, | |
242 const ppapi::FlashSiteSettings& sites) { | |
243 if (!flash_browser_operations_1_1_) | |
244 return false; | |
245 | |
246 if (sites.empty()) | |
247 return true; | |
248 | |
249 std::string data_str = ConvertPluginDataPath(plugin_data_path); | |
250 scoped_array<PP_Flash_BrowserOperations_SiteSetting> site_array( | |
251 new PP_Flash_BrowserOperations_SiteSetting[sites.size()]); | |
252 | |
253 for (size_t i = 0; i < sites.size(); ++i) { | |
254 site_array[i].site = ppapi::StringVar::StringToPPVar(sites[i].site); | |
255 site_array[i].permission = sites[i].permission; | |
256 } | |
257 | |
258 PP_Bool result = flash_browser_operations_1_1_->SetSitePermission( | |
259 data_str.c_str(), setting_type, sites.size(), site_array.get()); | |
260 | |
261 for (size_t i = 0; i < sites.size(); ++i) | |
262 ppapi::PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(site_array[i].site); | |
263 | |
264 return PP_ToBool(result); | |
265 } | |
OLD | NEW |