summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension_context_menu_api.cc
blob: ef004f56d3320c701a9e9a1f62fe2d8e560796f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/extension_context_menu_api.h"

#include "chrome/browser/extensions/extension_menu_manager.h"
#include "chrome/browser/extensions/extensions_service.h"
#include "chrome/browser/profile.h"

const wchar_t kEnabledContextsKey[] = L"enabledContexts";
const wchar_t kCheckedKey[] = L"checked";
const wchar_t kContextsKey[] = L"contexts";
const wchar_t kParentIdKey[] = L"parentId";
const wchar_t kTitleKey[] = L"title";
const wchar_t kTypeKey[] = L"type";

const char kTitleNeededError[] =
    "All menu items except for separators must have a title";
const char kCheckedError[] =
    "Only items with type RADIO or CHECKBOX can be checked";
const char kParentsMustBeNormalError[] =
    "Parent items must have type NORMAL";

bool ParseContexts(const DictionaryValue* properties,
                   const std::wstring& key,
                   ExtensionMenuItem::ContextList* result) {
  ListValue* list = NULL;
  if (!properties->GetList(key, &list)) {
    return true;
  }
  ExtensionMenuItem::ContextList tmp_result;

  std::string value;
  for (size_t i = 0; i < list->GetSize(); i++) {
    if (!list->GetString(i, &value))
      return false;

    if (value == "ALL")
      tmp_result.Add(ExtensionMenuItem::ALL);
    else if (value == "PAGE")
      tmp_result.Add(ExtensionMenuItem::PAGE);
    else if (value == "SELECTION")
      tmp_result.Add(ExtensionMenuItem::SELECTION);
    else if (value == "LINK")
      tmp_result.Add(ExtensionMenuItem::LINK);
    else if (value == "EDITABLE")
      tmp_result.Add(ExtensionMenuItem::EDITABLE);
    else if (value == "IMAGE")
      tmp_result.Add(ExtensionMenuItem::IMAGE);
    else if (value == "VIDEO")
      tmp_result.Add(ExtensionMenuItem::VIDEO);
    else if (value == "AUDIO")
      tmp_result.Add(ExtensionMenuItem::AUDIO);
    else
      return false;
  }
  *result = tmp_result;
  return true;
}

bool CreateContextMenuFunction::RunImpl() {
  EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY));
  const DictionaryValue* properties = args_as_dictionary();
  std::string title;
  if (properties->HasKey(kTitleKey))
    EXTENSION_FUNCTION_VALIDATE(properties->GetString(kTitleKey, &title));

  ExtensionMenuManager* menu_manager =
      profile()->GetExtensionsService()->menu_manager();

  ExtensionMenuItem::ContextList contexts(ExtensionMenuItem::PAGE);
  if (!ParseContexts(properties, kContextsKey, &contexts))
    EXTENSION_FUNCTION_ERROR("Invalid value for " + WideToASCII(kContextsKey));

  ExtensionMenuItem::ContextList enabled_contexts = contexts;
  if (!ParseContexts(properties, kEnabledContextsKey, &enabled_contexts))
    EXTENSION_FUNCTION_ERROR("Invalid value for " +
                             WideToASCII(kEnabledContextsKey));

  ExtensionMenuItem::Type type = ExtensionMenuItem::NORMAL;
  if (properties->HasKey(kTypeKey)) {
    std::string type_string;
    EXTENSION_FUNCTION_VALIDATE(properties->GetString(kTypeKey, &type_string));
    if (type_string == "CHECKBOX")
      type = ExtensionMenuItem::CHECKBOX;
    else if (type_string == "RADIO")
      type = ExtensionMenuItem::RADIO;
    else if (type_string == "SEPARATOR")
      type = ExtensionMenuItem::SEPARATOR;
    else if (type_string != "NORMAL")
      EXTENSION_FUNCTION_ERROR("Invalid type string '" + type_string + "'");
  }

  if (title.empty() && type != ExtensionMenuItem::SEPARATOR)
    EXTENSION_FUNCTION_ERROR(kTitleNeededError);

  bool checked = false;
  if (properties->HasKey(kCheckedKey)) {
    EXTENSION_FUNCTION_VALIDATE(properties->GetBoolean(kCheckedKey, &checked));
    if (checked && type != ExtensionMenuItem::CHECKBOX &&
        type != ExtensionMenuItem::RADIO)
      EXTENSION_FUNCTION_ERROR(kCheckedError);
  }

  scoped_ptr<ExtensionMenuItem> item(
      new ExtensionMenuItem(extension_id(), title, checked, type, contexts,
                            enabled_contexts));

  int id = 0;
  if (properties->HasKey(kParentIdKey)) {
    int parent_id = 0;
    EXTENSION_FUNCTION_VALIDATE(properties->GetInteger(kParentIdKey,
                                                       &parent_id));
    ExtensionMenuItem* parent = menu_manager->GetItemById(parent_id);
    if (!parent)
      EXTENSION_FUNCTION_ERROR("Cannot find menu item with id " +
                               IntToString(parent_id));
    if (parent->type() != ExtensionMenuItem::NORMAL)
      EXTENSION_FUNCTION_ERROR(kParentsMustBeNormalError);

    id = menu_manager->AddChildItem(parent_id, item.release());
  } else {
    id = menu_manager->AddContextItem(item.release());
  }
  EXTENSION_FUNCTION_VALIDATE(id > 0);

  if (has_callback())
    result_.reset(Value::CreateIntegerValue(id));

  return true;
}

bool RemoveContextMenuFunction::RunImpl() {
  EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_INTEGER));
  int id = 0;
  EXTENSION_FUNCTION_VALIDATE(args_->GetAsInteger(&id));
  ExtensionsService* service = profile()->GetExtensionsService();
  ExtensionMenuManager* manager = service->menu_manager();

  ExtensionMenuItem* item = manager->GetItemById(id);
  // Ensure one extension can't remove another's menu items.
  if (!item || item->extension_id() != extension_id())
    EXTENSION_FUNCTION_ERROR(
        StringPrintf("no menu item with id %d is registered", id));

  EXTENSION_FUNCTION_VALIDATE(manager->RemoveContextMenuItem(id));
  return true;
}