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.
5 #include "chrome/browser/extensions/api/web_view/web_view_internal_api.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/guest_view/web_view/web_view_permission_helper.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/common/extensions/api/web_view_internal.h"
15 #include "content/public/browser/render_process_host.h"
16 #include "content/public/browser/render_view_host.h"
17 #include "content/public/browser/storage_partition.h"
18 #include "content/public/browser/web_contents.h"
19 #include "content/public/common/stop_find_action.h"
20 #include "extensions/common/error_utils.h"
21 #include "third_party/WebKit/public/web/WebFindOptions.h"
23 using content::WebContents;
24 using extensions::api::tabs::InjectDetails;
25 using extensions::api::web_view_internal::SetPermission::Params;
26 namespace helpers = extensions::context_menus_api_helpers;
27 namespace webview = extensions::api::web_view_internal;
29 namespace extensions {
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;
50 bool WebViewInternalExtensionFunction::RunAsync() {
52 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &instance_id));
53 WebViewGuest* guest = WebViewGuest::From(
54 render_view_host()->GetProcess()->GetID(), instance_id);
58 return RunAsyncSafe(guest);
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());
69 Profile::FromBrowserContext(browser_context())->IsOffTheRecord(),
70 MenuItem::ExtensionKey(extension_id(), params->instance_id));
72 if (params->create_properties.id.get()) {
73 id.string_uid = *params->create_properties.id;
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));
82 bool success = extensions::context_menus_api_helpers::CreateMenuItem(
83 params->create_properties,
84 Profile::FromBrowserContext(browser_context()),
89 SendResponse(success);
93 bool WebViewInternalNavigateFunction::RunAsyncSafe(WebViewGuest* guest) {
94 scoped_ptr<webview::Navigate::Params> params(
95 webview::Navigate::Params::Create(*args_));
96 EXTENSION_FUNCTION_VALIDATE(params.get());
97 std::string src = params->src;
98 guest->NavigateGuest(src);
102 bool WebViewInternalContextMenusUpdateFunction::RunAsync() {
103 scoped_ptr<webview::ContextMenusUpdate::Params> params(
104 webview::ContextMenusUpdate::Params::Create(*args_));
105 EXTENSION_FUNCTION_VALIDATE(params.get());
107 Profile* profile = Profile::FromBrowserContext(browser_context());
108 MenuItem::Id item_id(
109 profile->IsOffTheRecord(),
110 MenuItem::ExtensionKey(extension_id(), params->instance_id));
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;
119 bool success = extensions::context_menus_api_helpers::UpdateMenuItem(
120 params->update_properties, profile, extension(), item_id, &error_);
121 SendResponse(success);
125 bool WebViewInternalContextMenusRemoveFunction::RunAsync() {
126 scoped_ptr<webview::ContextMenusRemove::Params> params(
127 webview::ContextMenusRemove::Params::Create(*args_));
128 EXTENSION_FUNCTION_VALIDATE(params.get());
130 MenuManager* menu_manager =
131 MenuManager::Get(Profile::FromBrowserContext(browser_context()));
134 Profile::FromBrowserContext(browser_context())->IsOffTheRecord(),
135 MenuItem::ExtensionKey(extension_id(), params->instance_id));
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;
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));
153 } else if (!menu_manager->RemoveContextMenuItem(id)) {
157 SendResponse(success);
161 bool WebViewInternalContextMenusRemoveAllFunction::RunAsync() {
162 scoped_ptr<webview::ContextMenusRemoveAll::Params> params(
163 webview::ContextMenusRemoveAll::Params::Create(*args_));
164 EXTENSION_FUNCTION_VALIDATE(params.get());
166 MenuManager* menu_manager =
167 MenuManager::Get(Profile::FromBrowserContext(browser_context()));
169 int webview_instance_id = params->instance_id;
170 menu_manager->RemoveAllContextItems(
171 MenuItem::ExtensionKey(extension()->id(), webview_instance_id));
176 WebViewInternalClearDataFunction::WebViewInternalClearDataFunction()
177 : remove_mask_(0), bad_message_(false) {
180 WebViewInternalClearDataFunction::~WebViewInternalClearDataFunction() {
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)) {
193 uint32 remove_mask = 0;
194 for (base::DictionaryValue::Iterator i(*data_to_remove); !i.IsAtEnd();
196 bool selected = false;
197 if (!i.value().GetAsBoolean(&selected)) {
202 remove_mask |= MaskForKey(i.key().c_str());
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));
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,
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);
231 remove_mask_ = GetRemovalMask();
235 AddRef(); // Balanced below or in WebViewInternalClearDataFunction::Done().
237 bool scheduled = false;
239 scheduled = guest->ClearData(
242 base::Bind(&WebViewInternalClearDataFunction::ClearDataDone, this));
244 if (!remove_mask_ || !scheduled) {
246 Release(); // Balanced above.
250 // Will finish asynchronously.
254 void WebViewInternalClearDataFunction::ClearDataDone() {
255 Release(); // Balanced in RunAsync().
259 WebViewInternalExecuteCodeFunction::WebViewInternalExecuteCodeFunction()
260 : guest_instance_id_(0), guest_src_(GURL::EmptyGURL()) {
263 WebViewInternalExecuteCodeFunction::~WebViewInternalExecuteCodeFunction() {
266 bool WebViewInternalExecuteCodeFunction::Init() {
270 if (!args_->GetInteger(0, &guest_instance_id_))
273 if (!guest_instance_id_)
277 if (!args_->GetString(1, &src))
280 guest_src_ = GURL(src);
281 if (!guest_src_.is_valid())
284 base::DictionaryValue* details_value = NULL;
285 if (!args_->GetDictionary(2, &details_value))
287 scoped_ptr<InjectDetails> details(new InjectDetails());
288 if (!InjectDetails::Populate(*details_value, details.get()))
291 details_ = details.Pass();
295 bool WebViewInternalExecuteCodeFunction::ShouldInsertCSS() const {
299 bool WebViewInternalExecuteCodeFunction::CanExecuteScriptOnPage() {
303 extensions::ScriptExecutor*
304 WebViewInternalExecuteCodeFunction::GetScriptExecutor() {
305 WebViewGuest* guest = WebViewGuest::From(
306 render_view_host()->GetProcess()->GetID(), guest_instance_id_);
310 return guest->script_executor();
313 bool WebViewInternalExecuteCodeFunction::IsWebView() const {
317 const GURL& WebViewInternalExecuteCodeFunction::GetWebViewSrc() const {
321 WebViewInternalExecuteScriptFunction::WebViewInternalExecuteScriptFunction() {
324 void WebViewInternalExecuteScriptFunction::OnExecuteCodeFinished(
325 const std::string& error,
327 const base::ListValue& result) {
329 SetResult(result.DeepCopy());
330 WebViewInternalExecuteCodeFunction::OnExecuteCodeFinished(
331 error, on_url, result);
334 WebViewInternalInsertCSSFunction::WebViewInternalInsertCSSFunction() {
337 bool WebViewInternalInsertCSSFunction::ShouldInsertCSS() const {
341 WebViewInternalCaptureVisibleRegionFunction::
342 WebViewInternalCaptureVisibleRegionFunction() {
345 WebViewInternalCaptureVisibleRegionFunction::
346 ~WebViewInternalCaptureVisibleRegionFunction() {
349 bool WebViewInternalCaptureVisibleRegionFunction::IsScreenshotEnabled() {
353 WebContents* WebViewInternalCaptureVisibleRegionFunction::GetWebContentsForID(
355 WebViewGuest* guest = WebViewGuest::From(
356 render_view_host()->GetProcess()->GetID(), instance_id);
357 return guest ? guest->guest_web_contents() : NULL;
360 void WebViewInternalCaptureVisibleRegionFunction::OnCaptureFailure(
361 FailureReason reason) {
365 WebViewInternalSetNameFunction::WebViewInternalSetNameFunction() {
368 WebViewInternalSetNameFunction::~WebViewInternalSetNameFunction() {
371 WebViewInternalSetZoomFunction::WebViewInternalSetZoomFunction() {
374 WebViewInternalSetZoomFunction::~WebViewInternalSetZoomFunction() {
377 bool WebViewInternalSetNameFunction::RunAsyncSafe(WebViewGuest* guest) {
378 scoped_ptr<webview::SetName::Params> params(
379 webview::SetName::Params::Create(*args_));
380 EXTENSION_FUNCTION_VALIDATE(params.get());
381 guest->SetName(params->frame_name);
386 bool WebViewInternalSetZoomFunction::RunAsyncSafe(WebViewGuest* guest) {
387 scoped_ptr<webview::SetZoom::Params> params(
388 webview::SetZoom::Params::Create(*args_));
389 EXTENSION_FUNCTION_VALIDATE(params.get());
390 guest->SetZoom(params->zoom_factor);
396 WebViewInternalGetZoomFunction::WebViewInternalGetZoomFunction() {
399 WebViewInternalGetZoomFunction::~WebViewInternalGetZoomFunction() {
402 bool WebViewInternalGetZoomFunction::RunAsyncSafe(WebViewGuest* guest) {
403 scoped_ptr<webview::GetZoom::Params> params(
404 webview::GetZoom::Params::Create(*args_));
405 EXTENSION_FUNCTION_VALIDATE(params.get());
407 double zoom_factor = guest->GetZoom();
408 SetResult(new base::FundamentalValue(zoom_factor));
413 WebViewInternalFindFunction::WebViewInternalFindFunction() {
416 WebViewInternalFindFunction::~WebViewInternalFindFunction() {
419 bool WebViewInternalFindFunction::RunAsyncSafe(WebViewGuest* guest) {
420 scoped_ptr<webview::Find::Params> params(
421 webview::Find::Params::Create(*args_));
422 EXTENSION_FUNCTION_VALIDATE(params.get());
424 // Convert the std::string search_text to string16.
425 base::string16 search_text;
427 params->search_text.c_str(), params->search_text.length(), &search_text);
429 // Set the find options to their default values.
430 blink::WebFindOptions options;
431 if (params->options) {
433 params->options->backward ? !*params->options->backward : true;
435 params->options->match_case ? *params->options->match_case : false;
438 guest->Find(search_text, options, this);
442 WebViewInternalStopFindingFunction::WebViewInternalStopFindingFunction() {
445 WebViewInternalStopFindingFunction::~WebViewInternalStopFindingFunction() {
448 bool WebViewInternalStopFindingFunction::RunAsyncSafe(WebViewGuest* guest) {
449 scoped_ptr<webview::StopFinding::Params> params(
450 webview::StopFinding::Params::Create(*args_));
451 EXTENSION_FUNCTION_VALIDATE(params.get());
453 // Set the StopFindAction.
454 content::StopFindAction action;
455 switch (params->action) {
456 case webview::StopFinding::Params::ACTION_CLEAR:
457 action = content::STOP_FIND_ACTION_CLEAR_SELECTION;
459 case webview::StopFinding::Params::ACTION_KEEP:
460 action = content::STOP_FIND_ACTION_KEEP_SELECTION;
462 case webview::StopFinding::Params::ACTION_ACTIVATE:
463 action = content::STOP_FIND_ACTION_ACTIVATE_SELECTION;
466 action = content::STOP_FIND_ACTION_KEEP_SELECTION;
469 guest->StopFinding(action);
473 WebViewInternalGoFunction::WebViewInternalGoFunction() {
476 WebViewInternalGoFunction::~WebViewInternalGoFunction() {
479 bool WebViewInternalGoFunction::RunAsyncSafe(WebViewGuest* guest) {
480 scoped_ptr<webview::Go::Params> params(webview::Go::Params::Create(*args_));
481 EXTENSION_FUNCTION_VALIDATE(params.get());
483 guest->Go(params->relative_index);
487 WebViewInternalReloadFunction::WebViewInternalReloadFunction() {
490 WebViewInternalReloadFunction::~WebViewInternalReloadFunction() {
493 bool WebViewInternalReloadFunction::RunAsyncSafe(WebViewGuest* guest) {
498 WebViewInternalSetPermissionFunction::WebViewInternalSetPermissionFunction() {
501 WebViewInternalSetPermissionFunction::~WebViewInternalSetPermissionFunction() {
504 bool WebViewInternalSetPermissionFunction::RunAsyncSafe(WebViewGuest* guest) {
505 scoped_ptr<webview::SetPermission::Params> params(
506 webview::SetPermission::Params::Create(*args_));
507 EXTENSION_FUNCTION_VALIDATE(params.get());
509 WebViewPermissionHelper::PermissionResponseAction action =
510 WebViewPermissionHelper::DEFAULT;
511 switch (params->action) {
512 case Params::ACTION_ALLOW:
513 action = WebViewPermissionHelper::ALLOW;
515 case Params::ACTION_DENY:
516 action = WebViewPermissionHelper::DENY;
518 case Params::ACTION_DEFAULT:
524 std::string user_input;
525 if (params->user_input)
526 user_input = *params->user_input;
528 WebViewPermissionHelper* web_view_permission_helper =
529 WebViewPermissionHelper:: FromWebContents(guest->guest_web_contents());
531 WebViewPermissionHelper::SetPermissionResult result =
532 web_view_permission_helper->SetPermission(
533 params->request_id, action, user_input);
535 EXTENSION_FUNCTION_VALIDATE(
536 result != WebViewPermissionHelper::SET_PERMISSION_INVALID);
538 SetResult(new base::FundamentalValue(
539 result == WebViewPermissionHelper::SET_PERMISSION_ALLOWED));
544 WebViewInternalShowContextMenuFunction::
545 WebViewInternalShowContextMenuFunction() {
548 WebViewInternalShowContextMenuFunction::
549 ~WebViewInternalShowContextMenuFunction() {
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());
557 // TODO(lazyboy): Actually implement filtering menu items, we pass NULL for
559 guest->ShowContextMenu(params->request_id, NULL);
565 WebViewInternalOverrideUserAgentFunction::
566 WebViewInternalOverrideUserAgentFunction() {
569 WebViewInternalOverrideUserAgentFunction::
570 ~WebViewInternalOverrideUserAgentFunction() {
573 bool WebViewInternalOverrideUserAgentFunction::RunAsyncSafe(
574 WebViewGuest* guest) {
575 scoped_ptr<webview::OverrideUserAgent::Params> params(
576 webview::OverrideUserAgent::Params::Create(*args_));
577 EXTENSION_FUNCTION_VALIDATE(params.get());
579 guest->SetUserAgentOverride(params->user_agent_override);
583 WebViewInternalStopFunction::WebViewInternalStopFunction() {
586 WebViewInternalStopFunction::~WebViewInternalStopFunction() {
589 bool WebViewInternalStopFunction::RunAsyncSafe(WebViewGuest* guest) {
594 WebViewInternalTerminateFunction::WebViewInternalTerminateFunction() {
597 WebViewInternalTerminateFunction::~WebViewInternalTerminateFunction() {
600 bool WebViewInternalTerminateFunction::RunAsyncSafe(WebViewGuest* guest) {
605 } // namespace extensions