Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(27)

Side by Side Diff: chrome/browser/extensions/menu_manager.cc

Issue 1871713002: Convert //chrome/browser/extensions from scoped_ptr to std::unique_ptr (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #include "chrome/browser/extensions/menu_manager.h" 5 #include "chrome/browser/extensions/menu_manager.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <tuple> 8 #include <tuple>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 continue; 82 continue;
83 MenuItem* item = MenuItem::Populate( 83 MenuItem* item = MenuItem::Populate(
84 extension_id, *dict, NULL); 84 extension_id, *dict, NULL);
85 if (!item) 85 if (!item)
86 continue; 86 continue;
87 items.push_back(item); 87 items.push_back(item);
88 } 88 }
89 return items; 89 return items;
90 } 90 }
91 91
92 scoped_ptr<base::Value> MenuItemsToValue(const MenuItem::List& items) { 92 std::unique_ptr<base::Value> MenuItemsToValue(const MenuItem::List& items) {
93 scoped_ptr<base::ListValue> list(new base::ListValue()); 93 std::unique_ptr<base::ListValue> list(new base::ListValue());
94 for (size_t i = 0; i < items.size(); ++i) 94 for (size_t i = 0; i < items.size(); ++i)
95 list->Append(items[i]->ToValue().release()); 95 list->Append(items[i]->ToValue().release());
96 return scoped_ptr<base::Value>(list.release()); 96 return std::unique_ptr<base::Value>(list.release());
97 } 97 }
98 98
99 bool GetStringList(const base::DictionaryValue& dict, 99 bool GetStringList(const base::DictionaryValue& dict,
100 const std::string& key, 100 const std::string& key,
101 std::vector<std::string>* out) { 101 std::vector<std::string>* out) {
102 if (!dict.HasKey(key)) 102 if (!dict.HasKey(key))
103 return true; 103 return true;
104 104
105 const base::ListValue* list = NULL; 105 const base::ListValue* list = NULL;
106 if (!dict.GetListWithoutPathExpansion(key, &list)) 106 if (!dict.GetListWithoutPathExpansion(key, &list))
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 return false; 188 return false;
189 checked_ = checked; 189 checked_ = checked;
190 return true; 190 return true;
191 } 191 }
192 192
193 void MenuItem::AddChild(MenuItem* item) { 193 void MenuItem::AddChild(MenuItem* item) {
194 item->parent_id_.reset(new Id(id_)); 194 item->parent_id_.reset(new Id(id_));
195 children_.push_back(item); 195 children_.push_back(item);
196 } 196 }
197 197
198 scoped_ptr<base::DictionaryValue> MenuItem::ToValue() const { 198 std::unique_ptr<base::DictionaryValue> MenuItem::ToValue() const {
199 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue); 199 std::unique_ptr<base::DictionaryValue> value(new base::DictionaryValue);
200 // Should only be called for extensions with event pages, which only have 200 // Should only be called for extensions with event pages, which only have
201 // string IDs for items. 201 // string IDs for items.
202 DCHECK_EQ(0, id_.uid); 202 DCHECK_EQ(0, id_.uid);
203 value->SetString(kStringUIDKey, id_.string_uid); 203 value->SetString(kStringUIDKey, id_.string_uid);
204 value->SetBoolean(kIncognitoKey, id_.incognito); 204 value->SetBoolean(kIncognitoKey, id_.incognito);
205 value->SetInteger(kTypeKey, type_); 205 value->SetInteger(kTypeKey, type_);
206 if (type_ != SEPARATOR) 206 if (type_ != SEPARATOR)
207 value->SetString(kTitleKey, title_); 207 value->SetString(kTitleKey, title_);
208 if (type_ == CHECKBOX || type_ == RADIO) 208 if (type_ == CHECKBOX || type_ == RADIO)
209 value->SetBoolean(kCheckedKey, checked_); 209 value->SetBoolean(kCheckedKey, checked_);
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
245 bool enabled = true; 245 bool enabled = true;
246 if (!value.GetBoolean(kEnabledKey, &enabled)) 246 if (!value.GetBoolean(kEnabledKey, &enabled))
247 return NULL; 247 return NULL;
248 ContextList contexts; 248 ContextList contexts;
249 const base::Value* contexts_value = NULL; 249 const base::Value* contexts_value = NULL;
250 if (!value.Get(kContextsKey, &contexts_value)) 250 if (!value.Get(kContextsKey, &contexts_value))
251 return NULL; 251 return NULL;
252 if (!contexts.Populate(*contexts_value)) 252 if (!contexts.Populate(*contexts_value))
253 return NULL; 253 return NULL;
254 254
255 scoped_ptr<MenuItem> result(new MenuItem( 255 std::unique_ptr<MenuItem> result(
256 id, title, checked, enabled, type, contexts)); 256 new MenuItem(id, title, checked, enabled, type, contexts));
257 257
258 std::vector<std::string> document_url_patterns; 258 std::vector<std::string> document_url_patterns;
259 if (!GetStringList(value, kDocumentURLPatternsKey, &document_url_patterns)) 259 if (!GetStringList(value, kDocumentURLPatternsKey, &document_url_patterns))
260 return NULL; 260 return NULL;
261 std::vector<std::string> target_url_patterns; 261 std::vector<std::string> target_url_patterns;
262 if (!GetStringList(value, kTargetURLPatternsKey, &target_url_patterns)) 262 if (!GetStringList(value, kTargetURLPatternsKey, &target_url_patterns))
263 return NULL; 263 return NULL;
264 264
265 if (!result->PopulateURLPatterns(&document_url_patterns, 265 if (!result->PopulateURLPatterns(&document_url_patterns,
266 &target_url_patterns, 266 &target_url_patterns,
267 error)) { 267 error)) {
268 return NULL; 268 return NULL;
269 } 269 }
270 270
271 // parent_id is filled in from the value, but it might not be valid. It's left 271 // parent_id is filled in from the value, but it might not be valid. It's left
272 // to be validated upon being added (via AddChildItem) to the menu manager. 272 // to be validated upon being added (via AddChildItem) to the menu manager.
273 scoped_ptr<Id> parent_id( 273 std::unique_ptr<Id> parent_id(
274 new Id(incognito, MenuItem::ExtensionKey(extension_id))); 274 new Id(incognito, MenuItem::ExtensionKey(extension_id)));
275 if (value.HasKey(kParentUIDKey)) { 275 if (value.HasKey(kParentUIDKey)) {
276 if (!value.GetString(kParentUIDKey, &parent_id->string_uid)) 276 if (!value.GetString(kParentUIDKey, &parent_id->string_uid))
277 return NULL; 277 return NULL;
278 result->parent_id_.swap(parent_id); 278 result->parent_id_.swap(parent_id);
279 } 279 }
280 return result.release(); 280 return result.release();
281 } 281 }
282 282
283 bool MenuItem::PopulateURLPatterns( 283 bool MenuItem::PopulateURLPatterns(
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after
629 if (!item) 629 if (!item)
630 return; 630 return;
631 631
632 ExtensionRegistry* registry = ExtensionRegistry::Get(browser_context_); 632 ExtensionRegistry* registry = ExtensionRegistry::Get(browser_context_);
633 const Extension* extension = 633 const Extension* extension =
634 registry->enabled_extensions().GetByID(item->extension_id()); 634 registry->enabled_extensions().GetByID(item->extension_id());
635 635
636 if (item->type() == MenuItem::RADIO) 636 if (item->type() == MenuItem::RADIO)
637 RadioItemSelected(item); 637 RadioItemSelected(item);
638 638
639 scoped_ptr<base::ListValue> args(new base::ListValue()); 639 std::unique_ptr<base::ListValue> args(new base::ListValue());
640 640
641 base::DictionaryValue* properties = new base::DictionaryValue(); 641 base::DictionaryValue* properties = new base::DictionaryValue();
642 SetIdKeyValue(properties, "menuItemId", item->id()); 642 SetIdKeyValue(properties, "menuItemId", item->id());
643 if (item->parent_id()) 643 if (item->parent_id())
644 SetIdKeyValue(properties, "parentMenuItemId", *item->parent_id()); 644 SetIdKeyValue(properties, "parentMenuItemId", *item->parent_id());
645 645
646 switch (params.media_type) { 646 switch (params.media_type) {
647 case blink::WebContextMenuData::MediaTypeImage: 647 case blink::WebContextMenuData::MediaTypeImage:
648 properties->SetString("mediaType", "image"); 648 properties->SetString("mediaType", "image");
649 break; 649 break;
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
712 // Note: web_contents are NULL in unit tests :( 712 // Note: web_contents are NULL in unit tests :(
713 if (web_contents && TabHelper::FromWebContents(web_contents)) { 713 if (web_contents && TabHelper::FromWebContents(web_contents)) {
714 TabHelper::FromWebContents(web_contents) 714 TabHelper::FromWebContents(web_contents)
715 ->active_tab_permission_granter() 715 ->active_tab_permission_granter()
716 ->GrantIfRequested(extension); 716 ->GrantIfRequested(extension);
717 } 717 }
718 718
719 { 719 {
720 // Dispatch to menu item's .onclick handler (this is the legacy API, from 720 // Dispatch to menu item's .onclick handler (this is the legacy API, from
721 // before chrome.contextMenus.onClicked existed). 721 // before chrome.contextMenus.onClicked existed).
722 scoped_ptr<Event> event( 722 std::unique_ptr<Event> event(
723 new Event(webview_guest ? events::WEB_VIEW_INTERNAL_CONTEXT_MENUS 723 new Event(webview_guest ? events::WEB_VIEW_INTERNAL_CONTEXT_MENUS
724 : events::CONTEXT_MENUS, 724 : events::CONTEXT_MENUS,
725 webview_guest ? kOnWebviewContextMenus : kOnContextMenus, 725 webview_guest ? kOnWebviewContextMenus : kOnContextMenus,
726 scoped_ptr<base::ListValue>(args->DeepCopy()))); 726 std::unique_ptr<base::ListValue>(args->DeepCopy())));
727 event->restrict_to_browser_context = context; 727 event->restrict_to_browser_context = context;
728 event->user_gesture = EventRouter::USER_GESTURE_ENABLED; 728 event->user_gesture = EventRouter::USER_GESTURE_ENABLED;
729 event_router->DispatchEventToExtension(item->extension_id(), 729 event_router->DispatchEventToExtension(item->extension_id(),
730 std::move(event)); 730 std::move(event));
731 } 731 }
732 { 732 {
733 // Dispatch to .contextMenus.onClicked handler. 733 // Dispatch to .contextMenus.onClicked handler.
734 scoped_ptr<Event> event(new Event( 734 std::unique_ptr<Event> event(new Event(
735 webview_guest ? events::CHROME_WEB_VIEW_INTERNAL_ON_CLICKED 735 webview_guest ? events::CHROME_WEB_VIEW_INTERNAL_ON_CLICKED
736 : events::CONTEXT_MENUS_ON_CLICKED, 736 : events::CONTEXT_MENUS_ON_CLICKED,
737 webview_guest ? api::chrome_web_view_internal::OnClicked::kEventName 737 webview_guest ? api::chrome_web_view_internal::OnClicked::kEventName
738 : api::context_menus::OnClicked::kEventName, 738 : api::context_menus::OnClicked::kEventName,
739 std::move(args))); 739 std::move(args)));
740 event->restrict_to_browser_context = context; 740 event->restrict_to_browser_context = context;
741 event->user_gesture = EventRouter::USER_GESTURE_ENABLED; 741 event->user_gesture = EventRouter::USER_GESTURE_ENABLED;
742 if (webview_guest) 742 if (webview_guest)
743 event->filter_info.SetInstanceID(webview_guest->view_instance_id()); 743 event->filter_info.SetInstanceID(webview_guest->view_instance_id());
744 event_router->DispatchEventToExtension(item->extension_id(), 744 event_router->DispatchEventToExtension(item->extension_id(),
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
819 } 819 }
820 } 820 }
821 821
822 if (store_) { 822 if (store_) {
823 store_->SetExtensionValue(extension->id(), kContextMenusKey, 823 store_->SetExtensionValue(extension->id(), kContextMenusKey,
824 MenuItemsToValue(all_items)); 824 MenuItemsToValue(all_items));
825 } 825 }
826 } 826 }
827 827
828 void MenuManager::ReadFromStorage(const std::string& extension_id, 828 void MenuManager::ReadFromStorage(const std::string& extension_id,
829 scoped_ptr<base::Value> value) { 829 std::unique_ptr<base::Value> value) {
830 const Extension* extension = ExtensionRegistry::Get(browser_context_) 830 const Extension* extension = ExtensionRegistry::Get(browser_context_)
831 ->enabled_extensions() 831 ->enabled_extensions()
832 .GetByID(extension_id); 832 .GetByID(extension_id);
833 if (!extension) 833 if (!extension)
834 return; 834 return;
835 835
836 MenuItem::List items = MenuItemsFromValue(extension_id, value.get()); 836 MenuItem::List items = MenuItemsFromValue(extension_id, value.get());
837 for (size_t i = 0; i < items.size(); ++i) { 837 for (size_t i = 0; i < items.size(); ++i) {
838 bool added = false; 838 bool added = false;
839 839
840 if (items[i]->parent_id()) { 840 if (items[i]->parent_id()) {
841 // Parent IDs are stored in the parent_id field for convenience, but 841 // Parent IDs are stored in the parent_id field for convenience, but
842 // they have not yet been validated. Separate them out here. 842 // they have not yet been validated. Separate them out here.
843 // Because of the order in which we store items in the prefs, parents will 843 // Because of the order in which we store items in the prefs, parents will
844 // precede children, so we should already know about any parent items. 844 // precede children, so we should already know about any parent items.
845 scoped_ptr<MenuItem::Id> parent_id; 845 std::unique_ptr<MenuItem::Id> parent_id;
846 parent_id.swap(items[i]->parent_id_); 846 parent_id.swap(items[i]->parent_id_);
847 added = AddChildItem(*parent_id, items[i]); 847 added = AddChildItem(*parent_id, items[i]);
848 } else { 848 } else {
849 added = AddContextItem(extension, items[i]); 849 added = AddContextItem(extension, items[i]);
850 } 850 }
851 851
852 if (!added) 852 if (!added)
853 delete items[i]; 853 delete items[i];
854 } 854 }
855 } 855 }
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
986 bool MenuItem::Id::operator!=(const Id& other) const { 986 bool MenuItem::Id::operator!=(const Id& other) const {
987 return !(*this == other); 987 return !(*this == other);
988 } 988 }
989 989
990 bool MenuItem::Id::operator<(const Id& other) const { 990 bool MenuItem::Id::operator<(const Id& other) const {
991 return std::tie(incognito, extension_key, uid, string_uid) < 991 return std::tie(incognito, extension_key, uid, string_uid) <
992 std::tie(other.incognito, other.extension_key, other.uid, other.string_uid); 992 std::tie(other.incognito, other.extension_key, other.uid, other.string_uid);
993 } 993 }
994 994
995 } // namespace extensions 995 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698