| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 // Definition of helper functions for the ContextMenus API. | 5 // Definition of helper functions for the ContextMenus API. |
| 6 | 6 |
| 7 #ifndef CHROME_BROWSER_EXTENSIONS_API_CONTEXT_MENUS_CONTEXT_MENUS_API_HELPERS_H_ | 7 #ifndef CHROME_BROWSER_EXTENSIONS_API_CONTEXT_MENUS_CONTEXT_MENUS_API_HELPERS_H_ |
| 8 #define CHROME_BROWSER_EXTENSIONS_API_CONTEXT_MENUS_CONTEXT_MENUS_API_HELPERS_H_ | 8 #define CHROME_BROWSER_EXTENSIONS_API_CONTEXT_MENUS_CONTEXT_MENUS_API_HELPERS_H_ |
| 9 | 9 |
| 10 #include "chrome/browser/extensions/menu_manager.h" | 10 #include "chrome/browser/extensions/menu_manager.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/common/extensions/api/context_menus.h" |
| 12 #include "extensions/common/error_utils.h" | 13 #include "extensions/common/error_utils.h" |
| 13 #include "extensions/common/manifest_handlers/background_info.h" | 14 #include "extensions/common/manifest_handlers/background_info.h" |
| 14 | 15 |
| 15 namespace extensions { | 16 namespace extensions { |
| 16 namespace context_menus_api_helpers { | 17 namespace context_menus_api_helpers { |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 template <typename PropertyWithEnumT> | 21 template <typename PropertyWithEnumT> |
| 21 scoped_ptr<extensions::MenuItem::Id> GetParentId( | 22 scoped_ptr<extensions::MenuItem::Id> GetParentId( |
| (...skipping 25 matching lines...) Expand all Loading... |
| 47 extern const char kOnclickDisallowedError[]; | 48 extern const char kOnclickDisallowedError[]; |
| 48 extern const char kParentsMustBeNormalError[]; | 49 extern const char kParentsMustBeNormalError[]; |
| 49 extern const char kTitleNeededError[]; | 50 extern const char kTitleNeededError[]; |
| 50 | 51 |
| 51 std::string GetIDString(const MenuItem::Id& id); | 52 std::string GetIDString(const MenuItem::Id& id); |
| 52 | 53 |
| 53 MenuItem* GetParent(MenuItem::Id parent_id, | 54 MenuItem* GetParent(MenuItem::Id parent_id, |
| 54 const MenuManager* menu_manager, | 55 const MenuManager* menu_manager, |
| 55 std::string* error); | 56 std::string* error); |
| 56 | 57 |
| 57 template<typename PropertyWithEnumT> | 58 MenuItem::ContextList GetContexts(const std::vector< |
| 58 MenuItem::ContextList GetContexts(const PropertyWithEnumT& property) { | 59 extensions::api::context_menus::ContextType>& in_contexts); |
| 59 MenuItem::ContextList contexts; | |
| 60 for (size_t i = 0; i < property.contexts->size(); ++i) { | |
| 61 switch (property.contexts->at(i)) { | |
| 62 case PropertyWithEnumT::CONTEXTS_TYPE_ALL: | |
| 63 contexts.Add(extensions::MenuItem::ALL); | |
| 64 break; | |
| 65 case PropertyWithEnumT::CONTEXTS_TYPE_PAGE: | |
| 66 contexts.Add(extensions::MenuItem::PAGE); | |
| 67 break; | |
| 68 case PropertyWithEnumT::CONTEXTS_TYPE_SELECTION: | |
| 69 contexts.Add(extensions::MenuItem::SELECTION); | |
| 70 break; | |
| 71 case PropertyWithEnumT::CONTEXTS_TYPE_LINK: | |
| 72 contexts.Add(extensions::MenuItem::LINK); | |
| 73 break; | |
| 74 case PropertyWithEnumT::CONTEXTS_TYPE_EDITABLE: | |
| 75 contexts.Add(extensions::MenuItem::EDITABLE); | |
| 76 break; | |
| 77 case PropertyWithEnumT::CONTEXTS_TYPE_IMAGE: | |
| 78 contexts.Add(extensions::MenuItem::IMAGE); | |
| 79 break; | |
| 80 case PropertyWithEnumT::CONTEXTS_TYPE_VIDEO: | |
| 81 contexts.Add(extensions::MenuItem::VIDEO); | |
| 82 break; | |
| 83 case PropertyWithEnumT::CONTEXTS_TYPE_AUDIO: | |
| 84 contexts.Add(extensions::MenuItem::AUDIO); | |
| 85 break; | |
| 86 case PropertyWithEnumT::CONTEXTS_TYPE_FRAME: | |
| 87 contexts.Add(extensions::MenuItem::FRAME); | |
| 88 break; | |
| 89 case PropertyWithEnumT::CONTEXTS_TYPE_LAUNCHER: | |
| 90 // Not available for <webview>. | |
| 91 contexts.Add(extensions::MenuItem::LAUNCHER); | |
| 92 break; | |
| 93 case PropertyWithEnumT::CONTEXTS_TYPE_BROWSER_ACTION: | |
| 94 // Not available for <webview>. | |
| 95 contexts.Add(extensions::MenuItem::BROWSER_ACTION); | |
| 96 break; | |
| 97 case PropertyWithEnumT::CONTEXTS_TYPE_PAGE_ACTION: | |
| 98 // Not available for <webview>. | |
| 99 contexts.Add(extensions::MenuItem::PAGE_ACTION); | |
| 100 break; | |
| 101 case PropertyWithEnumT::CONTEXTS_TYPE_NONE: | |
| 102 NOTREACHED(); | |
| 103 } | |
| 104 } | |
| 105 return contexts; | |
| 106 } | |
| 107 | 60 |
| 108 template<typename PropertyWithEnumT> | 61 MenuItem::Type GetType(extensions::api::context_menus::ItemType type, |
| 109 MenuItem::Type GetType(const PropertyWithEnumT& property, | 62 MenuItem::Type default_type); |
| 110 MenuItem::Type default_type) { | |
| 111 switch (property.type) { | |
| 112 case PropertyWithEnumT::TYPE_NONE: | |
| 113 return default_type; | |
| 114 case PropertyWithEnumT::TYPE_NORMAL: | |
| 115 return extensions::MenuItem::NORMAL; | |
| 116 case PropertyWithEnumT::TYPE_CHECKBOX: | |
| 117 return extensions::MenuItem::CHECKBOX; | |
| 118 case PropertyWithEnumT::TYPE_RADIO: | |
| 119 return extensions::MenuItem::RADIO; | |
| 120 case PropertyWithEnumT::TYPE_SEPARATOR: | |
| 121 return extensions::MenuItem::SEPARATOR; | |
| 122 } | |
| 123 return extensions::MenuItem::NORMAL; | |
| 124 } | |
| 125 | 63 |
| 126 // Creates and adds a menu item from |create_properties|. | 64 // Creates and adds a menu item from |create_properties|. |
| 127 template<typename PropertyWithEnumT> | 65 template<typename PropertyWithEnumT> |
| 128 bool CreateMenuItem(const PropertyWithEnumT& create_properties, | 66 bool CreateMenuItem(const PropertyWithEnumT& create_properties, |
| 129 Profile* profile, | 67 Profile* profile, |
| 130 const Extension* extension, | 68 const Extension* extension, |
| 131 const MenuItem::Id& item_id, | 69 const MenuItem::Id& item_id, |
| 132 std::string* error) { | 70 std::string* error) { |
| 133 bool is_webview = item_id.extension_key.webview_instance_id != 0; | 71 bool is_webview = item_id.extension_key.webview_instance_id != 0; |
| 134 MenuManager* menu_manager = MenuManager::Get(profile); | 72 MenuManager* menu_manager = MenuManager::Get(profile); |
| 135 | 73 |
| 136 if (menu_manager->GetItemById(item_id)) { | 74 if (menu_manager->GetItemById(item_id)) { |
| 137 *error = ErrorUtils::FormatErrorMessage(kDuplicateIDError, | 75 *error = ErrorUtils::FormatErrorMessage(kDuplicateIDError, |
| 138 GetIDString(item_id)); | 76 GetIDString(item_id)); |
| 139 return false; | 77 return false; |
| 140 } | 78 } |
| 141 | 79 |
| 142 if (!is_webview && BackgroundInfo::HasLazyBackgroundPage(extension) && | 80 if (!is_webview && BackgroundInfo::HasLazyBackgroundPage(extension) && |
| 143 create_properties.onclick.get()) { | 81 create_properties.onclick.get()) { |
| 144 *error = kOnclickDisallowedError; | 82 *error = kOnclickDisallowedError; |
| 145 return false; | 83 return false; |
| 146 } | 84 } |
| 147 | 85 |
| 148 // Contexts. | 86 // Contexts. |
| 149 MenuItem::ContextList contexts; | 87 MenuItem::ContextList contexts; |
| 150 if (create_properties.contexts.get()) | 88 if (create_properties.contexts.get()) |
| 151 contexts = GetContexts(create_properties); | 89 contexts = GetContexts(*create_properties.contexts); |
| 152 else | 90 else |
| 153 contexts.Add(MenuItem::PAGE); | 91 contexts.Add(MenuItem::PAGE); |
| 154 | 92 |
| 155 if (contexts.Contains(MenuItem::LAUNCHER)) { | 93 if (contexts.Contains(MenuItem::LAUNCHER)) { |
| 156 // Launcher item is not allowed for <webview>. | 94 // Launcher item is not allowed for <webview>. |
| 157 if (!extension->is_platform_app() || is_webview) { | 95 if (!extension->is_platform_app() || is_webview) { |
| 158 *error = kLauncherNotAllowedError; | 96 *error = kLauncherNotAllowedError; |
| 159 return false; | 97 return false; |
| 160 } | 98 } |
| 161 } | 99 } |
| 162 | 100 |
| 163 if (contexts.Contains(MenuItem::BROWSER_ACTION) || | 101 if (contexts.Contains(MenuItem::BROWSER_ACTION) || |
| 164 contexts.Contains(MenuItem::PAGE_ACTION)) { | 102 contexts.Contains(MenuItem::PAGE_ACTION)) { |
| 165 // Action items are not allowed for <webview>. | 103 // Action items are not allowed for <webview>. |
| 166 if (!extension->is_extension() || is_webview) { | 104 if (!extension->is_extension() || is_webview) { |
| 167 *error = kActionNotAllowedError; | 105 *error = kActionNotAllowedError; |
| 168 return false; | 106 return false; |
| 169 } | 107 } |
| 170 } | 108 } |
| 171 | 109 |
| 172 // Title. | 110 // Title. |
| 173 std::string title; | 111 std::string title; |
| 174 if (create_properties.title.get()) | 112 if (create_properties.title.get()) |
| 175 title = *create_properties.title; | 113 title = *create_properties.title; |
| 176 | 114 |
| 177 MenuItem::Type type = GetType(create_properties, MenuItem::NORMAL); | 115 MenuItem::Type type = GetType(create_properties.type, MenuItem::NORMAL); |
| 178 if (title.empty() && type != MenuItem::SEPARATOR) { | 116 if (title.empty() && type != MenuItem::SEPARATOR) { |
| 179 *error = kTitleNeededError; | 117 *error = kTitleNeededError; |
| 180 return false; | 118 return false; |
| 181 } | 119 } |
| 182 | 120 |
| 183 // Checked state. | 121 // Checked state. |
| 184 bool checked = false; | 122 bool checked = false; |
| 185 if (create_properties.checked.get()) | 123 if (create_properties.checked.get()) |
| 186 checked = *create_properties.checked; | 124 checked = *create_properties.checked; |
| 187 | 125 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 MenuManager* menu_manager = MenuManager::Get(profile); | 171 MenuManager* menu_manager = MenuManager::Get(profile); |
| 234 | 172 |
| 235 MenuItem* item = menu_manager->GetItemById(item_id); | 173 MenuItem* item = menu_manager->GetItemById(item_id); |
| 236 if (!item || item->extension_id() != extension->id()){ | 174 if (!item || item->extension_id() != extension->id()){ |
| 237 *error = ErrorUtils::FormatErrorMessage( | 175 *error = ErrorUtils::FormatErrorMessage( |
| 238 kCannotFindItemError, GetIDString(item_id)); | 176 kCannotFindItemError, GetIDString(item_id)); |
| 239 return false; | 177 return false; |
| 240 } | 178 } |
| 241 | 179 |
| 242 // Type. | 180 // Type. |
| 243 MenuItem::Type type = GetType(update_properties, item->type()); | 181 MenuItem::Type type = GetType(update_properties.type, item->type()); |
| 244 | 182 |
| 245 if (type != item->type()) { | 183 if (type != item->type()) { |
| 246 if (type == MenuItem::RADIO || item->type() == MenuItem::RADIO) | 184 if (type == MenuItem::RADIO || item->type() == MenuItem::RADIO) |
| 247 radio_item_updated = true; | 185 radio_item_updated = true; |
| 248 item->set_type(type); | 186 item->set_type(type); |
| 249 } | 187 } |
| 250 | 188 |
| 251 // Title. | 189 // Title. |
| 252 if (update_properties.title.get()) { | 190 if (update_properties.title.get()) { |
| 253 std::string title(*update_properties.title); | 191 std::string title(*update_properties.title); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 276 } | 214 } |
| 277 } | 215 } |
| 278 | 216 |
| 279 // Enabled. | 217 // Enabled. |
| 280 if (update_properties.enabled.get()) | 218 if (update_properties.enabled.get()) |
| 281 item->set_enabled(*update_properties.enabled); | 219 item->set_enabled(*update_properties.enabled); |
| 282 | 220 |
| 283 // Contexts. | 221 // Contexts. |
| 284 MenuItem::ContextList contexts; | 222 MenuItem::ContextList contexts; |
| 285 if (update_properties.contexts.get()) { | 223 if (update_properties.contexts.get()) { |
| 286 contexts = GetContexts(update_properties); | 224 contexts = GetContexts(*update_properties.contexts); |
| 287 | 225 |
| 288 if (contexts.Contains(MenuItem::LAUNCHER)) { | 226 if (contexts.Contains(MenuItem::LAUNCHER)) { |
| 289 // Launcher item is not allowed for <webview>. | 227 // Launcher item is not allowed for <webview>. |
| 290 if (!extension->is_platform_app() || is_webview) { | 228 if (!extension->is_platform_app() || is_webview) { |
| 291 *error = kLauncherNotAllowedError; | 229 *error = kLauncherNotAllowedError; |
| 292 return false; | 230 return false; |
| 293 } | 231 } |
| 294 } | 232 } |
| 295 | 233 |
| 296 if (contexts != item->contexts()) | 234 if (contexts != item->contexts()) |
| (...skipping 23 matching lines...) Expand all Loading... |
| 320 return false; | 258 return false; |
| 321 | 259 |
| 322 menu_manager->WriteToStorage(extension, item_id.extension_key); | 260 menu_manager->WriteToStorage(extension, item_id.extension_key); |
| 323 return true; | 261 return true; |
| 324 } | 262 } |
| 325 | 263 |
| 326 } // namespace context_menus_api_helpers | 264 } // namespace context_menus_api_helpers |
| 327 } // namespace extensions | 265 } // namespace extensions |
| 328 | 266 |
| 329 #endif // CHROME_BROWSER_EXTENSIONS_API_CONTEXT_MENUS_CONTEXT_MENUS_API_HELPERS
_H_ | 267 #endif // CHROME_BROWSER_EXTENSIONS_API_CONTEXT_MENUS_CONTEXT_MENUS_API_HELPERS
_H_ |
| OLD | NEW |