| 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" | |
| 12 #include "chrome/common/extensions/api/context_menus.h" | 11 #include "chrome/common/extensions/api/context_menus.h" |
| 12 #include "content/public/browser/browser_context.h" |
| 13 #include "extensions/common/error_utils.h" | 13 #include "extensions/common/error_utils.h" |
| 14 #include "extensions/common/manifest_handlers/background_info.h" | 14 #include "extensions/common/manifest_handlers/background_info.h" |
| 15 | 15 |
| 16 namespace extensions { | 16 namespace extensions { |
| 17 namespace context_menus_api_helpers { | 17 namespace context_menus_api_helpers { |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 template <typename PropertyWithEnumT> | 21 template <typename PropertyWithEnumT> |
| 22 std::unique_ptr<extensions::MenuItem::Id> GetParentId( | 22 std::unique_ptr<extensions::MenuItem::Id> GetParentId( |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 const MenuManager* menu_manager, | 55 const MenuManager* menu_manager, |
| 56 std::string* error); | 56 std::string* error); |
| 57 | 57 |
| 58 MenuItem::ContextList GetContexts(const std::vector< | 58 MenuItem::ContextList GetContexts(const std::vector< |
| 59 extensions::api::context_menus::ContextType>& in_contexts); | 59 extensions::api::context_menus::ContextType>& in_contexts); |
| 60 | 60 |
| 61 MenuItem::Type GetType(extensions::api::context_menus::ItemType type, | 61 MenuItem::Type GetType(extensions::api::context_menus::ItemType type, |
| 62 MenuItem::Type default_type); | 62 MenuItem::Type default_type); |
| 63 | 63 |
| 64 // Creates and adds a menu item from |create_properties|. | 64 // Creates and adds a menu item from |create_properties|. |
| 65 template<typename PropertyWithEnumT> | 65 template <typename PropertyWithEnumT> |
| 66 bool CreateMenuItem(const PropertyWithEnumT& create_properties, | 66 bool CreateMenuItem(const PropertyWithEnumT& create_properties, |
| 67 Profile* profile, | 67 content::BrowserContext* browser_context, |
| 68 const Extension* extension, | 68 const Extension* extension, |
| 69 const MenuItem::Id& item_id, | 69 const MenuItem::Id& item_id, |
| 70 std::string* error) { | 70 std::string* error) { |
| 71 bool is_webview = item_id.extension_key.webview_instance_id != 0; | 71 bool is_webview = item_id.extension_key.webview_instance_id != 0; |
| 72 MenuManager* menu_manager = MenuManager::Get(profile); | 72 MenuManager* menu_manager = MenuManager::Get(browser_context); |
| 73 | 73 |
| 74 if (menu_manager->GetItemById(item_id)) { | 74 if (menu_manager->GetItemById(item_id)) { |
| 75 *error = ErrorUtils::FormatErrorMessage(kDuplicateIDError, | 75 *error = ErrorUtils::FormatErrorMessage(kDuplicateIDError, |
| 76 GetIDString(item_id)); | 76 GetIDString(item_id)); |
| 77 return false; | 77 return false; |
| 78 } | 78 } |
| 79 | 79 |
| 80 if (!is_webview && BackgroundInfo::HasLazyBackgroundPage(extension) && | 80 if (!is_webview && BackgroundInfo::HasLazyBackgroundPage(extension) && |
| 81 create_properties.onclick.get()) { | 81 create_properties.onclick.get()) { |
| 82 *error = kOnclickDisallowedError; | 82 *error = kOnclickDisallowedError; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 // URL Patterns. | 134 // URL Patterns. |
| 135 if (!item->PopulateURLPatterns( | 135 if (!item->PopulateURLPatterns( |
| 136 create_properties.document_url_patterns.get(), | 136 create_properties.document_url_patterns.get(), |
| 137 create_properties.target_url_patterns.get(), | 137 create_properties.target_url_patterns.get(), |
| 138 error)) { | 138 error)) { |
| 139 return false; | 139 return false; |
| 140 } | 140 } |
| 141 | 141 |
| 142 // Parent id. | 142 // Parent id. |
| 143 bool success = true; | 143 bool success = true; |
| 144 std::unique_ptr<MenuItem::Id> parent_id(GetParentId( | 144 std::unique_ptr<MenuItem::Id> parent_id( |
| 145 create_properties, profile->IsOffTheRecord(), item_id.extension_key)); | 145 GetParentId(create_properties, browser_context->IsOffTheRecord(), |
| 146 item_id.extension_key)); |
| 146 if (parent_id.get()) { | 147 if (parent_id.get()) { |
| 147 MenuItem* parent = GetParent(*parent_id, menu_manager, error); | 148 MenuItem* parent = GetParent(*parent_id, menu_manager, error); |
| 148 if (!parent) | 149 if (!parent) |
| 149 return false; | 150 return false; |
| 150 success = menu_manager->AddChildItem(parent->id(), item.release()); | 151 success = menu_manager->AddChildItem(parent->id(), item.release()); |
| 151 } else { | 152 } else { |
| 152 success = menu_manager->AddContextItem(extension, item.release()); | 153 success = menu_manager->AddContextItem(extension, item.release()); |
| 153 } | 154 } |
| 154 | 155 |
| 155 if (!success) | 156 if (!success) |
| 156 return false; | 157 return false; |
| 157 | 158 |
| 158 menu_manager->WriteToStorage(extension, item_id.extension_key); | 159 menu_manager->WriteToStorage(extension, item_id.extension_key); |
| 159 return true; | 160 return true; |
| 160 } | 161 } |
| 161 | 162 |
| 162 // Updates a menu item from |update_properties|. | 163 // Updates a menu item from |update_properties|. |
| 163 template<typename PropertyWithEnumT> | 164 template <typename PropertyWithEnumT> |
| 164 bool UpdateMenuItem(const PropertyWithEnumT& update_properties, | 165 bool UpdateMenuItem(const PropertyWithEnumT& update_properties, |
| 165 Profile* profile, | 166 content::BrowserContext* browser_context, |
| 166 const Extension* extension, | 167 const Extension* extension, |
| 167 const MenuItem::Id& item_id, | 168 const MenuItem::Id& item_id, |
| 168 std::string* error) { | 169 std::string* error) { |
| 169 bool radio_item_updated = false; | 170 bool radio_item_updated = false; |
| 170 bool is_webview = item_id.extension_key.webview_instance_id != 0; | 171 bool is_webview = item_id.extension_key.webview_instance_id != 0; |
| 171 MenuManager* menu_manager = MenuManager::Get(profile); | 172 MenuManager* menu_manager = MenuManager::Get(browser_context); |
| 172 | 173 |
| 173 MenuItem* item = menu_manager->GetItemById(item_id); | 174 MenuItem* item = menu_manager->GetItemById(item_id); |
| 174 if (!item || item->extension_id() != extension->id()){ | 175 if (!item || item->extension_id() != extension->id()){ |
| 175 *error = ErrorUtils::FormatErrorMessage( | 176 *error = ErrorUtils::FormatErrorMessage( |
| 176 kCannotFindItemError, GetIDString(item_id)); | 177 kCannotFindItemError, GetIDString(item_id)); |
| 177 return false; | 178 return false; |
| 178 } | 179 } |
| 179 | 180 |
| 180 // Type. | 181 // Type. |
| 181 MenuItem::Type type = GetType(update_properties.type, item->type()); | 182 MenuItem::Type type = GetType(update_properties.type, item->type()); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 return false; | 231 return false; |
| 231 } | 232 } |
| 232 } | 233 } |
| 233 | 234 |
| 234 if (contexts != item->contexts()) | 235 if (contexts != item->contexts()) |
| 235 item->set_contexts(contexts); | 236 item->set_contexts(contexts); |
| 236 } | 237 } |
| 237 | 238 |
| 238 // Parent id. | 239 // Parent id. |
| 239 MenuItem* parent = NULL; | 240 MenuItem* parent = NULL; |
| 240 std::unique_ptr<MenuItem::Id> parent_id(GetParentId( | 241 std::unique_ptr<MenuItem::Id> parent_id( |
| 241 update_properties, profile->IsOffTheRecord(), item_id.extension_key)); | 242 GetParentId(update_properties, browser_context->IsOffTheRecord(), |
| 243 item_id.extension_key)); |
| 242 if (parent_id.get()) { | 244 if (parent_id.get()) { |
| 243 MenuItem* parent = GetParent(*parent_id, menu_manager, error); | 245 MenuItem* parent = GetParent(*parent_id, menu_manager, error); |
| 244 if (!parent || !menu_manager->ChangeParent(item->id(), &parent->id())) | 246 if (!parent || !menu_manager->ChangeParent(item->id(), &parent->id())) |
| 245 return false; | 247 return false; |
| 246 } | 248 } |
| 247 | 249 |
| 248 // URL Patterns. | 250 // URL Patterns. |
| 249 if (!item->PopulateURLPatterns( | 251 if (!item->PopulateURLPatterns( |
| 250 update_properties.document_url_patterns.get(), | 252 update_properties.document_url_patterns.get(), |
| 251 update_properties.target_url_patterns.get(), error)) { | 253 update_properties.target_url_patterns.get(), error)) { |
| 252 return false; | 254 return false; |
| 253 } | 255 } |
| 254 | 256 |
| 255 // There is no need to call ItemUpdated if ChangeParent is called because | 257 // There is no need to call ItemUpdated if ChangeParent is called because |
| 256 // all sanitation is taken care of in ChangeParent. | 258 // all sanitation is taken care of in ChangeParent. |
| 257 if (!parent && radio_item_updated && !menu_manager->ItemUpdated(item->id())) | 259 if (!parent && radio_item_updated && !menu_manager->ItemUpdated(item->id())) |
| 258 return false; | 260 return false; |
| 259 | 261 |
| 260 menu_manager->WriteToStorage(extension, item_id.extension_key); | 262 menu_manager->WriteToStorage(extension, item_id.extension_key); |
| 261 return true; | 263 return true; |
| 262 } | 264 } |
| 263 | 265 |
| 264 } // namespace context_menus_api_helpers | 266 } // namespace context_menus_api_helpers |
| 265 } // namespace extensions | 267 } // namespace extensions |
| 266 | 268 |
| 267 #endif // CHROME_BROWSER_EXTENSIONS_API_CONTEXT_MENUS_CONTEXT_MENUS_API_HELPERS
_H_ | 269 #endif // CHROME_BROWSER_EXTENSIONS_API_CONTEXT_MENUS_CONTEXT_MENUS_API_HELPERS
_H_ |
| OLD | NEW |