1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/api/web_view/chrome_web_view_internal_api.h"
#include "chrome/browser/extensions/api/context_menus/context_menus_api.h"
#include "chrome/browser/extensions/api/context_menus/context_menus_api_helpers.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/extensions/api/chrome_web_view_internal.h"
#include "extensions/common/error_utils.h"
namespace helpers = extensions::context_menus_api_helpers;
namespace webview = extensions::api::chrome_web_view_internal;
namespace extensions {
// TODO(lazyboy): Add checks similar to
// WebViewInternalExtensionFunction::RunAsyncSafe(WebViewGuest*).
bool ChromeWebViewInternalContextMenusCreateFunction::RunAsync() {
scoped_ptr<webview::ContextMenusCreate::Params> params(
webview::ContextMenusCreate::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get());
MenuItem::Id id(
Profile::FromBrowserContext(browser_context())->IsOffTheRecord(),
MenuItem::ExtensionKey(extension_id(), params->instance_id));
if (params->create_properties.id.get()) {
id.string_uid = *params->create_properties.id;
} else {
// The Generated Id is added by web_view_internal_custom_bindings.js.
base::DictionaryValue* properties = NULL;
EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &properties));
EXTENSION_FUNCTION_VALIDATE(
properties->GetInteger(helpers::kGeneratedIdKey, &id.uid));
}
bool success = extensions::context_menus_api_helpers::CreateMenuItem(
params->create_properties,
Profile::FromBrowserContext(browser_context()),
extension(),
id,
&error_);
SendResponse(success);
return success;
}
bool ChromeWebViewInternalContextMenusUpdateFunction::RunAsync() {
scoped_ptr<webview::ContextMenusUpdate::Params> params(
webview::ContextMenusUpdate::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get());
Profile* profile = Profile::FromBrowserContext(browser_context());
MenuItem::Id item_id(
profile->IsOffTheRecord(),
MenuItem::ExtensionKey(extension_id(), params->instance_id));
if (params->id.as_string)
item_id.string_uid = *params->id.as_string;
else if (params->id.as_integer)
item_id.uid = *params->id.as_integer;
else
NOTREACHED();
bool success = extensions::context_menus_api_helpers::UpdateMenuItem(
params->update_properties, profile, extension(), item_id, &error_);
SendResponse(success);
return success;
}
bool ChromeWebViewInternalContextMenusRemoveFunction::RunAsync() {
scoped_ptr<webview::ContextMenusRemove::Params> params(
webview::ContextMenusRemove::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get());
MenuManager* menu_manager =
MenuManager::Get(Profile::FromBrowserContext(browser_context()));
MenuItem::Id id(
Profile::FromBrowserContext(browser_context())->IsOffTheRecord(),
MenuItem::ExtensionKey(extension_id(), params->instance_id));
if (params->menu_item_id.as_string) {
id.string_uid = *params->menu_item_id.as_string;
} else if (params->menu_item_id.as_integer) {
id.uid = *params->menu_item_id.as_integer;
} else {
NOTREACHED();
}
bool success = true;
MenuItem* item = menu_manager->GetItemById(id);
// Ensure one <webview> can't remove another's menu items.
if (!item || item->id().extension_key != id.extension_key) {
error_ = ErrorUtils::FormatErrorMessage(
context_menus_api_helpers::kCannotFindItemError,
context_menus_api_helpers::GetIDString(id));
success = false;
} else if (!menu_manager->RemoveContextMenuItem(id)) {
success = false;
}
SendResponse(success);
return success;
}
bool ChromeWebViewInternalContextMenusRemoveAllFunction::RunAsync() {
scoped_ptr<webview::ContextMenusRemoveAll::Params> params(
webview::ContextMenusRemoveAll::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get());
MenuManager* menu_manager =
MenuManager::Get(Profile::FromBrowserContext(browser_context()));
int webview_instance_id = params->instance_id;
menu_manager->RemoveAllContextItems(
MenuItem::ExtensionKey(extension()->id(), webview_instance_id));
SendResponse(true);
return true;
}
ChromeWebViewInternalShowContextMenuFunction::
ChromeWebViewInternalShowContextMenuFunction() {
}
ChromeWebViewInternalShowContextMenuFunction::
~ChromeWebViewInternalShowContextMenuFunction() {
}
bool ChromeWebViewInternalShowContextMenuFunction::RunAsyncSafe(
WebViewGuest* guest) {
scoped_ptr<webview::ShowContextMenu::Params> params(
webview::ShowContextMenu::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get());
// TODO(lazyboy): Actually implement filtering menu items, we pass NULL for
// now.
guest->ShowContextMenu(params->request_id, NULL);
SendResponse(true);
return true;
}
} // namespace extensions
|