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/context_menus/context_menus_api_helpers.h"
7 #include "base/strings/string_number_conversions.h"
10 namespace context_menus_api_helpers
{
12 const char kActionNotAllowedError
[] =
13 "Only extensions are allowed to use action contexts";
14 const char kCannotFindItemError
[] = "Cannot find menu item with id *";
15 const char kCheckedError
[] =
16 "Only items with type \"radio\" or \"checkbox\" can be checked";
17 const char kDuplicateIDError
[] =
18 "Cannot create item with duplicate id *";
19 const char kGeneratedIdKey
[] = "generatedId";
20 const char kLauncherNotAllowedError
[] =
21 "Only packaged apps are allowed to use 'launcher' context";
22 const char kOnclickDisallowedError
[] = "Extensions using event pages cannot "
23 "pass an onclick parameter to chrome.contextMenus.create. Instead, use "
24 "the chrome.contextMenus.onClicked event.";
25 const char kParentsMustBeNormalError
[] =
26 "Parent items must have type \"normal\"";
27 const char kTitleNeededError
[] =
28 "All menu items except for separators must have a title";
31 std::string
GetIDString(const MenuItem::Id
& id
) {
35 return base::IntToString(id
.uid
);
38 MenuItem
* GetParent(MenuItem::Id parent_id
,
39 const MenuManager
* menu_manager
,
41 MenuItem
* parent
= menu_manager
->GetItemById(parent_id
);
43 *error
= ErrorUtils::FormatErrorMessage(
44 kCannotFindItemError
, GetIDString(parent_id
));
47 if (parent
->type() != MenuItem::NORMAL
) {
48 *error
= kParentsMustBeNormalError
;
54 MenuItem::ContextList
GetContexts(const std::vector
<
55 extensions::api::context_menus::ContextType
>& in_contexts
) {
56 MenuItem::ContextList contexts
;
57 for (size_t i
= 0; i
< in_contexts
.size(); ++i
) {
58 switch (in_contexts
[i
]) {
59 case extensions::api::context_menus::CONTEXT_TYPE_ALL
:
60 contexts
.Add(extensions::MenuItem::ALL
);
62 case extensions::api::context_menus::CONTEXT_TYPE_PAGE
:
63 contexts
.Add(extensions::MenuItem::PAGE
);
65 case extensions::api::context_menus::CONTEXT_TYPE_SELECTION
:
66 contexts
.Add(extensions::MenuItem::SELECTION
);
68 case extensions::api::context_menus::CONTEXT_TYPE_LINK
:
69 contexts
.Add(extensions::MenuItem::LINK
);
71 case extensions::api::context_menus::CONTEXT_TYPE_EDITABLE
:
72 contexts
.Add(extensions::MenuItem::EDITABLE
);
74 case extensions::api::context_menus::CONTEXT_TYPE_IMAGE
:
75 contexts
.Add(extensions::MenuItem::IMAGE
);
77 case extensions::api::context_menus::CONTEXT_TYPE_VIDEO
:
78 contexts
.Add(extensions::MenuItem::VIDEO
);
80 case extensions::api::context_menus::CONTEXT_TYPE_AUDIO
:
81 contexts
.Add(extensions::MenuItem::AUDIO
);
83 case extensions::api::context_menus::CONTEXT_TYPE_FRAME
:
84 contexts
.Add(extensions::MenuItem::FRAME
);
86 case extensions::api::context_menus::CONTEXT_TYPE_LAUNCHER
:
87 // Not available for <webview>.
88 contexts
.Add(extensions::MenuItem::LAUNCHER
);
90 case extensions::api::context_menus::CONTEXT_TYPE_BROWSER_ACTION
:
91 // Not available for <webview>.
92 contexts
.Add(extensions::MenuItem::BROWSER_ACTION
);
94 case extensions::api::context_menus::CONTEXT_TYPE_PAGE_ACTION
:
95 // Not available for <webview>.
96 contexts
.Add(extensions::MenuItem::PAGE_ACTION
);
98 case extensions::api::context_menus::CONTEXT_TYPE_NONE
:
105 MenuItem::Type
GetType(extensions::api::context_menus::ItemType type
,
106 MenuItem::Type default_type
) {
108 case extensions::api::context_menus::ITEM_TYPE_NONE
:
110 case extensions::api::context_menus::ITEM_TYPE_NORMAL
:
111 return extensions::MenuItem::NORMAL
;
112 case extensions::api::context_menus::ITEM_TYPE_CHECKBOX
:
113 return extensions::MenuItem::CHECKBOX
;
114 case extensions::api::context_menus::ITEM_TYPE_RADIO
:
115 return extensions::MenuItem::RADIO
;
116 case extensions::api::context_menus::ITEM_TYPE_SEPARATOR
:
117 return extensions::MenuItem::SEPARATOR
;
119 return extensions::MenuItem::NORMAL
;
122 } // namespace context_menus_api_helpers
123 } // namespace extensions