15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/extensions/api/context_menus/context_menus_api_helpers.h"
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/strings/string_number_conversions.h"
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace extensions {
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace context_menus_api_helpers {
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
126e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)const char kActionNotAllowedError[] =
136e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    "Only extensions are allowed to use action contexts";
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)const char kCannotFindItemError[] = "Cannot find menu item with id *";
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)const char kCheckedError[] =
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    "Only items with type \"radio\" or \"checkbox\" can be checked";
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)const char kDuplicateIDError[] =
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    "Cannot create item with duplicate id *";
19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)const char kGeneratedIdKey[] = "generatedId";
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)const char kLauncherNotAllowedError[] =
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    "Only packaged apps are allowed to use 'launcher' context";
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)const char kOnclickDisallowedError[] = "Extensions using event pages cannot "
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    "pass an onclick parameter to chrome.contextMenus.create. Instead, use "
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    "the chrome.contextMenus.onClicked event.";
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)const char kParentsMustBeNormalError[] =
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    "Parent items must have type \"normal\"";
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)const char kTitleNeededError[] =
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    "All menu items except for separators must have a title";
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)std::string GetIDString(const MenuItem::Id& id) {
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (id.uid == 0)
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return id.string_uid;
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  else
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return base::IntToString(id.uid);
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)MenuItem* GetParent(MenuItem::Id parent_id,
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                    const MenuManager* menu_manager,
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                    std::string* error) {
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MenuItem* parent = menu_manager->GetItemById(parent_id);
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!parent) {
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    *error = ErrorUtils::FormatErrorMessage(
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        kCannotFindItemError, GetIDString(parent_id));
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return NULL;
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (parent->type() != MenuItem::NORMAL) {
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    *error = kParentsMustBeNormalError;
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return NULL;
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return parent;
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace context_menus_api_helpers
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace extensions
56