| 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_ui_util.h" | 25 #include "chrome/browser/extensions/extension_ui_util.h" |
| 26 #include "chrome/browser/extensions/extension_uninstall_dialog.h" | 26 #include "chrome/browser/extensions/extension_uninstall_dialog.h" |
| 27 #include "chrome/browser/extensions/launch_util.h" | 27 #include "chrome/browser/extensions/launch_util.h" |
| 28 #include "chrome/browser/favicon/favicon_service_factory.h" | |
| 29 #include "chrome/browser/profiles/profile.h" | 28 #include "chrome/browser/profiles/profile.h" |
| 30 #include "chrome/browser/ui/browser_dialogs.h" | 29 #include "chrome/browser/ui/browser_dialogs.h" |
| 31 #include "chrome/browser/ui/browser_finder.h" | 30 #include "chrome/browser/ui/browser_finder.h" |
| 32 #include "chrome/browser/ui/browser_window.h" | 31 #include "chrome/browser/ui/browser_window.h" |
| 33 #include "chrome/browser/ui/extensions/application_launch.h" | 32 #include "chrome/browser/ui/extensions/application_launch.h" |
| 34 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h" | 33 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h" |
| 35 #include "chrome/common/chrome_switches.h" | 34 #include "chrome/common/chrome_switches.h" |
| 36 #include "chrome/common/chrome_utility_messages.h" | 35 #include "chrome/common/chrome_utility_messages.h" |
| 37 #include "chrome/common/extensions/api/management.h" | 36 #include "chrome/common/extensions/api/management.h" |
| 38 #include "chrome/common/extensions/extension_constants.h" | 37 #include "chrome/common/extensions/extension_constants.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 50 #include "extensions/browser/management_policy.h" | 49 #include "extensions/browser/management_policy.h" |
| 51 #include "extensions/common/constants.h" | 50 #include "extensions/common/constants.h" |
| 52 #include "extensions/common/error_utils.h" | 51 #include "extensions/common/error_utils.h" |
| 53 #include "extensions/common/extension.h" | 52 #include "extensions/common/extension.h" |
| 54 #include "extensions/common/extension_icon_set.h" | 53 #include "extensions/common/extension_icon_set.h" |
| 55 #include "extensions/common/manifest_handlers/icons_handler.h" | 54 #include "extensions/common/manifest_handlers/icons_handler.h" |
| 56 #include "extensions/common/manifest_handlers/offline_enabled_info.h" | 55 #include "extensions/common/manifest_handlers/offline_enabled_info.h" |
| 57 #include "extensions/common/permissions/permission_set.h" | 56 #include "extensions/common/permissions/permission_set.h" |
| 58 #include "extensions/common/permissions/permissions_data.h" | 57 #include "extensions/common/permissions/permissions_data.h" |
| 59 #include "extensions/common/url_pattern.h" | 58 #include "extensions/common/url_pattern.h" |
| 60 #include "ui/gfx/favicon_size.h" | |
| 61 | 59 |
| 62 #if !defined(OS_ANDROID) | 60 #if !defined(OS_ANDROID) |
| 63 #include "chrome/browser/ui/webui/ntp/core_app_launcher_handler.h" | 61 #include "chrome/browser/ui/webui/ntp/core_app_launcher_handler.h" |
| 64 #endif | 62 #endif |
| 65 | 63 |
| 66 using base::IntToString; | 64 using base::IntToString; |
| 67 using content::BrowserThread; | 65 using content::BrowserThread; |
| 68 using content::UtilityProcessHost; | 66 using content::UtilityProcessHost; |
| 69 using content::UtilityProcessHostClient; | 67 using content::UtilityProcessHostClient; |
| 70 | 68 |
| (...skipping 776 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 847 break; | 845 break; |
| 848 case management::LAUNCH_TYPE_NONE: | 846 case management::LAUNCH_TYPE_NONE: |
| 849 NOTREACHED(); | 847 NOTREACHED(); |
| 850 } | 848 } |
| 851 | 849 |
| 852 SetLaunchType(service(), params->id, launch_type); | 850 SetLaunchType(service(), params->id, launch_type); |
| 853 | 851 |
| 854 return true; | 852 return true; |
| 855 } | 853 } |
| 856 | 854 |
| 857 ManagementGenerateAppForLinkFunction::ManagementGenerateAppForLinkFunction() { | |
| 858 } | |
| 859 | |
| 860 ManagementGenerateAppForLinkFunction::~ManagementGenerateAppForLinkFunction() { | |
| 861 } | |
| 862 | |
| 863 void ManagementGenerateAppForLinkFunction::FinishCreateBookmarkApp( | |
| 864 const extensions::Extension* extension, | |
| 865 const WebApplicationInfo& web_app_info) { | |
| 866 if (extension) { | |
| 867 scoped_ptr<management::ExtensionInfo> info = | |
| 868 CreateExtensionInfo(*extension, ExtensionSystem::Get(GetProfile())); | |
| 869 results_ = management::GenerateAppForLink::Results::Create(*info); | |
| 870 | |
| 871 SendResponse(true); | |
| 872 Release(); | |
| 873 } else { | |
| 874 error_ = keys::kGenerateAppForLinkInstallError; | |
| 875 SendResponse(false); | |
| 876 Release(); | |
| 877 } | |
| 878 } | |
| 879 | |
| 880 void ManagementGenerateAppForLinkFunction::OnFaviconForApp( | |
| 881 const favicon_base::FaviconImageResult& image_result) { | |
| 882 WebApplicationInfo web_app; | |
| 883 web_app.title = base::UTF8ToUTF16(title_); | |
| 884 web_app.app_url = launch_url_; | |
| 885 | |
| 886 if (!image_result.image.IsEmpty()) { | |
| 887 WebApplicationInfo::IconInfo icon; | |
| 888 icon.data = image_result.image.AsBitmap(); | |
| 889 icon.width = icon.data.width(); | |
| 890 icon.height = icon.data.height(); | |
| 891 web_app.icons.push_back(icon); | |
| 892 } | |
| 893 | |
| 894 bookmark_app_helper_.reset(new BookmarkAppHelper(service(), web_app, NULL)); | |
| 895 bookmark_app_helper_->Create(base::Bind( | |
| 896 &ManagementGenerateAppForLinkFunction::FinishCreateBookmarkApp, this)); | |
| 897 } | |
| 898 | |
| 899 bool ManagementGenerateAppForLinkFunction::RunAsync() { | |
| 900 if (!user_gesture()) { | |
| 901 error_ = keys::kGestureNeededForGenerateAppForLinkError; | |
| 902 return false; | |
| 903 } | |
| 904 | |
| 905 scoped_ptr<management::GenerateAppForLink::Params> params( | |
| 906 management::GenerateAppForLink::Params::Create(*args_)); | |
| 907 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
| 908 | |
| 909 GURL launch_url(params->url); | |
| 910 if (!launch_url.is_valid() || !launch_url.SchemeIsHTTPOrHTTPS()) { | |
| 911 error_ = ErrorUtils::FormatErrorMessage(keys::kInvalidURLError, | |
| 912 params->url); | |
| 913 return false; | |
| 914 } | |
| 915 | |
| 916 if (params->title.empty()) { | |
| 917 error_ = keys::kEmptyTitleError; | |
| 918 return false; | |
| 919 } | |
| 920 | |
| 921 FaviconService* favicon_service = | |
| 922 FaviconServiceFactory::GetForProfile(GetProfile(), | |
| 923 Profile::EXPLICIT_ACCESS); | |
| 924 DCHECK(favicon_service); | |
| 925 | |
| 926 title_ = params->title; | |
| 927 launch_url_ = launch_url; | |
| 928 | |
| 929 favicon_service->GetFaviconImageForURL( | |
| 930 FaviconService::FaviconForURLParams( | |
| 931 launch_url, favicon_base::FAVICON, gfx::kFaviconSize), | |
| 932 base::Bind(&ManagementGenerateAppForLinkFunction::OnFaviconForApp, this), | |
| 933 &cancelable_task_tracker_); | |
| 934 | |
| 935 // Matched with a Release() in OnExtensionLoaded(). | |
| 936 AddRef(); | |
| 937 | |
| 938 // Response is sent async in OnExtensionLoaded(). | |
| 939 return true; | |
| 940 } | |
| 941 | |
| 942 ManagementEventRouter::ManagementEventRouter(Profile* profile) | 855 ManagementEventRouter::ManagementEventRouter(Profile* profile) |
| 943 : profile_(profile) { | 856 : profile_(profile) { |
| 944 int types[] = {chrome::NOTIFICATION_EXTENSION_INSTALLED_DEPRECATED, | 857 int types[] = {chrome::NOTIFICATION_EXTENSION_INSTALLED_DEPRECATED, |
| 945 chrome::NOTIFICATION_EXTENSION_UNINSTALLED_DEPRECATED, | 858 chrome::NOTIFICATION_EXTENSION_UNINSTALLED_DEPRECATED, |
| 946 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, | 859 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, |
| 947 chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED}; | 860 chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED}; |
| 948 | 861 |
| 949 CHECK(registrar_.IsEmpty()); | 862 CHECK(registrar_.IsEmpty()); |
| 950 for (size_t i = 0; i < arraysize(types); i++) { | 863 for (size_t i = 0; i < arraysize(types); i++) { |
| 951 registrar_.Add(this, | 864 registrar_.Add(this, |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1033 return g_factory.Pointer(); | 946 return g_factory.Pointer(); |
| 1034 } | 947 } |
| 1035 | 948 |
| 1036 void ManagementAPI::OnListenerAdded(const EventListenerInfo& details) { | 949 void ManagementAPI::OnListenerAdded(const EventListenerInfo& details) { |
| 1037 management_event_router_.reset( | 950 management_event_router_.reset( |
| 1038 new ManagementEventRouter(Profile::FromBrowserContext(browser_context_))); | 951 new ManagementEventRouter(Profile::FromBrowserContext(browser_context_))); |
| 1039 EventRouter::Get(browser_context_)->UnregisterObserver(this); | 952 EventRouter::Get(browser_context_)->UnregisterObserver(this); |
| 1040 } | 953 } |
| 1041 | 954 |
| 1042 } // namespace extensions | 955 } // namespace extensions |
| OLD | NEW |