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/chrome_web_view_internal_api.h"
7 #include "chrome/browser/extensions/api/context_menus/context_menus_api.h"
8 #include "chrome/browser/extensions/api/context_menus/context_menus_api_helpers.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/common/extensions/api/chrome_web_view_internal.h"
11 #include "content/public/browser/render_process_host.h"
12 #include "extensions/common/error_utils.h"
14 namespace helpers
= extensions::context_menus_api_helpers
;
15 namespace webview
= extensions::api::chrome_web_view_internal
;
17 namespace extensions
{
19 // TODO(lazyboy): Add checks similar to
20 // WebViewInternalExtensionFunction::RunAsyncSafe(WebViewGuest*).
21 bool ChromeWebViewInternalContextMenusCreateFunction::RunAsync() {
22 scoped_ptr
<webview::ContextMenusCreate::Params
> params(
23 webview::ContextMenusCreate::Params::Create(*args_
));
24 EXTENSION_FUNCTION_VALIDATE(params
.get());
27 Profile::FromBrowserContext(browser_context())->IsOffTheRecord(),
28 MenuItem::ExtensionKey(
30 GetSenderWebContents()->GetRenderProcessHost()->GetID(),
31 params
->instance_id
));
33 if (params
->create_properties
.id
.get()) {
34 id
.string_uid
= *params
->create_properties
.id
;
36 // The Generated Id is added by web_view_internal_custom_bindings.js.
37 base::DictionaryValue
* properties
= NULL
;
38 EXTENSION_FUNCTION_VALIDATE(args_
->GetDictionary(1, &properties
));
39 EXTENSION_FUNCTION_VALIDATE(
40 properties
->GetInteger(helpers::kGeneratedIdKey
, &id
.uid
));
43 bool success
= extensions::context_menus_api_helpers::CreateMenuItem(
44 params
->create_properties
,
45 Profile::FromBrowserContext(browser_context()),
50 SendResponse(success
);
54 bool ChromeWebViewInternalContextMenusUpdateFunction::RunAsync() {
55 scoped_ptr
<webview::ContextMenusUpdate::Params
> params(
56 webview::ContextMenusUpdate::Params::Create(*args_
));
57 EXTENSION_FUNCTION_VALIDATE(params
.get());
59 Profile
* profile
= Profile::FromBrowserContext(browser_context());
61 profile
->IsOffTheRecord(),
62 MenuItem::ExtensionKey(
64 GetSenderWebContents()->GetRenderProcessHost()->GetID(),
65 params
->instance_id
));
67 if (params
->id
.as_string
)
68 item_id
.string_uid
= *params
->id
.as_string
;
69 else if (params
->id
.as_integer
)
70 item_id
.uid
= *params
->id
.as_integer
;
74 bool success
= extensions::context_menus_api_helpers::UpdateMenuItem(
75 params
->update_properties
, profile
, extension(), item_id
, &error_
);
76 SendResponse(success
);
80 bool ChromeWebViewInternalContextMenusRemoveFunction::RunAsync() {
81 scoped_ptr
<webview::ContextMenusRemove::Params
> params(
82 webview::ContextMenusRemove::Params::Create(*args_
));
83 EXTENSION_FUNCTION_VALIDATE(params
.get());
85 MenuManager
* menu_manager
=
86 MenuManager::Get(Profile::FromBrowserContext(browser_context()));
89 Profile::FromBrowserContext(browser_context())->IsOffTheRecord(),
90 MenuItem::ExtensionKey(
92 GetSenderWebContents()->GetRenderProcessHost()->GetID(),
93 params
->instance_id
));
95 if (params
->menu_item_id
.as_string
) {
96 id
.string_uid
= *params
->menu_item_id
.as_string
;
97 } else if (params
->menu_item_id
.as_integer
) {
98 id
.uid
= *params
->menu_item_id
.as_integer
;
104 MenuItem
* item
= menu_manager
->GetItemById(id
);
105 // Ensure one <webview> can't remove another's menu items.
106 if (!item
|| item
->id().extension_key
!= id
.extension_key
) {
107 error_
= ErrorUtils::FormatErrorMessage(
108 context_menus_api_helpers::kCannotFindItemError
,
109 context_menus_api_helpers::GetIDString(id
));
111 } else if (!menu_manager
->RemoveContextMenuItem(id
)) {
115 SendResponse(success
);
119 bool ChromeWebViewInternalContextMenusRemoveAllFunction::RunAsync() {
120 scoped_ptr
<webview::ContextMenusRemoveAll::Params
> params(
121 webview::ContextMenusRemoveAll::Params::Create(*args_
));
122 EXTENSION_FUNCTION_VALIDATE(params
.get());
124 MenuManager
* menu_manager
=
125 MenuManager::Get(Profile::FromBrowserContext(browser_context()));
126 menu_manager
->RemoveAllContextItems(MenuItem::ExtensionKey(
128 GetSenderWebContents()->GetRenderProcessHost()->GetID(),
129 params
->instance_id
));
135 ChromeWebViewInternalShowContextMenuFunction::
136 ChromeWebViewInternalShowContextMenuFunction() {
139 ChromeWebViewInternalShowContextMenuFunction::
140 ~ChromeWebViewInternalShowContextMenuFunction() {
143 bool ChromeWebViewInternalShowContextMenuFunction::RunAsyncSafe(
144 WebViewGuest
* guest
) {
145 scoped_ptr
<webview::ShowContextMenu::Params
> params(
146 webview::ShowContextMenu::Params::Create(*args_
));
147 EXTENSION_FUNCTION_VALIDATE(params
.get());
149 // TODO(lazyboy): Actually implement filtering menu items, we pass NULL for
151 guest
->ShowContextMenu(params
->request_id
, NULL
);
157 } // namespace extensions