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/extensions/menu_manager.h" | |
| 13 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/common/extensions/api/context_menus.h" | |
| 14 #include "chrome/common/extensions/extension_error_utils.h" | 16 #include "chrome/common/extensions/extension_error_utils.h" |
| 17 #include "chrome/common/extensions/url_pattern_set.h" | |
| 15 | 18 |
| 16 namespace { | 19 namespace { |
| 17 | 20 |
| 18 const char kCheckedKey[] = "checked"; | |
| 19 const char kContextsKey[] = "contexts"; | |
| 20 const char kDocumentUrlPatternsKey[] = "documentUrlPatterns"; | |
| 21 const char kEnabledKey[] = "enabled"; | |
| 22 const char kGeneratedIdKey[] = "generatedId"; | 21 const char kGeneratedIdKey[] = "generatedId"; |
| 23 const char kIdKey[] = "id"; | |
| 24 const char kOnclickKey[] = "onclick"; | |
| 25 const char kParentIdKey[] = "parentId"; | |
| 26 const char kTargetUrlPatternsKey[] = "targetUrlPatterns"; | |
| 27 const char kTitleKey[] = "title"; | |
| 28 const char kTypeKey[] = "type"; | |
| 29 | 22 |
| 30 const char kCannotFindItemError[] = "Cannot find menu item with id *"; | 23 const char kCannotFindItemError[] = "Cannot find menu item with id *"; |
| 31 const char kOnclickDisallowedError[] = "Extensions using event pages cannot " | 24 const char kOnclickDisallowedError[] = "Extensions using event pages cannot " |
| 32 "pass an onclick parameter to chrome.contextMenus.create. Instead, use " | 25 "pass an onclick parameter to chrome.contextMenus.create. Instead, use " |
| 33 "the chrome.contextMenus.onClicked event."; | 26 "the chrome.contextMenus.onClicked event."; |
| 34 const char kCheckedError[] = | 27 const char kCheckedError[] = |
| 35 "Only items with type \"radio\" or \"checkbox\" can be checked"; | 28 "Only items with type \"radio\" or \"checkbox\" can be checked"; |
| 36 const char kDuplicateIDError[] = | 29 const char kDuplicateIDError[] = |
| 37 "Cannot create item with duplicate id *"; | 30 "Cannot create item with duplicate id *"; |
| 38 const char kIdRequiredError[] = "Extensions using event pages must pass an " | 31 const char kIdRequiredError[] = "Extensions using event pages must pass an " |
| 39 "id parameter to chrome.contextMenus.create"; | 32 "id parameter to chrome.contextMenus.create"; |
| 40 const char kInvalidValueError[] = "Invalid value for *"; | |
| 41 const char kInvalidTypeStringError[] = "Invalid type string '*'"; | |
| 42 const char kParentsMustBeNormalError[] = | 33 const char kParentsMustBeNormalError[] = |
| 43 "Parent items must have type \"normal\""; | 34 "Parent items must have type \"normal\""; |
| 44 const char kTitleNeededError[] = | 35 const char kTitleNeededError[] = |
| 45 "All menu items except for separators must have a title"; | 36 "All menu items except for separators must have a title"; |
| 46 | 37 |
| 47 std::string GetIDString(const extensions::MenuItem::Id& id) { | 38 std::string GetIDString(const extensions::MenuItem::Id& id) { |
| 48 if (id.uid == 0) | 39 if (id.uid == 0) |
| 49 return id.string_uid; | 40 return id.string_uid; |
| 50 else | 41 else |
| 51 return base::IntToString(id.uid); | 42 return base::IntToString(id.uid); |
| 52 } | 43 } |
| 53 | 44 |
| 45 template<typename PropertyWithEnumT> | |
| 46 extensions::MenuItem::ContextList GetContexts( | |
| 47 const PropertyWithEnumT& property) { | |
| 48 extensions::MenuItem::ContextList contexts; | |
| 49 for (size_t i = 0; i < property.contexts->size(); ++i) { | |
| 50 switch (property.contexts->at(i)) { | |
| 51 case PropertyWithEnumT::CONTEXTS_ELEMENT_ALL: | |
| 52 contexts.Add(extensions::MenuItem::ALL); | |
| 53 break; | |
| 54 case PropertyWithEnumT::CONTEXTS_ELEMENT_PAGE: | |
| 55 contexts.Add(extensions::MenuItem::PAGE); | |
| 56 break; | |
| 57 case PropertyWithEnumT::CONTEXTS_ELEMENT_SELECTION: | |
| 58 contexts.Add(extensions::MenuItem::SELECTION); | |
| 59 break; | |
| 60 case PropertyWithEnumT::CONTEXTS_ELEMENT_LINK: | |
| 61 contexts.Add(extensions::MenuItem::LINK); | |
| 62 break; | |
| 63 case PropertyWithEnumT::CONTEXTS_ELEMENT_EDITABLE: | |
| 64 contexts.Add(extensions::MenuItem::EDITABLE); | |
| 65 break; | |
| 66 case PropertyWithEnumT::CONTEXTS_ELEMENT_IMAGE: | |
| 67 contexts.Add(extensions::MenuItem::IMAGE); | |
| 68 break; | |
| 69 case PropertyWithEnumT::CONTEXTS_ELEMENT_VIDEO: | |
| 70 contexts.Add(extensions::MenuItem::VIDEO); | |
| 71 break; | |
| 72 case PropertyWithEnumT::CONTEXTS_ELEMENT_AUDIO: | |
| 73 contexts.Add(extensions::MenuItem::AUDIO); | |
| 74 break; | |
| 75 case PropertyWithEnumT::CONTEXTS_ELEMENT_FRAME: | |
| 76 contexts.Add(extensions::MenuItem::FRAME); | |
| 77 break; | |
| 78 } | |
| 79 } | |
| 80 return contexts; | |
| 81 } | |
| 82 | |
| 83 template<typename PropertyWithEnumT> | |
| 84 extensions::MenuItem::Type GetType(const PropertyWithEnumT& property) { | |
| 85 switch (property.type) { | |
| 86 case PropertyWithEnumT::TYPE_NONE: | |
| 87 case PropertyWithEnumT::TYPE_NORMAL: | |
| 88 return extensions::MenuItem::NORMAL; | |
| 89 case PropertyWithEnumT::TYPE_CHECKBOX: | |
| 90 return extensions::MenuItem::CHECKBOX; | |
| 91 case PropertyWithEnumT::TYPE_RADIO: | |
| 92 return extensions::MenuItem::RADIO; | |
| 93 case PropertyWithEnumT::TYPE_SEPARATOR: | |
| 94 return extensions::MenuItem::SEPARATOR; | |
| 95 } | |
| 96 return extensions::MenuItem::NORMAL; | |
| 97 } | |
| 98 | |
| 99 template<typename PropertyWithEnumT> | |
| 100 bool GetParentId(const PropertyWithEnumT& property, | |
| 101 bool is_off_the_record, | |
| 102 std::string extension_id, | |
| 103 extensions::MenuItem::Id* parent_id) { | |
| 104 *parent_id = extensions::MenuItem::Id(is_off_the_record, extension_id); | |
| 105 switch (property.parent_id_type) { | |
| 106 case PropertyWithEnumT::PARENT_ID_NONE: | |
| 107 return false; | |
| 108 case PropertyWithEnumT::PARENT_ID_INTEGER: | |
| 109 parent_id->uid = *property.parent_id_integer; | |
| 110 return true; | |
| 111 case PropertyWithEnumT::PARENT_ID_STRING: | |
| 112 parent_id->string_uid = *property.parent_id_string; | |
| 113 return true; | |
| 114 } | |
| 115 return false; | |
| 116 } | |
| 117 | |
| 118 extensions::MenuItem* GetParent(extensions::MenuItem::Id parent_id, | |
| 119 const extensions::MenuManager* menu_manager, | |
| 120 std::string* error) { | |
| 121 extensions::MenuItem* parent = menu_manager->GetItemById(parent_id); | |
| 122 if (!parent) { | |
| 123 *error = ExtensionErrorUtils::FormatErrorMessage( | |
| 124 kCannotFindItemError, GetIDString(parent_id)); | |
| 125 return NULL; | |
| 126 } | |
| 127 if (parent->type() != extensions::MenuItem::NORMAL) { | |
| 128 *error = kParentsMustBeNormalError; | |
| 129 return NULL; | |
| 130 } | |
| 131 | |
| 132 return parent; | |
| 133 } | |
| 134 | |
| 54 } // namespace | 135 } // namespace |
| 55 | 136 |
| 56 namespace extensions { | 137 namespace extensions { |
| 57 | 138 |
| 58 bool ExtensionContextMenuFunction::ParseContexts( | 139 namespace Create = api::context_menus::Create; |
| 59 const DictionaryValue& properties, | 140 namespace Remove = api::context_menus::Remove; |
| 60 const char* key, | 141 namespace Update = api::context_menus::Update; |
| 61 MenuItem::ContextList* result) { | |
| 62 const ListValue* list = NULL; | |
| 63 if (!properties.GetList(key, &list)) { | |
| 64 return true; | |
| 65 } | |
| 66 MenuItem::ContextList tmp_result; | |
| 67 | |
| 68 std::string value; | |
| 69 for (size_t i = 0; i < list->GetSize(); i++) { | |
| 70 if (!list->GetString(i, &value)) | |
| 71 return false; | |
| 72 | |
| 73 if (value == "all") { | |
| 74 tmp_result.Add(MenuItem::ALL); | |
| 75 } else if (value == "page") { | |
| 76 tmp_result.Add(MenuItem::PAGE); | |
| 77 } else if (value == "selection") { | |
| 78 tmp_result.Add(MenuItem::SELECTION); | |
| 79 } else if (value == "link") { | |
| 80 tmp_result.Add(MenuItem::LINK); | |
| 81 } else if (value == "editable") { | |
| 82 tmp_result.Add(MenuItem::EDITABLE); | |
| 83 } else if (value == "image") { | |
| 84 tmp_result.Add(MenuItem::IMAGE); | |
| 85 } else if (value == "video") { | |
| 86 tmp_result.Add(MenuItem::VIDEO); | |
| 87 } else if (value == "audio") { | |
| 88 tmp_result.Add(MenuItem::AUDIO); | |
| 89 } else if (value == "frame") { | |
| 90 tmp_result.Add(MenuItem::FRAME); | |
| 91 } else { | |
| 92 error_ = ExtensionErrorUtils::FormatErrorMessage(kInvalidValueError, key); | |
| 93 return false; | |
| 94 } | |
| 95 } | |
| 96 *result = tmp_result; | |
| 97 return true; | |
| 98 } | |
| 99 | |
| 100 bool ExtensionContextMenuFunction::ParseType( | |
| 101 const DictionaryValue& properties, | |
| 102 const MenuItem::Type& default_value, | |
| 103 MenuItem::Type* result) { | |
| 104 DCHECK(result); | |
| 105 if (!properties.HasKey(kTypeKey)) { | |
| 106 *result = default_value; | |
| 107 return true; | |
| 108 } | |
| 109 | |
| 110 std::string type_string; | |
| 111 if (!properties.GetString(kTypeKey, &type_string)) | |
| 112 return false; | |
| 113 | |
| 114 if (type_string == "normal") { | |
| 115 *result = MenuItem::NORMAL; | |
| 116 } else if (type_string == "checkbox") { | |
| 117 *result = MenuItem::CHECKBOX; | |
| 118 } else if (type_string == "radio") { | |
| 119 *result = MenuItem::RADIO; | |
| 120 } else if (type_string == "separator") { | |
| 121 *result = MenuItem::SEPARATOR; | |
| 122 } else { | |
| 123 error_ = ExtensionErrorUtils::FormatErrorMessage(kInvalidTypeStringError, | |
| 124 type_string); | |
| 125 return false; | |
| 126 } | |
| 127 return true; | |
| 128 } | |
| 129 | |
| 130 bool ExtensionContextMenuFunction::ParseChecked( | |
| 131 MenuItem::Type type, | |
| 132 const DictionaryValue& properties, | |
| 133 bool default_value, | |
| 134 bool* checked) { | |
| 135 if (!properties.HasKey(kCheckedKey)) { | |
| 136 *checked = default_value; | |
| 137 return true; | |
| 138 } | |
| 139 if (!properties.GetBoolean(kCheckedKey, checked)) | |
| 140 return false; | |
| 141 if (checked && type != MenuItem::CHECKBOX && type != MenuItem::RADIO) { | |
| 142 error_ = kCheckedError; | |
| 143 return false; | |
| 144 } | |
| 145 return true; | |
| 146 } | |
| 147 | |
| 148 bool ExtensionContextMenuFunction::ParseID(const Value* value, | |
| 149 MenuItem::Id* result) { | |
| 150 return (value->GetAsInteger(&result->uid) || | |
| 151 value->GetAsString(&result->string_uid)); | |
| 152 } | |
| 153 | |
| 154 bool ExtensionContextMenuFunction::GetParent(const DictionaryValue& properties, | |
| 155 const MenuManager& manager, | |
| 156 MenuItem** result) { | |
| 157 if (!properties.HasKey(kParentIdKey)) | |
| 158 return true; | |
| 159 MenuItem::Id parent_id(profile()->IsOffTheRecord(), extension_id()); | |
| 160 const Value* parent_id_value = NULL; | |
| 161 if (properties.Get(kParentIdKey, &parent_id_value) && | |
| 162 !ParseID(parent_id_value, &parent_id)) | |
| 163 return false; | |
| 164 | |
| 165 MenuItem* parent = manager.GetItemById(parent_id); | |
| 166 if (!parent) { | |
| 167 error_ = ExtensionErrorUtils::FormatErrorMessage( | |
| 168 kCannotFindItemError, GetIDString(parent_id)); | |
| 169 return false; | |
| 170 } | |
| 171 if (parent->type() != MenuItem::NORMAL) { | |
| 172 error_ = kParentsMustBeNormalError; | |
| 173 return false; | |
| 174 } | |
| 175 *result = parent; | |
| 176 return true; | |
| 177 } | |
| 178 | 142 |
| 179 bool CreateContextMenuFunction::RunImpl() { | 143 bool CreateContextMenuFunction::RunImpl() { |
| 180 DictionaryValue* properties; | 144 MenuItem::Id id(profile()->IsOffTheRecord(), extension_id()); |
| 181 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &properties)); | 145 scoped_ptr<Create::Params> params(Create::Params::Create(*args_)); |
| 182 EXTENSION_FUNCTION_VALIDATE(properties != NULL); | 146 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 183 | 147 |
| 184 MenuItem::Id id(profile()->IsOffTheRecord(), extension_id()); | 148 if (params->create_properties.id.get()) { |
| 185 if (properties->HasKey(kIdKey)) { | 149 id.string_uid = *params->create_properties.id; |
| 186 EXTENSION_FUNCTION_VALIDATE(properties->GetString(kIdKey, | |
| 187 &id.string_uid)); | |
| 188 } else { | 150 } else { |
| 189 if (GetExtension()->has_lazy_background_page()) { | 151 if (GetExtension()->has_lazy_background_page()) { |
| 190 error_ = kIdRequiredError; | 152 error_ = kIdRequiredError; |
| 191 return false; | 153 return false; |
| 192 } | 154 } |
| 155 | |
| 156 // The Generated Id is added by context_menus_custom_bindings.js. | |
| 157 DictionaryValue* properties = NULL; | |
| 158 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &properties)); | |
| 193 EXTENSION_FUNCTION_VALIDATE(properties->GetInteger(kGeneratedIdKey, | 159 EXTENSION_FUNCTION_VALIDATE(properties->GetInteger(kGeneratedIdKey, |
| 194 &id.uid)); | 160 &id.uid)); |
| 195 } | 161 } |
| 196 | 162 |
| 197 std::string title; | 163 std::string title; |
| 198 if (properties->HasKey(kTitleKey) && | 164 if (params->create_properties.title.get()) |
| 199 !properties->GetString(kTitleKey, &title)) | 165 title = *params->create_properties.title; |
| 200 return false; | |
| 201 | 166 |
| 202 MenuManager* menu_manager = profile()->GetExtensionService()->menu_manager(); | 167 MenuManager* menu_manager = profile()->GetExtensionService()->menu_manager(); |
| 203 | 168 |
| 204 if (menu_manager->GetItemById(id)) { | 169 if (menu_manager->GetItemById(id)) { |
| 205 error_ = ExtensionErrorUtils::FormatErrorMessage(kDuplicateIDError, | 170 error_ = ExtensionErrorUtils::FormatErrorMessage(kDuplicateIDError, |
| 206 GetIDString(id)); | 171 GetIDString(id)); |
| 207 return false; | 172 return false; |
| 208 } | 173 } |
| 209 | 174 |
| 210 if (GetExtension()->has_lazy_background_page() && | 175 if (GetExtension()->has_lazy_background_page() && |
| 211 properties->HasKey(kOnclickKey)) { | 176 params->create_properties.onclick.get()) { |
| 212 error_ = kOnclickDisallowedError; | 177 error_ = kOnclickDisallowedError; |
| 213 return false; | 178 return false; |
| 214 } | 179 } |
| 215 | 180 |
| 216 MenuItem::ContextList contexts(MenuItem::PAGE); | 181 MenuItem::ContextList contexts; |
| 217 if (!ParseContexts(*properties, kContextsKey, &contexts)) | 182 if (params->create_properties.contexts.get()) |
| 218 return false; | 183 contexts = GetContexts(params->create_properties); |
| 184 else | |
| 185 contexts.Add(MenuItem::PAGE); | |
| 219 | 186 |
| 220 MenuItem::Type type; | 187 MenuItem::Type type = GetType(params->create_properties); |
| 221 if (!ParseType(*properties, MenuItem::NORMAL, &type)) | |
| 222 return false; | |
| 223 | 188 |
| 224 if (title.empty() && type != MenuItem::SEPARATOR) { | 189 if (title.empty() && type != MenuItem::SEPARATOR) { |
| 225 error_ = kTitleNeededError; | 190 error_ = kTitleNeededError; |
| 226 return false; | 191 return false; |
| 227 } | 192 } |
| 228 | 193 |
| 229 bool checked; | 194 bool checked = false; |
| 230 if (!ParseChecked(type, *properties, false, &checked)) | 195 if (params->create_properties.checked.get()) |
| 231 return false; | 196 checked = *params->create_properties.checked; |
| 232 | 197 |
| 233 bool enabled = true; | 198 bool enabled = true; |
| 234 if (properties->HasKey(kEnabledKey)) | 199 if (params->create_properties.enabled.get()) |
| 235 EXTENSION_FUNCTION_VALIDATE(properties->GetBoolean(kEnabledKey, &enabled)); | 200 enabled = *params->create_properties.enabled; |
| 236 | 201 |
| 237 scoped_ptr<MenuItem> item( | 202 scoped_ptr<MenuItem> item( |
| 238 new MenuItem(id, title, checked, enabled, type, contexts)); | 203 new MenuItem(id, title, checked, enabled, type, contexts)); |
| 239 | 204 |
| 240 if (!item->PopulateURLPatterns( | 205 if (!item->PopulateURLPatterns( |
| 241 *properties, kDocumentUrlPatternsKey, kTargetUrlPatternsKey, &error_)) | 206 params->create_properties.document_url_patterns.get(), |
| 207 params->create_properties.target_url_patterns.get(), | |
| 208 &error_)) { | |
| 242 return false; | 209 return false; |
| 210 } | |
| 243 | 211 |
| 244 bool success = true; | 212 bool success = true; |
| 245 if (properties->HasKey(kParentIdKey)) { | 213 MenuItem::Id parent_id; |
| 246 MenuItem* parent = NULL; | 214 bool got_parent_id = GetParentId(params->create_properties, |
| 247 if (!GetParent(*properties, *menu_manager, &parent)) | 215 profile()->IsOffTheRecord(), |
| 216 extension_id(), | |
| 217 &parent_id); | |
|
not at google - send to devlin
2012/08/14 05:12:21
nit: style should align arguments vertically, so
chebert
2012/08/14 18:56:53
Done.
| |
| 218 | |
| 219 if (got_parent_id) { | |
| 220 MenuItem* parent = GetParent(parent_id, menu_manager, &error_); | |
| 221 if (!parent) | |
| 248 return false; | 222 return false; |
| 249 success = menu_manager->AddChildItem(parent->id(), item.release()); | 223 success = menu_manager->AddChildItem(parent->id(), item.release()); |
| 250 } else { | 224 } else { |
| 251 success = menu_manager->AddContextItem(GetExtension(), item.release()); | 225 success = menu_manager->AddContextItem(GetExtension(), item.release()); |
| 252 } | 226 } |
| 253 | 227 |
| 254 if (!success) | 228 if (!success) |
| 255 return false; | 229 return false; |
| 256 | 230 |
| 257 menu_manager->WriteToStorage(GetExtension()); | 231 menu_manager->WriteToStorage(GetExtension()); |
| 258 return true; | 232 return true; |
| 259 } | 233 } |
| 260 | 234 |
| 261 bool UpdateContextMenuFunction::RunImpl() { | 235 bool UpdateContextMenuFunction::RunImpl() { |
| 262 bool radioItemUpdated = false; | 236 bool radio_item_updated = false; |
| 263 MenuItem::Id item_id(profile()->IsOffTheRecord(), extension_id()); | 237 MenuItem::Id item_id(profile()->IsOffTheRecord(), extension_id()); |
| 264 Value* id_value = NULL; | 238 scoped_ptr<Update::Params> params(Update::Params::Create(*args_)); |
| 265 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &id_value)); | 239 |
| 266 EXTENSION_FUNCTION_VALIDATE(ParseID(id_value, &item_id)); | 240 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 241 switch (params->id_type) { | |
| 242 case Update::Params::ID_STRING: | |
| 243 item_id.string_uid = *params->id_string; | |
| 244 break; | |
| 245 case Update::Params::ID_INTEGER: | |
| 246 item_id.uid = *params->id_integer; | |
| 247 break; | |
| 248 } | |
| 267 | 249 |
| 268 ExtensionService* service = profile()->GetExtensionService(); | 250 ExtensionService* service = profile()->GetExtensionService(); |
| 269 MenuManager* manager = service->menu_manager(); | 251 MenuManager* manager = service->menu_manager(); |
| 270 MenuItem* item = manager->GetItemById(item_id); | 252 MenuItem* item = manager->GetItemById(item_id); |
| 271 if (!item || item->extension_id() != extension_id()) { | 253 if (!item || item->extension_id() != extension_id()) { |
| 272 error_ = ExtensionErrorUtils::FormatErrorMessage( | 254 error_ = ExtensionErrorUtils::FormatErrorMessage( |
| 273 kCannotFindItemError, GetIDString(item_id)); | 255 kCannotFindItemError, GetIDString(item_id)); |
| 274 return false; | 256 return false; |
| 275 } | 257 } |
| 276 | 258 |
| 277 DictionaryValue* properties = NULL; | 259 // Type. |
| 278 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &properties)); | 260 MenuItem::Type type = GetType(params->update_properties); |
| 279 EXTENSION_FUNCTION_VALIDATE(properties != NULL); | |
| 280 | 261 |
| 281 // Type. | |
| 282 MenuItem::Type type; | |
| 283 if (!ParseType(*properties, item->type(), &type)) | |
| 284 return false; | |
| 285 if (type != item->type()) { | 262 if (type != item->type()) { |
| 286 if (type == MenuItem::RADIO || item->type() == MenuItem::RADIO) { | 263 if (type == MenuItem::RADIO || item->type() == MenuItem::RADIO) |
| 287 radioItemUpdated = true; | 264 radio_item_updated = true; |
| 288 } | |
| 289 item->set_type(type); | 265 item->set_type(type); |
| 290 } | 266 } |
| 291 | 267 |
| 292 // Title. | 268 // Title. |
| 293 if (properties->HasKey(kTitleKey)) { | 269 if (params->update_properties.title.get()) { |
| 294 std::string title; | 270 std::string title(*params->update_properties.title); |
| 295 EXTENSION_FUNCTION_VALIDATE(properties->GetString(kTitleKey, &title)); | 271 if (title.empty() && item->type() != MenuItem::SEPARATOR) { |
| 296 if (title.empty() && type != MenuItem::SEPARATOR) { | |
| 297 error_ = kTitleNeededError; | 272 error_ = kTitleNeededError; |
| 298 return false; | 273 return false; |
| 299 } | 274 } |
| 300 item->set_title(title); | 275 item->set_title(title); |
| 301 } | 276 } |
| 302 | 277 |
| 303 // Checked state. | 278 // Checked state. |
| 304 bool checked; | 279 if (params->update_properties.checked.get()) { |
| 305 if (!ParseChecked(item->type(), *properties, item->checked(), &checked)) | 280 bool checked = *params->update_properties.checked; |
| 306 return false; | 281 if (checked && |
| 307 if (checked != item->checked()) { | 282 item->type() != MenuItem::CHECKBOX && |
| 308 if (!item->SetChecked(checked)) | 283 item->type() != MenuItem::RADIO) { |
| 284 error_ = kCheckedError; | |
| 309 return false; | 285 return false; |
| 310 radioItemUpdated = true; | 286 } |
| 287 if (checked != item->checked()) { | |
| 288 if (!item->SetChecked(checked)) { | |
| 289 error_ = kCheckedError; | |
| 290 return false; | |
| 291 } | |
| 292 radio_item_updated = true; | |
| 293 } | |
| 311 } | 294 } |
| 312 | 295 |
| 313 // Enabled. | 296 // Enabled. |
| 314 bool enabled; | 297 if (params->update_properties.enabled.get()) |
| 315 if (properties->HasKey(kEnabledKey)) { | 298 item->set_enabled(*params->update_properties.enabled); |
| 316 EXTENSION_FUNCTION_VALIDATE(properties->GetBoolean(kEnabledKey, &enabled)); | |
| 317 item->set_enabled(enabled); | |
| 318 } | |
| 319 | 299 |
| 320 // Contexts. | 300 // Contexts. |
| 321 MenuItem::ContextList contexts(item->contexts()); | 301 MenuItem::ContextList contexts; |
| 322 if (!ParseContexts(*properties, kContextsKey, &contexts)) | 302 if (params->update_properties.contexts.get()) { |
| 323 return false; | 303 contexts = GetContexts(params->update_properties); |
| 324 if (contexts != item->contexts()) | 304 if (contexts != item->contexts()) |
| 325 item->set_contexts(contexts); | 305 item->set_contexts(contexts); |
| 306 } | |
| 326 | 307 |
| 327 // Parent id. | 308 // Parent id. |
| 328 MenuItem* parent = NULL; | 309 MenuItem* parent = NULL; |
| 329 if (!GetParent(*properties, *manager, &parent)) | 310 MenuItem::Id parent_id; |
| 311 bool got_parent_id = GetParentId(params->update_properties, | |
| 312 profile()->IsOffTheRecord(), | |
| 313 extension_id(), | |
| 314 &parent_id); | |
| 315 if (got_parent_id) { | |
| 316 MenuItem* parent = GetParent(parent_id, manager, &error_); | |
| 317 if (!parent || !manager->ChangeParent(item->id(), &parent->id())) | |
| 318 return false; | |
| 319 } | |
| 320 | |
| 321 // URL Patterns. | |
| 322 if (!item->PopulateURLPatterns( | |
| 323 params->update_properties.document_url_patterns.get(), | |
| 324 params->update_properties.target_url_patterns.get(), &error_)) { | |
| 330 return false; | 325 return false; |
| 331 if (parent && !manager->ChangeParent(item->id(), &parent->id())) | 326 } |
| 332 return false; | |
| 333 | |
| 334 if (!item->PopulateURLPatterns( | |
| 335 *properties, kDocumentUrlPatternsKey, kTargetUrlPatternsKey, &error_)) | |
| 336 return false; | |
| 337 | 327 |
| 338 // There is no need to call ItemUpdated if ChangeParent is called because | 328 // There is no need to call ItemUpdated if ChangeParent is called because |
| 339 // all sanitation is taken care of in ChangeParent. | 329 // all sanitation is taken care of in ChangeParent. |
| 340 if (!parent && radioItemUpdated && !manager->ItemUpdated(item->id())) | 330 if (!parent && radio_item_updated && !manager->ItemUpdated(item->id())) |
| 341 return false; | 331 return false; |
| 342 | 332 |
| 343 manager->WriteToStorage(GetExtension()); | 333 manager->WriteToStorage(GetExtension()); |
| 344 return true; | 334 return true; |
| 345 } | 335 } |
| 346 | 336 |
| 347 bool RemoveContextMenuFunction::RunImpl() { | 337 bool RemoveContextMenuFunction::RunImpl() { |
| 348 MenuItem::Id id(profile()->IsOffTheRecord(), extension_id()); | 338 scoped_ptr<Remove::Params> params(Remove::Params::Create(*args_)); |
| 349 Value* id_value = NULL; | 339 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 350 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &id_value)); | 340 |
| 351 EXTENSION_FUNCTION_VALIDATE(ParseID(id_value, &id)); | |
| 352 ExtensionService* service = profile()->GetExtensionService(); | 341 ExtensionService* service = profile()->GetExtensionService(); |
| 353 MenuManager* manager = service->menu_manager(); | 342 MenuManager* manager = service->menu_manager(); |
| 354 | 343 |
| 344 MenuItem::Id id(profile()->IsOffTheRecord(), extension_id()); | |
| 345 switch (params->menu_item_id_type) { | |
| 346 case Remove::Params::MENU_ITEM_ID_STRING: | |
| 347 id.string_uid = *params->menu_item_id_string; | |
| 348 break; | |
| 349 case Remove::Params::MENU_ITEM_ID_INTEGER: | |
| 350 id.uid = *params->menu_item_id_integer; | |
| 351 } | |
| 352 | |
| 355 MenuItem* item = manager->GetItemById(id); | 353 MenuItem* item = manager->GetItemById(id); |
| 356 // Ensure one extension can't remove another's menu items. | 354 // Ensure one extension can't remove another's menu items. |
| 357 if (!item || item->extension_id() != extension_id()) { | 355 if (!item || item->extension_id() != extension_id()) { |
| 358 error_ = ExtensionErrorUtils::FormatErrorMessage( | 356 error_ = ExtensionErrorUtils::FormatErrorMessage( |
| 359 kCannotFindItemError, GetIDString(id)); | 357 kCannotFindItemError, GetIDString(id)); |
| 360 return false; | 358 return false; |
| 361 } | 359 } |
| 362 | 360 |
| 363 if (!manager->RemoveContextMenuItem(id)) | 361 if (!manager->RemoveContextMenuItem(id)) |
| 364 return false; | 362 return false; |
| 365 manager->WriteToStorage(GetExtension()); | 363 manager->WriteToStorage(GetExtension()); |
| 366 return true; | 364 return true; |
| 367 } | 365 } |
| 368 | 366 |
| 369 bool RemoveAllContextMenusFunction::RunImpl() { | 367 bool RemoveAllContextMenusFunction::RunImpl() { |
| 370 ExtensionService* service = profile()->GetExtensionService(); | 368 ExtensionService* service = profile()->GetExtensionService(); |
| 371 MenuManager* manager = service->menu_manager(); | 369 MenuManager* manager = service->menu_manager(); |
| 372 manager->RemoveAllContextItems(GetExtension()->id()); | 370 manager->RemoveAllContextItems(GetExtension()->id()); |
| 373 manager->WriteToStorage(GetExtension()); | 371 manager->WriteToStorage(GetExtension()); |
| 374 return true; | 372 return true; |
| 375 } | 373 } |
| 376 | 374 |
| 377 } // namespace extensions | 375 } // namespace extensions |
| OLD | NEW |