OLD | NEW |
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 <memory> |
8 #include <tuple> | 9 #include <tuple> |
9 #include <utility> | 10 #include <utility> |
10 | 11 |
11 #include "base/json/json_writer.h" | 12 #include "base/json/json_writer.h" |
12 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/memory/ptr_util.h" |
13 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
14 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
15 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
16 #include "base/values.h" | 18 #include "base/values.h" |
17 #include "chrome/browser/chrome_notification_types.h" | 19 #include "chrome/browser/chrome_notification_types.h" |
18 #include "chrome/browser/extensions/extension_tab_util.h" | 20 #include "chrome/browser/extensions/extension_tab_util.h" |
19 #include "chrome/browser/extensions/menu_manager_factory.h" | 21 #include "chrome/browser/extensions/menu_manager_factory.h" |
20 #include "chrome/browser/extensions/tab_helper.h" | 22 #include "chrome/browser/extensions/tab_helper.h" |
21 #include "chrome/browser/profiles/profile.h" | 23 #include "chrome/browser/profiles/profile.h" |
22 #include "chrome/common/extensions/api/chrome_web_view_internal.h" | 24 #include "chrome/common/extensions/api/chrome_web_view_internal.h" |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 continue; | 84 continue; |
83 MenuItem* item = MenuItem::Populate( | 85 MenuItem* item = MenuItem::Populate( |
84 extension_id, *dict, NULL); | 86 extension_id, *dict, NULL); |
85 if (!item) | 87 if (!item) |
86 continue; | 88 continue; |
87 items.push_back(item); | 89 items.push_back(item); |
88 } | 90 } |
89 return items; | 91 return items; |
90 } | 92 } |
91 | 93 |
92 std::unique_ptr<base::Value> MenuItemsToValue(const MenuItem::List& items) { | 94 std::unique_ptr<base::ListValue> MenuItemsToValue(const MenuItem::List& items) { |
93 std::unique_ptr<base::ListValue> list(new base::ListValue()); | 95 std::unique_ptr<base::ListValue> list(new base::ListValue()); |
94 for (size_t i = 0; i < items.size(); ++i) | 96 for (size_t i = 0; i < items.size(); ++i) |
95 list->Append(items[i]->ToValue()); | 97 list->Append(items[i]->ToValue()); |
96 return std::unique_ptr<base::Value>(list.release()); | 98 return list; |
97 } | 99 } |
98 | 100 |
99 bool GetStringList(const base::DictionaryValue& dict, | 101 bool GetStringList(const base::DictionaryValue& dict, |
100 const std::string& key, | 102 const std::string& key, |
101 std::vector<std::string>* out) { | 103 std::vector<std::string>* out) { |
102 if (!dict.HasKey(key)) | 104 if (!dict.HasKey(key)) |
103 return true; | 105 return true; |
104 | 106 |
105 const base::ListValue* list = NULL; | 107 const base::ListValue* list = NULL; |
106 if (!dict.GetListWithoutPathExpansion(key, &list)) | 108 if (!dict.GetListWithoutPathExpansion(key, &list)) |
(...skipping 524 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
631 | 633 |
632 ExtensionRegistry* registry = ExtensionRegistry::Get(browser_context_); | 634 ExtensionRegistry* registry = ExtensionRegistry::Get(browser_context_); |
633 const Extension* extension = | 635 const Extension* extension = |
634 registry->enabled_extensions().GetByID(item->extension_id()); | 636 registry->enabled_extensions().GetByID(item->extension_id()); |
635 | 637 |
636 if (item->type() == MenuItem::RADIO) | 638 if (item->type() == MenuItem::RADIO) |
637 RadioItemSelected(item); | 639 RadioItemSelected(item); |
638 | 640 |
639 std::unique_ptr<base::ListValue> args(new base::ListValue()); | 641 std::unique_ptr<base::ListValue> args(new base::ListValue()); |
640 | 642 |
641 base::DictionaryValue* properties = new base::DictionaryValue(); | 643 std::unique_ptr<base::DictionaryValue> properties( |
642 SetIdKeyValue(properties, "menuItemId", item->id()); | 644 new base::DictionaryValue()); |
| 645 SetIdKeyValue(properties.get(), "menuItemId", item->id()); |
643 if (item->parent_id()) | 646 if (item->parent_id()) |
644 SetIdKeyValue(properties, "parentMenuItemId", *item->parent_id()); | 647 SetIdKeyValue(properties.get(), "parentMenuItemId", *item->parent_id()); |
645 | 648 |
646 switch (params.media_type) { | 649 switch (params.media_type) { |
647 case blink::WebContextMenuData::MediaTypeImage: | 650 case blink::WebContextMenuData::MediaTypeImage: |
648 properties->SetString("mediaType", "image"); | 651 properties->SetString("mediaType", "image"); |
649 break; | 652 break; |
650 case blink::WebContextMenuData::MediaTypeVideo: | 653 case blink::WebContextMenuData::MediaTypeVideo: |
651 properties->SetString("mediaType", "video"); | 654 properties->SetString("mediaType", "video"); |
652 break; | 655 break; |
653 case blink::WebContextMenuData::MediaTypeAudio: | 656 case blink::WebContextMenuData::MediaTypeAudio: |
654 properties->SetString("mediaType", "audio"); | 657 properties->SetString("mediaType", "audio"); |
655 break; | 658 break; |
656 default: {} // Do nothing. | 659 default: {} // Do nothing. |
657 } | 660 } |
658 | 661 |
659 AddURLProperty(properties, "linkUrl", params.unfiltered_link_url); | 662 AddURLProperty(properties.get(), "linkUrl", params.unfiltered_link_url); |
660 AddURLProperty(properties, "srcUrl", params.src_url); | 663 AddURLProperty(properties.get(), "srcUrl", params.src_url); |
661 AddURLProperty(properties, "pageUrl", params.page_url); | 664 AddURLProperty(properties.get(), "pageUrl", params.page_url); |
662 AddURLProperty(properties, "frameUrl", params.frame_url); | 665 AddURLProperty(properties.get(), "frameUrl", params.frame_url); |
663 | 666 |
664 if (params.selection_text.length() > 0) | 667 if (params.selection_text.length() > 0) |
665 properties->SetString("selectionText", params.selection_text); | 668 properties->SetString("selectionText", params.selection_text); |
666 | 669 |
667 properties->SetBoolean("editable", params.is_editable); | 670 properties->SetBoolean("editable", params.is_editable); |
668 | 671 |
669 WebViewGuest* webview_guest = WebViewGuest::FromWebContents(web_contents); | 672 WebViewGuest* webview_guest = WebViewGuest::FromWebContents(web_contents); |
670 if (webview_guest) { | 673 if (webview_guest) { |
671 // This is used in web_view_internalcustom_bindings.js. | 674 // This is used in web_view_internalcustom_bindings.js. |
672 // The property is not exposed to developer API. | 675 // The property is not exposed to developer API. |
673 properties->SetInteger("webviewInstanceId", | 676 properties->SetInteger("webviewInstanceId", |
674 webview_guest->view_instance_id()); | 677 webview_guest->view_instance_id()); |
675 } | 678 } |
676 | 679 |
677 args->Append(properties); | 680 base::DictionaryValue* raw_properties = properties.get(); |
| 681 args->Append(std::move(properties)); |
678 | 682 |
679 // Add the tab info to the argument list. | 683 // Add the tab info to the argument list. |
680 // No tab info in a platform app. | 684 // No tab info in a platform app. |
681 if (!extension || !extension->is_platform_app()) { | 685 if (!extension || !extension->is_platform_app()) { |
682 // Note: web_contents are NULL in unit tests :( | 686 // Note: web_contents are NULL in unit tests :( |
683 if (web_contents) { | 687 if (web_contents) { |
684 int frame_id = ExtensionApiFrameIdMap::GetFrameId(render_frame_host); | 688 int frame_id = ExtensionApiFrameIdMap::GetFrameId(render_frame_host); |
685 if (frame_id != ExtensionApiFrameIdMap::kInvalidFrameId) | 689 if (frame_id != ExtensionApiFrameIdMap::kInvalidFrameId) |
686 properties->SetInteger("frameId", frame_id); | 690 raw_properties->SetInteger("frameId", frame_id); |
687 | 691 |
688 args->Append(ExtensionTabUtil::CreateTabObject(web_contents)->ToValue()); | 692 args->Append(ExtensionTabUtil::CreateTabObject(web_contents)->ToValue()); |
689 } else { | 693 } else { |
690 args->Append(new base::DictionaryValue()); | 694 args->Append(base::MakeUnique<base::DictionaryValue>()); |
691 } | 695 } |
692 } | 696 } |
693 | 697 |
694 if (item->type() == MenuItem::CHECKBOX || | 698 if (item->type() == MenuItem::CHECKBOX || |
695 item->type() == MenuItem::RADIO) { | 699 item->type() == MenuItem::RADIO) { |
696 bool was_checked = item->checked(); | 700 bool was_checked = item->checked(); |
697 properties->SetBoolean("wasChecked", was_checked); | 701 raw_properties->SetBoolean("wasChecked", was_checked); |
698 | 702 |
699 // RADIO items always get set to true when you click on them, but CHECKBOX | 703 // RADIO items always get set to true when you click on them, but CHECKBOX |
700 // items get their state toggled. | 704 // items get their state toggled. |
701 bool checked = | 705 bool checked = |
702 (item->type() == MenuItem::RADIO) ? true : !was_checked; | 706 (item->type() == MenuItem::RADIO) ? true : !was_checked; |
703 | 707 |
704 item->SetChecked(checked); | 708 item->SetChecked(checked); |
705 properties->SetBoolean("checked", item->checked()); | 709 raw_properties->SetBoolean("checked", item->checked()); |
706 | 710 |
707 if (extension) | 711 if (extension) |
708 WriteToStorage(extension, item->id().extension_key); | 712 WriteToStorage(extension, item->id().extension_key); |
709 } | 713 } |
710 | 714 |
711 // Note: web_contents are NULL in unit tests :( | 715 // Note: web_contents are NULL in unit tests :( |
712 if (web_contents && TabHelper::FromWebContents(web_contents)) { | 716 if (web_contents && TabHelper::FromWebContents(web_contents)) { |
713 TabHelper::FromWebContents(web_contents) | 717 TabHelper::FromWebContents(web_contents) |
714 ->active_tab_permission_granter() | 718 ->active_tab_permission_granter() |
715 ->GrantIfRequested(extension); | 719 ->GrantIfRequested(extension); |
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
985 bool MenuItem::Id::operator!=(const Id& other) const { | 989 bool MenuItem::Id::operator!=(const Id& other) const { |
986 return !(*this == other); | 990 return !(*this == other); |
987 } | 991 } |
988 | 992 |
989 bool MenuItem::Id::operator<(const Id& other) const { | 993 bool MenuItem::Id::operator<(const Id& other) const { |
990 return std::tie(incognito, extension_key, uid, string_uid) < | 994 return std::tie(incognito, extension_key, uid, string_uid) < |
991 std::tie(other.incognito, other.extension_key, other.uid, other.string_uid); | 995 std::tie(other.incognito, other.extension_key, other.uid, other.string_uid); |
992 } | 996 } |
993 | 997 |
994 } // namespace extensions | 998 } // namespace extensions |
OLD | NEW |