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/crx_installer.h" | |
| 24 #include "chrome/browser/extensions/extension_service.h" | 25 #include "chrome/browser/extensions/extension_service.h" |
| 25 #include "chrome/browser/extensions/extension_uninstall_dialog.h" | 26 #include "chrome/browser/extensions/extension_uninstall_dialog.h" |
| 26 #include "chrome/browser/extensions/launch_util.h" | 27 #include "chrome/browser/extensions/launch_util.h" |
| 28 #include "chrome/browser/favicon/favicon_service_factory.h" | |
| 27 #include "chrome/browser/profiles/profile.h" | 29 #include "chrome/browser/profiles/profile.h" |
| 28 #include "chrome/browser/ui/browser_dialogs.h" | 30 #include "chrome/browser/ui/browser_dialogs.h" |
| 29 #include "chrome/browser/ui/browser_finder.h" | 31 #include "chrome/browser/ui/browser_finder.h" |
| 30 #include "chrome/browser/ui/browser_window.h" | 32 #include "chrome/browser/ui/browser_window.h" |
| 31 #include "chrome/browser/ui/extensions/application_launch.h" | 33 #include "chrome/browser/ui/extensions/application_launch.h" |
| 32 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h" | 34 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h" |
| 33 #include "chrome/common/chrome_utility_messages.h" | 35 #include "chrome/common/chrome_utility_messages.h" |
| 34 #include "chrome/common/extensions/api/management.h" | 36 #include "chrome/common/extensions/api/management.h" |
| 35 #include "chrome/common/extensions/extension_constants.h" | 37 #include "chrome/common/extensions/extension_constants.h" |
| 36 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h" | 38 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h" |
| 37 #include "chrome/common/extensions/manifest_url_handler.h" | 39 #include "chrome/common/extensions/manifest_url_handler.h" |
| 40 #include "chrome/common/web_application_info.h" | |
| 38 #include "content/public/browser/notification_details.h" | 41 #include "content/public/browser/notification_details.h" |
| 39 #include "content/public/browser/notification_source.h" | 42 #include "content/public/browser/notification_source.h" |
| 40 #include "content/public/browser/utility_process_host.h" | 43 #include "content/public/browser/utility_process_host.h" |
| 41 #include "content/public/browser/utility_process_host_client.h" | 44 #include "content/public/browser/utility_process_host_client.h" |
| 42 #include "extensions/browser/event_router.h" | 45 #include "extensions/browser/event_router.h" |
| 43 #include "extensions/browser/extension_registry.h" | 46 #include "extensions/browser/extension_registry.h" |
| 44 #include "extensions/browser/extension_system.h" | 47 #include "extensions/browser/extension_system.h" |
| 45 #include "extensions/browser/management_policy.h" | 48 #include "extensions/browser/management_policy.h" |
| 46 #include "extensions/common/constants.h" | 49 #include "extensions/common/constants.h" |
| 47 #include "extensions/common/error_utils.h" | 50 #include "extensions/common/error_utils.h" |
| 48 #include "extensions/common/extension.h" | 51 #include "extensions/common/extension.h" |
| 49 #include "extensions/common/extension_icon_set.h" | 52 #include "extensions/common/extension_icon_set.h" |
| 53 #include "extensions/common/manifest_constants.h" | |
| 50 #include "extensions/common/manifest_handlers/icons_handler.h" | 54 #include "extensions/common/manifest_handlers/icons_handler.h" |
| 51 #include "extensions/common/manifest_handlers/offline_enabled_info.h" | 55 #include "extensions/common/manifest_handlers/offline_enabled_info.h" |
| 52 #include "extensions/common/permissions/permission_set.h" | 56 #include "extensions/common/permissions/permission_set.h" |
| 53 #include "extensions/common/permissions/permissions_data.h" | 57 #include "extensions/common/permissions/permissions_data.h" |
| 54 #include "extensions/common/url_pattern.h" | 58 #include "extensions/common/url_pattern.h" |
| 59 #include "ui/gfx/favicon_size.h" | |
| 55 | 60 |
| 56 #if !defined(OS_ANDROID) | 61 #if !defined(OS_ANDROID) |
| 57 #include "chrome/browser/ui/webui/ntp/core_app_launcher_handler.h" | 62 #include "chrome/browser/ui/webui/ntp/core_app_launcher_handler.h" |
| 58 #endif | 63 #endif |
| 59 | 64 |
| 60 using base::IntToString; | 65 using base::IntToString; |
| 61 using content::BrowserThread; | 66 using content::BrowserThread; |
| 62 using content::UtilityProcessHost; | 67 using content::UtilityProcessHost; |
| 63 using content::UtilityProcessHostClient; | 68 using content::UtilityProcessHostClient; |
| 64 | 69 |
| (...skipping 648 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 713 base::Bind(&ManagementCreateAppShortcutFunction::OnCloseShortcutPrompt, | 718 base::Bind(&ManagementCreateAppShortcutFunction::OnCloseShortcutPrompt, |
| 714 this)); | 719 this)); |
| 715 } else { | 720 } else { |
| 716 OnCloseShortcutPrompt(auto_confirm_for_test == PROCEED); | 721 OnCloseShortcutPrompt(auto_confirm_for_test == PROCEED); |
| 717 } | 722 } |
| 718 | 723 |
| 719 // Response is sent async in OnCloseShortcutPrompt(). | 724 // Response is sent async in OnCloseShortcutPrompt(). |
| 720 return true; | 725 return true; |
| 721 } | 726 } |
| 722 | 727 |
| 728 ManagementGenerateAppForLinkFunction::ManagementGenerateAppForLinkFunction() { | |
| 729 } | |
| 730 | |
| 731 ManagementGenerateAppForLinkFunction::~ManagementGenerateAppForLinkFunction() { | |
| 732 } | |
| 733 | |
| 734 void ManagementGenerateAppForLinkFunction::Observe( | |
| 735 int type, | |
| 736 const content::NotificationSource& source, | |
| 737 const content::NotificationDetails& details) { | |
| 738 switch (type) { | |
| 739 case chrome::NOTIFICATION_CRX_INSTALLER_DONE: { | |
| 740 const Extension* extension = | |
| 741 content::Details<const Extension>(details).ptr(); | |
| 742 if (extension->from_bookmark() && | |
| 743 extension->name() == install_info_->title && | |
| 744 AppLaunchInfo::GetLaunchWebURL(extension) == | |
| 745 install_info_->launch_url) { | |
| 746 scoped_ptr<management::ExtensionInfo> info = | |
| 747 CreateExtensionInfo(*extension, ExtensionSystem::Get(GetProfile())); | |
| 748 results_ = management::GenerateAppForLink::Results::Create(*info); | |
| 749 | |
| 750 SendResponse(true); | |
| 751 Release(); | |
| 752 break; | |
| 753 } | |
| 754 // Fall through if the information does not match. | |
| 755 } | |
| 756 case chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR: | |
| 757 error_ = keys::kGenerateAppForLinkInstallError; | |
| 758 SendResponse(false); | |
| 759 Release(); | |
| 760 break; | |
| 761 default: | |
| 762 NOTREACHED(); | |
| 763 } | |
| 764 } | |
| 765 | |
| 766 void ManagementGenerateAppForLinkFunction::OnFaviconForApp( | |
| 767 const favicon_base::FaviconImageResult& image_result) { | |
| 768 scoped_ptr<WebApplicationInfo> web_app(new WebApplicationInfo()); | |
| 769 web_app->title = base::UTF8ToUTF16(install_info_->title); | |
| 770 web_app->app_url = install_info_->launch_url; | |
| 771 | |
| 772 if (!image_result.image.IsEmpty()) { | |
| 773 WebApplicationInfo::IconInfo icon; | |
| 774 icon.data = image_result.image.AsBitmap(); | |
| 775 icon.width = icon.data.width(); | |
| 776 icon.height = icon.data.height(); | |
| 777 web_app->icons.push_back(icon); | |
| 778 } | |
| 779 | |
| 780 scoped_refptr<CrxInstaller> installer(CrxInstaller::CreateSilent(service())); | |
| 781 installer->set_error_on_unsupported_requirements(true); | |
| 782 | |
| 783 registrar_.Add(this, | |
| 784 chrome::NOTIFICATION_CRX_INSTALLER_DONE, | |
| 785 content::Source<CrxInstaller>(installer.get())); | |
| 786 | |
| 787 registrar_.Add(this, | |
| 788 chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR, | |
| 789 content::Source<CrxInstaller>(installer.get())); | |
| 790 | |
| 791 installer->InstallWebApp(*web_app); | |
|
calamity
2014/05/13 01:59:04
You can use BookmarkAppHelper::CreateOrUpdateBookm
wjywbs
2014/05/20 00:01:59
I think I need to register for the notifications u
calamity
2014/05/20 01:39:35
Ah ok.
| |
| 792 } | |
| 793 | |
| 794 bool ManagementGenerateAppForLinkFunction::RunAsync() { | |
| 795 if (!user_gesture()) { | |
| 796 error_ = keys::kGestureNeededForGenerateAppForLinkError; | |
| 797 return false; | |
| 798 } | |
| 799 | |
| 800 scoped_ptr<management::GenerateAppForLink::Params> params( | |
| 801 management::GenerateAppForLink::Params::Create(*args_)); | |
| 802 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
| 803 | |
| 804 GURL launch_url(params->url); | |
| 805 if (!launch_url.is_valid() || !launch_url.SchemeIsHTTPOrHTTPS()) { | |
| 806 error_ = ErrorUtils::FormatErrorMessage(keys::kInvalidURLError, | |
| 807 params->url); | |
| 808 return false; | |
| 809 } | |
| 810 | |
| 811 if (params->title.empty()) { | |
| 812 error_ = keys::kEmptyTitleError; | |
| 813 return false; | |
| 814 } | |
| 815 | |
| 816 FaviconService* favicon_service = | |
| 817 FaviconServiceFactory::GetForProfile(GetProfile(), | |
| 818 Profile::EXPLICIT_ACCESS); | |
| 819 if (!favicon_service) { | |
| 820 error_ = keys::kNoFaviconServiceToGenerateAppForLink; | |
| 821 return false; | |
| 822 } | |
| 823 | |
| 824 install_info_.reset(new AppInstallInfo()); | |
| 825 install_info_->launch_url = launch_url; | |
| 826 install_info_->title = params->title; | |
| 827 | |
| 828 favicon_service->GetFaviconImageForURL( | |
| 829 FaviconService::FaviconForURLParams( | |
| 830 launch_url, favicon_base::FAVICON, gfx::kFaviconSize), | |
| 831 base::Bind(&ManagementGenerateAppForLinkFunction::OnFaviconForApp, this), | |
| 832 &cancelable_task_tracker_); | |
|
calamity
2014/05/13 01:59:04
Given that this will only ever grab 16x16 icons, w
wjywbs
2014/05/20 00:01:59
I didn't find this BookmarkAppHelper::GenerateCont
calamity
2014/05/20 01:39:35
It recently got renamed to GenerateIcon(). It's in
wjywbs
2014/05/20 04:31:50
Done.
| |
| 833 | |
| 834 // Matched with a Release() in OnExtensionLoaded(). | |
| 835 AddRef(); | |
| 836 | |
| 837 // Response is sent async in OnExtensionLoaded(). | |
| 838 return true; | |
| 839 } | |
| 840 | |
| 723 ManagementEventRouter::ManagementEventRouter(Profile* profile) | 841 ManagementEventRouter::ManagementEventRouter(Profile* profile) |
| 724 : profile_(profile) { | 842 : profile_(profile) { |
| 725 int types[] = {chrome::NOTIFICATION_EXTENSION_INSTALLED, | 843 int types[] = {chrome::NOTIFICATION_EXTENSION_INSTALLED, |
| 726 chrome::NOTIFICATION_EXTENSION_UNINSTALLED, | 844 chrome::NOTIFICATION_EXTENSION_UNINSTALLED, |
| 727 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, | 845 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, |
| 728 chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED}; | 846 chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED}; |
| 729 | 847 |
| 730 CHECK(registrar_.IsEmpty()); | 848 CHECK(registrar_.IsEmpty()); |
| 731 for (size_t i = 0; i < arraysize(types); i++) { | 849 for (size_t i = 0; i < arraysize(types); i++) { |
| 732 registrar_.Add(this, | 850 registrar_.Add(this, |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 814 return g_factory.Pointer(); | 932 return g_factory.Pointer(); |
| 815 } | 933 } |
| 816 | 934 |
| 817 void ManagementAPI::OnListenerAdded(const EventListenerInfo& details) { | 935 void ManagementAPI::OnListenerAdded(const EventListenerInfo& details) { |
| 818 management_event_router_.reset( | 936 management_event_router_.reset( |
| 819 new ManagementEventRouter(Profile::FromBrowserContext(browser_context_))); | 937 new ManagementEventRouter(Profile::FromBrowserContext(browser_context_))); |
| 820 EventRouter::Get(browser_context_)->UnregisterObserver(this); | 938 EventRouter::Get(browser_context_)->UnregisterObserver(this); |
| 821 } | 939 } |
| 822 | 940 |
| 823 } // namespace extensions | 941 } // namespace extensions |
| OLD | NEW |