| 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 "chrome/common/extensions/api/context_menus.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 scoped_ptr<extensions::MenuItem::Id> GetParentId( | 22 std::unique_ptr<extensions::MenuItem::Id> GetParentId( |
| 23 const PropertyWithEnumT& property, | 23 const PropertyWithEnumT& property, |
| 24 bool is_off_the_record, | 24 bool is_off_the_record, |
| 25 const MenuItem::ExtensionKey& key) { | 25 const MenuItem::ExtensionKey& key) { |
| 26 if (!property.parent_id) | 26 if (!property.parent_id) |
| 27 return scoped_ptr<extensions::MenuItem::Id>(); | 27 return std::unique_ptr<extensions::MenuItem::Id>(); |
| 28 | 28 |
| 29 scoped_ptr<extensions::MenuItem::Id> parent_id( | 29 std::unique_ptr<extensions::MenuItem::Id> parent_id( |
| 30 new extensions::MenuItem::Id(is_off_the_record, key)); | 30 new extensions::MenuItem::Id(is_off_the_record, key)); |
| 31 if (property.parent_id->as_integer) | 31 if (property.parent_id->as_integer) |
| 32 parent_id->uid = *property.parent_id->as_integer; | 32 parent_id->uid = *property.parent_id->as_integer; |
| 33 else if (property.parent_id->as_string) | 33 else if (property.parent_id->as_string) |
| 34 parent_id->string_uid = *property.parent_id->as_string; | 34 parent_id->string_uid = *property.parent_id->as_string; |
| 35 else | 35 else |
| 36 NOTREACHED(); | 36 NOTREACHED(); |
| 37 return parent_id; | 37 return parent_id; |
| 38 } | 38 } |
| 39 | 39 |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 // Checked state. | 121 // Checked state. |
| 122 bool checked = false; | 122 bool checked = false; |
| 123 if (create_properties.checked.get()) | 123 if (create_properties.checked.get()) |
| 124 checked = *create_properties.checked; | 124 checked = *create_properties.checked; |
| 125 | 125 |
| 126 // Enabled. | 126 // Enabled. |
| 127 bool enabled = true; | 127 bool enabled = true; |
| 128 if (create_properties.enabled.get()) | 128 if (create_properties.enabled.get()) |
| 129 enabled = *create_properties.enabled; | 129 enabled = *create_properties.enabled; |
| 130 | 130 |
| 131 scoped_ptr<MenuItem> item( | 131 std::unique_ptr<MenuItem> item( |
| 132 new MenuItem(item_id, title, checked, enabled, type, contexts)); | 132 new MenuItem(item_id, title, checked, enabled, type, contexts)); |
| 133 | 133 |
| 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 scoped_ptr<MenuItem::Id> parent_id(GetParentId( | 144 std::unique_ptr<MenuItem::Id> parent_id(GetParentId( |
| 145 create_properties, profile->IsOffTheRecord(), item_id.extension_key)); | 145 create_properties, profile->IsOffTheRecord(), item_id.extension_key)); |
| 146 if (parent_id.get()) { | 146 if (parent_id.get()) { |
| 147 MenuItem* parent = GetParent(*parent_id, menu_manager, error); | 147 MenuItem* parent = GetParent(*parent_id, menu_manager, error); |
| 148 if (!parent) | 148 if (!parent) |
| 149 return false; | 149 return false; |
| 150 success = menu_manager->AddChildItem(parent->id(), item.release()); | 150 success = menu_manager->AddChildItem(parent->id(), item.release()); |
| 151 } else { | 151 } else { |
| 152 success = menu_manager->AddContextItem(extension, item.release()); | 152 success = menu_manager->AddContextItem(extension, item.release()); |
| 153 } | 153 } |
| 154 | 154 |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 return false; | 230 return false; |
| 231 } | 231 } |
| 232 } | 232 } |
| 233 | 233 |
| 234 if (contexts != item->contexts()) | 234 if (contexts != item->contexts()) |
| 235 item->set_contexts(contexts); | 235 item->set_contexts(contexts); |
| 236 } | 236 } |
| 237 | 237 |
| 238 // Parent id. | 238 // Parent id. |
| 239 MenuItem* parent = NULL; | 239 MenuItem* parent = NULL; |
| 240 scoped_ptr<MenuItem::Id> parent_id(GetParentId( | 240 std::unique_ptr<MenuItem::Id> parent_id(GetParentId( |
| 241 update_properties, profile->IsOffTheRecord(), item_id.extension_key)); | 241 update_properties, profile->IsOffTheRecord(), item_id.extension_key)); |
| 242 if (parent_id.get()) { | 242 if (parent_id.get()) { |
| 243 MenuItem* parent = GetParent(*parent_id, menu_manager, error); | 243 MenuItem* parent = GetParent(*parent_id, menu_manager, error); |
| 244 if (!parent || !menu_manager->ChangeParent(item->id(), &parent->id())) | 244 if (!parent || !menu_manager->ChangeParent(item->id(), &parent->id())) |
| 245 return false; | 245 return false; |
| 246 } | 246 } |
| 247 | 247 |
| 248 // URL Patterns. | 248 // URL Patterns. |
| 249 if (!item->PopulateURLPatterns( | 249 if (!item->PopulateURLPatterns( |
| 250 update_properties.document_url_patterns.get(), | 250 update_properties.document_url_patterns.get(), |
| 251 update_properties.target_url_patterns.get(), error)) { | 251 update_properties.target_url_patterns.get(), error)) { |
| 252 return false; | 252 return false; |
| 253 } | 253 } |
| 254 | 254 |
| 255 // There is no need to call ItemUpdated if ChangeParent is called because | 255 // There is no need to call ItemUpdated if ChangeParent is called because |
| 256 // all sanitation is taken care of in ChangeParent. | 256 // all sanitation is taken care of in ChangeParent. |
| 257 if (!parent && radio_item_updated && !menu_manager->ItemUpdated(item->id())) | 257 if (!parent && radio_item_updated && !menu_manager->ItemUpdated(item->id())) |
| 258 return false; | 258 return false; |
| 259 | 259 |
| 260 menu_manager->WriteToStorage(extension, item_id.extension_key); | 260 menu_manager->WriteToStorage(extension, item_id.extension_key); |
| 261 return true; | 261 return true; |
| 262 } | 262 } |
| 263 | 263 |
| 264 } // namespace context_menus_api_helpers | 264 } // namespace context_menus_api_helpers |
| 265 } // namespace extensions | 265 } // namespace extensions |
| 266 | 266 |
| 267 #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 |