summaryrefslogtreecommitdiffstats
path: root/chromium/chrome/browser/extensions/api/context_menus/context_menus_api_helpers.h
blob: 28335e39113e05b1d6548b96b35f09ce91d69e3e (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Copyright 2014 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.

// Definition of helper functions for the ContextMenus API.

#ifndef CHROME_BROWSER_EXTENSIONS_API_CONTEXT_MENUS_CONTEXT_MENUS_API_HELPERS_H_
#define CHROME_BROWSER_EXTENSIONS_API_CONTEXT_MENUS_CONTEXT_MENUS_API_HELPERS_H_

#include "chrome/browser/extensions/menu_manager.h"
#include "chrome/common/extensions/api/context_menus.h"
#include "content/public/browser/browser_context.h"
#include "extensions/common/error_utils.h"
#include "extensions/common/manifest_handlers/background_info.h"

namespace extensions {
namespace context_menus_api_helpers {

namespace {

template <typename PropertyWithEnumT>
std::unique_ptr<extensions::MenuItem::Id> GetParentId(
    const PropertyWithEnumT& property,
    bool is_off_the_record,
    const MenuItem::ExtensionKey& key) {
  if (!property.parent_id)
    return std::unique_ptr<extensions::MenuItem::Id>();

  std::unique_ptr<extensions::MenuItem::Id> parent_id(
      new extensions::MenuItem::Id(is_off_the_record, key));
  if (property.parent_id->as_integer)
    parent_id->uid = *property.parent_id->as_integer;
  else if (property.parent_id->as_string)
    parent_id->string_uid = *property.parent_id->as_string;
  else
    NOTREACHED();
  return parent_id;
}

}  // namespace

extern const char kActionNotAllowedError[];
extern const char kCannotFindItemError[];
extern const char kCheckedError[];
extern const char kDuplicateIDError[];
extern const char kGeneratedIdKey[];
extern const char kLauncherNotAllowedError[];
extern const char kOnclickDisallowedError[];
extern const char kParentsMustBeNormalError[];
extern const char kTitleNeededError[];

std::string GetIDString(const MenuItem::Id& id);

MenuItem* GetParent(MenuItem::Id parent_id,
                    const MenuManager* menu_manager,
                    std::string* error);

MenuItem::ContextList GetContexts(const std::vector<
    extensions::api::context_menus::ContextType>& in_contexts);

MenuItem::Type GetType(extensions::api::context_menus::ItemType type,
                       MenuItem::Type default_type);

bool HasLazyContext(const Extension* extension);

// Creates and adds a menu item from |create_properties|.
template <typename PropertyWithEnumT>
bool CreateMenuItem(const PropertyWithEnumT& create_properties,
                    content::BrowserContext* browser_context,
                    const Extension* extension,
                    const MenuItem::Id& item_id,
                    std::string* error) {
  bool is_webview = item_id.extension_key.webview_instance_id != 0;
  MenuManager* menu_manager = MenuManager::Get(browser_context);

  if (menu_manager->GetItemById(item_id)) {
    *error = ErrorUtils::FormatErrorMessage(kDuplicateIDError,
                                            GetIDString(item_id));
    return false;
  }

  if (!is_webview && HasLazyContext(extension) &&
      create_properties.onclick.get()) {
    *error = kOnclickDisallowedError;
    return false;
  }

  // Contexts.
  MenuItem::ContextList contexts;
  if (create_properties.contexts.get())
    contexts = GetContexts(*create_properties.contexts);
  else
    contexts.Add(MenuItem::PAGE);

  if (contexts.Contains(MenuItem::LAUNCHER)) {
    // Launcher item is not allowed for <webview>.
    if (!extension->is_platform_app() || is_webview) {
      *error = kLauncherNotAllowedError;
      return false;
    }
  }

  if (contexts.Contains(MenuItem::BROWSER_ACTION) ||
      contexts.Contains(MenuItem::PAGE_ACTION)) {
    // Action items are not allowed for <webview>.
    if (!extension->is_extension() || is_webview) {
      *error = kActionNotAllowedError;
      return false;
    }
  }

  // Title.
  std::string title;
  if (create_properties.title.get())
    title = *create_properties.title;

  MenuItem::Type type = GetType(create_properties.type, MenuItem::NORMAL);
  if (title.empty() && type != MenuItem::SEPARATOR) {
    *error = kTitleNeededError;
    return false;
  }

  // Visibility state.
  bool visible = true;
  if (create_properties.visible)
    visible = *create_properties.visible;

  // Checked state.
  bool checked = false;
  if (create_properties.checked.get())
    checked = *create_properties.checked;

  // Enabled.
  bool enabled = true;
  if (create_properties.enabled.get())
    enabled = *create_properties.enabled;

  std::unique_ptr<MenuItem> item(
      new MenuItem(item_id, title, checked, visible, enabled, type, contexts));

  // URL Patterns.
  if (!item->PopulateURLPatterns(
          create_properties.document_url_patterns.get(),
          create_properties.target_url_patterns.get(),
          error)) {
    return false;
  }

  // Parent id.
  bool success = true;
  std::unique_ptr<MenuItem::Id> parent_id(
      GetParentId(create_properties, browser_context->IsOffTheRecord(),
                  item_id.extension_key));
  if (parent_id.get()) {
    MenuItem* parent = GetParent(*parent_id, menu_manager, error);
    if (!parent)
      return false;
    success = menu_manager->AddChildItem(parent->id(), std::move(item));
  } else {
    success = menu_manager->AddContextItem(extension, std::move(item));
  }

  if (!success)
    return false;

  menu_manager->WriteToStorage(extension, item_id.extension_key);
  return true;
}

// Updates a menu item from |update_properties|.
template <typename PropertyWithEnumT>
bool UpdateMenuItem(const PropertyWithEnumT& update_properties,
                    content::BrowserContext* browser_context,
                    const Extension* extension,
                    const MenuItem::Id& item_id,
                    std::string* error) {
  bool radio_item_updated = false;
  bool is_webview = item_id.extension_key.webview_instance_id != 0;
  MenuManager* menu_manager = MenuManager::Get(browser_context);

  MenuItem* item = menu_manager->GetItemById(item_id);
  if (!item || item->extension_id() != extension->id()){
    *error = ErrorUtils::FormatErrorMessage(
        kCannotFindItemError, GetIDString(item_id));
    return false;
  }

  // Type.
  MenuItem::Type type = GetType(update_properties.type, item->type());

  if (type != item->type()) {
    if (type == MenuItem::RADIO || item->type() == MenuItem::RADIO)
      radio_item_updated = true;
    item->set_type(type);
  }

  // Title.
  if (update_properties.title.get()) {
    std::string title(*update_properties.title);
    if (title.empty() && item->type() != MenuItem::SEPARATOR) {
      *error = kTitleNeededError;
      return false;
    }
    item->set_title(title);
  }

  // Checked state.
  if (update_properties.checked.get()) {
    bool checked = *update_properties.checked;
    if (checked &&
        item->type() != MenuItem::CHECKBOX &&
        item->type() != MenuItem::RADIO) {
      *error = kCheckedError;
      return false;
    }

    const bool should_toggle_checked =
        // If radio item was unchecked nothing should happen. The radio item
        // should remain checked because there should always be one item checked
        // in the radio list.
        (item->type() == MenuItem::RADIO && checked) ||
        // Checkboxes are always updated.
        item->type() == MenuItem::CHECKBOX;

    if (should_toggle_checked) {
      if (!item->SetChecked(checked)) {
        *error = kCheckedError;
        return false;
      }
      radio_item_updated = true;
    }
  }

  // Visibility state.
  if (update_properties.visible)
    item->set_visible(*update_properties.visible);

  // Enabled.
  if (update_properties.enabled.get())
    item->set_enabled(*update_properties.enabled);

  // Contexts.
  MenuItem::ContextList contexts;
  if (update_properties.contexts.get()) {
    contexts = GetContexts(*update_properties.contexts);

    if (contexts.Contains(MenuItem::LAUNCHER)) {
      // Launcher item is not allowed for <webview>.
      if (!extension->is_platform_app() || is_webview) {
        *error = kLauncherNotAllowedError;
        return false;
      }
    }

    if (contexts != item->contexts())
      item->set_contexts(contexts);
  }

  // Parent id.
  MenuItem* parent = NULL;
  std::unique_ptr<MenuItem::Id> parent_id(
      GetParentId(update_properties, browser_context->IsOffTheRecord(),
                  item_id.extension_key));
  if (parent_id.get()) {
    MenuItem* parent = GetParent(*parent_id, menu_manager, error);
    if (!parent || !menu_manager->ChangeParent(item->id(), &parent->id()))
      return false;
  }

  // URL Patterns.
  if (!item->PopulateURLPatterns(
          update_properties.document_url_patterns.get(),
          update_properties.target_url_patterns.get(), error)) {
    return false;
  }

  // There is no need to call ItemUpdated if ChangeParent is called because
  // all sanitation is taken care of in ChangeParent.
  if (!parent && radio_item_updated && !menu_manager->ItemUpdated(item->id()))
    return false;

  menu_manager->WriteToStorage(extension, item_id.extension_key);
  return true;
}

}  // namespace context_menus_api_helpers
}  // namespace extensions

#endif  // CHROME_BROWSER_EXTENSIONS_API_CONTEXT_MENUS_CONTEXT_MENUS_API_HELPERS_H_