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: { | |
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 } else { | |
765 NOTREACHED(); | |
766 } | |
767 break; | |
768 } | |
769 case chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR: | |
770 error_ = keys::kGenerateAppForLinkInstallError; | |
771 SendResponse(false); | |
772 Release(); | |
773 break; | |
774 default: | |
775 NOTREACHED(); | |
776 } | |
777 } | |
778 | |
779 void ManagementGenerateAppForLinkFunction::OnFaviconForApp( | |
780 const favicon_base::FaviconImageResult& image_result) { | |
781 scoped_ptr<WebApplicationInfo> web_app(new WebApplicationInfo()); | |
782 web_app->title = base::UTF8ToUTF16(install_info_->title); | |
783 web_app->app_url = install_info_->launch_url; | |
784 | |
785 char icon_letter = | |
786 BookmarkAppHelper::GetIconLetterForGeneratedIcon(web_app->app_url); | |
787 SkColor background_color = SK_ColorBLACK; | |
788 | |
789 if (!image_result.image.IsEmpty()) { | |
790 WebApplicationInfo::IconInfo icon; | |
791 icon.data = image_result.image.AsBitmap(); | |
792 icon.width = icon.data.width(); | |
793 icon.height = icon.data.height(); | |
794 web_app->icons.push_back(icon); | |
795 | |
796 background_color = | |
797 BookmarkAppHelper::GetIconColorForGeneratedIcon(icon.data); | |
798 } | |
799 | |
800 std::map<int, SkBitmap> icon_bitmaps; | |
801 // Generate the small, medium and 2x icons. | |
802 BookmarkAppHelper::GenerateIcon(&icon_bitmaps, | |
803 extension_misc::EXTENSION_ICON_SMALL, | |
804 background_color, | |
805 icon_letter); | |
806 BookmarkAppHelper::GenerateIcon(&icon_bitmaps, | |
807 extension_misc::EXTENSION_ICON_SMALL * 2, | |
808 background_color, | |
809 icon_letter); | |
810 BookmarkAppHelper::GenerateIcon(&icon_bitmaps, | |
811 extension_misc::EXTENSION_ICON_MEDIUM, | |
812 background_color, | |
813 icon_letter); | |
814 BookmarkAppHelper::GenerateIcon(&icon_bitmaps, | |
815 extension_misc::EXTENSION_ICON_MEDIUM * 2, | |
816 background_color, | |
817 icon_letter); | |
818 | |
819 // Populate the icon data into the WebApplicationInfo we are using to | |
820 // install the bookmark app. | |
821 for (std::map<int, SkBitmap>::const_iterator icon_bitmaps_it = | |
822 icon_bitmaps.begin(); | |
823 icon_bitmaps_it != icon_bitmaps.end(); | |
824 ++icon_bitmaps_it) { | |
825 WebApplicationInfo::IconInfo icon; | |
826 icon.data = icon_bitmaps_it->second; | |
827 icon.width = icon.data.width(); | |
828 icon.height = icon.data.height(); | |
829 web_app->icons.push_back(icon); | |
830 } | |
831 | |
832 scoped_refptr<CrxInstaller> installer(CrxInstaller::CreateSilent(service())); | |
833 installer->set_error_on_unsupported_requirements(true); | |
834 | |
835 registrar_.Add(this, | |
836 chrome::NOTIFICATION_CRX_INSTALLER_DONE, | |
837 content::Source<CrxInstaller>(installer.get())); | |
838 | |
839 registrar_.Add(this, | |
840 chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR, | |
841 content::Source<CrxInstaller>(installer.get())); | |
842 | |
843 installer->InstallWebApp(*web_app); | |
844 } | |
845 | |
846 bool ManagementGenerateAppForLinkFunction::RunAsync() { | |
847 if (!user_gesture()) { | |
848 error_ = keys::kGestureNeededForGenerateAppForLinkError; | |
849 return false; | |
850 } | |
851 | |
852 scoped_ptr<management::GenerateAppForLink::Params> params( | |
853 management::GenerateAppForLink::Params::Create(*args_)); | |
854 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
855 | |
856 GURL launch_url(params->url); | |
857 if (!launch_url.is_valid() || !launch_url.SchemeIsHTTPOrHTTPS()) { | |
858 error_ = ErrorUtils::FormatErrorMessage(keys::kInvalidURLError, | |
859 params->url); | |
860 return false; | |
861 } | |
862 | |
863 if (params->title.empty()) { | |
864 error_ = keys::kEmptyTitleError; | |
865 return false; | |
866 } | |
867 | |
868 FaviconService* favicon_service = | |
869 FaviconServiceFactory::GetForProfile(GetProfile(), | |
870 Profile::EXPLICIT_ACCESS); | |
871 if (!favicon_service) { | |
872 error_ = keys::kNoFaviconServiceToGenerateAppForLink; | |
benwells
2014/05/25 23:59:43
This error is unactionable for an extension develo
wjywbs
2014/05/26 03:22:24
Done.
| |
873 return false; | |
874 } | |
875 | |
876 install_info_.reset(new AppInstallInfo()); | |
877 install_info_->launch_url = launch_url; | |
878 install_info_->title = params->title; | |
879 | |
880 favicon_service->GetFaviconImageForURL( | |
881 FaviconService::FaviconForURLParams( | |
882 launch_url, favicon_base::FAVICON, gfx::kFaviconSize), | |
883 base::Bind(&ManagementGenerateAppForLinkFunction::OnFaviconForApp, this), | |
884 &cancelable_task_tracker_); | |
885 | |
886 // Matched with a Release() in OnExtensionLoaded(). | |
887 AddRef(); | |
888 | |
889 // Response is sent async in OnExtensionLoaded(). | |
890 return true; | |
891 } | |
892 | |
734 ManagementEventRouter::ManagementEventRouter(Profile* profile) | 893 ManagementEventRouter::ManagementEventRouter(Profile* profile) |
735 : profile_(profile) { | 894 : profile_(profile) { |
736 int types[] = {chrome::NOTIFICATION_EXTENSION_INSTALLED, | 895 int types[] = {chrome::NOTIFICATION_EXTENSION_INSTALLED, |
737 chrome::NOTIFICATION_EXTENSION_UNINSTALLED, | 896 chrome::NOTIFICATION_EXTENSION_UNINSTALLED, |
738 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, | 897 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, |
739 chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED}; | 898 chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED}; |
740 | 899 |
741 CHECK(registrar_.IsEmpty()); | 900 CHECK(registrar_.IsEmpty()); |
742 for (size_t i = 0; i < arraysize(types); i++) { | 901 for (size_t i = 0; i < arraysize(types); i++) { |
743 registrar_.Add(this, | 902 registrar_.Add(this, |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
825 return g_factory.Pointer(); | 984 return g_factory.Pointer(); |
826 } | 985 } |
827 | 986 |
828 void ManagementAPI::OnListenerAdded(const EventListenerInfo& details) { | 987 void ManagementAPI::OnListenerAdded(const EventListenerInfo& details) { |
829 management_event_router_.reset( | 988 management_event_router_.reset( |
830 new ManagementEventRouter(Profile::FromBrowserContext(browser_context_))); | 989 new ManagementEventRouter(Profile::FromBrowserContext(browser_context_))); |
831 EventRouter::Get(browser_context_)->UnregisterObserver(this); | 990 EventRouter::Get(browser_context_)->UnregisterObserver(this); |
832 } | 991 } |
833 | 992 |
834 } // namespace extensions | 993 } // namespace extensions |
OLD | NEW |