Index: chrome/browser/extensions/api/management/management_api.cc |
=================================================================== |
--- chrome/browser/extensions/api/management/management_api.cc (revision 271412) |
+++ chrome/browser/extensions/api/management/management_api.cc (working copy) |
@@ -21,9 +21,12 @@ |
#include "base/strings/utf_string_conversions.h" |
#include "chrome/browser/chrome_notification_types.h" |
#include "chrome/browser/extensions/api/management/management_api_constants.h" |
+#include "chrome/browser/extensions/bookmark_app_helper.h" |
+#include "chrome/browser/extensions/crx_installer.h" |
#include "chrome/browser/extensions/extension_service.h" |
#include "chrome/browser/extensions/extension_uninstall_dialog.h" |
#include "chrome/browser/extensions/launch_util.h" |
+#include "chrome/browser/favicon/favicon_service_factory.h" |
#include "chrome/browser/profiles/profile.h" |
#include "chrome/browser/ui/browser_dialogs.h" |
#include "chrome/browser/ui/browser_finder.h" |
@@ -35,6 +38,7 @@ |
#include "chrome/common/extensions/extension_constants.h" |
#include "chrome/common/extensions/manifest_handlers/app_launch_info.h" |
#include "chrome/common/extensions/manifest_url_handler.h" |
+#include "chrome/common/web_application_info.h" |
#include "content/public/browser/notification_details.h" |
#include "content/public/browser/notification_source.h" |
#include "content/public/browser/utility_process_host.h" |
@@ -47,11 +51,13 @@ |
#include "extensions/common/error_utils.h" |
#include "extensions/common/extension.h" |
#include "extensions/common/extension_icon_set.h" |
+#include "extensions/common/manifest_constants.h" |
#include "extensions/common/manifest_handlers/icons_handler.h" |
#include "extensions/common/manifest_handlers/offline_enabled_info.h" |
#include "extensions/common/permissions/permission_set.h" |
#include "extensions/common/permissions/permissions_data.h" |
#include "extensions/common/url_pattern.h" |
+#include "ui/gfx/favicon_size.h" |
#if !defined(OS_ANDROID) |
#include "chrome/browser/ui/webui/ntp/core_app_launcher_handler.h" |
@@ -731,6 +737,159 @@ |
return true; |
} |
+ManagementGenerateAppForLinkFunction::ManagementGenerateAppForLinkFunction() { |
+} |
+ |
+ManagementGenerateAppForLinkFunction::~ManagementGenerateAppForLinkFunction() { |
+} |
+ |
+void ManagementGenerateAppForLinkFunction::Observe( |
+ int type, |
+ const content::NotificationSource& source, |
+ const content::NotificationDetails& details) { |
+ switch (type) { |
+ case chrome::NOTIFICATION_CRX_INSTALLER_DONE: { |
+ const Extension* extension = |
+ content::Details<const Extension>(details).ptr(); |
+ if (extension->from_bookmark() && |
+ extension->name() == install_info_->title && |
+ AppLaunchInfo::GetLaunchWebURL(extension) == |
+ install_info_->launch_url) { |
+ scoped_ptr<management::ExtensionInfo> info = |
+ CreateExtensionInfo(*extension, ExtensionSystem::Get(GetProfile())); |
+ results_ = management::GenerateAppForLink::Results::Create(*info); |
+ |
+ SendResponse(true); |
+ Release(); |
+ } else { |
+ NOTREACHED(); |
+ } |
+ break; |
+ } |
+ case chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR: |
+ error_ = keys::kGenerateAppForLinkInstallError; |
+ SendResponse(false); |
+ Release(); |
+ break; |
+ default: |
+ NOTREACHED(); |
+ } |
+} |
+ |
+void ManagementGenerateAppForLinkFunction::OnFaviconForApp( |
+ const favicon_base::FaviconImageResult& image_result) { |
+ scoped_ptr<WebApplicationInfo> web_app(new WebApplicationInfo()); |
+ web_app->title = base::UTF8ToUTF16(install_info_->title); |
+ web_app->app_url = install_info_->launch_url; |
+ |
+ char icon_letter = |
+ BookmarkAppHelper::GetIconLetterForGeneratedIcon(web_app->app_url); |
+ SkColor background_color = SK_ColorBLACK; |
+ |
+ if (!image_result.image.IsEmpty()) { |
+ WebApplicationInfo::IconInfo icon; |
+ icon.data = image_result.image.AsBitmap(); |
+ icon.width = icon.data.width(); |
+ icon.height = icon.data.height(); |
+ web_app->icons.push_back(icon); |
+ |
+ background_color = |
+ BookmarkAppHelper::GetIconColorForGeneratedIcon(icon.data); |
+ } |
+ |
+ std::map<int, SkBitmap> icon_bitmaps; |
+ // Generate the small, medium and 2x icons. |
+ BookmarkAppHelper::GenerateIcon(&icon_bitmaps, |
+ extension_misc::EXTENSION_ICON_SMALL, |
+ background_color, |
+ icon_letter); |
+ BookmarkAppHelper::GenerateIcon(&icon_bitmaps, |
+ extension_misc::EXTENSION_ICON_SMALL * 2, |
+ background_color, |
+ icon_letter); |
+ BookmarkAppHelper::GenerateIcon(&icon_bitmaps, |
+ extension_misc::EXTENSION_ICON_MEDIUM, |
+ background_color, |
+ icon_letter); |
+ BookmarkAppHelper::GenerateIcon(&icon_bitmaps, |
+ extension_misc::EXTENSION_ICON_MEDIUM * 2, |
+ background_color, |
+ icon_letter); |
+ |
+ // Populate the icon data into the WebApplicationInfo we are using to |
+ // install the bookmark app. |
+ for (std::map<int, SkBitmap>::const_iterator icon_bitmaps_it = |
+ icon_bitmaps.begin(); |
+ icon_bitmaps_it != icon_bitmaps.end(); |
+ ++icon_bitmaps_it) { |
+ WebApplicationInfo::IconInfo icon; |
+ icon.data = icon_bitmaps_it->second; |
+ icon.width = icon.data.width(); |
+ icon.height = icon.data.height(); |
+ web_app->icons.push_back(icon); |
+ } |
+ |
+ scoped_refptr<CrxInstaller> installer(CrxInstaller::CreateSilent(service())); |
+ installer->set_error_on_unsupported_requirements(true); |
+ |
+ registrar_.Add(this, |
+ chrome::NOTIFICATION_CRX_INSTALLER_DONE, |
+ content::Source<CrxInstaller>(installer.get())); |
+ |
+ registrar_.Add(this, |
+ chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR, |
+ content::Source<CrxInstaller>(installer.get())); |
+ |
+ installer->InstallWebApp(*web_app); |
+} |
+ |
+bool ManagementGenerateAppForLinkFunction::RunAsync() { |
+ if (!user_gesture()) { |
+ error_ = keys::kGestureNeededForGenerateAppForLinkError; |
+ return false; |
+ } |
+ |
+ scoped_ptr<management::GenerateAppForLink::Params> params( |
+ management::GenerateAppForLink::Params::Create(*args_)); |
+ EXTENSION_FUNCTION_VALIDATE(params.get()); |
+ |
+ GURL launch_url(params->url); |
+ if (!launch_url.is_valid() || !launch_url.SchemeIsHTTPOrHTTPS()) { |
+ error_ = ErrorUtils::FormatErrorMessage(keys::kInvalidURLError, |
+ params->url); |
+ return false; |
+ } |
+ |
+ if (params->title.empty()) { |
+ error_ = keys::kEmptyTitleError; |
+ return false; |
+ } |
+ |
+ FaviconService* favicon_service = |
+ FaviconServiceFactory::GetForProfile(GetProfile(), |
+ Profile::EXPLICIT_ACCESS); |
+ if (!favicon_service) { |
+ 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.
|
+ return false; |
+ } |
+ |
+ install_info_.reset(new AppInstallInfo()); |
+ install_info_->launch_url = launch_url; |
+ install_info_->title = params->title; |
+ |
+ favicon_service->GetFaviconImageForURL( |
+ FaviconService::FaviconForURLParams( |
+ launch_url, favicon_base::FAVICON, gfx::kFaviconSize), |
+ base::Bind(&ManagementGenerateAppForLinkFunction::OnFaviconForApp, this), |
+ &cancelable_task_tracker_); |
+ |
+ // Matched with a Release() in OnExtensionLoaded(). |
+ AddRef(); |
+ |
+ // Response is sent async in OnExtensionLoaded(). |
+ return true; |
+} |
+ |
ManagementEventRouter::ManagementEventRouter(Profile* profile) |
: profile_(profile) { |
int types[] = {chrome::NOTIFICATION_EXTENSION_INSTALLED, |