Chromium Code Reviews| 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/api/management/management_api.h" | 5 #include "chrome/browser/extensions/api/management/management_api.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/json/json_writer.h" | 13 #include "base/json/json_writer.h" |
| 14 #include "base/lazy_instance.h" | 14 #include "base/lazy_instance.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/memory/linked_ptr.h" | 16 #include "base/memory/linked_ptr.h" |
| 17 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
| 18 #include "base/metrics/histogram.h" | 18 #include "base/metrics/histogram.h" |
| 19 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
| 20 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
| 21 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
| 22 #include "chrome/browser/chrome_notification_types.h" | 22 #include "chrome/browser/chrome_notification_types.h" |
| 23 #include "chrome/browser/extensions/api/management/management_api_constants.h" | 23 #include "chrome/browser/extensions/api/management/management_api_constants.h" |
| 24 #include "chrome/browser/extensions/bookmark_app_helper.h" | |
| 25 #include "chrome/browser/extensions/crx_installer.h" | |
| 24 #include "chrome/browser/extensions/extension_service.h" | 26 #include "chrome/browser/extensions/extension_service.h" |
| 25 #include "chrome/browser/extensions/extension_uninstall_dialog.h" | 27 #include "chrome/browser/extensions/extension_uninstall_dialog.h" |
| 26 #include "chrome/browser/extensions/launch_util.h" | 28 #include "chrome/browser/extensions/launch_util.h" |
| 29 #include "chrome/browser/favicon/favicon_service_factory.h" | |
| 27 #include "chrome/browser/profiles/profile.h" | 30 #include "chrome/browser/profiles/profile.h" |
| 28 #include "chrome/browser/ui/browser_dialogs.h" | 31 #include "chrome/browser/ui/browser_dialogs.h" |
| 29 #include "chrome/browser/ui/browser_finder.h" | 32 #include "chrome/browser/ui/browser_finder.h" |
| 30 #include "chrome/browser/ui/browser_window.h" | 33 #include "chrome/browser/ui/browser_window.h" |
| 31 #include "chrome/browser/ui/extensions/application_launch.h" | 34 #include "chrome/browser/ui/extensions/application_launch.h" |
| 32 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h" | 35 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h" |
| 33 #include "chrome/common/chrome_utility_messages.h" | 36 #include "chrome/common/chrome_utility_messages.h" |
| 34 #include "chrome/common/extensions/api/management.h" | 37 #include "chrome/common/extensions/api/management.h" |
| 35 #include "chrome/common/extensions/extension_constants.h" | 38 #include "chrome/common/extensions/extension_constants.h" |
| 36 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h" | 39 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h" |
| 37 #include "chrome/common/extensions/manifest_url_handler.h" | 40 #include "chrome/common/extensions/manifest_url_handler.h" |
| 41 #include "chrome/common/web_application_info.h" | |
| 38 #include "content/public/browser/notification_details.h" | 42 #include "content/public/browser/notification_details.h" |
| 39 #include "content/public/browser/notification_source.h" | 43 #include "content/public/browser/notification_source.h" |
| 40 #include "content/public/browser/utility_process_host.h" | 44 #include "content/public/browser/utility_process_host.h" |
| 41 #include "content/public/browser/utility_process_host_client.h" | 45 #include "content/public/browser/utility_process_host_client.h" |
| 42 #include "extensions/browser/event_router.h" | 46 #include "extensions/browser/event_router.h" |
| 43 #include "extensions/browser/extension_registry.h" | 47 #include "extensions/browser/extension_registry.h" |
| 44 #include "extensions/browser/extension_system.h" | 48 #include "extensions/browser/extension_system.h" |
| 45 #include "extensions/browser/management_policy.h" | 49 #include "extensions/browser/management_policy.h" |
| 46 #include "extensions/common/constants.h" | 50 #include "extensions/common/constants.h" |
| 47 #include "extensions/common/error_utils.h" | 51 #include "extensions/common/error_utils.h" |
| 48 #include "extensions/common/extension.h" | 52 #include "extensions/common/extension.h" |
| 49 #include "extensions/common/extension_icon_set.h" | 53 #include "extensions/common/extension_icon_set.h" |
| 54 #include "extensions/common/manifest_constants.h" | |
| 50 #include "extensions/common/manifest_handlers/icons_handler.h" | 55 #include "extensions/common/manifest_handlers/icons_handler.h" |
| 51 #include "extensions/common/manifest_handlers/offline_enabled_info.h" | 56 #include "extensions/common/manifest_handlers/offline_enabled_info.h" |
| 52 #include "extensions/common/permissions/permission_set.h" | 57 #include "extensions/common/permissions/permission_set.h" |
| 53 #include "extensions/common/permissions/permissions_data.h" | 58 #include "extensions/common/permissions/permissions_data.h" |
| 54 #include "extensions/common/url_pattern.h" | 59 #include "extensions/common/url_pattern.h" |
| 60 #include "ui/gfx/favicon_size.h" | |
| 55 | 61 |
| 56 #if !defined(OS_ANDROID) | 62 #if !defined(OS_ANDROID) |
| 57 #include "chrome/browser/ui/webui/ntp/core_app_launcher_handler.h" | 63 #include "chrome/browser/ui/webui/ntp/core_app_launcher_handler.h" |
| 58 #endif | 64 #endif |
| 59 | 65 |
| 60 using base::IntToString; | 66 using base::IntToString; |
| 61 using content::BrowserThread; | 67 using content::BrowserThread; |
| 62 using content::UtilityProcessHost; | 68 using content::UtilityProcessHost; |
| 63 using content::UtilityProcessHostClient; | 69 using content::UtilityProcessHostClient; |
| 64 | 70 |
| (...skipping 659 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 724 base::Bind(&ManagementCreateAppShortcutFunction::OnCloseShortcutPrompt, | 730 base::Bind(&ManagementCreateAppShortcutFunction::OnCloseShortcutPrompt, |
| 725 this)); | 731 this)); |
| 726 } else { | 732 } else { |
| 727 OnCloseShortcutPrompt(auto_confirm_for_test == PROCEED); | 733 OnCloseShortcutPrompt(auto_confirm_for_test == PROCEED); |
| 728 } | 734 } |
| 729 | 735 |
| 730 // Response is sent async in OnCloseShortcutPrompt(). | 736 // Response is sent async in OnCloseShortcutPrompt(). |
| 731 return true; | 737 return true; |
| 732 } | 738 } |
| 733 | 739 |
| 740 ManagementGenerateAppForLinkFunction::ManagementGenerateAppForLinkFunction() { | |
| 741 } | |
| 742 | |
| 743 ManagementGenerateAppForLinkFunction::~ManagementGenerateAppForLinkFunction() { | |
| 744 } | |
| 745 | |
| 746 void ManagementGenerateAppForLinkFunction::Observe( | |
| 747 int type, | |
| 748 const content::NotificationSource& source, | |
| 749 const content::NotificationDetails& details) { | |
| 750 switch (type) { | |
| 751 case chrome::NOTIFICATION_CRX_INSTALLER_DONE: { | |
|
benwells
2014/05/22 02:49:25
Could we push all this code into BookmarkAppHelper
wjywbs
2014/05/22 03:21:35
BookmarkAppHelper needs a content::WebContents, an
benwells
2014/05/25 23:59:43
You would need to make it so BookmarkAppHelper can
wjywbs
2014/05/26 03:22:24
I looked at BookmarkAppHelper again and merging th
| |
| 752 const Extension* extension = | |
| 753 content::Details<const Extension>(details).ptr(); | |
| 754 if (extension->from_bookmark() && | |
| 755 extension->name() == install_info_->title && | |
| 756 AppLaunchInfo::GetLaunchWebURL(extension) == | |
| 757 install_info_->launch_url) { | |
| 758 scoped_ptr<management::ExtensionInfo> info = | |
| 759 CreateExtensionInfo(*extension, ExtensionSystem::Get(GetProfile())); | |
| 760 results_ = management::GenerateAppForLink::Results::Create(*info); | |
| 761 | |
| 762 SendResponse(true); | |
| 763 Release(); | |
| 764 break; | |
| 765 } | |
| 766 // Fall through if the information does not match. | |
|
benwells
2014/05/22 02:49:25
Is there any way this can happen, or would this in
wjywbs
2014/05/22 03:21:35
I don't think this can happen and it didn't happen
| |
| 767 } | |
| 768 case chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR: | |
| 769 error_ = keys::kGenerateAppForLinkInstallError; | |
| 770 SendResponse(false); | |
| 771 Release(); | |
| 772 break; | |
| 773 default: | |
| 774 NOTREACHED(); | |
| 775 } | |
| 776 } | |
| 777 | |
| 778 void ManagementGenerateAppForLinkFunction::OnFaviconForApp( | |
| 779 const favicon_base::FaviconImageResult& image_result) { | |
| 780 scoped_ptr<WebApplicationInfo> web_app(new WebApplicationInfo()); | |
| 781 web_app->title = base::UTF8ToUTF16(install_info_->title); | |
| 782 web_app->app_url = install_info_->launch_url; | |
| 783 | |
| 784 char icon_letter = | |
| 785 BookmarkAppHelper::GetIconLetterForGeneratedIcon(web_app->app_url); | |
| 786 SkColor background_color = SK_ColorBLACK; | |
| 787 | |
| 788 if (!image_result.image.IsEmpty()) { | |
| 789 WebApplicationInfo::IconInfo icon; | |
| 790 icon.data = image_result.image.AsBitmap(); | |
| 791 icon.width = icon.data.width(); | |
| 792 icon.height = icon.data.height(); | |
| 793 web_app->icons.push_back(icon); | |
| 794 | |
| 795 background_color = | |
| 796 BookmarkAppHelper::GetIconColorForGeneratedIcon(icon.data); | |
| 797 } | |
| 798 | |
| 799 std::map<int, SkBitmap> icon_bitmaps; | |
| 800 // Generate the small, medium and 2x icons. | |
| 801 BookmarkAppHelper::GenerateIcon(&icon_bitmaps, | |
| 802 extension_misc::EXTENSION_ICON_SMALL, | |
| 803 background_color, | |
| 804 icon_letter); | |
| 805 BookmarkAppHelper::GenerateIcon(&icon_bitmaps, | |
| 806 extension_misc::EXTENSION_ICON_SMALL * 2, | |
| 807 background_color, | |
| 808 icon_letter); | |
| 809 BookmarkAppHelper::GenerateIcon(&icon_bitmaps, | |
| 810 extension_misc::EXTENSION_ICON_MEDIUM, | |
| 811 background_color, | |
| 812 icon_letter); | |
| 813 BookmarkAppHelper::GenerateIcon(&icon_bitmaps, | |
| 814 extension_misc::EXTENSION_ICON_MEDIUM * 2, | |
| 815 background_color, | |
| 816 icon_letter); | |
| 817 | |
| 818 // Populate the icon data into the WebApplicationInfo we are using to | |
| 819 // install the bookmark app. | |
| 820 for (std::map<int, SkBitmap>::const_iterator icon_bitmaps_it = | |
| 821 icon_bitmaps.begin(); | |
| 822 icon_bitmaps_it != icon_bitmaps.end(); | |
| 823 ++icon_bitmaps_it) { | |
| 824 WebApplicationInfo::IconInfo icon; | |
| 825 icon.data = icon_bitmaps_it->second; | |
| 826 icon.width = icon.data.width(); | |
| 827 icon.height = icon.data.height(); | |
| 828 web_app->icons.push_back(icon); | |
| 829 } | |
| 830 | |
| 831 scoped_refptr<CrxInstaller> installer(CrxInstaller::CreateSilent(service())); | |
| 832 installer->set_error_on_unsupported_requirements(true); | |
| 833 | |
| 834 registrar_.Add(this, | |
| 835 chrome::NOTIFICATION_CRX_INSTALLER_DONE, | |
| 836 content::Source<CrxInstaller>(installer.get())); | |
| 837 | |
| 838 registrar_.Add(this, | |
| 839 chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR, | |
| 840 content::Source<CrxInstaller>(installer.get())); | |
| 841 | |
| 842 installer->InstallWebApp(*web_app); | |
| 843 } | |
| 844 | |
| 845 bool ManagementGenerateAppForLinkFunction::RunAsync() { | |
| 846 if (!user_gesture()) { | |
| 847 error_ = keys::kGestureNeededForGenerateAppForLinkError; | |
| 848 return false; | |
| 849 } | |
| 850 | |
| 851 scoped_ptr<management::GenerateAppForLink::Params> params( | |
| 852 management::GenerateAppForLink::Params::Create(*args_)); | |
| 853 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
| 854 | |
| 855 GURL launch_url(params->url); | |
| 856 if (!launch_url.is_valid() || !launch_url.SchemeIsHTTPOrHTTPS()) { | |
| 857 error_ = ErrorUtils::FormatErrorMessage(keys::kInvalidURLError, | |
| 858 params->url); | |
| 859 return false; | |
| 860 } | |
| 861 | |
| 862 if (params->title.empty()) { | |
| 863 error_ = keys::kEmptyTitleError; | |
| 864 return false; | |
| 865 } | |
| 866 | |
| 867 FaviconService* favicon_service = | |
| 868 FaviconServiceFactory::GetForProfile(GetProfile(), | |
| 869 Profile::EXPLICIT_ACCESS); | |
| 870 if (!favicon_service) { | |
| 871 error_ = keys::kNoFaviconServiceToGenerateAppForLink; | |
| 872 return false; | |
| 873 } | |
| 874 | |
| 875 install_info_.reset(new AppInstallInfo()); | |
| 876 install_info_->launch_url = launch_url; | |
| 877 install_info_->title = params->title; | |
| 878 | |
| 879 favicon_service->GetFaviconImageForURL( | |
| 880 FaviconService::FaviconForURLParams( | |
| 881 launch_url, favicon_base::FAVICON, gfx::kFaviconSize), | |
| 882 base::Bind(&ManagementGenerateAppForLinkFunction::OnFaviconForApp, this), | |
| 883 &cancelable_task_tracker_); | |
| 884 | |
| 885 // Matched with a Release() in OnExtensionLoaded(). | |
| 886 AddRef(); | |
| 887 | |
| 888 // Response is sent async in OnExtensionLoaded(). | |
| 889 return true; | |
| 890 } | |
| 891 | |
| 734 ManagementEventRouter::ManagementEventRouter(Profile* profile) | 892 ManagementEventRouter::ManagementEventRouter(Profile* profile) |
| 735 : profile_(profile) { | 893 : profile_(profile) { |
| 736 int types[] = {chrome::NOTIFICATION_EXTENSION_INSTALLED, | 894 int types[] = {chrome::NOTIFICATION_EXTENSION_INSTALLED, |
| 737 chrome::NOTIFICATION_EXTENSION_UNINSTALLED, | 895 chrome::NOTIFICATION_EXTENSION_UNINSTALLED, |
| 738 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, | 896 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, |
| 739 chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED}; | 897 chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED}; |
| 740 | 898 |
| 741 CHECK(registrar_.IsEmpty()); | 899 CHECK(registrar_.IsEmpty()); |
| 742 for (size_t i = 0; i < arraysize(types); i++) { | 900 for (size_t i = 0; i < arraysize(types); i++) { |
| 743 registrar_.Add(this, | 901 registrar_.Add(this, |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 825 return g_factory.Pointer(); | 983 return g_factory.Pointer(); |
| 826 } | 984 } |
| 827 | 985 |
| 828 void ManagementAPI::OnListenerAdded(const EventListenerInfo& details) { | 986 void ManagementAPI::OnListenerAdded(const EventListenerInfo& details) { |
| 829 management_event_router_.reset( | 987 management_event_router_.reset( |
| 830 new ManagementEventRouter(Profile::FromBrowserContext(browser_context_))); | 988 new ManagementEventRouter(Profile::FromBrowserContext(browser_context_))); |
| 831 EventRouter::Get(browser_context_)->UnregisterObserver(this); | 989 EventRouter::Get(browser_context_)->UnregisterObserver(this); |
| 832 } | 990 } |
| 833 | 991 |
| 834 } // namespace extensions | 992 } // namespace extensions |
| OLD | NEW |