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_menus/context_menus_api.h" | 5 #include "chrome/browser/extensions/api/context_menus/context_menus_api.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
11 #include "base/values.h" | 11 #include "base/values.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/extensions/menu_manager.h" |
14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/common/extensions/api/context_menus.h" | 15 #include "chrome/common/extensions/api/context_menus.h" |
| 16 #include "chrome/common/extensions/background_info.h" |
16 #include "extensions/common/error_utils.h" | 17 #include "extensions/common/error_utils.h" |
17 #include "extensions/common/url_pattern_set.h" | 18 #include "extensions/common/url_pattern_set.h" |
18 | 19 |
19 using extensions::ErrorUtils; | 20 using extensions::ErrorUtils; |
20 | 21 |
21 namespace { | 22 namespace { |
22 | 23 |
23 const char kGeneratedIdKey[] = "generatedId"; | 24 const char kGeneratedIdKey[] = "generatedId"; |
24 | 25 |
25 const char kCannotFindItemError[] = "Cannot find menu item with id *"; | 26 const char kCannotFindItemError[] = "Cannot find menu item with id *"; |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 namespace Update = api::context_menus::Update; | 151 namespace Update = api::context_menus::Update; |
151 | 152 |
152 bool ContextMenusCreateFunction::RunImpl() { | 153 bool ContextMenusCreateFunction::RunImpl() { |
153 MenuItem::Id id(profile()->IsOffTheRecord(), extension_id()); | 154 MenuItem::Id id(profile()->IsOffTheRecord(), extension_id()); |
154 scoped_ptr<Create::Params> params(Create::Params::Create(*args_)); | 155 scoped_ptr<Create::Params> params(Create::Params::Create(*args_)); |
155 EXTENSION_FUNCTION_VALIDATE(params.get()); | 156 EXTENSION_FUNCTION_VALIDATE(params.get()); |
156 | 157 |
157 if (params->create_properties.id.get()) { | 158 if (params->create_properties.id.get()) { |
158 id.string_uid = *params->create_properties.id; | 159 id.string_uid = *params->create_properties.id; |
159 } else { | 160 } else { |
160 if (GetExtension()->has_lazy_background_page()) { | 161 if (BackgroundInfo::HasLazyBackgroundPage(GetExtension())) { |
161 error_ = kIdRequiredError; | 162 error_ = kIdRequiredError; |
162 return false; | 163 return false; |
163 } | 164 } |
164 | 165 |
165 // The Generated Id is added by context_menus_custom_bindings.js. | 166 // The Generated Id is added by context_menus_custom_bindings.js. |
166 DictionaryValue* properties = NULL; | 167 DictionaryValue* properties = NULL; |
167 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &properties)); | 168 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &properties)); |
168 EXTENSION_FUNCTION_VALIDATE(properties->GetInteger(kGeneratedIdKey, | 169 EXTENSION_FUNCTION_VALIDATE(properties->GetInteger(kGeneratedIdKey, |
169 &id.uid)); | 170 &id.uid)); |
170 } | 171 } |
171 | 172 |
172 std::string title; | 173 std::string title; |
173 if (params->create_properties.title.get()) | 174 if (params->create_properties.title.get()) |
174 title = *params->create_properties.title; | 175 title = *params->create_properties.title; |
175 | 176 |
176 MenuManager* menu_manager = profile()->GetExtensionService()->menu_manager(); | 177 MenuManager* menu_manager = profile()->GetExtensionService()->menu_manager(); |
177 | 178 |
178 if (menu_manager->GetItemById(id)) { | 179 if (menu_manager->GetItemById(id)) { |
179 error_ = ErrorUtils::FormatErrorMessage(kDuplicateIDError, | 180 error_ = ErrorUtils::FormatErrorMessage(kDuplicateIDError, |
180 GetIDString(id)); | 181 GetIDString(id)); |
181 return false; | 182 return false; |
182 } | 183 } |
183 | 184 |
184 if (GetExtension()->has_lazy_background_page() && | 185 if (BackgroundInfo::HasLazyBackgroundPage(GetExtension()) && |
185 params->create_properties.onclick.get()) { | 186 params->create_properties.onclick.get()) { |
186 error_ = kOnclickDisallowedError; | 187 error_ = kOnclickDisallowedError; |
187 return false; | 188 return false; |
188 } | 189 } |
189 | 190 |
190 MenuItem::ContextList contexts; | 191 MenuItem::ContextList contexts; |
191 if (params->create_properties.contexts.get()) | 192 if (params->create_properties.contexts.get()) |
192 contexts = GetContexts(params->create_properties); | 193 contexts = GetContexts(params->create_properties); |
193 else | 194 else |
194 contexts.Add(MenuItem::PAGE); | 195 contexts.Add(MenuItem::PAGE); |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
380 | 381 |
381 bool ContextMenusRemoveAllFunction::RunImpl() { | 382 bool ContextMenusRemoveAllFunction::RunImpl() { |
382 ExtensionService* service = profile()->GetExtensionService(); | 383 ExtensionService* service = profile()->GetExtensionService(); |
383 MenuManager* manager = service->menu_manager(); | 384 MenuManager* manager = service->menu_manager(); |
384 manager->RemoveAllContextItems(GetExtension()->id()); | 385 manager->RemoveAllContextItems(GetExtension()->id()); |
385 manager->WriteToStorage(GetExtension()); | 386 manager->WriteToStorage(GetExtension()); |
386 return true; | 387 return true; |
387 } | 388 } |
388 | 389 |
389 } // namespace extensions | 390 } // namespace extensions |
OLD | NEW |