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" |
28 #include "chrome/browser/profiles/profile.h" | 29 #include "chrome/browser/profiles/profile.h" |
29 #include "chrome/browser/ui/browser_dialogs.h" | 30 #include "chrome/browser/ui/browser_dialogs.h" |
30 #include "chrome/browser/ui/browser_finder.h" | 31 #include "chrome/browser/ui/browser_finder.h" |
31 #include "chrome/browser/ui/browser_window.h" | 32 #include "chrome/browser/ui/browser_window.h" |
32 #include "chrome/browser/ui/extensions/application_launch.h" | 33 #include "chrome/browser/ui/extensions/application_launch.h" |
33 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h" | 34 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h" |
34 #include "chrome/common/chrome_switches.h" | 35 #include "chrome/common/chrome_switches.h" |
35 #include "chrome/common/chrome_utility_messages.h" | 36 #include "chrome/common/chrome_utility_messages.h" |
36 #include "chrome/common/extensions/api/management.h" | 37 #include "chrome/common/extensions/api/management.h" |
37 #include "chrome/common/extensions/extension_constants.h" | 38 #include "chrome/common/extensions/extension_constants.h" |
(...skipping 11 matching lines...) Expand all Loading... |
49 #include "extensions/browser/management_policy.h" | 50 #include "extensions/browser/management_policy.h" |
50 #include "extensions/common/constants.h" | 51 #include "extensions/common/constants.h" |
51 #include "extensions/common/error_utils.h" | 52 #include "extensions/common/error_utils.h" |
52 #include "extensions/common/extension.h" | 53 #include "extensions/common/extension.h" |
53 #include "extensions/common/extension_icon_set.h" | 54 #include "extensions/common/extension_icon_set.h" |
54 #include "extensions/common/manifest_handlers/icons_handler.h" | 55 #include "extensions/common/manifest_handlers/icons_handler.h" |
55 #include "extensions/common/manifest_handlers/offline_enabled_info.h" | 56 #include "extensions/common/manifest_handlers/offline_enabled_info.h" |
56 #include "extensions/common/permissions/permission_set.h" | 57 #include "extensions/common/permissions/permission_set.h" |
57 #include "extensions/common/permissions/permissions_data.h" | 58 #include "extensions/common/permissions/permissions_data.h" |
58 #include "extensions/common/url_pattern.h" | 59 #include "extensions/common/url_pattern.h" |
| 60 #include "ui/gfx/favicon_size.h" |
59 | 61 |
60 #if !defined(OS_ANDROID) | 62 #if !defined(OS_ANDROID) |
61 #include "chrome/browser/ui/webui/ntp/core_app_launcher_handler.h" | 63 #include "chrome/browser/ui/webui/ntp/core_app_launcher_handler.h" |
62 #endif | 64 #endif |
63 | 65 |
64 using base::IntToString; | 66 using base::IntToString; |
65 using content::BrowserThread; | 67 using content::BrowserThread; |
66 using content::UtilityProcessHost; | 68 using content::UtilityProcessHost; |
67 using content::UtilityProcessHostClient; | 69 using content::UtilityProcessHostClient; |
68 | 70 |
(...skipping 776 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
845 break; | 847 break; |
846 case management::LAUNCH_TYPE_NONE: | 848 case management::LAUNCH_TYPE_NONE: |
847 NOTREACHED(); | 849 NOTREACHED(); |
848 } | 850 } |
849 | 851 |
850 SetLaunchType(service(), params->id, launch_type); | 852 SetLaunchType(service(), params->id, launch_type); |
851 | 853 |
852 return true; | 854 return true; |
853 } | 855 } |
854 | 856 |
| 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 |
855 ManagementEventRouter::ManagementEventRouter(Profile* profile) | 942 ManagementEventRouter::ManagementEventRouter(Profile* profile) |
856 : profile_(profile) { | 943 : profile_(profile) { |
857 int types[] = {chrome::NOTIFICATION_EXTENSION_INSTALLED_DEPRECATED, | 944 int types[] = {chrome::NOTIFICATION_EXTENSION_INSTALLED_DEPRECATED, |
858 chrome::NOTIFICATION_EXTENSION_UNINSTALLED_DEPRECATED, | 945 chrome::NOTIFICATION_EXTENSION_UNINSTALLED_DEPRECATED, |
859 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, | 946 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, |
860 chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED}; | 947 chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED}; |
861 | 948 |
862 CHECK(registrar_.IsEmpty()); | 949 CHECK(registrar_.IsEmpty()); |
863 for (size_t i = 0; i < arraysize(types); i++) { | 950 for (size_t i = 0; i < arraysize(types); i++) { |
864 registrar_.Add(this, | 951 registrar_.Add(this, |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
946 return g_factory.Pointer(); | 1033 return g_factory.Pointer(); |
947 } | 1034 } |
948 | 1035 |
949 void ManagementAPI::OnListenerAdded(const EventListenerInfo& details) { | 1036 void ManagementAPI::OnListenerAdded(const EventListenerInfo& details) { |
950 management_event_router_.reset( | 1037 management_event_router_.reset( |
951 new ManagementEventRouter(Profile::FromBrowserContext(browser_context_))); | 1038 new ManagementEventRouter(Profile::FromBrowserContext(browser_context_))); |
952 EventRouter::Get(browser_context_)->UnregisterObserver(this); | 1039 EventRouter::Get(browser_context_)->UnregisterObserver(this); |
953 } | 1040 } |
954 | 1041 |
955 } // namespace extensions | 1042 } // namespace extensions |
OLD | NEW |