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. Instead, use " |
| 33 "the chrome.contextMenus.onClicked event."; |
30 const char kCheckedError[] = | 34 const char kCheckedError[] = |
31 "Only items with type \"radio\" or \"checkbox\" can be checked"; | 35 "Only items with type \"radio\" or \"checkbox\" can be checked"; |
32 const char kDuplicateIDError[] = | 36 const char kDuplicateIDError[] = |
33 "Cannot create item with duplicate id *"; | 37 "Cannot create item with duplicate id *"; |
34 const char kIdRequiredError[] = "Extensions using event pages must pass an " | 38 const char kIdRequiredError[] = "Extensions using event pages must pass an " |
35 "id parameter to chrome.contextMenus.create"; | 39 "id parameter to chrome.contextMenus.create"; |
36 const char kInvalidValueError[] = "Invalid value for *"; | 40 const char kInvalidValueError[] = "Invalid value for *"; |
37 const char kInvalidTypeStringError[] = "Invalid type string '*'"; | 41 const char kInvalidTypeStringError[] = "Invalid type string '*'"; |
38 const char kParentsMustBeNormalError[] = | 42 const char kParentsMustBeNormalError[] = |
39 "Parent items must have type \"normal\""; | 43 "Parent items must have type \"normal\""; |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 | 204 |
201 ExtensionMenuManager* menu_manager = | 205 ExtensionMenuManager* menu_manager = |
202 profile()->GetExtensionService()->menu_manager(); | 206 profile()->GetExtensionService()->menu_manager(); |
203 | 207 |
204 if (menu_manager->GetItemById(id)) { | 208 if (menu_manager->GetItemById(id)) { |
205 error_ = ExtensionErrorUtils::FormatErrorMessage(kDuplicateIDError, | 209 error_ = ExtensionErrorUtils::FormatErrorMessage(kDuplicateIDError, |
206 GetIDString(id)); | 210 GetIDString(id)); |
207 return false; | 211 return false; |
208 } | 212 } |
209 | 213 |
| 214 if (GetExtension()->has_lazy_background_page() && |
| 215 properties->HasKey(kOnclickKey)) { |
| 216 error_ = kOnclickDisallowedError; |
| 217 return false; |
| 218 } |
| 219 |
210 ExtensionMenuItem::ContextList contexts(ExtensionMenuItem::PAGE); | 220 ExtensionMenuItem::ContextList contexts(ExtensionMenuItem::PAGE); |
211 if (!ParseContexts(*properties, kContextsKey, &contexts)) | 221 if (!ParseContexts(*properties, kContextsKey, &contexts)) |
212 return false; | 222 return false; |
213 | 223 |
214 ExtensionMenuItem::Type type; | 224 ExtensionMenuItem::Type type; |
215 if (!ParseType(*properties, ExtensionMenuItem::NORMAL, &type)) | 225 if (!ParseType(*properties, ExtensionMenuItem::NORMAL, &type)) |
216 return false; | 226 return false; |
217 | 227 |
218 if (title.empty() && type != ExtensionMenuItem::SEPARATOR) { | 228 if (title.empty() && type != ExtensionMenuItem::SEPARATOR) { |
219 error_ = kTitleNeededError; | 229 error_ = kTitleNeededError; |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
363 | 373 |
364 bool RemoveAllContextMenusFunction::RunImpl() { | 374 bool RemoveAllContextMenusFunction::RunImpl() { |
365 ExtensionService* service = profile()->GetExtensionService(); | 375 ExtensionService* service = profile()->GetExtensionService(); |
366 ExtensionMenuManager* manager = service->menu_manager(); | 376 ExtensionMenuManager* manager = service->menu_manager(); |
367 manager->RemoveAllContextItems(GetExtension()->id()); | 377 manager->RemoveAllContextItems(GetExtension()->id()); |
368 manager->WriteToPrefs(GetExtension()); | 378 manager->WriteToPrefs(GetExtension()); |
369 return true; | 379 return true; |
370 } | 380 } |
371 | 381 |
372 } // namespace extensions | 382 } // namespace extensions |
OLD | NEW |