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 PropertyWithEnumT const& property) { | |
|
not at google - send to devlin
2012/08/07 02:00:11
as in
const PropertyWithEnumT& property
const& i
chebert
2012/08/13 22:41:52
Done.
| |
| 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(PropertyWithEnumT const& property) { | |
|
not at google - send to devlin
2012/08/07 02:00:11
ditto
chebert
2012/08/13 22:41:52
Done.
| |
| 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(PropertyWithEnumT const& property, | |
|
not at google - send to devlin
2012/08/07 02:00:11
ditto
chebert
2012/08/13 22:41:52
Done.
| |
| 101 extensions::MenuItem::Id* parent_id) { | |
| 102 switch (property.parent_id_type) { | |
| 103 case PropertyWithEnumT::PARENT_ID_NONE: | |
| 104 return false; | |
| 105 case PropertyWithEnumT::PARENT_ID_INTEGER: | |
| 106 parent_id->uid = *property.parent_id_integer; | |
| 107 return true; | |
| 108 case PropertyWithEnumT::PARENT_ID_STRING: | |
| 109 parent_id->string_uid = *property.parent_id_string; | |
| 110 return true; | |
| 111 } | |
| 112 return false; | |
|
not at google - send to devlin
2012/08/07 02:00:11
Change this method to return an extensions::MenuIt
chebert
2012/08/13 22:41:52
I've left it this way because we check whether the
not at google - send to devlin
2012/08/14 05:12:21
Consider returning a scoped_ptr<MenuItem::Id> then
chebert
2012/08/14 18:56:53
Done.
chebert
2012/08/14 18:56:53
Done.
| |
| 113 } | |
| 114 | |
| 115 extensions::MenuItem* GetParent(extensions::MenuItem::Id parent_id, | |
| 116 const extensions::MenuManager* menu_manager, | |
| 117 std::string* error) { | |
| 118 extensions::MenuItem* parent = menu_manager->GetItemById(parent_id); | |
| 119 if (!parent) { | |
| 120 *error = ExtensionErrorUtils::FormatErrorMessage( | |
| 121 kCannotFindItemError, GetIDString(parent_id)); | |
| 122 return NULL; | |
| 123 } | |
| 124 if (parent->type() != extensions::MenuItem::NORMAL) { | |
| 125 *error = kParentsMustBeNormalError; | |
| 126 return NULL; | |
| 127 } | |
| 128 | |
| 129 return parent; | |
| 130 } | |
| 131 | |
| 54 } // namespace | 132 } // namespace |
| 55 | 133 |
| 56 namespace extensions { | 134 namespace extensions { |
| 57 | 135 |
| 58 bool ExtensionContextMenuFunction::ParseContexts( | 136 namespace Create = api::context_menus::Create; |
| 59 const DictionaryValue& properties, | 137 namespace Remove = api::context_menus::Remove; |
| 60 const char* key, | 138 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 | 139 |
| 179 bool CreateContextMenuFunction::RunImpl() { | 140 bool CreateContextMenuFunction::RunImpl() { |
| 180 DictionaryValue* properties; | 141 MenuItem::Id id(profile()->IsOffTheRecord(), extension_id()); |
| 181 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &properties)); | 142 scoped_ptr<Create::Params> params(Create::Params::Create(*args_)); |
| 182 EXTENSION_FUNCTION_VALIDATE(properties != NULL); | 143 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 183 | 144 |
| 184 MenuItem::Id id(profile()->IsOffTheRecord(), extension_id()); | 145 if (params->create_properties.id.get()) { |
| 185 if (properties->HasKey(kIdKey)) { | 146 id.string_uid = *params->create_properties.id; |
| 186 EXTENSION_FUNCTION_VALIDATE(properties->GetString(kIdKey, | |
| 187 &id.string_uid)); | |
| 188 } else { | 147 } else { |
| 189 if (GetExtension()->has_lazy_background_page()) { | 148 if (GetExtension()->has_lazy_background_page()) { |
| 190 error_ = kIdRequiredError; | 149 error_ = kIdRequiredError; |
| 191 return false; | 150 return false; |
| 192 } | 151 } |
| 152 | |
| 153 // The Generated Id is added by context_menus_custom_bindings.js. | |
| 154 DictionaryValue* properties = NULL; | |
| 155 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &properties)); | |
| 193 EXTENSION_FUNCTION_VALIDATE(properties->GetInteger(kGeneratedIdKey, | 156 EXTENSION_FUNCTION_VALIDATE(properties->GetInteger(kGeneratedIdKey, |
| 194 &id.uid)); | 157 &id.uid)); |
| 195 } | 158 } |
| 196 | 159 |
| 197 std::string title; | 160 std::string title; |
| 198 if (properties->HasKey(kTitleKey) && | 161 if (params->create_properties.title.get()) |
| 199 !properties->GetString(kTitleKey, &title)) | 162 title = *params->create_properties.title; |
| 200 return false; | |
| 201 | 163 |
| 202 MenuManager* menu_manager = profile()->GetExtensionService()->menu_manager(); | 164 MenuManager* menu_manager = profile()->GetExtensionService()->menu_manager(); |
| 203 | 165 |
| 204 if (menu_manager->GetItemById(id)) { | 166 if (menu_manager->GetItemById(id)) { |
| 205 error_ = ExtensionErrorUtils::FormatErrorMessage(kDuplicateIDError, | 167 error_ = ExtensionErrorUtils::FormatErrorMessage(kDuplicateIDError, |
| 206 GetIDString(id)); | 168 GetIDString(id)); |
| 207 return false; | 169 return false; |
| 208 } | 170 } |
| 209 | 171 |
| 210 if (GetExtension()->has_lazy_background_page() && | 172 if (GetExtension()->has_lazy_background_page() && |
| 211 properties->HasKey(kOnclickKey)) { | 173 params->create_properties.onclick.get()) { |
| 212 error_ = kOnclickDisallowedError; | 174 error_ = kOnclickDisallowedError; |
| 213 return false; | 175 return false; |
| 214 } | 176 } |
| 215 | 177 |
| 216 MenuItem::ContextList contexts(MenuItem::PAGE); | 178 MenuItem::ContextList contexts; |
| 217 if (!ParseContexts(*properties, kContextsKey, &contexts)) | 179 if (params->create_properties.contexts.get()) |
| 218 return false; | 180 contexts = GetContexts(params->create_properties); |
| 181 else | |
| 182 contexts.Add(MenuItem::PAGE); | |
| 219 | 183 |
| 220 MenuItem::Type type; | 184 MenuItem::Type type = GetType(params->create_properties); |
| 221 if (!ParseType(*properties, MenuItem::NORMAL, &type)) | |
| 222 return false; | |
| 223 | 185 |
| 224 if (title.empty() && type != MenuItem::SEPARATOR) { | 186 if (title.empty() && type != MenuItem::SEPARATOR) { |
| 225 error_ = kTitleNeededError; | 187 error_ = kTitleNeededError; |
| 226 return false; | 188 return false; |
| 227 } | 189 } |
| 228 | 190 |
| 229 bool checked; | 191 bool checked = false; |
| 230 if (!ParseChecked(type, *properties, false, &checked)) | 192 if (params->create_properties.checked.get()) |
| 231 return false; | 193 checked = *params->create_properties.checked; |
| 232 | 194 |
| 233 bool enabled = true; | 195 bool enabled = true; |
| 234 if (properties->HasKey(kEnabledKey)) | 196 if (params->create_properties.enabled.get()) |
| 235 EXTENSION_FUNCTION_VALIDATE(properties->GetBoolean(kEnabledKey, &enabled)); | 197 enabled = *params->create_properties.enabled; |
| 236 | 198 |
| 237 scoped_ptr<MenuItem> item( | 199 scoped_ptr<MenuItem> item( |
| 238 new MenuItem(id, title, checked, enabled, type, contexts)); | 200 new MenuItem(id, title, checked, enabled, type, contexts)); |
| 239 | 201 |
| 240 if (!item->PopulateURLPatterns( | 202 if (!item->PopulateURLPatterns( |
| 241 *properties, kDocumentUrlPatternsKey, kTargetUrlPatternsKey, &error_)) | 203 params->create_properties.document_url_patterns.get(), |
| 204 params->create_properties.target_url_patterns.get(), | |
| 205 &error_)) { | |
| 242 return false; | 206 return false; |
| 207 } | |
| 243 | 208 |
| 244 bool success = true; | 209 bool success = true; |
| 245 if (properties->HasKey(kParentIdKey)) { | 210 MenuItem::Id parent_id(profile()->IsOffTheRecord(), extension_id()); |
| 246 MenuItem* parent = NULL; | 211 if (GetParentId(params->create_properties, &parent_id)) { |
| 247 if (!GetParent(*properties, *menu_manager, &parent)) | 212 MenuItem* parent = GetParent(parent_id, menu_manager, &error_); |
| 213 if (!parent) | |
| 248 return false; | 214 return false; |
| 249 success = menu_manager->AddChildItem(parent->id(), item.release()); | 215 success = menu_manager->AddChildItem(parent->id(), item.release()); |
| 250 } else { | 216 } else { |
| 251 success = menu_manager->AddContextItem(GetExtension(), item.release()); | 217 success = menu_manager->AddContextItem(GetExtension(), item.release()); |
| 252 } | 218 } |
| 253 | 219 |
| 254 if (!success) | 220 if (!success) |
| 255 return false; | 221 return false; |
| 256 | 222 |
| 257 menu_manager->WriteToStorage(GetExtension()); | 223 menu_manager->WriteToStorage(GetExtension()); |
| 258 return true; | 224 return true; |
| 259 } | 225 } |
| 260 | 226 |
| 261 bool UpdateContextMenuFunction::RunImpl() { | 227 bool UpdateContextMenuFunction::RunImpl() { |
| 262 bool radioItemUpdated = false; | 228 bool radio_item_updated = false; |
| 263 MenuItem::Id item_id(profile()->IsOffTheRecord(), extension_id()); | 229 MenuItem::Id item_id(profile()->IsOffTheRecord(), extension_id()); |
| 264 Value* id_value = NULL; | 230 scoped_ptr<Update::Params> params(Update::Params::Create(*args_)); |
| 265 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &id_value)); | 231 |
| 266 EXTENSION_FUNCTION_VALIDATE(ParseID(id_value, &item_id)); | 232 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 233 switch (params->id_type) { | |
| 234 case Update::Params::ID_STRING: | |
| 235 item_id.string_uid = *params->id_string; | |
| 236 break; | |
| 237 case Update::Params::ID_INTEGER: | |
| 238 item_id.uid = *params->id_integer; | |
| 239 break; | |
| 240 } | |
| 267 | 241 |
| 268 ExtensionService* service = profile()->GetExtensionService(); | 242 ExtensionService* service = profile()->GetExtensionService(); |
| 269 MenuManager* manager = service->menu_manager(); | 243 MenuManager* manager = service->menu_manager(); |
| 270 MenuItem* item = manager->GetItemById(item_id); | 244 MenuItem* item = manager->GetItemById(item_id); |
| 271 if (!item || item->extension_id() != extension_id()) { | 245 if (!item || item->extension_id() != extension_id()) { |
| 272 error_ = ExtensionErrorUtils::FormatErrorMessage( | 246 error_ = ExtensionErrorUtils::FormatErrorMessage( |
| 273 kCannotFindItemError, GetIDString(item_id)); | 247 kCannotFindItemError, GetIDString(item_id)); |
| 274 return false; | 248 return false; |
| 275 } | 249 } |
| 276 | 250 |
| 277 DictionaryValue* properties = NULL; | 251 // Type. |
| 278 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &properties)); | 252 MenuItem::Type type = GetType(params->update_properties); |
| 279 EXTENSION_FUNCTION_VALIDATE(properties != NULL); | |
| 280 | 253 |
| 281 // Type. | |
| 282 MenuItem::Type type; | |
| 283 if (!ParseType(*properties, item->type(), &type)) | |
| 284 return false; | |
| 285 if (type != item->type()) { | 254 if (type != item->type()) { |
| 286 if (type == MenuItem::RADIO || item->type() == MenuItem::RADIO) { | 255 if (type == MenuItem::RADIO || item->type() == MenuItem::RADIO) |
| 287 radioItemUpdated = true; | 256 radio_item_updated = true; |
| 288 } | |
| 289 item->set_type(type); | 257 item->set_type(type); |
| 290 } | 258 } |
| 291 | 259 |
| 292 // Title. | 260 // Title. |
| 293 if (properties->HasKey(kTitleKey)) { | 261 if (params->update_properties.title.get()) { |
| 294 std::string title; | 262 std::string title(*params->update_properties.title); |
| 295 EXTENSION_FUNCTION_VALIDATE(properties->GetString(kTitleKey, &title)); | 263 if (title.empty() && item->type() != MenuItem::SEPARATOR) { |
| 296 if (title.empty() && type != MenuItem::SEPARATOR) { | |
| 297 error_ = kTitleNeededError; | 264 error_ = kTitleNeededError; |
| 298 return false; | 265 return false; |
| 299 } | 266 } |
| 300 item->set_title(title); | 267 item->set_title(title); |
| 301 } | 268 } |
| 302 | 269 |
| 303 // Checked state. | 270 // Checked state. |
| 304 bool checked; | 271 if (params->update_properties.checked.get()) { |
| 305 if (!ParseChecked(item->type(), *properties, item->checked(), &checked)) | 272 bool checked = *params->update_properties.checked; |
| 306 return false; | 273 if (checked && |
| 307 if (checked != item->checked()) { | 274 item->type() != MenuItem::CHECKBOX && |
| 308 if (!item->SetChecked(checked)) | 275 item->type() != MenuItem::RADIO) { |
| 276 error_ = kCheckedError; | |
| 309 return false; | 277 return false; |
| 310 radioItemUpdated = true; | 278 } |
| 279 if (checked != item->checked()) { | |
| 280 if (!item->SetChecked(checked)) { | |
| 281 error_ = kCheckedError; | |
| 282 return false; | |
| 283 } | |
| 284 radio_item_updated = true; | |
| 285 } | |
| 311 } | 286 } |
| 312 | 287 |
| 313 // Enabled. | 288 // Enabled. |
| 314 bool enabled; | 289 if (params->update_properties.enabled.get()) |
| 315 if (properties->HasKey(kEnabledKey)) { | 290 item->set_enabled(*params->update_properties.enabled); |
| 316 EXTENSION_FUNCTION_VALIDATE(properties->GetBoolean(kEnabledKey, &enabled)); | |
| 317 item->set_enabled(enabled); | |
| 318 } | |
| 319 | 291 |
| 320 // Contexts. | 292 // Contexts. |
| 321 MenuItem::ContextList contexts(item->contexts()); | 293 MenuItem::ContextList contexts; |
| 322 if (!ParseContexts(*properties, kContextsKey, &contexts)) | 294 if (params->update_properties.contexts.get()) { |
| 323 return false; | 295 contexts = GetContexts(params->update_properties); |
| 324 if (contexts != item->contexts()) | 296 if (contexts != item->contexts()) |
| 325 item->set_contexts(contexts); | 297 item->set_contexts(contexts); |
| 298 } | |
| 326 | 299 |
| 327 // Parent id. | 300 // Parent id. |
| 328 MenuItem* parent = NULL; | 301 MenuItem* parent = NULL; |
| 329 if (!GetParent(*properties, *manager, &parent)) | 302 MenuItem::Id parent_id(profile()->IsOffTheRecord(), extension_id()); |
| 303 if (GetParentId(params->update_properties, &parent_id)) { | |
| 304 MenuItem* parent = GetParent(parent_id, manager, &error_); | |
| 305 if (!parent || !manager->ChangeParent(item->id(), &parent->id())) | |
| 306 return false; | |
| 307 } | |
| 308 | |
| 309 // URL Patterns. | |
| 310 if (!item->PopulateURLPatterns( | |
| 311 params->update_properties.document_url_patterns.get(), | |
| 312 params->update_properties.target_url_patterns.get(), &error_)) { | |
| 330 return false; | 313 return false; |
| 331 if (parent && !manager->ChangeParent(item->id(), &parent->id())) | 314 } |
| 332 return false; | |
| 333 | |
| 334 if (!item->PopulateURLPatterns( | |
| 335 *properties, kDocumentUrlPatternsKey, kTargetUrlPatternsKey, &error_)) | |
| 336 return false; | |
| 337 | 315 |
| 338 // There is no need to call ItemUpdated if ChangeParent is called because | 316 // There is no need to call ItemUpdated if ChangeParent is called because |
| 339 // all sanitation is taken care of in ChangeParent. | 317 // all sanitation is taken care of in ChangeParent. |
| 340 if (!parent && radioItemUpdated && !manager->ItemUpdated(item->id())) | 318 if (!parent && radio_item_updated && !manager->ItemUpdated(item->id())) |
| 341 return false; | 319 return false; |
| 342 | 320 |
| 343 manager->WriteToStorage(GetExtension()); | 321 manager->WriteToStorage(GetExtension()); |
| 344 return true; | 322 return true; |
| 345 } | 323 } |
| 346 | 324 |
| 347 bool RemoveContextMenuFunction::RunImpl() { | 325 bool RemoveContextMenuFunction::RunImpl() { |
| 348 MenuItem::Id id(profile()->IsOffTheRecord(), extension_id()); | 326 scoped_ptr<Remove::Params> params(Remove::Params::Create(*args_)); |
| 349 Value* id_value = NULL; | 327 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 350 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &id_value)); | 328 |
| 351 EXTENSION_FUNCTION_VALIDATE(ParseID(id_value, &id)); | |
| 352 ExtensionService* service = profile()->GetExtensionService(); | 329 ExtensionService* service = profile()->GetExtensionService(); |
| 353 MenuManager* manager = service->menu_manager(); | 330 MenuManager* manager = service->menu_manager(); |
| 354 | 331 |
| 332 MenuItem::Id id(profile()->IsOffTheRecord(), extension_id()); | |
| 333 switch (params->menu_item_id_type) { | |
| 334 case Remove::Params::MENU_ITEM_ID_STRING: | |
| 335 id.string_uid = *params->menu_item_id_string; | |
| 336 break; | |
| 337 case Remove::Params::MENU_ITEM_ID_INTEGER: | |
| 338 id.uid = *params->menu_item_id_integer; | |
| 339 } | |
| 340 | |
| 355 MenuItem* item = manager->GetItemById(id); | 341 MenuItem* item = manager->GetItemById(id); |
| 356 // Ensure one extension can't remove another's menu items. | 342 // Ensure one extension can't remove another's menu items. |
| 357 if (!item || item->extension_id() != extension_id()) { | 343 if (!item || item->extension_id() != extension_id()) { |
| 358 error_ = ExtensionErrorUtils::FormatErrorMessage( | 344 error_ = ExtensionErrorUtils::FormatErrorMessage( |
| 359 kCannotFindItemError, GetIDString(id)); | 345 kCannotFindItemError, GetIDString(id)); |
| 360 return false; | 346 return false; |
| 361 } | 347 } |
| 362 | 348 |
| 363 if (!manager->RemoveContextMenuItem(id)) | 349 if (!manager->RemoveContextMenuItem(id)) |
| 364 return false; | 350 return false; |
| 365 manager->WriteToStorage(GetExtension()); | 351 manager->WriteToStorage(GetExtension()); |
| 366 return true; | 352 return true; |
| 367 } | 353 } |
| 368 | 354 |
| 369 bool RemoveAllContextMenusFunction::RunImpl() { | 355 bool RemoveAllContextMenusFunction::RunImpl() { |
| 370 ExtensionService* service = profile()->GetExtensionService(); | 356 ExtensionService* service = profile()->GetExtensionService(); |
| 371 MenuManager* manager = service->menu_manager(); | 357 MenuManager* manager = service->menu_manager(); |
| 372 manager->RemoveAllContextItems(GetExtension()->id()); | 358 manager->RemoveAllContextItems(GetExtension()->id()); |
| 373 manager->WriteToStorage(GetExtension()); | 359 manager->WriteToStorage(GetExtension()); |
| 374 return true; | 360 return true; |
| 375 } | 361 } |
| 376 | 362 |
| 377 } // namespace extensions | 363 } // namespace extensions |
| OLD | NEW |