| 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/extension_service.h" | 24 #include "chrome/browser/extensions/extension_service.h" |
| 25 #include "chrome/browser/extensions/extension_uninstall_dialog.h" | 25 #include "chrome/browser/extensions/extension_uninstall_dialog.h" |
| 26 #include "chrome/browser/extensions/launch_util.h" | 26 #include "chrome/browser/extensions/launch_util.h" |
| 27 #include "chrome/browser/favicon/favicon_service_factory.h" |
| 27 #include "chrome/browser/profiles/profile.h" | 28 #include "chrome/browser/profiles/profile.h" |
| 28 #include "chrome/browser/ui/browser_dialogs.h" | 29 #include "chrome/browser/ui/browser_dialogs.h" |
| 29 #include "chrome/browser/ui/browser_finder.h" | 30 #include "chrome/browser/ui/browser_finder.h" |
| 30 #include "chrome/browser/ui/browser_window.h" | 31 #include "chrome/browser/ui/browser_window.h" |
| 31 #include "chrome/browser/ui/extensions/application_launch.h" | 32 #include "chrome/browser/ui/extensions/application_launch.h" |
| 32 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h" | 33 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h" |
| 33 #include "chrome/common/chrome_utility_messages.h" | 34 #include "chrome/common/chrome_utility_messages.h" |
| 34 #include "chrome/common/extensions/api/management.h" | 35 #include "chrome/common/extensions/api/management.h" |
| 35 #include "chrome/common/extensions/extension_constants.h" | 36 #include "chrome/common/extensions/extension_constants.h" |
| 36 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h" | 37 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h" |
| 37 #include "chrome/common/extensions/manifest_url_handler.h" | 38 #include "chrome/common/extensions/manifest_url_handler.h" |
| 39 #include "chrome/common/web_application_info.h" |
| 38 #include "content/public/browser/notification_details.h" | 40 #include "content/public/browser/notification_details.h" |
| 39 #include "content/public/browser/notification_source.h" | 41 #include "content/public/browser/notification_source.h" |
| 40 #include "content/public/browser/utility_process_host.h" | 42 #include "content/public/browser/utility_process_host.h" |
| 41 #include "content/public/browser/utility_process_host_client.h" | 43 #include "content/public/browser/utility_process_host_client.h" |
| 42 #include "extensions/browser/event_router.h" | 44 #include "extensions/browser/event_router.h" |
| 43 #include "extensions/browser/extension_registry.h" | 45 #include "extensions/browser/extension_registry.h" |
| 44 #include "extensions/browser/extension_system.h" | 46 #include "extensions/browser/extension_system.h" |
| 45 #include "extensions/browser/management_policy.h" | 47 #include "extensions/browser/management_policy.h" |
| 46 #include "extensions/common/constants.h" | 48 #include "extensions/common/constants.h" |
| 47 #include "extensions/common/error_utils.h" | 49 #include "extensions/common/error_utils.h" |
| 48 #include "extensions/common/extension.h" | 50 #include "extensions/common/extension.h" |
| 49 #include "extensions/common/extension_icon_set.h" | 51 #include "extensions/common/extension_icon_set.h" |
| 50 #include "extensions/common/manifest_handlers/icons_handler.h" | 52 #include "extensions/common/manifest_handlers/icons_handler.h" |
| 51 #include "extensions/common/manifest_handlers/offline_enabled_info.h" | 53 #include "extensions/common/manifest_handlers/offline_enabled_info.h" |
| 52 #include "extensions/common/permissions/permission_set.h" | 54 #include "extensions/common/permissions/permission_set.h" |
| 53 #include "extensions/common/permissions/permissions_data.h" | 55 #include "extensions/common/permissions/permissions_data.h" |
| 54 #include "extensions/common/url_pattern.h" | 56 #include "extensions/common/url_pattern.h" |
| 57 #include "ui/gfx/favicon_size.h" |
| 55 | 58 |
| 56 #if !defined(OS_ANDROID) | 59 #if !defined(OS_ANDROID) |
| 57 #include "chrome/browser/ui/webui/ntp/core_app_launcher_handler.h" | 60 #include "chrome/browser/ui/webui/ntp/core_app_launcher_handler.h" |
| 58 #endif | 61 #endif |
| 59 | 62 |
| 60 using base::IntToString; | 63 using base::IntToString; |
| 61 using content::BrowserThread; | 64 using content::BrowserThread; |
| 62 using content::UtilityProcessHost; | 65 using content::UtilityProcessHost; |
| 63 using content::UtilityProcessHostClient; | 66 using content::UtilityProcessHostClient; |
| 64 | 67 |
| (...skipping 659 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 724 base::Bind(&ManagementCreateAppShortcutFunction::OnCloseShortcutPrompt, | 727 base::Bind(&ManagementCreateAppShortcutFunction::OnCloseShortcutPrompt, |
| 725 this)); | 728 this)); |
| 726 } else { | 729 } else { |
| 727 OnCloseShortcutPrompt(auto_confirm_for_test == PROCEED); | 730 OnCloseShortcutPrompt(auto_confirm_for_test == PROCEED); |
| 728 } | 731 } |
| 729 | 732 |
| 730 // Response is sent async in OnCloseShortcutPrompt(). | 733 // Response is sent async in OnCloseShortcutPrompt(). |
| 731 return true; | 734 return true; |
| 732 } | 735 } |
| 733 | 736 |
| 737 ManagementGenerateAppForLinkFunction::ManagementGenerateAppForLinkFunction() { |
| 738 } |
| 739 |
| 740 ManagementGenerateAppForLinkFunction::~ManagementGenerateAppForLinkFunction() { |
| 741 } |
| 742 |
| 743 void ManagementGenerateAppForLinkFunction::FinishCreateBookmarkApp( |
| 744 const extensions::Extension* extension, |
| 745 const WebApplicationInfo& web_app_info) { |
| 746 if (extension) { |
| 747 scoped_ptr<management::ExtensionInfo> info = |
| 748 CreateExtensionInfo(*extension, ExtensionSystem::Get(GetProfile())); |
| 749 results_ = management::GenerateAppForLink::Results::Create(*info); |
| 750 |
| 751 SendResponse(true); |
| 752 Release(); |
| 753 } else { |
| 754 error_ = keys::kGenerateAppForLinkInstallError; |
| 755 SendResponse(false); |
| 756 Release(); |
| 757 } |
| 758 } |
| 759 |
| 760 void ManagementGenerateAppForLinkFunction::OnFaviconForApp( |
| 761 const favicon_base::FaviconImageResult& image_result) { |
| 762 WebApplicationInfo web_app; |
| 763 web_app.title = base::UTF8ToUTF16(install_info_->title); |
| 764 web_app.app_url = install_info_->launch_url; |
| 765 |
| 766 if (!image_result.image.IsEmpty()) { |
| 767 WebApplicationInfo::IconInfo icon; |
| 768 icon.data = image_result.image.AsBitmap(); |
| 769 icon.width = icon.data.width(); |
| 770 icon.height = icon.data.height(); |
| 771 web_app.icons.push_back(icon); |
| 772 } |
| 773 |
| 774 bookmark_app_helper_.reset(new BookmarkAppHelper(service(), web_app, NULL)); |
| 775 bookmark_app_helper_->Create(base::Bind( |
| 776 &ManagementGenerateAppForLinkFunction::FinishCreateBookmarkApp, this)); |
| 777 } |
| 778 |
| 779 bool ManagementGenerateAppForLinkFunction::RunAsync() { |
| 780 if (!user_gesture()) { |
| 781 error_ = keys::kGestureNeededForGenerateAppForLinkError; |
| 782 return false; |
| 783 } |
| 784 |
| 785 scoped_ptr<management::GenerateAppForLink::Params> params( |
| 786 management::GenerateAppForLink::Params::Create(*args_)); |
| 787 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 788 |
| 789 GURL launch_url(params->url); |
| 790 if (!launch_url.is_valid() || !launch_url.SchemeIsHTTPOrHTTPS()) { |
| 791 error_ = ErrorUtils::FormatErrorMessage(keys::kInvalidURLError, |
| 792 params->url); |
| 793 return false; |
| 794 } |
| 795 |
| 796 if (params->title.empty()) { |
| 797 error_ = keys::kEmptyTitleError; |
| 798 return false; |
| 799 } |
| 800 |
| 801 FaviconService* favicon_service = |
| 802 FaviconServiceFactory::GetForProfile(GetProfile(), |
| 803 Profile::EXPLICIT_ACCESS); |
| 804 DCHECK(favicon_service); |
| 805 |
| 806 install_info_.reset(new AppInstallInfo()); |
| 807 install_info_->launch_url = launch_url; |
| 808 install_info_->title = params->title; |
| 809 |
| 810 favicon_service->GetFaviconImageForURL( |
| 811 FaviconService::FaviconForURLParams( |
| 812 launch_url, favicon_base::FAVICON, gfx::kFaviconSize), |
| 813 base::Bind(&ManagementGenerateAppForLinkFunction::OnFaviconForApp, this), |
| 814 &cancelable_task_tracker_); |
| 815 |
| 816 // Matched with a Release() in OnExtensionLoaded(). |
| 817 AddRef(); |
| 818 |
| 819 // Response is sent async in OnExtensionLoaded(). |
| 820 return true; |
| 821 } |
| 822 |
| 734 ManagementEventRouter::ManagementEventRouter(Profile* profile) | 823 ManagementEventRouter::ManagementEventRouter(Profile* profile) |
| 735 : profile_(profile) { | 824 : profile_(profile) { |
| 736 int types[] = {chrome::NOTIFICATION_EXTENSION_INSTALLED, | 825 int types[] = {chrome::NOTIFICATION_EXTENSION_INSTALLED, |
| 737 chrome::NOTIFICATION_EXTENSION_UNINSTALLED, | 826 chrome::NOTIFICATION_EXTENSION_UNINSTALLED, |
| 738 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, | 827 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, |
| 739 chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED}; | 828 chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED}; |
| 740 | 829 |
| 741 CHECK(registrar_.IsEmpty()); | 830 CHECK(registrar_.IsEmpty()); |
| 742 for (size_t i = 0; i < arraysize(types); i++) { | 831 for (size_t i = 0; i < arraysize(types); i++) { |
| 743 registrar_.Add(this, | 832 registrar_.Add(this, |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 825 return g_factory.Pointer(); | 914 return g_factory.Pointer(); |
| 826 } | 915 } |
| 827 | 916 |
| 828 void ManagementAPI::OnListenerAdded(const EventListenerInfo& details) { | 917 void ManagementAPI::OnListenerAdded(const EventListenerInfo& details) { |
| 829 management_event_router_.reset( | 918 management_event_router_.reset( |
| 830 new ManagementEventRouter(Profile::FromBrowserContext(browser_context_))); | 919 new ManagementEventRouter(Profile::FromBrowserContext(browser_context_))); |
| 831 EventRouter::Get(browser_context_)->UnregisterObserver(this); | 920 EventRouter::Get(browser_context_)->UnregisterObserver(this); |
| 832 } | 921 } |
| 833 | 922 |
| 834 } // namespace extensions | 923 } // namespace extensions |
| OLD | NEW |