OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "chrome/browser/extensions/api/web_view/web_view_internal_api.h" | 5 #include "extensions/browser/api/web_view/web_view_internal_api.h" |
6 | 6 |
7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "chrome/browser/extensions/api/browsing_data/browsing_data_api.h" | |
9 #include "chrome/browser/extensions/api/context_menus/context_menus_api.h" | |
10 #include "chrome/browser/extensions/api/context_menus/context_menus_api_helpers.
h" | |
11 #include "chrome/browser/extensions/tab_helper.h" | |
12 #include "chrome/browser/profiles/profile.h" | |
13 #include "chrome/common/extensions/api/web_view_internal.h" | |
14 #include "content/public/browser/render_process_host.h" | 8 #include "content/public/browser/render_process_host.h" |
15 #include "content/public/browser/render_view_host.h" | 9 #include "content/public/browser/render_view_host.h" |
16 #include "content/public/browser/storage_partition.h" | |
17 #include "content/public/browser/web_contents.h" | 10 #include "content/public/browser/web_contents.h" |
18 #include "content/public/common/stop_find_action.h" | 11 #include "content/public/common/stop_find_action.h" |
19 #include "extensions/browser/guest_view/web_view/web_view_permission_helper.h" | 12 #include "extensions/common/api/web_view_internal.h" |
20 #include "extensions/common/error_utils.h" | |
21 #include "third_party/WebKit/public/web/WebFindOptions.h" | 13 #include "third_party/WebKit/public/web/WebFindOptions.h" |
22 | 14 |
23 using content::WebContents; | 15 using content::WebContents; |
24 using extensions::api::web_view_internal::SetPermission::Params; | 16 using extensions::core_api::web_view_internal::SetPermission::Params; |
25 using extensions::core_api::extension_types::InjectDetails; | 17 using extensions::core_api::extension_types::InjectDetails; |
26 namespace helpers = extensions::context_menus_api_helpers; | 18 namespace webview = extensions::core_api::web_view_internal; |
27 namespace webview = extensions::api::web_view_internal; | |
28 | 19 |
29 namespace extensions { | 20 namespace extensions { |
30 | 21 |
31 namespace { | |
32 int MaskForKey(const char* key) { | |
33 if (strcmp(key, extension_browsing_data_api_constants::kAppCacheKey) == 0) | |
34 return content::StoragePartition::REMOVE_DATA_MASK_APPCACHE; | |
35 if (strcmp(key, extension_browsing_data_api_constants::kCookiesKey) == 0) | |
36 return content::StoragePartition::REMOVE_DATA_MASK_COOKIES; | |
37 if (strcmp(key, extension_browsing_data_api_constants::kFileSystemsKey) == 0) | |
38 return content::StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS; | |
39 if (strcmp(key, extension_browsing_data_api_constants::kIndexedDBKey) == 0) | |
40 return content::StoragePartition::REMOVE_DATA_MASK_INDEXEDDB; | |
41 if (strcmp(key, extension_browsing_data_api_constants::kLocalStorageKey) == 0) | |
42 return content::StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE; | |
43 if (strcmp(key, extension_browsing_data_api_constants::kWebSQLKey) == 0) | |
44 return content::StoragePartition::REMOVE_DATA_MASK_WEBSQL; | |
45 return 0; | |
46 } | |
47 | |
48 } // namespace | |
49 | |
50 bool WebViewInternalExtensionFunction::RunAsync() { | 22 bool WebViewInternalExtensionFunction::RunAsync() { |
51 int instance_id = 0; | 23 int instance_id = 0; |
52 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &instance_id)); | 24 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &instance_id)); |
53 WebViewGuest* guest = WebViewGuest::From( | 25 WebViewGuest* guest = WebViewGuest::From( |
54 render_view_host()->GetProcess()->GetID(), instance_id); | 26 render_view_host()->GetProcess()->GetID(), instance_id); |
55 if (!guest) | 27 if (!guest) |
56 return false; | 28 return false; |
57 | 29 |
58 return RunAsyncSafe(guest); | 30 return RunAsyncSafe(guest); |
59 } | 31 } |
60 | 32 |
61 // TODO(lazyboy): Add checks similar to | |
62 // WebViewInternalExtensionFunction::RunAsyncSafe(WebViewGuest*). | |
63 bool WebViewInternalContextMenusCreateFunction::RunAsync() { | |
64 scoped_ptr<webview::ContextMenusCreate::Params> params( | |
65 webview::ContextMenusCreate::Params::Create(*args_)); | |
66 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
67 | |
68 MenuItem::Id id( | |
69 Profile::FromBrowserContext(browser_context())->IsOffTheRecord(), | |
70 MenuItem::ExtensionKey(extension_id(), params->instance_id)); | |
71 | |
72 if (params->create_properties.id.get()) { | |
73 id.string_uid = *params->create_properties.id; | |
74 } else { | |
75 // The Generated Id is added by web_view_internal_custom_bindings.js. | |
76 base::DictionaryValue* properties = NULL; | |
77 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &properties)); | |
78 EXTENSION_FUNCTION_VALIDATE( | |
79 properties->GetInteger(helpers::kGeneratedIdKey, &id.uid)); | |
80 } | |
81 | |
82 bool success = extensions::context_menus_api_helpers::CreateMenuItem( | |
83 params->create_properties, | |
84 Profile::FromBrowserContext(browser_context()), | |
85 extension(), | |
86 id, | |
87 &error_); | |
88 | |
89 SendResponse(success); | |
90 return success; | |
91 } | |
92 | |
93 bool WebViewInternalNavigateFunction::RunAsyncSafe(WebViewGuest* guest) { | 33 bool WebViewInternalNavigateFunction::RunAsyncSafe(WebViewGuest* guest) { |
94 scoped_ptr<webview::Navigate::Params> params( | 34 scoped_ptr<webview::Navigate::Params> params( |
95 webview::Navigate::Params::Create(*args_)); | 35 webview::Navigate::Params::Create(*args_)); |
96 EXTENSION_FUNCTION_VALIDATE(params.get()); | 36 EXTENSION_FUNCTION_VALIDATE(params.get()); |
97 std::string src = params->src; | 37 std::string src = params->src; |
98 guest->NavigateGuest(src); | 38 guest->NavigateGuest(src); |
99 return true; | 39 return true; |
100 } | 40 } |
101 | 41 |
102 bool WebViewInternalContextMenusUpdateFunction::RunAsync() { | |
103 scoped_ptr<webview::ContextMenusUpdate::Params> params( | |
104 webview::ContextMenusUpdate::Params::Create(*args_)); | |
105 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
106 | |
107 Profile* profile = Profile::FromBrowserContext(browser_context()); | |
108 MenuItem::Id item_id( | |
109 profile->IsOffTheRecord(), | |
110 MenuItem::ExtensionKey(extension_id(), params->instance_id)); | |
111 | |
112 if (params->id.as_string) | |
113 item_id.string_uid = *params->id.as_string; | |
114 else if (params->id.as_integer) | |
115 item_id.uid = *params->id.as_integer; | |
116 else | |
117 NOTREACHED(); | |
118 | |
119 bool success = extensions::context_menus_api_helpers::UpdateMenuItem( | |
120 params->update_properties, profile, extension(), item_id, &error_); | |
121 SendResponse(success); | |
122 return success; | |
123 } | |
124 | |
125 bool WebViewInternalContextMenusRemoveFunction::RunAsync() { | |
126 scoped_ptr<webview::ContextMenusRemove::Params> params( | |
127 webview::ContextMenusRemove::Params::Create(*args_)); | |
128 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
129 | |
130 MenuManager* menu_manager = | |
131 MenuManager::Get(Profile::FromBrowserContext(browser_context())); | |
132 | |
133 MenuItem::Id id( | |
134 Profile::FromBrowserContext(browser_context())->IsOffTheRecord(), | |
135 MenuItem::ExtensionKey(extension_id(), params->instance_id)); | |
136 | |
137 if (params->menu_item_id.as_string) { | |
138 id.string_uid = *params->menu_item_id.as_string; | |
139 } else if (params->menu_item_id.as_integer) { | |
140 id.uid = *params->menu_item_id.as_integer; | |
141 } else { | |
142 NOTREACHED(); | |
143 } | |
144 | |
145 bool success = true; | |
146 MenuItem* item = menu_manager->GetItemById(id); | |
147 // Ensure one <webview> can't remove another's menu items. | |
148 if (!item || item->id().extension_key != id.extension_key) { | |
149 error_ = ErrorUtils::FormatErrorMessage( | |
150 context_menus_api_helpers::kCannotFindItemError, | |
151 context_menus_api_helpers::GetIDString(id)); | |
152 success = false; | |
153 } else if (!menu_manager->RemoveContextMenuItem(id)) { | |
154 success = false; | |
155 } | |
156 | |
157 SendResponse(success); | |
158 return success; | |
159 } | |
160 | |
161 bool WebViewInternalContextMenusRemoveAllFunction::RunAsync() { | |
162 scoped_ptr<webview::ContextMenusRemoveAll::Params> params( | |
163 webview::ContextMenusRemoveAll::Params::Create(*args_)); | |
164 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
165 | |
166 MenuManager* menu_manager = | |
167 MenuManager::Get(Profile::FromBrowserContext(browser_context())); | |
168 | |
169 int webview_instance_id = params->instance_id; | |
170 menu_manager->RemoveAllContextItems( | |
171 MenuItem::ExtensionKey(extension()->id(), webview_instance_id)); | |
172 SendResponse(true); | |
173 return true; | |
174 } | |
175 | |
176 WebViewInternalClearDataFunction::WebViewInternalClearDataFunction() | |
177 : remove_mask_(0), bad_message_(false) { | |
178 } | |
179 | |
180 WebViewInternalClearDataFunction::~WebViewInternalClearDataFunction() { | |
181 } | |
182 | |
183 // Parses the |dataToRemove| argument to generate the remove mask. Sets | |
184 // |bad_message_| (like EXTENSION_FUNCTION_VALIDATE would if this were a bool | |
185 // method) if 'dataToRemove' is not present. | |
186 uint32 WebViewInternalClearDataFunction::GetRemovalMask() { | |
187 base::DictionaryValue* data_to_remove; | |
188 if (!args_->GetDictionary(2, &data_to_remove)) { | |
189 bad_message_ = true; | |
190 return 0; | |
191 } | |
192 | |
193 uint32 remove_mask = 0; | |
194 for (base::DictionaryValue::Iterator i(*data_to_remove); !i.IsAtEnd(); | |
195 i.Advance()) { | |
196 bool selected = false; | |
197 if (!i.value().GetAsBoolean(&selected)) { | |
198 bad_message_ = true; | |
199 return 0; | |
200 } | |
201 if (selected) | |
202 remove_mask |= MaskForKey(i.key().c_str()); | |
203 } | |
204 | |
205 return remove_mask; | |
206 } | |
207 | |
208 // TODO(lazyboy): Parameters in this extension function are similar (or a | |
209 // sub-set) to BrowsingDataRemoverFunction. How can we share this code? | |
210 bool WebViewInternalClearDataFunction::RunAsyncSafe(WebViewGuest* guest) { | |
211 // Grab the initial |options| parameter, and parse out the arguments. | |
212 base::DictionaryValue* options; | |
213 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &options)); | |
214 DCHECK(options); | |
215 | |
216 // If |ms_since_epoch| isn't set, default it to 0. | |
217 double ms_since_epoch; | |
218 if (!options->GetDouble(extension_browsing_data_api_constants::kSinceKey, | |
219 &ms_since_epoch)) { | |
220 ms_since_epoch = 0; | |
221 } | |
222 | |
223 // base::Time takes a double that represents seconds since epoch. JavaScript | |
224 // gives developers milliseconds, so do a quick conversion before populating | |
225 // the object. Also, Time::FromDoubleT converts double time 0 to empty Time | |
226 // object. So we need to do special handling here. | |
227 remove_since_ = (ms_since_epoch == 0) | |
228 ? base::Time::UnixEpoch() | |
229 : base::Time::FromDoubleT(ms_since_epoch / 1000.0); | |
230 | |
231 remove_mask_ = GetRemovalMask(); | |
232 if (bad_message_) | |
233 return false; | |
234 | |
235 AddRef(); // Balanced below or in WebViewInternalClearDataFunction::Done(). | |
236 | |
237 bool scheduled = false; | |
238 if (remove_mask_) { | |
239 scheduled = guest->ClearData( | |
240 remove_since_, | |
241 remove_mask_, | |
242 base::Bind(&WebViewInternalClearDataFunction::ClearDataDone, this)); | |
243 } | |
244 if (!remove_mask_ || !scheduled) { | |
245 SendResponse(false); | |
246 Release(); // Balanced above. | |
247 return false; | |
248 } | |
249 | |
250 // Will finish asynchronously. | |
251 return true; | |
252 } | |
253 | |
254 void WebViewInternalClearDataFunction::ClearDataDone() { | |
255 Release(); // Balanced in RunAsync(). | |
256 SendResponse(true); | |
257 } | |
258 | |
259 WebViewInternalExecuteCodeFunction::WebViewInternalExecuteCodeFunction() | 42 WebViewInternalExecuteCodeFunction::WebViewInternalExecuteCodeFunction() |
260 : guest_instance_id_(0), guest_src_(GURL::EmptyGURL()) { | 43 : guest_instance_id_(0), guest_src_(GURL::EmptyGURL()) { |
261 } | 44 } |
262 | 45 |
263 WebViewInternalExecuteCodeFunction::~WebViewInternalExecuteCodeFunction() { | 46 WebViewInternalExecuteCodeFunction::~WebViewInternalExecuteCodeFunction() { |
264 } | 47 } |
265 | 48 |
266 bool WebViewInternalExecuteCodeFunction::Init() { | 49 bool WebViewInternalExecuteCodeFunction::Init() { |
267 if (details_.get()) | 50 if (details_.get()) |
268 return true; | 51 return true; |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
525 if (params->user_input) | 308 if (params->user_input) |
526 user_input = *params->user_input; | 309 user_input = *params->user_input; |
527 | 310 |
528 WebViewPermissionHelper* web_view_permission_helper = | 311 WebViewPermissionHelper* web_view_permission_helper = |
529 WebViewPermissionHelper::FromWebContents(guest->web_contents()); | 312 WebViewPermissionHelper::FromWebContents(guest->web_contents()); |
530 | 313 |
531 WebViewPermissionHelper::SetPermissionResult result = | 314 WebViewPermissionHelper::SetPermissionResult result = |
532 web_view_permission_helper->SetPermission( | 315 web_view_permission_helper->SetPermission( |
533 params->request_id, action, user_input); | 316 params->request_id, action, user_input); |
534 | 317 |
535 EXTENSION_FUNCTION_VALIDATE( | 318 EXTENSION_FUNCTION_VALIDATE(result != |
536 result != WebViewPermissionHelper::SET_PERMISSION_INVALID); | 319 WebViewPermissionHelper::SET_PERMISSION_INVALID); |
537 | 320 |
538 SetResult(new base::FundamentalValue( | 321 SetResult(new base::FundamentalValue( |
539 result == WebViewPermissionHelper::SET_PERMISSION_ALLOWED)); | 322 result == WebViewPermissionHelper::SET_PERMISSION_ALLOWED)); |
540 SendResponse(true); | 323 SendResponse(true); |
541 return true; | 324 return true; |
542 } | 325 } |
543 | 326 |
544 WebViewInternalShowContextMenuFunction:: | |
545 WebViewInternalShowContextMenuFunction() { | |
546 } | |
547 | |
548 WebViewInternalShowContextMenuFunction:: | |
549 ~WebViewInternalShowContextMenuFunction() { | |
550 } | |
551 | |
552 bool WebViewInternalShowContextMenuFunction::RunAsyncSafe(WebViewGuest* guest) { | |
553 scoped_ptr<webview::ShowContextMenu::Params> params( | |
554 webview::ShowContextMenu::Params::Create(*args_)); | |
555 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
556 | |
557 // TODO(lazyboy): Actually implement filtering menu items, we pass NULL for | |
558 // now. | |
559 guest->ShowContextMenu(params->request_id, NULL); | |
560 | |
561 SendResponse(true); | |
562 return true; | |
563 } | |
564 | |
565 WebViewInternalOverrideUserAgentFunction:: | 327 WebViewInternalOverrideUserAgentFunction:: |
566 WebViewInternalOverrideUserAgentFunction() { | 328 WebViewInternalOverrideUserAgentFunction() { |
567 } | 329 } |
568 | 330 |
569 WebViewInternalOverrideUserAgentFunction:: | 331 WebViewInternalOverrideUserAgentFunction:: |
570 ~WebViewInternalOverrideUserAgentFunction() { | 332 ~WebViewInternalOverrideUserAgentFunction() { |
571 } | 333 } |
572 | 334 |
573 bool WebViewInternalOverrideUserAgentFunction::RunAsyncSafe( | 335 bool WebViewInternalOverrideUserAgentFunction::RunAsyncSafe( |
574 WebViewGuest* guest) { | 336 WebViewGuest* guest) { |
(...skipping 21 matching lines...) Expand all Loading... |
596 | 358 |
597 WebViewInternalTerminateFunction::~WebViewInternalTerminateFunction() { | 359 WebViewInternalTerminateFunction::~WebViewInternalTerminateFunction() { |
598 } | 360 } |
599 | 361 |
600 bool WebViewInternalTerminateFunction::RunAsyncSafe(WebViewGuest* guest) { | 362 bool WebViewInternalTerminateFunction::RunAsyncSafe(WebViewGuest* guest) { |
601 guest->Terminate(); | 363 guest->Terminate(); |
602 return true; | 364 return true; |
603 } | 365 } |
604 | 366 |
605 } // namespace extensions | 367 } // namespace extensions |
OLD | NEW |