Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / context_menus / context_menus_api_helpers.cc
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.
4
5 #include "chrome/browser/extensions/api/context_menus/context_menus_api_helpers.h"
6
7 #include "base/strings/string_number_conversions.h"
8
9 namespace extensions {
10 namespace context_menus_api_helpers {
11
12 const char kCannotFindItemError[] = "Cannot find menu item with id *";
13 const char kCheckedError[] =
14     "Only items with type \"radio\" or \"checkbox\" can be checked";
15 const char kDuplicateIDError[] =
16     "Cannot create item with duplicate id *";
17 const char kLauncherNotAllowedError[] =
18     "Only packaged apps are allowed to use 'launcher' context";
19 const char kOnclickDisallowedError[] = "Extensions using event pages cannot "
20     "pass an onclick parameter to chrome.contextMenus.create. Instead, use "
21     "the chrome.contextMenus.onClicked event.";
22 const char kParentsMustBeNormalError[] =
23     "Parent items must have type \"normal\"";
24 const char kTitleNeededError[] =
25     "All menu items except for separators must have a title";
26
27
28 std::string GetIDString(const MenuItem::Id& id) {
29   if (id.uid == 0)
30     return id.string_uid;
31   else
32     return base::IntToString(id.uid);
33 }
34
35 MenuItem* GetParent(MenuItem::Id parent_id,
36                     const MenuManager* menu_manager,
37                     std::string* error) {
38   MenuItem* parent = menu_manager->GetItemById(parent_id);
39   if (!parent) {
40     *error = ErrorUtils::FormatErrorMessage(
41         kCannotFindItemError, GetIDString(parent_id));
42     return NULL;
43   }
44   if (parent->type() != MenuItem::NORMAL) {
45     *error = kParentsMustBeNormalError;
46     return NULL;
47   }
48   return parent;
49 }
50
51 }  // namespace context_menus_api_helpers
52 }  // namespace extensions