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_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 else | 45 else |
46 return base::IntToString(id.uid); | 46 return base::IntToString(id.uid); |
47 } | 47 } |
48 | 48 |
49 template<typename PropertyWithEnumT> | 49 template<typename PropertyWithEnumT> |
50 extensions::MenuItem::ContextList GetContexts( | 50 extensions::MenuItem::ContextList GetContexts( |
51 const PropertyWithEnumT& property) { | 51 const PropertyWithEnumT& property) { |
52 extensions::MenuItem::ContextList contexts; | 52 extensions::MenuItem::ContextList contexts; |
53 for (size_t i = 0; i < property.contexts->size(); ++i) { | 53 for (size_t i = 0; i < property.contexts->size(); ++i) { |
54 switch (property.contexts->at(i)) { | 54 switch (property.contexts->at(i)) { |
55 case PropertyWithEnumT::CONTEXTS_ELEMENT_ALL: | 55 case PropertyWithEnumT::CONTEXTS_TYPE_ALL: |
56 contexts.Add(extensions::MenuItem::ALL); | 56 contexts.Add(extensions::MenuItem::ALL); |
57 break; | 57 break; |
58 case PropertyWithEnumT::CONTEXTS_ELEMENT_PAGE: | 58 case PropertyWithEnumT::CONTEXTS_TYPE_PAGE: |
59 contexts.Add(extensions::MenuItem::PAGE); | 59 contexts.Add(extensions::MenuItem::PAGE); |
60 break; | 60 break; |
61 case PropertyWithEnumT::CONTEXTS_ELEMENT_SELECTION: | 61 case PropertyWithEnumT::CONTEXTS_TYPE_SELECTION: |
62 contexts.Add(extensions::MenuItem::SELECTION); | 62 contexts.Add(extensions::MenuItem::SELECTION); |
63 break; | 63 break; |
64 case PropertyWithEnumT::CONTEXTS_ELEMENT_LINK: | 64 case PropertyWithEnumT::CONTEXTS_TYPE_LINK: |
65 contexts.Add(extensions::MenuItem::LINK); | 65 contexts.Add(extensions::MenuItem::LINK); |
66 break; | 66 break; |
67 case PropertyWithEnumT::CONTEXTS_ELEMENT_EDITABLE: | 67 case PropertyWithEnumT::CONTEXTS_TYPE_EDITABLE: |
68 contexts.Add(extensions::MenuItem::EDITABLE); | 68 contexts.Add(extensions::MenuItem::EDITABLE); |
69 break; | 69 break; |
70 case PropertyWithEnumT::CONTEXTS_ELEMENT_IMAGE: | 70 case PropertyWithEnumT::CONTEXTS_TYPE_IMAGE: |
71 contexts.Add(extensions::MenuItem::IMAGE); | 71 contexts.Add(extensions::MenuItem::IMAGE); |
72 break; | 72 break; |
73 case PropertyWithEnumT::CONTEXTS_ELEMENT_VIDEO: | 73 case PropertyWithEnumT::CONTEXTS_TYPE_VIDEO: |
74 contexts.Add(extensions::MenuItem::VIDEO); | 74 contexts.Add(extensions::MenuItem::VIDEO); |
75 break; | 75 break; |
76 case PropertyWithEnumT::CONTEXTS_ELEMENT_AUDIO: | 76 case PropertyWithEnumT::CONTEXTS_TYPE_AUDIO: |
77 contexts.Add(extensions::MenuItem::AUDIO); | 77 contexts.Add(extensions::MenuItem::AUDIO); |
78 break; | 78 break; |
79 case PropertyWithEnumT::CONTEXTS_ELEMENT_FRAME: | 79 case PropertyWithEnumT::CONTEXTS_TYPE_FRAME: |
80 contexts.Add(extensions::MenuItem::FRAME); | 80 contexts.Add(extensions::MenuItem::FRAME); |
81 break; | 81 break; |
82 case PropertyWithEnumT::CONTEXTS_ELEMENT_LAUNCHER: | 82 case PropertyWithEnumT::CONTEXTS_TYPE_LAUNCHER: |
83 contexts.Add(extensions::MenuItem::LAUNCHER); | 83 contexts.Add(extensions::MenuItem::LAUNCHER); |
84 break; | 84 break; |
85 case PropertyWithEnumT::CONTEXTS_ELEMENT_NONE: | 85 case PropertyWithEnumT::CONTEXTS_TYPE_NONE: |
86 NOTREACHED(); | 86 NOTREACHED(); |
87 } | 87 } |
88 } | 88 } |
89 return contexts; | 89 return contexts; |
90 } | 90 } |
91 | 91 |
92 template<typename PropertyWithEnumT> | 92 template<typename PropertyWithEnumT> |
93 extensions::MenuItem::Type GetType(const PropertyWithEnumT& property) { | 93 extensions::MenuItem::Type GetType(const PropertyWithEnumT& property) { |
94 switch (property.type) { | 94 switch (property.type) { |
95 case PropertyWithEnumT::TYPE_NONE: | 95 case PropertyWithEnumT::TYPE_NONE: |
96 case PropertyWithEnumT::TYPE_NORMAL: | 96 case PropertyWithEnumT::TYPE_NORMAL: |
97 return extensions::MenuItem::NORMAL; | 97 return extensions::MenuItem::NORMAL; |
98 case PropertyWithEnumT::TYPE_CHECKBOX: | 98 case PropertyWithEnumT::TYPE_CHECKBOX: |
99 return extensions::MenuItem::CHECKBOX; | 99 return extensions::MenuItem::CHECKBOX; |
100 case PropertyWithEnumT::TYPE_RADIO: | 100 case PropertyWithEnumT::TYPE_RADIO: |
101 return extensions::MenuItem::RADIO; | 101 return extensions::MenuItem::RADIO; |
102 case PropertyWithEnumT::TYPE_SEPARATOR: | 102 case PropertyWithEnumT::TYPE_SEPARATOR: |
103 return extensions::MenuItem::SEPARATOR; | 103 return extensions::MenuItem::SEPARATOR; |
104 } | 104 } |
105 return extensions::MenuItem::NORMAL; | 105 return extensions::MenuItem::NORMAL; |
106 } | 106 } |
107 | 107 |
108 template<typename PropertyWithEnumT> | 108 template<typename PropertyWithEnumT> |
109 scoped_ptr<extensions::MenuItem::Id> GetParentId( | 109 scoped_ptr<extensions::MenuItem::Id> GetParentId( |
110 const PropertyWithEnumT& property, | 110 const PropertyWithEnumT& property, |
111 bool is_off_the_record, | 111 bool is_off_the_record, |
112 std::string extension_id) { | 112 std::string extension_id) { |
| 113 if (!property.parent_id) |
| 114 return scoped_ptr<extensions::MenuItem::Id>(); |
| 115 |
113 scoped_ptr<extensions::MenuItem::Id> parent_id( | 116 scoped_ptr<extensions::MenuItem::Id> parent_id( |
114 new extensions::MenuItem::Id(is_off_the_record, extension_id)); | 117 new extensions::MenuItem::Id(is_off_the_record, extension_id)); |
115 switch (property.parent_id_type) { | 118 if (property.parent_id->as_integer) |
116 case PropertyWithEnumT::PARENT_ID_NONE: | 119 parent_id->uid = *property.parent_id->as_integer; |
117 return scoped_ptr<extensions::MenuItem::Id>().Pass(); | 120 else if (property.parent_id->as_string) |
118 case PropertyWithEnumT::PARENT_ID_INTEGER: | 121 parent_id->string_uid = *property.parent_id->as_string; |
119 parent_id->uid = *property.parent_id_integer; | 122 else |
120 break; | 123 NOTREACHED(); |
121 case PropertyWithEnumT::PARENT_ID_STRING: | |
122 parent_id->string_uid = *property.parent_id_string; | |
123 break; | |
124 } | |
125 return parent_id.Pass(); | 124 return parent_id.Pass(); |
126 } | 125 } |
127 | 126 |
128 extensions::MenuItem* GetParent(extensions::MenuItem::Id parent_id, | 127 extensions::MenuItem* GetParent(extensions::MenuItem::Id parent_id, |
129 const extensions::MenuManager* menu_manager, | 128 const extensions::MenuManager* menu_manager, |
130 std::string* error) { | 129 std::string* error) { |
131 extensions::MenuItem* parent = menu_manager->GetItemById(parent_id); | 130 extensions::MenuItem* parent = menu_manager->GetItemById(parent_id); |
132 if (!parent) { | 131 if (!parent) { |
133 *error = ErrorUtils::FormatErrorMessage( | 132 *error = ErrorUtils::FormatErrorMessage( |
134 kCannotFindItemError, GetIDString(parent_id)); | 133 kCannotFindItemError, GetIDString(parent_id)); |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 menu_manager->WriteToStorage(GetExtension()); | 243 menu_manager->WriteToStorage(GetExtension()); |
245 return true; | 244 return true; |
246 } | 245 } |
247 | 246 |
248 bool ContextMenusUpdateFunction::RunImpl() { | 247 bool ContextMenusUpdateFunction::RunImpl() { |
249 bool radio_item_updated = false; | 248 bool radio_item_updated = false; |
250 MenuItem::Id item_id(profile()->IsOffTheRecord(), extension_id()); | 249 MenuItem::Id item_id(profile()->IsOffTheRecord(), extension_id()); |
251 scoped_ptr<Update::Params> params(Update::Params::Create(*args_)); | 250 scoped_ptr<Update::Params> params(Update::Params::Create(*args_)); |
252 | 251 |
253 EXTENSION_FUNCTION_VALIDATE(params.get()); | 252 EXTENSION_FUNCTION_VALIDATE(params.get()); |
254 switch (params->id_type) { | 253 if (params->id.as_string) |
255 case Update::Params::ID_STRING: | 254 item_id.string_uid = *params->id.as_string; |
256 item_id.string_uid = *params->id_string; | 255 else if (params->id.as_integer) |
257 break; | 256 item_id.uid = *params->id.as_integer; |
258 case Update::Params::ID_INTEGER: | 257 else |
259 item_id.uid = *params->id_integer; | 258 NOTREACHED(); |
260 break; | |
261 case Update::Params::ID_NONE: | |
262 NOTREACHED(); | |
263 } | |
264 | 259 |
265 ExtensionService* service = profile()->GetExtensionService(); | 260 ExtensionService* service = profile()->GetExtensionService(); |
266 MenuManager* manager = service->menu_manager(); | 261 MenuManager* manager = service->menu_manager(); |
267 MenuItem* item = manager->GetItemById(item_id); | 262 MenuItem* item = manager->GetItemById(item_id); |
268 if (!item || item->extension_id() != extension_id()) { | 263 if (!item || item->extension_id() != extension_id()) { |
269 error_ = ErrorUtils::FormatErrorMessage( | 264 error_ = ErrorUtils::FormatErrorMessage( |
270 kCannotFindItemError, GetIDString(item_id)); | 265 kCannotFindItemError, GetIDString(item_id)); |
271 return false; | 266 return false; |
272 } | 267 } |
273 | 268 |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
355 } | 350 } |
356 | 351 |
357 bool ContextMenusRemoveFunction::RunImpl() { | 352 bool ContextMenusRemoveFunction::RunImpl() { |
358 scoped_ptr<Remove::Params> params(Remove::Params::Create(*args_)); | 353 scoped_ptr<Remove::Params> params(Remove::Params::Create(*args_)); |
359 EXTENSION_FUNCTION_VALIDATE(params.get()); | 354 EXTENSION_FUNCTION_VALIDATE(params.get()); |
360 | 355 |
361 ExtensionService* service = profile()->GetExtensionService(); | 356 ExtensionService* service = profile()->GetExtensionService(); |
362 MenuManager* manager = service->menu_manager(); | 357 MenuManager* manager = service->menu_manager(); |
363 | 358 |
364 MenuItem::Id id(profile()->IsOffTheRecord(), extension_id()); | 359 MenuItem::Id id(profile()->IsOffTheRecord(), extension_id()); |
365 switch (params->menu_item_id_type) { | 360 if (params->menu_item_id.as_string) |
366 case Remove::Params::MENU_ITEM_ID_STRING: | 361 id.string_uid = *params->menu_item_id.as_string; |
367 id.string_uid = *params->menu_item_id_string; | 362 else if (params->menu_item_id.as_integer) |
368 break; | 363 id.uid = *params->menu_item_id.as_integer; |
369 case Remove::Params::MENU_ITEM_ID_INTEGER: | 364 else |
370 id.uid = *params->menu_item_id_integer; | 365 NOTREACHED(); |
371 break; | |
372 case Remove::Params::MENU_ITEM_ID_NONE: | |
373 NOTREACHED(); | |
374 } | |
375 | 366 |
376 MenuItem* item = manager->GetItemById(id); | 367 MenuItem* item = manager->GetItemById(id); |
377 // Ensure one extension can't remove another's menu items. | 368 // Ensure one extension can't remove another's menu items. |
378 if (!item || item->extension_id() != extension_id()) { | 369 if (!item || item->extension_id() != extension_id()) { |
379 error_ = ErrorUtils::FormatErrorMessage( | 370 error_ = ErrorUtils::FormatErrorMessage( |
380 kCannotFindItemError, GetIDString(id)); | 371 kCannotFindItemError, GetIDString(id)); |
381 return false; | 372 return false; |
382 } | 373 } |
383 | 374 |
384 if (!manager->RemoveContextMenuItem(id)) | 375 if (!manager->RemoveContextMenuItem(id)) |
385 return false; | 376 return false; |
386 manager->WriteToStorage(GetExtension()); | 377 manager->WriteToStorage(GetExtension()); |
387 return true; | 378 return true; |
388 } | 379 } |
389 | 380 |
390 bool ContextMenusRemoveAllFunction::RunImpl() { | 381 bool ContextMenusRemoveAllFunction::RunImpl() { |
391 ExtensionService* service = profile()->GetExtensionService(); | 382 ExtensionService* service = profile()->GetExtensionService(); |
392 MenuManager* manager = service->menu_manager(); | 383 MenuManager* manager = service->menu_manager(); |
393 manager->RemoveAllContextItems(GetExtension()->id()); | 384 manager->RemoveAllContextItems(GetExtension()->id()); |
394 manager->WriteToStorage(GetExtension()); | 385 manager->WriteToStorage(GetExtension()); |
395 return true; | 386 return true; |
396 } | 387 } |
397 | 388 |
398 } // namespace extensions | 389 } // namespace extensions |
OLD | NEW |