| 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/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chrome/browser/extensions/extension_service.h" | |
| 13 #include "chrome/browser/extensions/menu_manager.h" | 12 #include "chrome/browser/extensions/menu_manager.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/common/extensions/api/context_menus.h" | 14 #include "chrome/common/extensions/api/context_menus.h" |
| 16 #include "extensions/common/error_utils.h" | 15 #include "extensions/common/error_utils.h" |
| 17 #include "extensions/common/manifest_handlers/background_info.h" | 16 #include "extensions/common/manifest_handlers/background_info.h" |
| 18 #include "extensions/common/url_pattern_set.h" | 17 #include "extensions/common/url_pattern_set.h" |
| 19 | 18 |
| 20 using extensions::ErrorUtils; | 19 using extensions::ErrorUtils; |
| 21 | 20 |
| 22 namespace { | 21 namespace { |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 base::DictionaryValue* properties = NULL; | 168 base::DictionaryValue* properties = NULL; |
| 170 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &properties)); | 169 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &properties)); |
| 171 EXTENSION_FUNCTION_VALIDATE(properties->GetInteger(kGeneratedIdKey, | 170 EXTENSION_FUNCTION_VALIDATE(properties->GetInteger(kGeneratedIdKey, |
| 172 &id.uid)); | 171 &id.uid)); |
| 173 } | 172 } |
| 174 | 173 |
| 175 std::string title; | 174 std::string title; |
| 176 if (params->create_properties.title.get()) | 175 if (params->create_properties.title.get()) |
| 177 title = *params->create_properties.title; | 176 title = *params->create_properties.title; |
| 178 | 177 |
| 179 MenuManager* menu_manager = | 178 MenuManager* menu_manager = MenuManager::Get(GetProfile()); |
| 180 GetProfile()->GetExtensionService()->menu_manager(); | |
| 181 | 179 |
| 182 if (menu_manager->GetItemById(id)) { | 180 if (menu_manager->GetItemById(id)) { |
| 183 error_ = ErrorUtils::FormatErrorMessage(kDuplicateIDError, | 181 error_ = ErrorUtils::FormatErrorMessage(kDuplicateIDError, |
| 184 GetIDString(id)); | 182 GetIDString(id)); |
| 185 return false; | 183 return false; |
| 186 } | 184 } |
| 187 | 185 |
| 188 if (BackgroundInfo::HasLazyBackgroundPage(GetExtension()) && | 186 if (BackgroundInfo::HasLazyBackgroundPage(GetExtension()) && |
| 189 params->create_properties.onclick.get()) { | 187 params->create_properties.onclick.get()) { |
| 190 error_ = kOnclickDisallowedError; | 188 error_ = kOnclickDisallowedError; |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 scoped_ptr<Update::Params> params(Update::Params::Create(*args_)); | 252 scoped_ptr<Update::Params> params(Update::Params::Create(*args_)); |
| 255 | 253 |
| 256 EXTENSION_FUNCTION_VALIDATE(params.get()); | 254 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 257 if (params->id.as_string) | 255 if (params->id.as_string) |
| 258 item_id.string_uid = *params->id.as_string; | 256 item_id.string_uid = *params->id.as_string; |
| 259 else if (params->id.as_integer) | 257 else if (params->id.as_integer) |
| 260 item_id.uid = *params->id.as_integer; | 258 item_id.uid = *params->id.as_integer; |
| 261 else | 259 else |
| 262 NOTREACHED(); | 260 NOTREACHED(); |
| 263 | 261 |
| 264 ExtensionService* service = GetProfile()->GetExtensionService(); | 262 MenuManager* manager = MenuManager::Get(GetProfile()); |
| 265 MenuManager* manager = service->menu_manager(); | |
| 266 MenuItem* item = manager->GetItemById(item_id); | 263 MenuItem* item = manager->GetItemById(item_id); |
| 267 if (!item || item->extension_id() != extension_id()) { | 264 if (!item || item->extension_id() != extension_id()) { |
| 268 error_ = ErrorUtils::FormatErrorMessage( | 265 error_ = ErrorUtils::FormatErrorMessage( |
| 269 kCannotFindItemError, GetIDString(item_id)); | 266 kCannotFindItemError, GetIDString(item_id)); |
| 270 return false; | 267 return false; |
| 271 } | 268 } |
| 272 | 269 |
| 273 // Type. | 270 // Type. |
| 274 MenuItem::Type type = GetType(params->update_properties, item->type()); | 271 MenuItem::Type type = GetType(params->update_properties, item->type()); |
| 275 | 272 |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 350 return false; | 347 return false; |
| 351 | 348 |
| 352 manager->WriteToStorage(GetExtension()); | 349 manager->WriteToStorage(GetExtension()); |
| 353 return true; | 350 return true; |
| 354 } | 351 } |
| 355 | 352 |
| 356 bool ContextMenusRemoveFunction::RunImpl() { | 353 bool ContextMenusRemoveFunction::RunImpl() { |
| 357 scoped_ptr<Remove::Params> params(Remove::Params::Create(*args_)); | 354 scoped_ptr<Remove::Params> params(Remove::Params::Create(*args_)); |
| 358 EXTENSION_FUNCTION_VALIDATE(params.get()); | 355 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 359 | 356 |
| 360 ExtensionService* service = GetProfile()->GetExtensionService(); | 357 MenuManager* manager = MenuManager::Get(GetProfile()); |
| 361 MenuManager* manager = service->menu_manager(); | |
| 362 | 358 |
| 363 MenuItem::Id id(GetProfile()->IsOffTheRecord(), extension_id()); | 359 MenuItem::Id id(GetProfile()->IsOffTheRecord(), extension_id()); |
| 364 if (params->menu_item_id.as_string) | 360 if (params->menu_item_id.as_string) |
| 365 id.string_uid = *params->menu_item_id.as_string; | 361 id.string_uid = *params->menu_item_id.as_string; |
| 366 else if (params->menu_item_id.as_integer) | 362 else if (params->menu_item_id.as_integer) |
| 367 id.uid = *params->menu_item_id.as_integer; | 363 id.uid = *params->menu_item_id.as_integer; |
| 368 else | 364 else |
| 369 NOTREACHED(); | 365 NOTREACHED(); |
| 370 | 366 |
| 371 MenuItem* item = manager->GetItemById(id); | 367 MenuItem* item = manager->GetItemById(id); |
| 372 // Ensure one extension can't remove another's menu items. | 368 // Ensure one extension can't remove another's menu items. |
| 373 if (!item || item->extension_id() != extension_id()) { | 369 if (!item || item->extension_id() != extension_id()) { |
| 374 error_ = ErrorUtils::FormatErrorMessage( | 370 error_ = ErrorUtils::FormatErrorMessage( |
| 375 kCannotFindItemError, GetIDString(id)); | 371 kCannotFindItemError, GetIDString(id)); |
| 376 return false; | 372 return false; |
| 377 } | 373 } |
| 378 | 374 |
| 379 if (!manager->RemoveContextMenuItem(id)) | 375 if (!manager->RemoveContextMenuItem(id)) |
| 380 return false; | 376 return false; |
| 381 manager->WriteToStorage(GetExtension()); | 377 manager->WriteToStorage(GetExtension()); |
| 382 return true; | 378 return true; |
| 383 } | 379 } |
| 384 | 380 |
| 385 bool ContextMenusRemoveAllFunction::RunImpl() { | 381 bool ContextMenusRemoveAllFunction::RunImpl() { |
| 386 ExtensionService* service = GetProfile()->GetExtensionService(); | 382 MenuManager* manager = MenuManager::Get(GetProfile()); |
| 387 MenuManager* manager = service->menu_manager(); | |
| 388 manager->RemoveAllContextItems(GetExtension()->id()); | 383 manager->RemoveAllContextItems(GetExtension()->id()); |
| 389 manager->WriteToStorage(GetExtension()); | 384 manager->WriteToStorage(GetExtension()); |
| 390 return true; | 385 return true; |
| 391 } | 386 } |
| 392 | 387 |
| 393 } // namespace extensions | 388 } // namespace extensions |
| OLD | NEW |