diff options
author | jeremya@chromium.org <jeremya@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-02-05 23:20:50 +0000 |
---|---|---|
committer | jeremya@chromium.org <jeremya@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-02-05 23:20:50 +0000 |
commit | 4fd2ab531d34619af2b1f115b79672998906cec2 (patch) | |
tree | 26ed07e4dbbc1e3d51484871abec8990eb416f7b /chrome | |
parent | bcc7784d0033b3c56c20ce278f102cd46ca39f47 (diff) | |
download | chromium_src-4fd2ab531d34619af2b1f115b79672998906cec2.zip chromium_src-4fd2ab531d34619af2b1f115b79672998906cec2.tar.gz chromium_src-4fd2ab531d34619af2b1f115b79672998906cec2.tar.bz2 |
Load icon before trying to create an app shortcut.
This includes a refactor to make the app icon loading infrastructure more
central.
Review URL: https://codereview.chromium.org/12183015
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@180815 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/browser/extensions/app_shortcut_manager.cc | 84 | ||||
-rw-r--r-- | chrome/browser/extensions/app_shortcut_manager.h | 8 | ||||
-rw-r--r-- | chrome/browser/ui/cocoa/browser_window_cocoa.mm | 5 | ||||
-rw-r--r-- | chrome/browser/ui/web_applications/web_app_ui.cc | 88 | ||||
-rw-r--r-- | chrome/browser/ui/web_applications/web_app_ui.h | 16 |
5 files changed, 107 insertions, 94 deletions
diff --git a/chrome/browser/extensions/app_shortcut_manager.cc b/chrome/browser/extensions/app_shortcut_manager.cc index ea467f0..467c844 100644 --- a/chrome/browser/extensions/app_shortcut_manager.cc +++ b/chrome/browser/extensions/app_shortcut_manager.cc @@ -29,26 +29,6 @@ namespace extensions { -namespace { - -#if defined(OS_MACOSX) -const int kDesiredSizes[] = {16, 32, 128, 256, 512}; -#else -const int kDesiredSizes[] = {32}; -#endif - -ShellIntegration::ShortcutInfo ShortcutInfoForExtensionAndProfile( - const Extension* extension, Profile* profile) { - ShellIntegration::ShortcutInfo shortcut_info; - web_app::UpdateShortcutInfoForApp(*extension, profile, &shortcut_info); - shortcut_info.create_in_applications_menu = true; - shortcut_info.create_in_quick_launch_bar = true; - shortcut_info.create_on_desktop = true; - return shortcut_info; -} - -} // namespace - AppShortcutManager::AppShortcutManager(Profile* profile) : profile_(profile), ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { @@ -113,72 +93,14 @@ void AppShortcutManager::OnAppHostInstallationComplete( void AppShortcutManager::UpdateApplicationShortcuts( const Extension* extension) { - shortcut_info_ = ShortcutInfoForExtensionAndProfile(extension, profile_); - - std::vector<ImageLoader::ImageRepresentation> info_list; - for (size_t i = 0; i < arraysize(kDesiredSizes); ++i) { - int size = kDesiredSizes[i]; - ExtensionResource resource = extension->GetIconResource( - size, ExtensionIconSet::MATCH_EXACTLY); - if (!resource.empty()) { - info_list.push_back(ImageLoader::ImageRepresentation( - resource, - ImageLoader::ImageRepresentation::RESIZE_WHEN_LARGER, - gfx::Size(size, size), - ui::SCALE_FACTOR_100P)); - } - } - - if (info_list.empty()) { - size_t i = arraysize(kDesiredSizes) - 1; - int size = kDesiredSizes[i]; - - // If there is no icon at the desired sizes, we will resize what we can get. - // Making a large icon smaller is preferred to making a small icon larger, - // so look for a larger icon first: - ExtensionResource resource = extension->GetIconResource( - size, ExtensionIconSet::MATCH_BIGGER); - if (resource.empty()) { - resource = extension->GetIconResource( - size, ExtensionIconSet::MATCH_SMALLER); - } - info_list.push_back(ImageLoader::ImageRepresentation( - resource, - ImageLoader::ImageRepresentation::RESIZE_WHEN_LARGER, - gfx::Size(size, size), - ui::SCALE_FACTOR_100P)); - } - - // |info_list| may still be empty at this point, in which case LoadImage - // will call the OnImageLoaded callback with an empty image and exit - // immediately. - ImageLoader::Get(profile_)->LoadImagesAsync(extension, info_list, - base::Bind(&AppShortcutManager::OnImageLoaded, - weak_factory_.GetWeakPtr())); -} - -void AppShortcutManager::OnImageLoaded(const gfx::Image& image) { - // If the image failed to load (e.g. if the resource being loaded was empty) - // use the standard application icon. - if (image.IsEmpty()) { - gfx::Image default_icon = - ResourceBundle::GetSharedInstance().GetImageNamed(IDR_APP_DEFAULT_ICON); - int size = kDesiredSizes[arraysize(kDesiredSizes) - 1]; - SkBitmap bmp = skia::ImageOperations::Resize( - *default_icon.ToSkBitmap(), skia::ImageOperations::RESIZE_BEST, - size, size); - shortcut_info_.favicon = gfx::Image::CreateFrom1xBitmap(bmp); - } else { - shortcut_info_.favicon = image; - } - - web_app::UpdateAllShortcuts(shortcut_info_); + web_app::UpdateShortcutInfoAndIconForApp(*extension, profile_, + base::Bind(&web_app::UpdateAllShortcuts)); } void AppShortcutManager::DeleteApplicationShortcuts( const Extension* extension) { ShellIntegration::ShortcutInfo delete_info = - ShortcutInfoForExtensionAndProfile(extension, profile_); + web_app::ShortcutInfoForExtensionAndProfile(extension, profile_); web_app::DeleteAllShortcuts(delete_info); } diff --git a/chrome/browser/extensions/app_shortcut_manager.h b/chrome/browser/extensions/app_shortcut_manager.h index c855683..d3b16d6 100644 --- a/chrome/browser/extensions/app_shortcut_manager.h +++ b/chrome/browser/extensions/app_shortcut_manager.h @@ -30,12 +30,6 @@ class AppShortcutManager : public content::NotificationObserver { private: void UpdateApplicationShortcuts(const Extension* extension); - // Implement ImageLoadingTracker::Observer. |tracker_| is used to - // load the application's icon, which is done when we start creating an - // application's shortcuts. This method receives the icon, and completes - // the process of installing the shortcuts. - void OnImageLoaded(const gfx::Image& image); - #if defined(OS_WIN) void OnAppHostInstallationComplete(scoped_refptr<Extension> extension, bool app_host_install_success); @@ -47,8 +41,6 @@ class AppShortcutManager : public content::NotificationObserver { Profile* profile_; // Fields used when installing application shortcuts. - ShellIntegration::ShortcutInfo shortcut_info_; - base::WeakPtrFactory<AppShortcutManager> weak_factory_; DISALLOW_COPY_AND_ASSIGN(AppShortcutManager); diff --git a/chrome/browser/ui/cocoa/browser_window_cocoa.mm b/chrome/browser/ui/cocoa/browser_window_cocoa.mm index 69da961..d32c920 100644 --- a/chrome/browser/ui/cocoa/browser_window_cocoa.mm +++ b/chrome/browser/ui/cocoa/browser_window_cocoa.mm @@ -567,9 +567,8 @@ void BrowserWindowCocoa::ShowCreateChromeAppShortcutsDialog( Profile* profile, const extensions::Extension* app) { // Normally we would show a dialog, but since we always create the app // shortcut in /Applications there are no options for the user to choose. - ShellIntegration::ShortcutInfo shortcut_info; - web_app::UpdateShortcutInfoForApp(*app, profile, &shortcut_info); - web_app::CreateShortcuts(shortcut_info); + web_app::UpdateShortcutInfoAndIconForApp(*app, profile, + base::Bind(&web_app::CreateShortcuts)); } void BrowserWindowCocoa::Cut() { diff --git a/chrome/browser/ui/web_applications/web_app_ui.cc b/chrome/browser/ui/web_applications/web_app_ui.cc index a1734d9..a41fbfb 100644 --- a/chrome/browser/ui/web_applications/web_app_ui.cc +++ b/chrome/browser/ui/web_applications/web_app_ui.cc @@ -10,6 +10,7 @@ #include "base/path_service.h" #include "base/string16.h" #include "base/utf_string_conversions.h" +#include "chrome/browser/extensions/image_loader.h" #include "chrome/browser/extensions/tab_helper.h" #include "chrome/browser/favicon/favicon_tab_helper.h" #include "chrome/browser/favicon/favicon_util.h" @@ -23,7 +24,10 @@ #include "content/public/browser/notification_source.h" #include "content/public/browser/web_contents.h" #include "googleurl/src/gurl.h" +#include "grit/theme_resources.h" +#include "skia/ext/image_operations.h" #include "third_party/skia/include/core/SkBitmap.h" +#include "ui/base/resource/resource_bundle.h" #if defined(OS_POSIX) && !defined(OS_MACOSX) #include "base/environment.h" @@ -40,6 +44,12 @@ using content::WebContents; namespace { +#if defined(OS_MACOSX) +const int kDesiredSizes[] = {16, 32, 128, 256, 512}; +#else +const int kDesiredSizes[] = {32}; +#endif + #if defined(OS_WIN) // UpdateShortcutWorker holds all context data needed for update shortcut. // It schedules a pre-update check to find all shortcuts that needs to be @@ -311,10 +321,40 @@ void UpdateShortcutWorker::DeleteMeOnUIThread() { } #endif // defined(OS_WIN) +void OnImageLoaded(ShellIntegration::ShortcutInfo shortcut_info, + web_app::ShortcutInfoCallback callback, + const gfx::Image& image) { + // If the image failed to load (e.g. if the resource being loaded was empty) + // use the standard application icon. + if (image.IsEmpty()) { + gfx::Image default_icon = + ResourceBundle::GetSharedInstance().GetImageNamed(IDR_APP_DEFAULT_ICON); + int size = kDesiredSizes[arraysize(kDesiredSizes) - 1]; + SkBitmap bmp = skia::ImageOperations::Resize( + *default_icon.ToSkBitmap(), skia::ImageOperations::RESIZE_BEST, + size, size); + shortcut_info.favicon = gfx::Image::CreateFrom1xBitmap(bmp); + } else { + shortcut_info.favicon = image; + } + + callback.Run(shortcut_info); +} + } // namespace namespace web_app { +ShellIntegration::ShortcutInfo ShortcutInfoForExtensionAndProfile( + const extensions::Extension* extension, Profile* profile) { + ShellIntegration::ShortcutInfo shortcut_info; + web_app::UpdateShortcutInfoForApp(*extension, profile, &shortcut_info); + shortcut_info.create_in_applications_menu = true; + shortcut_info.create_in_quick_launch_bar = true; + shortcut_info.create_on_desktop = true; + return shortcut_info; +} + void GetShortcutInfoForTab(WebContents* web_contents, ShellIntegration::ShortcutInfo* info) { DCHECK(info); // Must provide a valid info. @@ -359,4 +399,52 @@ void UpdateShortcutInfoForApp(const extensions::Extension& app, shortcut_info->profile_path = profile->GetPath(); } +void UpdateShortcutInfoAndIconForApp( + const extensions::Extension& extension, + Profile* profile, + const web_app::ShortcutInfoCallback& callback) { + ShellIntegration::ShortcutInfo shortcut_info = + ShortcutInfoForExtensionAndProfile(&extension, profile); + + std::vector<extensions::ImageLoader::ImageRepresentation> info_list; + for (size_t i = 0; i < arraysize(kDesiredSizes); ++i) { + int size = kDesiredSizes[i]; + ExtensionResource resource = extension.GetIconResource( + size, ExtensionIconSet::MATCH_EXACTLY); + if (!resource.empty()) { + info_list.push_back(extensions::ImageLoader::ImageRepresentation( + resource, + extensions::ImageLoader::ImageRepresentation::RESIZE_WHEN_LARGER, + gfx::Size(size, size), + ui::SCALE_FACTOR_100P)); + } + } + + if (info_list.empty()) { + size_t i = arraysize(kDesiredSizes) - 1; + int size = kDesiredSizes[i]; + + // If there is no icon at the desired sizes, we will resize what we can get. + // Making a large icon smaller is preferred to making a small icon larger, + // so look for a larger icon first: + ExtensionResource resource = extension.GetIconResource( + size, ExtensionIconSet::MATCH_BIGGER); + if (resource.empty()) { + resource = extension.GetIconResource( + size, ExtensionIconSet::MATCH_SMALLER); + } + info_list.push_back(extensions::ImageLoader::ImageRepresentation( + resource, + extensions::ImageLoader::ImageRepresentation::RESIZE_WHEN_LARGER, + gfx::Size(size, size), + ui::SCALE_FACTOR_100P)); + } + + // |info_list| may still be empty at this point, in which case LoadImage + // will call the OnImageLoaded callback with an empty image and exit + // immediately. + extensions::ImageLoader::Get(profile)->LoadImagesAsync(&extension, info_list, + base::Bind(&OnImageLoaded, shortcut_info, callback)); +} + } // namespace web_app diff --git a/chrome/browser/ui/web_applications/web_app_ui.h b/chrome/browser/ui/web_applications/web_app_ui.h index 627ab9e..19ee0ce 100644 --- a/chrome/browser/ui/web_applications/web_app_ui.h +++ b/chrome/browser/ui/web_applications/web_app_ui.h @@ -7,6 +7,7 @@ #include <vector> +#include "base/callback.h" #include "chrome/browser/shell_integration.h" namespace content { @@ -21,6 +22,12 @@ class Profile; namespace web_app { +typedef base::Callback<void(const ShellIntegration::ShortcutInfo&)> + ShortcutInfoCallback; + +ShellIntegration::ShortcutInfo ShortcutInfoForExtensionAndProfile( + const extensions::Extension* extension, Profile* profile); + // Extracts shortcut info of the given WebContents. void GetShortcutInfoForTab(content::WebContents* web_contents, ShellIntegration::ShortcutInfo* info); @@ -33,12 +40,17 @@ void GetShortcutInfoForTab(content::WebContents* web_contents, void UpdateShortcutForTabContents(content::WebContents* web_contents); // Updates the shortcut info for |extension| and |profile|. -// TODO(benwells): make this download the icon as well to remove boilerplate -// code from call sites. void UpdateShortcutInfoForApp(const extensions::Extension& extension, Profile* profile, ShellIntegration::ShortcutInfo* shortcut_info); +// Fetches the icon for |extension| and calls |callback| with shortcut info +// filled out as by UpdateShortcutInfoForApp. +void UpdateShortcutInfoAndIconForApp( + const extensions::Extension& extension, + Profile* profile, + const ShortcutInfoCallback& callback); + } // namespace web_app #endif // CHROME_BROWSER_UI_WEB_APPLICATIONS_WEB_APP_UI_H_ |