Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(934)

Side by Side Diff: chrome/browser/extensions/api/context_menu/context_menu_api.cc

Issue 10809094: Context Menus now uses the JSON Schema Compiler. (Closed) Base URL: http://git.chromium.org/chromium/src.git@json_functions_as_properties
Patch Set: GetParentId returns a scoped_ptr instead of a bool Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/api/context_menu/context_menu_api.h" 5 #include "chrome/browser/extensions/api/context_menu/context_menu_api.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/values.h" 9 #include "base/values.h"
10 #include "base/string_number_conversions.h" 10 #include "base/string_number_conversions.h"
11 #include "base/string_util.h" 11 #include "base/string_util.h"
12 #include "chrome/browser/extensions/extension_service.h" 12 #include "chrome/browser/extensions/extension_service.h"
13 #include "chrome/browser/extensions/menu_manager.h"
13 #include "chrome/browser/profiles/profile.h" 14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/common/extensions/api/context_menus.h"
14 #include "chrome/common/extensions/extension_error_utils.h" 16 #include "chrome/common/extensions/extension_error_utils.h"
17 #include "chrome/common/extensions/url_pattern_set.h"
15 18
16 namespace { 19 namespace {
17 20
18 const char kCheckedKey[] = "checked";
19 const char kContextsKey[] = "contexts";
20 const char kDocumentUrlPatternsKey[] = "documentUrlPatterns";
21 const char kEnabledKey[] = "enabled";
22 const char kGeneratedIdKey[] = "generatedId"; 21 const char kGeneratedIdKey[] = "generatedId";
23 const char kIdKey[] = "id";
24 const char kOnclickKey[] = "onclick";
25 const char kParentIdKey[] = "parentId";
26 const char kTargetUrlPatternsKey[] = "targetUrlPatterns";
27 const char kTitleKey[] = "title";
28 const char kTypeKey[] = "type";
29 22
30 const char kCannotFindItemError[] = "Cannot find menu item with id *"; 23 const char kCannotFindItemError[] = "Cannot find menu item with id *";
31 const char kOnclickDisallowedError[] = "Extensions using event pages cannot " 24 const char kOnclickDisallowedError[] = "Extensions using event pages cannot "
32 "pass an onclick parameter to chrome.contextMenus.create. Instead, use " 25 "pass an onclick parameter to chrome.contextMenus.create. Instead, use "
33 "the chrome.contextMenus.onClicked event."; 26 "the chrome.contextMenus.onClicked event.";
34 const char kCheckedError[] = 27 const char kCheckedError[] =
35 "Only items with type \"radio\" or \"checkbox\" can be checked"; 28 "Only items with type \"radio\" or \"checkbox\" can be checked";
36 const char kDuplicateIDError[] = 29 const char kDuplicateIDError[] =
37 "Cannot create item with duplicate id *"; 30 "Cannot create item with duplicate id *";
38 const char kIdRequiredError[] = "Extensions using event pages must pass an " 31 const char kIdRequiredError[] = "Extensions using event pages must pass an "
39 "id parameter to chrome.contextMenus.create"; 32 "id parameter to chrome.contextMenus.create";
40 const char kInvalidValueError[] = "Invalid value for *";
41 const char kInvalidTypeStringError[] = "Invalid type string '*'";
42 const char kParentsMustBeNormalError[] = 33 const char kParentsMustBeNormalError[] =
43 "Parent items must have type \"normal\""; 34 "Parent items must have type \"normal\"";
44 const char kTitleNeededError[] = 35 const char kTitleNeededError[] =
45 "All menu items except for separators must have a title"; 36 "All menu items except for separators must have a title";
46 37
47 std::string GetIDString(const extensions::MenuItem::Id& id) { 38 std::string GetIDString(const extensions::MenuItem::Id& id) {
48 if (id.uid == 0) 39 if (id.uid == 0)
49 return id.string_uid; 40 return id.string_uid;
50 else 41 else
51 return base::IntToString(id.uid); 42 return base::IntToString(id.uid);
52 } 43 }
53 44
45 template<typename PropertyWithEnumT>
46 extensions::MenuItem::ContextList GetContexts(
47 const PropertyWithEnumT& property) {
48 extensions::MenuItem::ContextList contexts;
49 for (size_t i = 0; i < property.contexts->size(); ++i) {
50 switch (property.contexts->at(i)) {
51 case PropertyWithEnumT::CONTEXTS_ELEMENT_ALL:
52 contexts.Add(extensions::MenuItem::ALL);
53 break;
54 case PropertyWithEnumT::CONTEXTS_ELEMENT_PAGE:
55 contexts.Add(extensions::MenuItem::PAGE);
56 break;
57 case PropertyWithEnumT::CONTEXTS_ELEMENT_SELECTION:
58 contexts.Add(extensions::MenuItem::SELECTION);
59 break;
60 case PropertyWithEnumT::CONTEXTS_ELEMENT_LINK:
61 contexts.Add(extensions::MenuItem::LINK);
62 break;
63 case PropertyWithEnumT::CONTEXTS_ELEMENT_EDITABLE:
64 contexts.Add(extensions::MenuItem::EDITABLE);
65 break;
66 case PropertyWithEnumT::CONTEXTS_ELEMENT_IMAGE:
67 contexts.Add(extensions::MenuItem::IMAGE);
68 break;
69 case PropertyWithEnumT::CONTEXTS_ELEMENT_VIDEO:
70 contexts.Add(extensions::MenuItem::VIDEO);
71 break;
72 case PropertyWithEnumT::CONTEXTS_ELEMENT_AUDIO:
73 contexts.Add(extensions::MenuItem::AUDIO);
74 break;
75 case PropertyWithEnumT::CONTEXTS_ELEMENT_FRAME:
76 contexts.Add(extensions::MenuItem::FRAME);
77 break;
78 }
79 }
80 return contexts;
81 }
82
83 template<typename PropertyWithEnumT>
84 extensions::MenuItem::Type GetType(const PropertyWithEnumT& property) {
85 switch (property.type) {
86 case PropertyWithEnumT::TYPE_NONE:
87 case PropertyWithEnumT::TYPE_NORMAL:
88 return extensions::MenuItem::NORMAL;
89 case PropertyWithEnumT::TYPE_CHECKBOX:
90 return extensions::MenuItem::CHECKBOX;
91 case PropertyWithEnumT::TYPE_RADIO:
92 return extensions::MenuItem::RADIO;
93 case PropertyWithEnumT::TYPE_SEPARATOR:
94 return extensions::MenuItem::SEPARATOR;
95 }
96 return extensions::MenuItem::NORMAL;
97 }
98
99 template<typename PropertyWithEnumT>
100 scoped_ptr<extensions::MenuItem::Id> GetParentId(
101 const PropertyWithEnumT& property,
102 bool is_off_the_record,
103 std::string extension_id) {
104 scoped_ptr<extensions::MenuItem::Id> parent_id(
105 new extensions::MenuItem::Id(is_off_the_record, extension_id));
106 switch (property.parent_id_type) {
107 case PropertyWithEnumT::PARENT_ID_NONE:
108 return scoped_ptr<extensions::MenuItem::Id>().Pass();
109 case PropertyWithEnumT::PARENT_ID_INTEGER:
110 parent_id->uid = *property.parent_id_integer;
111 break;
112 case PropertyWithEnumT::PARENT_ID_STRING:
113 parent_id->string_uid = *property.parent_id_string;
114 break;
115 }
116 return parent_id.Pass();
117 }
118
119 extensions::MenuItem* GetParent(extensions::MenuItem::Id parent_id,
120 const extensions::MenuManager* menu_manager,
121 std::string* error) {
122 extensions::MenuItem* parent = menu_manager->GetItemById(parent_id);
123 if (!parent) {
124 *error = ExtensionErrorUtils::FormatErrorMessage(
125 kCannotFindItemError, GetIDString(parent_id));
126 return NULL;
127 }
128 if (parent->type() != extensions::MenuItem::NORMAL) {
129 *error = kParentsMustBeNormalError;
130 return NULL;
131 }
132
133 return parent;
134 }
135
54 } // namespace 136 } // namespace
55 137
56 namespace extensions { 138 namespace extensions {
57 139
58 bool ExtensionContextMenuFunction::ParseContexts( 140 namespace Create = api::context_menus::Create;
59 const DictionaryValue& properties, 141 namespace Remove = api::context_menus::Remove;
60 const char* key, 142 namespace Update = api::context_menus::Update;
61 MenuItem::ContextList* result) {
62 const ListValue* list = NULL;
63 if (!properties.GetList(key, &list)) {
64 return true;
65 }
66 MenuItem::ContextList tmp_result;
67
68 std::string value;
69 for (size_t i = 0; i < list->GetSize(); i++) {
70 if (!list->GetString(i, &value))
71 return false;
72
73 if (value == "all") {
74 tmp_result.Add(MenuItem::ALL);
75 } else if (value == "page") {
76 tmp_result.Add(MenuItem::PAGE);
77 } else if (value == "selection") {
78 tmp_result.Add(MenuItem::SELECTION);
79 } else if (value == "link") {
80 tmp_result.Add(MenuItem::LINK);
81 } else if (value == "editable") {
82 tmp_result.Add(MenuItem::EDITABLE);
83 } else if (value == "image") {
84 tmp_result.Add(MenuItem::IMAGE);
85 } else if (value == "video") {
86 tmp_result.Add(MenuItem::VIDEO);
87 } else if (value == "audio") {
88 tmp_result.Add(MenuItem::AUDIO);
89 } else if (value == "frame") {
90 tmp_result.Add(MenuItem::FRAME);
91 } else {
92 error_ = ExtensionErrorUtils::FormatErrorMessage(kInvalidValueError, key);
93 return false;
94 }
95 }
96 *result = tmp_result;
97 return true;
98 }
99
100 bool ExtensionContextMenuFunction::ParseType(
101 const DictionaryValue& properties,
102 const MenuItem::Type& default_value,
103 MenuItem::Type* result) {
104 DCHECK(result);
105 if (!properties.HasKey(kTypeKey)) {
106 *result = default_value;
107 return true;
108 }
109
110 std::string type_string;
111 if (!properties.GetString(kTypeKey, &type_string))
112 return false;
113
114 if (type_string == "normal") {
115 *result = MenuItem::NORMAL;
116 } else if (type_string == "checkbox") {
117 *result = MenuItem::CHECKBOX;
118 } else if (type_string == "radio") {
119 *result = MenuItem::RADIO;
120 } else if (type_string == "separator") {
121 *result = MenuItem::SEPARATOR;
122 } else {
123 error_ = ExtensionErrorUtils::FormatErrorMessage(kInvalidTypeStringError,
124 type_string);
125 return false;
126 }
127 return true;
128 }
129
130 bool ExtensionContextMenuFunction::ParseChecked(
131 MenuItem::Type type,
132 const DictionaryValue& properties,
133 bool default_value,
134 bool* checked) {
135 if (!properties.HasKey(kCheckedKey)) {
136 *checked = default_value;
137 return true;
138 }
139 if (!properties.GetBoolean(kCheckedKey, checked))
140 return false;
141 if (checked && type != MenuItem::CHECKBOX && type != MenuItem::RADIO) {
142 error_ = kCheckedError;
143 return false;
144 }
145 return true;
146 }
147
148 bool ExtensionContextMenuFunction::ParseID(const Value* value,
149 MenuItem::Id* result) {
150 return (value->GetAsInteger(&result->uid) ||
151 value->GetAsString(&result->string_uid));
152 }
153
154 bool ExtensionContextMenuFunction::GetParent(const DictionaryValue& properties,
155 const MenuManager& manager,
156 MenuItem** result) {
157 if (!properties.HasKey(kParentIdKey))
158 return true;
159 MenuItem::Id parent_id(profile()->IsOffTheRecord(), extension_id());
160 const Value* parent_id_value = NULL;
161 if (properties.Get(kParentIdKey, &parent_id_value) &&
162 !ParseID(parent_id_value, &parent_id))
163 return false;
164
165 MenuItem* parent = manager.GetItemById(parent_id);
166 if (!parent) {
167 error_ = ExtensionErrorUtils::FormatErrorMessage(
168 kCannotFindItemError, GetIDString(parent_id));
169 return false;
170 }
171 if (parent->type() != MenuItem::NORMAL) {
172 error_ = kParentsMustBeNormalError;
173 return false;
174 }
175 *result = parent;
176 return true;
177 }
178 143
179 bool CreateContextMenuFunction::RunImpl() { 144 bool CreateContextMenuFunction::RunImpl() {
180 DictionaryValue* properties; 145 MenuItem::Id id(profile()->IsOffTheRecord(), extension_id());
181 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &properties)); 146 scoped_ptr<Create::Params> params(Create::Params::Create(*args_));
182 EXTENSION_FUNCTION_VALIDATE(properties != NULL); 147 EXTENSION_FUNCTION_VALIDATE(params.get());
183 148
184 MenuItem::Id id(profile()->IsOffTheRecord(), extension_id()); 149 if (params->create_properties.id.get()) {
185 if (properties->HasKey(kIdKey)) { 150 id.string_uid = *params->create_properties.id;
186 EXTENSION_FUNCTION_VALIDATE(properties->GetString(kIdKey,
187 &id.string_uid));
188 } else { 151 } else {
189 if (GetExtension()->has_lazy_background_page()) { 152 if (GetExtension()->has_lazy_background_page()) {
190 error_ = kIdRequiredError; 153 error_ = kIdRequiredError;
191 return false; 154 return false;
192 } 155 }
156
157 // The Generated Id is added by context_menus_custom_bindings.js.
158 DictionaryValue* properties = NULL;
159 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &properties));
193 EXTENSION_FUNCTION_VALIDATE(properties->GetInteger(kGeneratedIdKey, 160 EXTENSION_FUNCTION_VALIDATE(properties->GetInteger(kGeneratedIdKey,
194 &id.uid)); 161 &id.uid));
195 } 162 }
196 163
197 std::string title; 164 std::string title;
198 if (properties->HasKey(kTitleKey) && 165 if (params->create_properties.title.get())
199 !properties->GetString(kTitleKey, &title)) 166 title = *params->create_properties.title;
200 return false;
201 167
202 MenuManager* menu_manager = profile()->GetExtensionService()->menu_manager(); 168 MenuManager* menu_manager = profile()->GetExtensionService()->menu_manager();
203 169
204 if (menu_manager->GetItemById(id)) { 170 if (menu_manager->GetItemById(id)) {
205 error_ = ExtensionErrorUtils::FormatErrorMessage(kDuplicateIDError, 171 error_ = ExtensionErrorUtils::FormatErrorMessage(kDuplicateIDError,
206 GetIDString(id)); 172 GetIDString(id));
207 return false; 173 return false;
208 } 174 }
209 175
210 if (GetExtension()->has_lazy_background_page() && 176 if (GetExtension()->has_lazy_background_page() &&
211 properties->HasKey(kOnclickKey)) { 177 params->create_properties.onclick.get()) {
212 error_ = kOnclickDisallowedError; 178 error_ = kOnclickDisallowedError;
213 return false; 179 return false;
214 } 180 }
215 181
216 MenuItem::ContextList contexts(MenuItem::PAGE); 182 MenuItem::ContextList contexts;
217 if (!ParseContexts(*properties, kContextsKey, &contexts)) 183 if (params->create_properties.contexts.get())
218 return false; 184 contexts = GetContexts(params->create_properties);
185 else
186 contexts.Add(MenuItem::PAGE);
219 187
220 MenuItem::Type type; 188 MenuItem::Type type = GetType(params->create_properties);
221 if (!ParseType(*properties, MenuItem::NORMAL, &type))
222 return false;
223 189
224 if (title.empty() && type != MenuItem::SEPARATOR) { 190 if (title.empty() && type != MenuItem::SEPARATOR) {
225 error_ = kTitleNeededError; 191 error_ = kTitleNeededError;
226 return false; 192 return false;
227 } 193 }
228 194
229 bool checked; 195 bool checked = false;
230 if (!ParseChecked(type, *properties, false, &checked)) 196 if (params->create_properties.checked.get())
231 return false; 197 checked = *params->create_properties.checked;
232 198
233 bool enabled = true; 199 bool enabled = true;
234 if (properties->HasKey(kEnabledKey)) 200 if (params->create_properties.enabled.get())
235 EXTENSION_FUNCTION_VALIDATE(properties->GetBoolean(kEnabledKey, &enabled)); 201 enabled = *params->create_properties.enabled;
236 202
237 scoped_ptr<MenuItem> item( 203 scoped_ptr<MenuItem> item(
238 new MenuItem(id, title, checked, enabled, type, contexts)); 204 new MenuItem(id, title, checked, enabled, type, contexts));
239 205
240 if (!item->PopulateURLPatterns( 206 if (!item->PopulateURLPatterns(
241 *properties, kDocumentUrlPatternsKey, kTargetUrlPatternsKey, &error_)) 207 params->create_properties.document_url_patterns.get(),
208 params->create_properties.target_url_patterns.get(),
209 &error_)) {
242 return false; 210 return false;
211 }
243 212
244 bool success = true; 213 bool success = true;
245 if (properties->HasKey(kParentIdKey)) { 214 scoped_ptr<MenuItem::Id> parent_id(GetParentId(params->create_properties,
246 MenuItem* parent = NULL; 215 profile()->IsOffTheRecord(),
247 if (!GetParent(*properties, *menu_manager, &parent)) 216 extension_id()));
217 if (parent_id.get()) {
218 MenuItem* parent = GetParent(*parent_id, menu_manager, &error_);
219 if (!parent)
248 return false; 220 return false;
249 success = menu_manager->AddChildItem(parent->id(), item.release()); 221 success = menu_manager->AddChildItem(parent->id(), item.release());
250 } else { 222 } else {
251 success = menu_manager->AddContextItem(GetExtension(), item.release()); 223 success = menu_manager->AddContextItem(GetExtension(), item.release());
252 } 224 }
253 225
254 if (!success) 226 if (!success)
255 return false; 227 return false;
256 228
257 menu_manager->WriteToStorage(GetExtension()); 229 menu_manager->WriteToStorage(GetExtension());
258 return true; 230 return true;
259 } 231 }
260 232
261 bool UpdateContextMenuFunction::RunImpl() { 233 bool UpdateContextMenuFunction::RunImpl() {
262 bool radioItemUpdated = false; 234 bool radio_item_updated = false;
263 MenuItem::Id item_id(profile()->IsOffTheRecord(), extension_id()); 235 MenuItem::Id item_id(profile()->IsOffTheRecord(), extension_id());
264 Value* id_value = NULL; 236 scoped_ptr<Update::Params> params(Update::Params::Create(*args_));
265 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &id_value)); 237
266 EXTENSION_FUNCTION_VALIDATE(ParseID(id_value, &item_id)); 238 EXTENSION_FUNCTION_VALIDATE(params.get());
239 switch (params->id_type) {
240 case Update::Params::ID_STRING:
241 item_id.string_uid = *params->id_string;
242 break;
243 case Update::Params::ID_INTEGER:
244 item_id.uid = *params->id_integer;
245 break;
246 }
267 247
268 ExtensionService* service = profile()->GetExtensionService(); 248 ExtensionService* service = profile()->GetExtensionService();
269 MenuManager* manager = service->menu_manager(); 249 MenuManager* manager = service->menu_manager();
270 MenuItem* item = manager->GetItemById(item_id); 250 MenuItem* item = manager->GetItemById(item_id);
271 if (!item || item->extension_id() != extension_id()) { 251 if (!item || item->extension_id() != extension_id()) {
272 error_ = ExtensionErrorUtils::FormatErrorMessage( 252 error_ = ExtensionErrorUtils::FormatErrorMessage(
273 kCannotFindItemError, GetIDString(item_id)); 253 kCannotFindItemError, GetIDString(item_id));
274 return false; 254 return false;
275 } 255 }
276 256
277 DictionaryValue* properties = NULL; 257 // Type.
278 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &properties)); 258 MenuItem::Type type = GetType(params->update_properties);
279 EXTENSION_FUNCTION_VALIDATE(properties != NULL);
280 259
281 // Type.
282 MenuItem::Type type;
283 if (!ParseType(*properties, item->type(), &type))
284 return false;
285 if (type != item->type()) { 260 if (type != item->type()) {
286 if (type == MenuItem::RADIO || item->type() == MenuItem::RADIO) { 261 if (type == MenuItem::RADIO || item->type() == MenuItem::RADIO)
287 radioItemUpdated = true; 262 radio_item_updated = true;
288 }
289 item->set_type(type); 263 item->set_type(type);
290 } 264 }
291 265
292 // Title. 266 // Title.
293 if (properties->HasKey(kTitleKey)) { 267 if (params->update_properties.title.get()) {
294 std::string title; 268 std::string title(*params->update_properties.title);
295 EXTENSION_FUNCTION_VALIDATE(properties->GetString(kTitleKey, &title)); 269 if (title.empty() && item->type() != MenuItem::SEPARATOR) {
296 if (title.empty() && type != MenuItem::SEPARATOR) {
297 error_ = kTitleNeededError; 270 error_ = kTitleNeededError;
298 return false; 271 return false;
299 } 272 }
300 item->set_title(title); 273 item->set_title(title);
301 } 274 }
302 275
303 // Checked state. 276 // Checked state.
304 bool checked; 277 if (params->update_properties.checked.get()) {
305 if (!ParseChecked(item->type(), *properties, item->checked(), &checked)) 278 bool checked = *params->update_properties.checked;
306 return false; 279 if (checked &&
307 if (checked != item->checked()) { 280 item->type() != MenuItem::CHECKBOX &&
308 if (!item->SetChecked(checked)) 281 item->type() != MenuItem::RADIO) {
282 error_ = kCheckedError;
309 return false; 283 return false;
310 radioItemUpdated = true; 284 }
285 if (checked != item->checked()) {
286 if (!item->SetChecked(checked)) {
287 error_ = kCheckedError;
288 return false;
289 }
290 radio_item_updated = true;
291 }
311 } 292 }
312 293
313 // Enabled. 294 // Enabled.
314 bool enabled; 295 if (params->update_properties.enabled.get())
315 if (properties->HasKey(kEnabledKey)) { 296 item->set_enabled(*params->update_properties.enabled);
316 EXTENSION_FUNCTION_VALIDATE(properties->GetBoolean(kEnabledKey, &enabled));
317 item->set_enabled(enabled);
318 }
319 297
320 // Contexts. 298 // Contexts.
321 MenuItem::ContextList contexts(item->contexts()); 299 MenuItem::ContextList contexts;
322 if (!ParseContexts(*properties, kContextsKey, &contexts)) 300 if (params->update_properties.contexts.get()) {
323 return false; 301 contexts = GetContexts(params->update_properties);
324 if (contexts != item->contexts()) 302 if (contexts != item->contexts())
325 item->set_contexts(contexts); 303 item->set_contexts(contexts);
304 }
326 305
327 // Parent id. 306 // Parent id.
328 MenuItem* parent = NULL; 307 MenuItem* parent = NULL;
329 if (!GetParent(*properties, *manager, &parent)) 308 scoped_ptr<MenuItem::Id> parent_id(GetParentId(params->update_properties,
309 profile()->IsOffTheRecord(),
310 extension_id()));
311 if (parent_id.get()) {
312 MenuItem* parent = GetParent(*parent_id, manager, &error_);
313 if (!parent || !manager->ChangeParent(item->id(), &parent->id()))
314 return false;
315 }
316
317 // URL Patterns.
318 if (!item->PopulateURLPatterns(
319 params->update_properties.document_url_patterns.get(),
320 params->update_properties.target_url_patterns.get(), &error_)) {
330 return false; 321 return false;
331 if (parent && !manager->ChangeParent(item->id(), &parent->id())) 322 }
332 return false;
333
334 if (!item->PopulateURLPatterns(
335 *properties, kDocumentUrlPatternsKey, kTargetUrlPatternsKey, &error_))
336 return false;
337 323
338 // There is no need to call ItemUpdated if ChangeParent is called because 324 // There is no need to call ItemUpdated if ChangeParent is called because
339 // all sanitation is taken care of in ChangeParent. 325 // all sanitation is taken care of in ChangeParent.
340 if (!parent && radioItemUpdated && !manager->ItemUpdated(item->id())) 326 if (!parent && radio_item_updated && !manager->ItemUpdated(item->id()))
341 return false; 327 return false;
342 328
343 manager->WriteToStorage(GetExtension()); 329 manager->WriteToStorage(GetExtension());
344 return true; 330 return true;
345 } 331 }
346 332
347 bool RemoveContextMenuFunction::RunImpl() { 333 bool RemoveContextMenuFunction::RunImpl() {
348 MenuItem::Id id(profile()->IsOffTheRecord(), extension_id()); 334 scoped_ptr<Remove::Params> params(Remove::Params::Create(*args_));
349 Value* id_value = NULL; 335 EXTENSION_FUNCTION_VALIDATE(params.get());
350 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &id_value)); 336
351 EXTENSION_FUNCTION_VALIDATE(ParseID(id_value, &id));
352 ExtensionService* service = profile()->GetExtensionService(); 337 ExtensionService* service = profile()->GetExtensionService();
353 MenuManager* manager = service->menu_manager(); 338 MenuManager* manager = service->menu_manager();
354 339
340 MenuItem::Id id(profile()->IsOffTheRecord(), extension_id());
341 switch (params->menu_item_id_type) {
342 case Remove::Params::MENU_ITEM_ID_STRING:
343 id.string_uid = *params->menu_item_id_string;
344 break;
345 case Remove::Params::MENU_ITEM_ID_INTEGER:
346 id.uid = *params->menu_item_id_integer;
347 }
348
355 MenuItem* item = manager->GetItemById(id); 349 MenuItem* item = manager->GetItemById(id);
356 // Ensure one extension can't remove another's menu items. 350 // Ensure one extension can't remove another's menu items.
357 if (!item || item->extension_id() != extension_id()) { 351 if (!item || item->extension_id() != extension_id()) {
358 error_ = ExtensionErrorUtils::FormatErrorMessage( 352 error_ = ExtensionErrorUtils::FormatErrorMessage(
359 kCannotFindItemError, GetIDString(id)); 353 kCannotFindItemError, GetIDString(id));
360 return false; 354 return false;
361 } 355 }
362 356
363 if (!manager->RemoveContextMenuItem(id)) 357 if (!manager->RemoveContextMenuItem(id))
364 return false; 358 return false;
365 manager->WriteToStorage(GetExtension()); 359 manager->WriteToStorage(GetExtension());
366 return true; 360 return true;
367 } 361 }
368 362
369 bool RemoveAllContextMenusFunction::RunImpl() { 363 bool RemoveAllContextMenusFunction::RunImpl() {
370 ExtensionService* service = profile()->GetExtensionService(); 364 ExtensionService* service = profile()->GetExtensionService();
371 MenuManager* manager = service->menu_manager(); 365 MenuManager* manager = service->menu_manager();
372 manager->RemoveAllContextItems(GetExtension()->id()); 366 manager->RemoveAllContextItems(GetExtension()->id());
373 manager->WriteToStorage(GetExtension()); 367 manager->WriteToStorage(GetExtension());
374 return true; 368 return true;
375 } 369 }
376 370
377 } // namespace extensions 371 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/context_menu/context_menu_api.h ('k') | chrome/browser/extensions/menu_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698