| 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 "extensions/common/error_utils.h" | 12 #include "extensions/common/error_utils.h" |
| 13 #include "extensions/common/manifest_handlers/background_info.h" | 13 #include "extensions/common/manifest_handlers/background_info.h" |
| 14 | 14 |
| 15 namespace extensions { | 15 namespace extensions { |
| 16 namespace context_menus_api_helpers { | 16 namespace context_menus_api_helpers { |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 template<typename PropertyWithEnumT> | 20 template <typename PropertyWithEnumT> |
| 21 scoped_ptr<extensions::MenuItem::Id> GetParentId( | 21 scoped_ptr<extensions::MenuItem::Id> GetParentId( |
| 22 const PropertyWithEnumT& property, | 22 const PropertyWithEnumT& property, |
| 23 bool is_off_the_record, | 23 bool is_off_the_record, |
| 24 std::string extension_id) { | 24 const MenuItem::ExtensionKey& key) { |
| 25 if (!property.parent_id) | 25 if (!property.parent_id) |
| 26 return scoped_ptr<extensions::MenuItem::Id>(); | 26 return scoped_ptr<extensions::MenuItem::Id>(); |
| 27 | 27 |
| 28 scoped_ptr<extensions::MenuItem::Id> parent_id( | 28 scoped_ptr<extensions::MenuItem::Id> parent_id( |
| 29 new extensions::MenuItem::Id(is_off_the_record, extension_id)); | 29 new extensions::MenuItem::Id(is_off_the_record, key)); |
| 30 if (property.parent_id->as_integer) | 30 if (property.parent_id->as_integer) |
| 31 parent_id->uid = *property.parent_id->as_integer; | 31 parent_id->uid = *property.parent_id->as_integer; |
| 32 else if (property.parent_id->as_string) | 32 else if (property.parent_id->as_string) |
| 33 parent_id->string_uid = *property.parent_id->as_string; | 33 parent_id->string_uid = *property.parent_id->as_string; |
| 34 else | 34 else |
| 35 NOTREACHED(); | 35 NOTREACHED(); |
| 36 return parent_id.Pass(); | 36 return parent_id.Pass(); |
| 37 } | 37 } |
| 38 | 38 |
| 39 } // namespace | 39 } // namespace |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 case PropertyWithEnumT::CONTEXTS_TYPE_VIDEO: | 79 case PropertyWithEnumT::CONTEXTS_TYPE_VIDEO: |
| 80 contexts.Add(extensions::MenuItem::VIDEO); | 80 contexts.Add(extensions::MenuItem::VIDEO); |
| 81 break; | 81 break; |
| 82 case PropertyWithEnumT::CONTEXTS_TYPE_AUDIO: | 82 case PropertyWithEnumT::CONTEXTS_TYPE_AUDIO: |
| 83 contexts.Add(extensions::MenuItem::AUDIO); | 83 contexts.Add(extensions::MenuItem::AUDIO); |
| 84 break; | 84 break; |
| 85 case PropertyWithEnumT::CONTEXTS_TYPE_FRAME: | 85 case PropertyWithEnumT::CONTEXTS_TYPE_FRAME: |
| 86 contexts.Add(extensions::MenuItem::FRAME); | 86 contexts.Add(extensions::MenuItem::FRAME); |
| 87 break; | 87 break; |
| 88 case PropertyWithEnumT::CONTEXTS_TYPE_LAUNCHER: | 88 case PropertyWithEnumT::CONTEXTS_TYPE_LAUNCHER: |
| 89 // Not available for <webview>. |
| 89 contexts.Add(extensions::MenuItem::LAUNCHER); | 90 contexts.Add(extensions::MenuItem::LAUNCHER); |
| 90 break; | 91 break; |
| 91 case PropertyWithEnumT::CONTEXTS_TYPE_NONE: | 92 case PropertyWithEnumT::CONTEXTS_TYPE_NONE: |
| 92 NOTREACHED(); | 93 NOTREACHED(); |
| 93 } | 94 } |
| 94 } | 95 } |
| 95 return contexts; | 96 return contexts; |
| 96 } | 97 } |
| 97 | 98 |
| 98 template<typename PropertyWithEnumT> | 99 template<typename PropertyWithEnumT> |
| (...skipping 14 matching lines...) Expand all Loading... |
| 113 return extensions::MenuItem::NORMAL; | 114 return extensions::MenuItem::NORMAL; |
| 114 } | 115 } |
| 115 | 116 |
| 116 // Creates and adds a menu item from |create_properties|. | 117 // Creates and adds a menu item from |create_properties|. |
| 117 template<typename PropertyWithEnumT> | 118 template<typename PropertyWithEnumT> |
| 118 bool CreateMenuItem(const PropertyWithEnumT& create_properties, | 119 bool CreateMenuItem(const PropertyWithEnumT& create_properties, |
| 119 Profile* profile, | 120 Profile* profile, |
| 120 const Extension* extension, | 121 const Extension* extension, |
| 121 const MenuItem::Id& item_id, | 122 const MenuItem::Id& item_id, |
| 122 std::string* error) { | 123 std::string* error) { |
| 124 bool is_webview = item_id.extension_key.webview_instance_id != 0; |
| 123 MenuManager* menu_manager = MenuManager::Get(profile); | 125 MenuManager* menu_manager = MenuManager::Get(profile); |
| 124 | 126 |
| 125 if (menu_manager->GetItemById(item_id)) { | 127 if (menu_manager->GetItemById(item_id)) { |
| 126 *error = ErrorUtils::FormatErrorMessage(kDuplicateIDError, | 128 *error = ErrorUtils::FormatErrorMessage(kDuplicateIDError, |
| 127 GetIDString(item_id)); | 129 GetIDString(item_id)); |
| 128 return false; | 130 return false; |
| 129 } | 131 } |
| 130 | 132 |
| 131 if (BackgroundInfo::HasLazyBackgroundPage(extension) && | 133 if (!is_webview && BackgroundInfo::HasLazyBackgroundPage(extension) && |
| 132 create_properties.onclick.get()) { | 134 create_properties.onclick.get()) { |
| 133 *error = kOnclickDisallowedError; | 135 *error = kOnclickDisallowedError; |
| 134 return false; | 136 return false; |
| 135 } | 137 } |
| 136 | 138 |
| 137 // Contexts. | 139 // Contexts. |
| 138 MenuItem::ContextList contexts; | 140 MenuItem::ContextList contexts; |
| 139 if (create_properties.contexts.get()) | 141 if (create_properties.contexts.get()) |
| 140 contexts = GetContexts(create_properties); | 142 contexts = GetContexts(create_properties); |
| 141 else | 143 else |
| 142 contexts.Add(MenuItem::PAGE); | 144 contexts.Add(MenuItem::PAGE); |
| 143 | 145 |
| 144 if (contexts.Contains(MenuItem::LAUNCHER) && !extension->is_platform_app()) { | 146 if (contexts.Contains(MenuItem::LAUNCHER)) { |
| 145 *error = kLauncherNotAllowedError; | 147 // Launcher item is not allowed for <webview>. |
| 146 return false; | 148 if (!extension->is_platform_app() || is_webview) { |
| 149 *error = kLauncherNotAllowedError; |
| 150 return false; |
| 151 } |
| 147 } | 152 } |
| 148 | 153 |
| 149 // Title. | 154 // Title. |
| 150 std::string title; | 155 std::string title; |
| 151 if (create_properties.title.get()) | 156 if (create_properties.title.get()) |
| 152 title = *create_properties.title; | 157 title = *create_properties.title; |
| 153 | 158 |
| 154 MenuItem::Type type = GetType(create_properties, MenuItem::NORMAL); | 159 MenuItem::Type type = GetType(create_properties, MenuItem::NORMAL); |
| 155 if (title.empty() && type != MenuItem::SEPARATOR) { | 160 if (title.empty() && type != MenuItem::SEPARATOR) { |
| 156 *error = kTitleNeededError; | 161 *error = kTitleNeededError; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 173 // URL Patterns. | 178 // URL Patterns. |
| 174 if (!item->PopulateURLPatterns( | 179 if (!item->PopulateURLPatterns( |
| 175 create_properties.document_url_patterns.get(), | 180 create_properties.document_url_patterns.get(), |
| 176 create_properties.target_url_patterns.get(), | 181 create_properties.target_url_patterns.get(), |
| 177 error)) { | 182 error)) { |
| 178 return false; | 183 return false; |
| 179 } | 184 } |
| 180 | 185 |
| 181 // Parent id. | 186 // Parent id. |
| 182 bool success = true; | 187 bool success = true; |
| 183 scoped_ptr<MenuItem::Id> parent_id(GetParentId(create_properties, | 188 scoped_ptr<MenuItem::Id> parent_id(GetParentId( |
| 184 profile->IsOffTheRecord(), | 189 create_properties, profile->IsOffTheRecord(), item_id.extension_key)); |
| 185 extension->id())); | |
| 186 if (parent_id.get()) { | 190 if (parent_id.get()) { |
| 187 MenuItem* parent = GetParent(*parent_id, menu_manager, error); | 191 MenuItem* parent = GetParent(*parent_id, menu_manager, error); |
| 188 if (!parent) | 192 if (!parent) |
| 189 return false; | 193 return false; |
| 190 success = menu_manager->AddChildItem(parent->id(), item.release()); | 194 success = menu_manager->AddChildItem(parent->id(), item.release()); |
| 191 } else { | 195 } else { |
| 192 success = menu_manager->AddContextItem(extension, item.release()); | 196 success = menu_manager->AddContextItem(extension, item.release()); |
| 193 } | 197 } |
| 194 | 198 |
| 195 if (!success) | 199 if (!success) |
| 196 return false; | 200 return false; |
| 197 | 201 |
| 198 menu_manager->WriteToStorage(extension); | 202 menu_manager->WriteToStorage(extension, item_id.extension_key); |
| 199 return true; | 203 return true; |
| 200 } | 204 } |
| 201 | 205 |
| 202 // Updates a menu item from |update_properties|. | 206 // Updates a menu item from |update_properties|. |
| 203 template<typename PropertyWithEnumT> | 207 template<typename PropertyWithEnumT> |
| 204 bool UpdateMenuItem(const PropertyWithEnumT& update_properties, | 208 bool UpdateMenuItem(const PropertyWithEnumT& update_properties, |
| 205 Profile* profile, | 209 Profile* profile, |
| 206 const Extension* extension, | 210 const Extension* extension, |
| 207 const MenuItem::Id& item_id, | 211 const MenuItem::Id& item_id, |
| 208 std::string* error) { | 212 std::string* error) { |
| 209 bool radio_item_updated = false; | 213 bool radio_item_updated = false; |
| 214 bool is_webview = item_id.extension_key.webview_instance_id != 0; |
| 210 MenuManager* menu_manager = MenuManager::Get(profile); | 215 MenuManager* menu_manager = MenuManager::Get(profile); |
| 211 | 216 |
| 212 MenuItem* item = menu_manager->GetItemById(item_id); | 217 MenuItem* item = menu_manager->GetItemById(item_id); |
| 213 if (!item || item->extension_id() != extension->id()){ | 218 if (!item || item->extension_id() != extension->id()){ |
| 214 *error = ErrorUtils::FormatErrorMessage( | 219 *error = ErrorUtils::FormatErrorMessage( |
| 215 kCannotFindItemError, GetIDString(item_id)); | 220 kCannotFindItemError, GetIDString(item_id)); |
| 216 return false; | 221 return false; |
| 217 } | 222 } |
| 218 | 223 |
| 219 // Type. | 224 // Type. |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 | 260 |
| 256 // Enabled. | 261 // Enabled. |
| 257 if (update_properties.enabled.get()) | 262 if (update_properties.enabled.get()) |
| 258 item->set_enabled(*update_properties.enabled); | 263 item->set_enabled(*update_properties.enabled); |
| 259 | 264 |
| 260 // Contexts. | 265 // Contexts. |
| 261 MenuItem::ContextList contexts; | 266 MenuItem::ContextList contexts; |
| 262 if (update_properties.contexts.get()) { | 267 if (update_properties.contexts.get()) { |
| 263 contexts = GetContexts(update_properties); | 268 contexts = GetContexts(update_properties); |
| 264 | 269 |
| 265 if (contexts.Contains(MenuItem::LAUNCHER) && | 270 if (contexts.Contains(MenuItem::LAUNCHER)) { |
| 266 !extension->is_platform_app()) { | 271 // Launcher item is not allowed for <webview>. |
| 267 *error = kLauncherNotAllowedError; | 272 if (!extension->is_platform_app() || is_webview) { |
| 268 return false; | 273 *error = kLauncherNotAllowedError; |
| 274 return false; |
| 275 } |
| 269 } | 276 } |
| 270 | 277 |
| 271 if (contexts != item->contexts()) | 278 if (contexts != item->contexts()) |
| 272 item->set_contexts(contexts); | 279 item->set_contexts(contexts); |
| 273 } | 280 } |
| 274 | 281 |
| 275 // Parent id. | 282 // Parent id. |
| 276 MenuItem* parent = NULL; | 283 MenuItem* parent = NULL; |
| 277 scoped_ptr<MenuItem::Id> parent_id(GetParentId(update_properties, | 284 scoped_ptr<MenuItem::Id> parent_id(GetParentId( |
| 278 profile->IsOffTheRecord(), | 285 update_properties, profile->IsOffTheRecord(), item_id.extension_key)); |
| 279 extension->id())); | |
| 280 if (parent_id.get()) { | 286 if (parent_id.get()) { |
| 281 MenuItem* parent = GetParent(*parent_id, menu_manager, error); | 287 MenuItem* parent = GetParent(*parent_id, menu_manager, error); |
| 282 if (!parent || !menu_manager->ChangeParent(item->id(), &parent->id())) | 288 if (!parent || !menu_manager->ChangeParent(item->id(), &parent->id())) |
| 283 return false; | 289 return false; |
| 284 } | 290 } |
| 285 | 291 |
| 286 // URL Patterns. | 292 // URL Patterns. |
| 287 if (!item->PopulateURLPatterns( | 293 if (!item->PopulateURLPatterns( |
| 288 update_properties.document_url_patterns.get(), | 294 update_properties.document_url_patterns.get(), |
| 289 update_properties.target_url_patterns.get(), error)) { | 295 update_properties.target_url_patterns.get(), error)) { |
| 290 return false; | 296 return false; |
| 291 } | 297 } |
| 292 | 298 |
| 293 // There is no need to call ItemUpdated if ChangeParent is called because | 299 // There is no need to call ItemUpdated if ChangeParent is called because |
| 294 // all sanitation is taken care of in ChangeParent. | 300 // all sanitation is taken care of in ChangeParent. |
| 295 if (!parent && radio_item_updated && !menu_manager->ItemUpdated(item->id())) | 301 if (!parent && radio_item_updated && !menu_manager->ItemUpdated(item->id())) |
| 296 return false; | 302 return false; |
| 297 | 303 |
| 298 menu_manager->WriteToStorage(extension); | 304 menu_manager->WriteToStorage(extension, item_id.extension_key); |
| 299 return true; | 305 return true; |
| 300 } | 306 } |
| 301 | 307 |
| 302 } // namespace context_menus_api_helpers | 308 } // namespace context_menus_api_helpers |
| 303 } // namespace extensions | 309 } // namespace extensions |
| 304 | 310 |
| 305 #endif // CHROME_BROWSER_EXTENSIONS_API_CONTEXT_MENUS_CONTEXT_MENUS_API_HELPERS
_H_ | 311 #endif // CHROME_BROWSER_EXTENSIONS_API_CONTEXT_MENUS_CONTEXT_MENUS_API_HELPERS
_H_ |
| OLD | NEW |