Chromium Code Reviews| Index: chrome/browser/extensions/api/management/management_api.cc |
| =================================================================== |
| --- chrome/browser/extensions/api/management/management_api.cc (revision 268958) |
| +++ chrome/browser/extensions/api/management/management_api.cc (working copy) |
| @@ -21,9 +21,11 @@ |
| #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/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 +37,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 +50,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" |
| @@ -720,6 +725,119 @@ |
| 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(); |
| + break; |
| + } |
| + // Fall through if the information does not match. |
| + } |
| + 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; |
| + |
| + 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); |
| + } |
| + |
| + 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); |
|
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.
|
| +} |
| + |
| +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; |
| + 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_); |
|
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.
|
| + |
| + // 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, |