Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/common/extensions/extension_error_utils.h" | 14 #include "chrome/common/extensions/extension_error_utils.h" |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 const char kCheckedKey[] = "checked"; | 18 const char kCheckedKey[] = "checked"; |
| 19 const char kContextsKey[] = "contexts"; | 19 const char kContextsKey[] = "contexts"; |
| 20 const char kDocumentUrlPatternsKey[] = "documentUrlPatterns"; | 20 const char kDocumentUrlPatternsKey[] = "documentUrlPatterns"; |
| 21 const char kEnabledKey[] = "enabled"; | 21 const char kEnabledKey[] = "enabled"; |
| 22 const char kGeneratedIdKey[] = "generatedId"; | 22 const char kGeneratedIdKey[] = "generatedId"; |
| 23 const char kIdKey[] = "id"; | 23 const char kIdKey[] = "id"; |
| 24 const char kOnclickKey[] = "onclick"; | |
| 24 const char kParentIdKey[] = "parentId"; | 25 const char kParentIdKey[] = "parentId"; |
| 25 const char kTargetUrlPatternsKey[] = "targetUrlPatterns"; | 26 const char kTargetUrlPatternsKey[] = "targetUrlPatterns"; |
| 26 const char kTitleKey[] = "title"; | 27 const char kTitleKey[] = "title"; |
| 27 const char kTypeKey[] = "type"; | 28 const char kTypeKey[] = "type"; |
| 28 | 29 |
| 29 const char kCannotFindItemError[] = "Cannot find menu item with id *"; | 30 const char kCannotFindItemError[] = "Cannot find menu item with id *"; |
| 31 const char kOnclickDisallowedError[] = "Extensions using event pages cannot " | |
| 32 "pass an onclick parameter to chrome.contextMenus.create"; | |
|
Matt Perry
2012/06/01 01:57:15
nit: add "Use the chrome.contextMenus.onClicked ev
Yoyo Zhou
2012/06/01 18:17:52
Done.
| |
| 30 const char kCheckedError[] = | 33 const char kCheckedError[] = |
| 31 "Only items with type \"radio\" or \"checkbox\" can be checked"; | 34 "Only items with type \"radio\" or \"checkbox\" can be checked"; |
| 32 const char kDuplicateIDError[] = | 35 const char kDuplicateIDError[] = |
| 33 "Cannot create item with duplicate id *"; | 36 "Cannot create item with duplicate id *"; |
| 34 const char kIdRequiredError[] = "Extensions using event pages must pass an " | 37 const char kIdRequiredError[] = "Extensions using event pages must pass an " |
| 35 "id parameter to chrome.contextMenus.create"; | 38 "id parameter to chrome.contextMenus.create"; |
| 36 const char kInvalidValueError[] = "Invalid value for *"; | 39 const char kInvalidValueError[] = "Invalid value for *"; |
| 37 const char kInvalidTypeStringError[] = "Invalid type string '*'"; | 40 const char kInvalidTypeStringError[] = "Invalid type string '*'"; |
| 38 const char kParentsMustBeNormalError[] = | 41 const char kParentsMustBeNormalError[] = |
| 39 "Parent items must have type \"normal\""; | 42 "Parent items must have type \"normal\""; |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 200 | 203 |
| 201 ExtensionMenuManager* menu_manager = | 204 ExtensionMenuManager* menu_manager = |
| 202 profile()->GetExtensionService()->menu_manager(); | 205 profile()->GetExtensionService()->menu_manager(); |
| 203 | 206 |
| 204 if (menu_manager->GetItemById(id)) { | 207 if (menu_manager->GetItemById(id)) { |
| 205 error_ = ExtensionErrorUtils::FormatErrorMessage(kDuplicateIDError, | 208 error_ = ExtensionErrorUtils::FormatErrorMessage(kDuplicateIDError, |
| 206 GetIDString(id)); | 209 GetIDString(id)); |
| 207 return false; | 210 return false; |
| 208 } | 211 } |
| 209 | 212 |
| 213 if (GetExtension()->has_lazy_background_page() && | |
| 214 properties->HasKey(kOnclickKey)) { | |
| 215 error_ = kOnclickDisallowedError; | |
| 216 return false; | |
| 217 } | |
| 218 | |
| 210 ExtensionMenuItem::ContextList contexts(ExtensionMenuItem::PAGE); | 219 ExtensionMenuItem::ContextList contexts(ExtensionMenuItem::PAGE); |
| 211 if (!ParseContexts(*properties, kContextsKey, &contexts)) | 220 if (!ParseContexts(*properties, kContextsKey, &contexts)) |
| 212 return false; | 221 return false; |
| 213 | 222 |
| 214 ExtensionMenuItem::Type type; | 223 ExtensionMenuItem::Type type; |
| 215 if (!ParseType(*properties, ExtensionMenuItem::NORMAL, &type)) | 224 if (!ParseType(*properties, ExtensionMenuItem::NORMAL, &type)) |
| 216 return false; | 225 return false; |
| 217 | 226 |
| 218 if (title.empty() && type != ExtensionMenuItem::SEPARATOR) { | 227 if (title.empty() && type != ExtensionMenuItem::SEPARATOR) { |
| 219 error_ = kTitleNeededError; | 228 error_ = kTitleNeededError; |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 363 | 372 |
| 364 bool RemoveAllContextMenusFunction::RunImpl() { | 373 bool RemoveAllContextMenusFunction::RunImpl() { |
| 365 ExtensionService* service = profile()->GetExtensionService(); | 374 ExtensionService* service = profile()->GetExtensionService(); |
| 366 ExtensionMenuManager* manager = service->menu_manager(); | 375 ExtensionMenuManager* manager = service->menu_manager(); |
| 367 manager->RemoveAllContextItems(GetExtension()->id()); | 376 manager->RemoveAllContextItems(GetExtension()->id()); |
| 368 manager->WriteToPrefs(GetExtension()); | 377 manager->WriteToPrefs(GetExtension()); |
| 369 return true; | 378 return true; |
| 370 } | 379 } |
| 371 | 380 |
| 372 } // namespace extensions | 381 } // namespace extensions |
| OLD | NEW |