Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / extensions / api / context_menus / context_menus_api_helpers.cc
blob0a4b4ddc9ee3e01cd818adb4b098141488cc8da2
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"
9 namespace extensions {
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) {
32 if (id.uid == 0)
33 return id.string_uid;
34 else
35 return base::IntToString(id.uid);
38 MenuItem* GetParent(MenuItem::Id parent_id,
39 const MenuManager* menu_manager,
40 std::string* error) {
41 MenuItem* parent = menu_manager->GetItemById(parent_id);
42 if (!parent) {
43 *error = ErrorUtils::FormatErrorMessage(
44 kCannotFindItemError, GetIDString(parent_id));
45 return NULL;
47 if (parent->type() != MenuItem::NORMAL) {
48 *error = kParentsMustBeNormalError;
49 return NULL;
51 return parent;
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);
61 break;
62 case extensions::api::context_menus::CONTEXT_TYPE_PAGE:
63 contexts.Add(extensions::MenuItem::PAGE);
64 break;
65 case extensions::api::context_menus::CONTEXT_TYPE_SELECTION:
66 contexts.Add(extensions::MenuItem::SELECTION);
67 break;
68 case extensions::api::context_menus::CONTEXT_TYPE_LINK:
69 contexts.Add(extensions::MenuItem::LINK);
70 break;
71 case extensions::api::context_menus::CONTEXT_TYPE_EDITABLE:
72 contexts.Add(extensions::MenuItem::EDITABLE);
73 break;
74 case extensions::api::context_menus::CONTEXT_TYPE_IMAGE:
75 contexts.Add(extensions::MenuItem::IMAGE);
76 break;
77 case extensions::api::context_menus::CONTEXT_TYPE_VIDEO:
78 contexts.Add(extensions::MenuItem::VIDEO);
79 break;
80 case extensions::api::context_menus::CONTEXT_TYPE_AUDIO:
81 contexts.Add(extensions::MenuItem::AUDIO);
82 break;
83 case extensions::api::context_menus::CONTEXT_TYPE_FRAME:
84 contexts.Add(extensions::MenuItem::FRAME);
85 break;
86 case extensions::api::context_menus::CONTEXT_TYPE_LAUNCHER:
87 // Not available for <webview>.
88 contexts.Add(extensions::MenuItem::LAUNCHER);
89 break;
90 case extensions::api::context_menus::CONTEXT_TYPE_BROWSER_ACTION:
91 // Not available for <webview>.
92 contexts.Add(extensions::MenuItem::BROWSER_ACTION);
93 break;
94 case extensions::api::context_menus::CONTEXT_TYPE_PAGE_ACTION:
95 // Not available for <webview>.
96 contexts.Add(extensions::MenuItem::PAGE_ACTION);
97 break;
98 case extensions::api::context_menus::CONTEXT_TYPE_NONE:
99 NOTREACHED();
102 return contexts;
105 MenuItem::Type GetType(extensions::api::context_menus::ItemType type,
106 MenuItem::Type default_type) {
107 switch (type) {
108 case extensions::api::context_menus::ITEM_TYPE_NONE:
109 return default_type;
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