| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/extension_context_menu_api.h" | 5 #include "chrome/browser/extensions/extension_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/extensions_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 const char kCheckedKey[] = "checked"; | 16 const char kCheckedKey[] = "checked"; |
| 17 const char kContextsKey[] = "contexts"; | 17 const char kContextsKey[] = "contexts"; |
| 18 const char kDocumentUrlPatternsKey[] = "documentUrlPatterns"; | 18 const char kDocumentUrlPatternsKey[] = "documentUrlPatterns"; |
| 19 const char kGeneratedIdKey[] = "generatedId"; | 19 const char kGeneratedIdKey[] = "generatedId"; |
| 20 const char kParentIdKey[] = "parentId"; | 20 const char kParentIdKey[] = "parentId"; |
| 21 const char kTargetUrlPatternsKey[] = "targetUrlPatterns"; | 21 const char kTargetUrlPatternsKey[] = "targetUrlPatterns"; |
| 22 const char kTitleKey[] = "title"; | 22 const char kTitleKey[] = "title"; |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 | 206 |
| 207 ExtensionMenuItem::Id id(profile(), extension_id(), 0); | 207 ExtensionMenuItem::Id id(profile(), extension_id(), 0); |
| 208 EXTENSION_FUNCTION_VALIDATE(properties->GetInteger(kGeneratedIdKey, | 208 EXTENSION_FUNCTION_VALIDATE(properties->GetInteger(kGeneratedIdKey, |
| 209 &id.uid)); | 209 &id.uid)); |
| 210 std::string title; | 210 std::string title; |
| 211 if (properties->HasKey(kTitleKey) && | 211 if (properties->HasKey(kTitleKey) && |
| 212 !properties->GetString(kTitleKey, &title)) | 212 !properties->GetString(kTitleKey, &title)) |
| 213 return false; | 213 return false; |
| 214 | 214 |
| 215 ExtensionMenuManager* menu_manager = | 215 ExtensionMenuManager* menu_manager = |
| 216 profile()->GetExtensionsService()->menu_manager(); | 216 profile()->GetExtensionService()->menu_manager(); |
| 217 | 217 |
| 218 ExtensionMenuItem::ContextList contexts(ExtensionMenuItem::PAGE); | 218 ExtensionMenuItem::ContextList contexts(ExtensionMenuItem::PAGE); |
| 219 if (!ParseContexts(*properties, kContextsKey, &contexts)) | 219 if (!ParseContexts(*properties, kContextsKey, &contexts)) |
| 220 return false; | 220 return false; |
| 221 | 221 |
| 222 ExtensionMenuItem::Type type; | 222 ExtensionMenuItem::Type type; |
| 223 if (!ParseType(*properties, ExtensionMenuItem::NORMAL, &type)) | 223 if (!ParseType(*properties, ExtensionMenuItem::NORMAL, &type)) |
| 224 return false; | 224 return false; |
| 225 | 225 |
| 226 if (title.empty() && type != ExtensionMenuItem::SEPARATOR) { | 226 if (title.empty() && type != ExtensionMenuItem::SEPARATOR) { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 if (!success) | 261 if (!success) |
| 262 return false; | 262 return false; |
| 263 | 263 |
| 264 return true; | 264 return true; |
| 265 } | 265 } |
| 266 | 266 |
| 267 bool UpdateContextMenuFunction::RunImpl() { | 267 bool UpdateContextMenuFunction::RunImpl() { |
| 268 ExtensionMenuItem::Id item_id(profile(), extension_id(), 0); | 268 ExtensionMenuItem::Id item_id(profile(), extension_id(), 0); |
| 269 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &item_id.uid)); | 269 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &item_id.uid)); |
| 270 | 270 |
| 271 ExtensionsService* service = profile()->GetExtensionsService(); | 271 ExtensionService* service = profile()->GetExtensionService(); |
| 272 ExtensionMenuManager* manager = service->menu_manager(); | 272 ExtensionMenuManager* manager = service->menu_manager(); |
| 273 ExtensionMenuItem* item = manager->GetItemById(item_id); | 273 ExtensionMenuItem* item = manager->GetItemById(item_id); |
| 274 if (!item || item->extension_id() != extension_id()) { | 274 if (!item || item->extension_id() != extension_id()) { |
| 275 error_ = ExtensionErrorUtils::FormatErrorMessage( | 275 error_ = ExtensionErrorUtils::FormatErrorMessage( |
| 276 kCannotFindItemError, base::IntToString(item_id.uid)); | 276 kCannotFindItemError, base::IntToString(item_id.uid)); |
| 277 return false; | 277 return false; |
| 278 } | 278 } |
| 279 | 279 |
| 280 DictionaryValue *properties = NULL; | 280 DictionaryValue *properties = NULL; |
| 281 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &properties)); | 281 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &properties)); |
| 282 EXTENSION_FUNCTION_VALIDATE(properties != NULL); | 282 EXTENSION_FUNCTION_VALIDATE(properties != NULL); |
| 283 | 283 |
| 284 ExtensionMenuManager* menu_manager = | 284 ExtensionMenuManager* menu_manager = |
| 285 profile()->GetExtensionsService()->menu_manager(); | 285 profile()->GetExtensionService()->menu_manager(); |
| 286 | 286 |
| 287 // Type. | 287 // Type. |
| 288 ExtensionMenuItem::Type type; | 288 ExtensionMenuItem::Type type; |
| 289 if (!ParseType(*properties, item->type(), &type)) | 289 if (!ParseType(*properties, item->type(), &type)) |
| 290 return false; | 290 return false; |
| 291 if (type != item->type()) | 291 if (type != item->type()) |
| 292 item->set_type(type); | 292 item->set_type(type); |
| 293 | 293 |
| 294 // Title. | 294 // Title. |
| 295 if (properties->HasKey(kTitleKey)) { | 295 if (properties->HasKey(kTitleKey)) { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 327 | 327 |
| 328 if (!SetURLPatterns(*properties, item)) | 328 if (!SetURLPatterns(*properties, item)) |
| 329 return false; | 329 return false; |
| 330 | 330 |
| 331 return true; | 331 return true; |
| 332 } | 332 } |
| 333 | 333 |
| 334 bool RemoveContextMenuFunction::RunImpl() { | 334 bool RemoveContextMenuFunction::RunImpl() { |
| 335 ExtensionMenuItem::Id id(profile(), extension_id(), 0); | 335 ExtensionMenuItem::Id id(profile(), extension_id(), 0); |
| 336 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &id.uid)); | 336 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &id.uid)); |
| 337 ExtensionsService* service = profile()->GetExtensionsService(); | 337 ExtensionService* service = profile()->GetExtensionService(); |
| 338 ExtensionMenuManager* manager = service->menu_manager(); | 338 ExtensionMenuManager* manager = service->menu_manager(); |
| 339 | 339 |
| 340 ExtensionMenuItem* item = manager->GetItemById(id); | 340 ExtensionMenuItem* item = manager->GetItemById(id); |
| 341 // Ensure one extension can't remove another's menu items. | 341 // Ensure one extension can't remove another's menu items. |
| 342 if (!item || item->extension_id() != extension_id()) { | 342 if (!item || item->extension_id() != extension_id()) { |
| 343 error_ = ExtensionErrorUtils::FormatErrorMessage( | 343 error_ = ExtensionErrorUtils::FormatErrorMessage( |
| 344 kCannotFindItemError, base::IntToString(id.uid)); | 344 kCannotFindItemError, base::IntToString(id.uid)); |
| 345 return false; | 345 return false; |
| 346 } | 346 } |
| 347 | 347 |
| 348 return manager->RemoveContextMenuItem(id); | 348 return manager->RemoveContextMenuItem(id); |
| 349 } | 349 } |
| 350 | 350 |
| 351 bool RemoveAllContextMenusFunction::RunImpl() { | 351 bool RemoveAllContextMenusFunction::RunImpl() { |
| 352 ExtensionsService* service = profile()->GetExtensionsService(); | 352 ExtensionService* service = profile()->GetExtensionService(); |
| 353 ExtensionMenuManager* manager = service->menu_manager(); | 353 ExtensionMenuManager* manager = service->menu_manager(); |
| 354 manager->RemoveAllContextItems(extension_id()); | 354 manager->RemoveAllContextItems(extension_id()); |
| 355 return true; | 355 return true; |
| 356 } | 356 } |
| OLD | NEW |