Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(162)

Side by Side Diff: chrome/browser/extensions/api/web_view/web_view_internal_api.cc

Issue 541753004: Split web_view_internal_api and move part of it to extensions. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/extensions/api/web_view/web_view_internal_api.h"
6
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"
15 #include "content/public/browser/render_view_host.h"
16 #include "content/public/browser/storage_partition.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/common/stop_find_action.h"
19 #include "extensions/browser/api/capture_web_contents_function_impl.h"
20 #include "extensions/browser/api/execute_code_function_impl.h"
21 #include "extensions/browser/guest_view/web_view/web_view_permission_helper.h"
22 #include "extensions/common/error_utils.h"
23 #include "third_party/WebKit/public/web/WebFindOptions.h"
24
25 using content::WebContents;
26 using extensions::api::web_view_internal::SetPermission::Params;
27 using extensions::core_api::extension_types::InjectDetails;
28 namespace helpers = extensions::context_menus_api_helpers;
29 namespace webview = extensions::api::web_view_internal;
30
31 namespace extensions {
32
33 namespace {
34 int MaskForKey(const char* key) {
35 if (strcmp(key, extension_browsing_data_api_constants::kAppCacheKey) == 0)
36 return content::StoragePartition::REMOVE_DATA_MASK_APPCACHE;
37 if (strcmp(key, extension_browsing_data_api_constants::kCookiesKey) == 0)
38 return content::StoragePartition::REMOVE_DATA_MASK_COOKIES;
39 if (strcmp(key, extension_browsing_data_api_constants::kFileSystemsKey) == 0)
40 return content::StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS;
41 if (strcmp(key, extension_browsing_data_api_constants::kIndexedDBKey) == 0)
42 return content::StoragePartition::REMOVE_DATA_MASK_INDEXEDDB;
43 if (strcmp(key, extension_browsing_data_api_constants::kLocalStorageKey) == 0)
44 return content::StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE;
45 if (strcmp(key, extension_browsing_data_api_constants::kWebSQLKey) == 0)
46 return content::StoragePartition::REMOVE_DATA_MASK_WEBSQL;
47 return 0;
48 }
49
50 } // namespace
51
52 bool WebViewInternalExtensionFunction::RunAsync() {
53 int instance_id = 0;
54 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &instance_id));
55 WebViewGuest* guest = WebViewGuest::From(
56 render_view_host()->GetProcess()->GetID(), instance_id);
57 if (!guest)
58 return false;
59
60 return RunAsyncSafe(guest);
61 }
62
63 // TODO(lazyboy): Add checks similar to
64 // WebViewInternalExtensionFunction::RunAsyncSafe(WebViewGuest*).
65 bool WebViewInternalContextMenusCreateFunction::RunAsync() {
66 scoped_ptr<webview::ContextMenusCreate::Params> params(
67 webview::ContextMenusCreate::Params::Create(*args_));
68 EXTENSION_FUNCTION_VALIDATE(params.get());
69
70 MenuItem::Id id(
71 Profile::FromBrowserContext(browser_context())->IsOffTheRecord(),
72 MenuItem::ExtensionKey(extension_id(), params->instance_id));
73
74 if (params->create_properties.id.get()) {
75 id.string_uid = *params->create_properties.id;
76 } else {
77 // The Generated Id is added by web_view_internal_custom_bindings.js.
78 base::DictionaryValue* properties = NULL;
79 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &properties));
80 EXTENSION_FUNCTION_VALIDATE(
81 properties->GetInteger(helpers::kGeneratedIdKey, &id.uid));
82 }
83
84 bool success = extensions::context_menus_api_helpers::CreateMenuItem(
85 params->create_properties,
86 Profile::FromBrowserContext(browser_context()),
87 extension(),
88 id,
89 &error_);
90
91 SendResponse(success);
92 return success;
93 }
94
95 bool WebViewInternalNavigateFunction::RunAsyncSafe(WebViewGuest* guest) {
96 scoped_ptr<webview::Navigate::Params> params(
97 webview::Navigate::Params::Create(*args_));
98 EXTENSION_FUNCTION_VALIDATE(params.get());
99 std::string src = params->src;
100 guest->NavigateGuest(src);
101 return true;
102 }
103
104 bool WebViewInternalContextMenusUpdateFunction::RunAsync() {
105 scoped_ptr<webview::ContextMenusUpdate::Params> params(
106 webview::ContextMenusUpdate::Params::Create(*args_));
107 EXTENSION_FUNCTION_VALIDATE(params.get());
108
109 Profile* profile = Profile::FromBrowserContext(browser_context());
110 MenuItem::Id item_id(
111 profile->IsOffTheRecord(),
112 MenuItem::ExtensionKey(extension_id(), params->instance_id));
113
114 if (params->id.as_string)
115 item_id.string_uid = *params->id.as_string;
116 else if (params->id.as_integer)
117 item_id.uid = *params->id.as_integer;
118 else
119 NOTREACHED();
120
121 bool success = extensions::context_menus_api_helpers::UpdateMenuItem(
122 params->update_properties, profile, extension(), item_id, &error_);
123 SendResponse(success);
124 return success;
125 }
126
127 bool WebViewInternalContextMenusRemoveFunction::RunAsync() {
128 scoped_ptr<webview::ContextMenusRemove::Params> params(
129 webview::ContextMenusRemove::Params::Create(*args_));
130 EXTENSION_FUNCTION_VALIDATE(params.get());
131
132 MenuManager* menu_manager =
133 MenuManager::Get(Profile::FromBrowserContext(browser_context()));
134
135 MenuItem::Id id(
136 Profile::FromBrowserContext(browser_context())->IsOffTheRecord(),
137 MenuItem::ExtensionKey(extension_id(), params->instance_id));
138
139 if (params->menu_item_id.as_string) {
140 id.string_uid = *params->menu_item_id.as_string;
141 } else if (params->menu_item_id.as_integer) {
142 id.uid = *params->menu_item_id.as_integer;
143 } else {
144 NOTREACHED();
145 }
146
147 bool success = true;
148 MenuItem* item = menu_manager->GetItemById(id);
149 // Ensure one <webview> can't remove another's menu items.
150 if (!item || item->id().extension_key != id.extension_key) {
151 error_ = ErrorUtils::FormatErrorMessage(
152 context_menus_api_helpers::kCannotFindItemError,
153 context_menus_api_helpers::GetIDString(id));
154 success = false;
155 } else if (!menu_manager->RemoveContextMenuItem(id)) {
156 success = false;
157 }
158
159 SendResponse(success);
160 return success;
161 }
162
163 bool WebViewInternalContextMenusRemoveAllFunction::RunAsync() {
164 scoped_ptr<webview::ContextMenusRemoveAll::Params> params(
165 webview::ContextMenusRemoveAll::Params::Create(*args_));
166 EXTENSION_FUNCTION_VALIDATE(params.get());
167
168 MenuManager* menu_manager =
169 MenuManager::Get(Profile::FromBrowserContext(browser_context()));
170
171 int webview_instance_id = params->instance_id;
172 menu_manager->RemoveAllContextItems(
173 MenuItem::ExtensionKey(extension()->id(), webview_instance_id));
174 SendResponse(true);
175 return true;
176 }
177
178 WebViewInternalClearDataFunction::WebViewInternalClearDataFunction()
179 : remove_mask_(0), bad_message_(false) {
180 }
181
182 WebViewInternalClearDataFunction::~WebViewInternalClearDataFunction() {
183 }
184
185 // Parses the |dataToRemove| argument to generate the remove mask. Sets
186 // |bad_message_| (like EXTENSION_FUNCTION_VALIDATE would if this were a bool
187 // method) if 'dataToRemove' is not present.
188 uint32 WebViewInternalClearDataFunction::GetRemovalMask() {
189 base::DictionaryValue* data_to_remove;
190 if (!args_->GetDictionary(2, &data_to_remove)) {
191 bad_message_ = true;
192 return 0;
193 }
194
195 uint32 remove_mask = 0;
196 for (base::DictionaryValue::Iterator i(*data_to_remove); !i.IsAtEnd();
197 i.Advance()) {
198 bool selected = false;
199 if (!i.value().GetAsBoolean(&selected)) {
200 bad_message_ = true;
201 return 0;
202 }
203 if (selected)
204 remove_mask |= MaskForKey(i.key().c_str());
205 }
206
207 return remove_mask;
208 }
209
210 // TODO(lazyboy): Parameters in this extension function are similar (or a
211 // sub-set) to BrowsingDataRemoverFunction. How can we share this code?
212 bool WebViewInternalClearDataFunction::RunAsyncSafe(WebViewGuest* guest) {
213 // Grab the initial |options| parameter, and parse out the arguments.
214 base::DictionaryValue* options;
215 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &options));
216 DCHECK(options);
217
218 // If |ms_since_epoch| isn't set, default it to 0.
219 double ms_since_epoch;
220 if (!options->GetDouble(extension_browsing_data_api_constants::kSinceKey,
221 &ms_since_epoch)) {
222 ms_since_epoch = 0;
223 }
224
225 // base::Time takes a double that represents seconds since epoch. JavaScript
226 // gives developers milliseconds, so do a quick conversion before populating
227 // the object. Also, Time::FromDoubleT converts double time 0 to empty Time
228 // object. So we need to do special handling here.
229 remove_since_ = (ms_since_epoch == 0)
230 ? base::Time::UnixEpoch()
231 : base::Time::FromDoubleT(ms_since_epoch / 1000.0);
232
233 remove_mask_ = GetRemovalMask();
234 if (bad_message_)
235 return false;
236
237 AddRef(); // Balanced below or in WebViewInternalClearDataFunction::Done().
238
239 bool scheduled = false;
240 if (remove_mask_) {
241 scheduled = guest->ClearData(
242 remove_since_,
243 remove_mask_,
244 base::Bind(&WebViewInternalClearDataFunction::ClearDataDone, this));
245 }
246 if (!remove_mask_ || !scheduled) {
247 SendResponse(false);
248 Release(); // Balanced above.
249 return false;
250 }
251
252 // Will finish asynchronously.
253 return true;
254 }
255
256 void WebViewInternalClearDataFunction::ClearDataDone() {
257 Release(); // Balanced in RunAsync().
258 SendResponse(true);
259 }
260
261 WebViewInternalExecuteCodeFunction::WebViewInternalExecuteCodeFunction()
262 : guest_instance_id_(0), guest_src_(GURL::EmptyGURL()) {
263 }
264
265 WebViewInternalExecuteCodeFunction::~WebViewInternalExecuteCodeFunction() {
266 }
267
268 bool WebViewInternalExecuteCodeFunction::Init() {
269 if (details_.get())
270 return true;
271
272 if (!args_->GetInteger(0, &guest_instance_id_))
273 return false;
274
275 if (!guest_instance_id_)
276 return false;
277
278 std::string src;
279 if (!args_->GetString(1, &src))
280 return false;
281
282 guest_src_ = GURL(src);
283 if (!guest_src_.is_valid())
284 return false;
285
286 base::DictionaryValue* details_value = NULL;
287 if (!args_->GetDictionary(2, &details_value))
288 return false;
289 scoped_ptr<InjectDetails> details(new InjectDetails());
290 if (!InjectDetails::Populate(*details_value, details.get()))
291 return false;
292
293 details_ = details.Pass();
294 return true;
295 }
296
297 bool WebViewInternalExecuteCodeFunction::ShouldInsertCSS() const {
298 return false;
299 }
300
301 bool WebViewInternalExecuteCodeFunction::CanExecuteScriptOnPage() {
302 return true;
303 }
304
305 extensions::ScriptExecutor*
306 WebViewInternalExecuteCodeFunction::GetScriptExecutor() {
307 WebViewGuest* guest = WebViewGuest::From(
308 render_view_host()->GetProcess()->GetID(), guest_instance_id_);
309 if (!guest)
310 return NULL;
311
312 return guest->script_executor();
313 }
314
315 bool WebViewInternalExecuteCodeFunction::IsWebView() const {
316 return true;
317 }
318
319 const GURL& WebViewInternalExecuteCodeFunction::GetWebViewSrc() const {
320 return guest_src_;
321 }
322
323 WebViewInternalExecuteScriptFunction::WebViewInternalExecuteScriptFunction() {
324 }
325
326 void WebViewInternalExecuteScriptFunction::OnExecuteCodeFinished(
327 const std::string& error,
328 const GURL& on_url,
329 const base::ListValue& result) {
330 if (error.empty())
331 SetResult(result.DeepCopy());
332 WebViewInternalExecuteCodeFunction::OnExecuteCodeFinished(
333 error, on_url, result);
334 }
335
336 WebViewInternalInsertCSSFunction::WebViewInternalInsertCSSFunction() {
337 }
338
339 bool WebViewInternalInsertCSSFunction::ShouldInsertCSS() const {
340 return true;
341 }
342
343 WebViewInternalCaptureVisibleRegionFunction::
344 WebViewInternalCaptureVisibleRegionFunction() {
345 }
346
347 WebViewInternalCaptureVisibleRegionFunction::
348 ~WebViewInternalCaptureVisibleRegionFunction() {
349 }
350
351 bool WebViewInternalCaptureVisibleRegionFunction::IsScreenshotEnabled() {
352 return true;
353 }
354
355 WebContents* WebViewInternalCaptureVisibleRegionFunction::GetWebContentsForID(
356 int instance_id) {
357 WebViewGuest* guest = WebViewGuest::From(
358 render_view_host()->GetProcess()->GetID(), instance_id);
359 return guest ? guest->web_contents() : NULL;
360 }
361
362 void WebViewInternalCaptureVisibleRegionFunction::OnCaptureFailure(
363 FailureReason reason) {
364 SendResponse(false);
365 }
366
367 WebViewInternalSetNameFunction::WebViewInternalSetNameFunction() {
368 }
369
370 WebViewInternalSetNameFunction::~WebViewInternalSetNameFunction() {
371 }
372
373 WebViewInternalSetZoomFunction::WebViewInternalSetZoomFunction() {
374 }
375
376 WebViewInternalSetZoomFunction::~WebViewInternalSetZoomFunction() {
377 }
378
379 bool WebViewInternalSetNameFunction::RunAsyncSafe(WebViewGuest* guest) {
380 scoped_ptr<webview::SetName::Params> params(
381 webview::SetName::Params::Create(*args_));
382 EXTENSION_FUNCTION_VALIDATE(params.get());
383 guest->SetName(params->frame_name);
384 SendResponse(true);
385 return true;
386 }
387
388 bool WebViewInternalSetZoomFunction::RunAsyncSafe(WebViewGuest* guest) {
389 scoped_ptr<webview::SetZoom::Params> params(
390 webview::SetZoom::Params::Create(*args_));
391 EXTENSION_FUNCTION_VALIDATE(params.get());
392 guest->SetZoom(params->zoom_factor);
393
394 SendResponse(true);
395 return true;
396 }
397
398 WebViewInternalGetZoomFunction::WebViewInternalGetZoomFunction() {
399 }
400
401 WebViewInternalGetZoomFunction::~WebViewInternalGetZoomFunction() {
402 }
403
404 bool WebViewInternalGetZoomFunction::RunAsyncSafe(WebViewGuest* guest) {
405 scoped_ptr<webview::GetZoom::Params> params(
406 webview::GetZoom::Params::Create(*args_));
407 EXTENSION_FUNCTION_VALIDATE(params.get());
408
409 double zoom_factor = guest->GetZoom();
410 SetResult(new base::FundamentalValue(zoom_factor));
411 SendResponse(true);
412 return true;
413 }
414
415 WebViewInternalFindFunction::WebViewInternalFindFunction() {
416 }
417
418 WebViewInternalFindFunction::~WebViewInternalFindFunction() {
419 }
420
421 bool WebViewInternalFindFunction::RunAsyncSafe(WebViewGuest* guest) {
422 scoped_ptr<webview::Find::Params> params(
423 webview::Find::Params::Create(*args_));
424 EXTENSION_FUNCTION_VALIDATE(params.get());
425
426 // Convert the std::string search_text to string16.
427 base::string16 search_text;
428 base::UTF8ToUTF16(
429 params->search_text.c_str(), params->search_text.length(), &search_text);
430
431 // Set the find options to their default values.
432 blink::WebFindOptions options;
433 if (params->options) {
434 options.forward =
435 params->options->backward ? !*params->options->backward : true;
436 options.matchCase =
437 params->options->match_case ? *params->options->match_case : false;
438 }
439
440 guest->Find(search_text, options, this);
441 return true;
442 }
443
444 WebViewInternalStopFindingFunction::WebViewInternalStopFindingFunction() {
445 }
446
447 WebViewInternalStopFindingFunction::~WebViewInternalStopFindingFunction() {
448 }
449
450 bool WebViewInternalStopFindingFunction::RunAsyncSafe(WebViewGuest* guest) {
451 scoped_ptr<webview::StopFinding::Params> params(
452 webview::StopFinding::Params::Create(*args_));
453 EXTENSION_FUNCTION_VALIDATE(params.get());
454
455 // Set the StopFindAction.
456 content::StopFindAction action;
457 switch (params->action) {
458 case webview::StopFinding::Params::ACTION_CLEAR:
459 action = content::STOP_FIND_ACTION_CLEAR_SELECTION;
460 break;
461 case webview::StopFinding::Params::ACTION_KEEP:
462 action = content::STOP_FIND_ACTION_KEEP_SELECTION;
463 break;
464 case webview::StopFinding::Params::ACTION_ACTIVATE:
465 action = content::STOP_FIND_ACTION_ACTIVATE_SELECTION;
466 break;
467 default:
468 action = content::STOP_FIND_ACTION_KEEP_SELECTION;
469 }
470
471 guest->StopFinding(action);
472 return true;
473 }
474
475 WebViewInternalGoFunction::WebViewInternalGoFunction() {
476 }
477
478 WebViewInternalGoFunction::~WebViewInternalGoFunction() {
479 }
480
481 bool WebViewInternalGoFunction::RunAsyncSafe(WebViewGuest* guest) {
482 scoped_ptr<webview::Go::Params> params(webview::Go::Params::Create(*args_));
483 EXTENSION_FUNCTION_VALIDATE(params.get());
484
485 guest->Go(params->relative_index);
486 return true;
487 }
488
489 WebViewInternalReloadFunction::WebViewInternalReloadFunction() {
490 }
491
492 WebViewInternalReloadFunction::~WebViewInternalReloadFunction() {
493 }
494
495 bool WebViewInternalReloadFunction::RunAsyncSafe(WebViewGuest* guest) {
496 guest->Reload();
497 return true;
498 }
499
500 WebViewInternalSetPermissionFunction::WebViewInternalSetPermissionFunction() {
501 }
502
503 WebViewInternalSetPermissionFunction::~WebViewInternalSetPermissionFunction() {
504 }
505
506 bool WebViewInternalSetPermissionFunction::RunAsyncSafe(WebViewGuest* guest) {
507 scoped_ptr<webview::SetPermission::Params> params(
508 webview::SetPermission::Params::Create(*args_));
509 EXTENSION_FUNCTION_VALIDATE(params.get());
510
511 WebViewPermissionHelper::PermissionResponseAction action =
512 WebViewPermissionHelper::DEFAULT;
513 switch (params->action) {
514 case Params::ACTION_ALLOW:
515 action = WebViewPermissionHelper::ALLOW;
516 break;
517 case Params::ACTION_DENY:
518 action = WebViewPermissionHelper::DENY;
519 break;
520 case Params::ACTION_DEFAULT:
521 break;
522 default:
523 NOTREACHED();
524 }
525
526 std::string user_input;
527 if (params->user_input)
528 user_input = *params->user_input;
529
530 WebViewPermissionHelper* web_view_permission_helper =
531 WebViewPermissionHelper::FromWebContents(guest->web_contents());
532
533 WebViewPermissionHelper::SetPermissionResult result =
534 web_view_permission_helper->SetPermission(
535 params->request_id, action, user_input);
536
537 EXTENSION_FUNCTION_VALIDATE(
538 result != WebViewPermissionHelper::SET_PERMISSION_INVALID);
539
540 SetResult(new base::FundamentalValue(
541 result == WebViewPermissionHelper::SET_PERMISSION_ALLOWED));
542 SendResponse(true);
543 return true;
544 }
545
546 WebViewInternalShowContextMenuFunction::
547 WebViewInternalShowContextMenuFunction() {
548 }
549
550 WebViewInternalShowContextMenuFunction::
551 ~WebViewInternalShowContextMenuFunction() {
552 }
553
554 bool WebViewInternalShowContextMenuFunction::RunAsyncSafe(WebViewGuest* guest) {
555 scoped_ptr<webview::ShowContextMenu::Params> params(
556 webview::ShowContextMenu::Params::Create(*args_));
557 EXTENSION_FUNCTION_VALIDATE(params.get());
558
559 // TODO(lazyboy): Actually implement filtering menu items, we pass NULL for
560 // now.
561 guest->ShowContextMenu(params->request_id, NULL);
562
563 SendResponse(true);
564 return true;
565 }
566
567 WebViewInternalOverrideUserAgentFunction::
568 WebViewInternalOverrideUserAgentFunction() {
569 }
570
571 WebViewInternalOverrideUserAgentFunction::
572 ~WebViewInternalOverrideUserAgentFunction() {
573 }
574
575 bool WebViewInternalOverrideUserAgentFunction::RunAsyncSafe(
576 WebViewGuest* guest) {
577 scoped_ptr<webview::OverrideUserAgent::Params> params(
578 webview::OverrideUserAgent::Params::Create(*args_));
579 EXTENSION_FUNCTION_VALIDATE(params.get());
580
581 guest->SetUserAgentOverride(params->user_agent_override);
582 return true;
583 }
584
585 WebViewInternalStopFunction::WebViewInternalStopFunction() {
586 }
587
588 WebViewInternalStopFunction::~WebViewInternalStopFunction() {
589 }
590
591 bool WebViewInternalStopFunction::RunAsyncSafe(WebViewGuest* guest) {
592 guest->Stop();
593 return true;
594 }
595
596 WebViewInternalTerminateFunction::WebViewInternalTerminateFunction() {
597 }
598
599 WebViewInternalTerminateFunction::~WebViewInternalTerminateFunction() {
600 }
601
602 bool WebViewInternalTerminateFunction::RunAsyncSafe(WebViewGuest* guest) {
603 guest->Terminate();
604 return true;
605 }
606
607 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698