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