Chromium Code Reviews| 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/app_shortcut_manager.h" | 5 #include "chrome/browser/extensions/app_shortcut_manager.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
| 9 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h" | |
| 10 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/web_applications/web_app.h" | 10 #include "chrome/browser/web_applications/web_app.h" |
| 12 #include "chrome/common/chrome_notification_types.h" | 11 #include "chrome/common/chrome_notification_types.h" |
| 13 #include "chrome/common/chrome_switches.h" | 12 #include "chrome/common/chrome_switches.h" |
| 14 #include "chrome/common/extensions/extension_resource.h" | 13 #include "chrome/common/extensions/extension_resource.h" |
| 15 #include "content/public/browser/notification_details.h" | 14 #include "content/public/browser/notification_details.h" |
| 16 #include "content/public/browser/notification_source.h" | 15 #include "content/public/browser/notification_source.h" |
| 17 #include "grit/theme_resources.h" | 16 #include "grit/theme_resources.h" |
| 17 #include "skia/ext/image_operations.h" | |
| 18 #include "ui/base/resource/resource_bundle.h" | |
| 18 | 19 |
| 19 namespace { | 20 namespace { |
| 20 // Allow tests to disable shortcut creation, to prevent developers' desktops | 21 // Allow tests to disable shortcut creation, to prevent developers' desktops |
| 21 // becoming overrun with shortcuts. | 22 // becoming overrun with shortcuts. |
| 22 bool disable_shortcut_creation_for_tests = false; | 23 bool disable_shortcut_creation_for_tests = false; |
| 23 } // namespace | 24 |
| 25 #if defined(OS_MACOSX) | |
| 26 const int kDesiredSizes[] = {16, 32, 128, 256, 512}; | |
| 27 #else | |
| 28 const int kDesiredSizes[] = {32}; | |
| 29 #endif | |
| 30 } // namespace | |
| 24 | 31 |
| 25 AppShortcutManager::AppShortcutManager(Profile* profile) | 32 AppShortcutManager::AppShortcutManager(Profile* profile) |
| 26 : profile_(profile), | 33 : profile_(profile), |
| 27 tracker_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 34 tracker_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| 28 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED, | 35 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED, |
| 29 content::Source<Profile>(profile_)); | 36 content::Source<Profile>(profile_)); |
| 30 } | 37 } |
| 31 | 38 |
| 32 void AppShortcutManager::OnImageLoaded(SkBitmap *image, | 39 void AppShortcutManager::OnImageLoaded(const gfx::Image* image, |
| 33 const ExtensionResource &resource, | 40 const std::string& extension_id, |
| 34 int index) { | 41 int index) { |
| 35 // If the image failed to load (e.g. if the resource being loaded was empty) | 42 // If the image failed to load (e.g. if the resource being loaded was empty) |
| 36 // use the standard application icon. | 43 // use the standard application icon. |
| 37 if (!image || image->isNull()) | 44 if (!image) { |
| 38 image = ExtensionIconSource::LoadImageByResourceId(IDR_APP_DEFAULT_ICON); | 45 gfx::Image default_icon = |
| 46 ResourceBundle::GetSharedInstance().GetImageNamed(IDR_APP_DEFAULT_ICON); | |
| 47 SkBitmap bmp = skia::ImageOperations::Resize( | |
| 48 *default_icon.ToSkBitmap(), skia::ImageOperations::RESIZE_BEST, | |
| 49 kDesiredSizes[0], kDesiredSizes[0]); | |
|
Mihai Parparita -not on Chrome
2012/02/23 00:19:41
This ends up using the 16x16 size on the Mac, defa
sail
2012/02/23 02:54:27
Done.
| |
| 50 shortcut_info_.favicon = gfx::Image(new SkBitmap(bmp)); | |
| 51 } else { | |
| 52 shortcut_info_.favicon = *image; | |
| 53 } | |
| 39 | 54 |
| 40 shortcut_info_.favicon = *image; | |
| 41 web_app::CreateShortcut(profile_->GetPath(), shortcut_info_); | 55 web_app::CreateShortcut(profile_->GetPath(), shortcut_info_); |
| 42 } | 56 } |
| 43 | 57 |
| 44 void AppShortcutManager::Observe(int type, | 58 void AppShortcutManager::Observe(int type, |
| 45 const content::NotificationSource& source, | 59 const content::NotificationSource& source, |
| 46 const content::NotificationDetails& details) { | 60 const content::NotificationDetails& details) { |
| 47 DCHECK(type == chrome::NOTIFICATION_EXTENSION_INSTALLED); | 61 DCHECK(type == chrome::NOTIFICATION_EXTENSION_INSTALLED); |
| 48 const Extension* extension = content::Details<const Extension>(details).ptr(); | 62 const Extension* extension = content::Details<const Extension>(details).ptr(); |
| 49 if (!disable_shortcut_creation_for_tests && extension->is_platform_app()) | 63 if (!disable_shortcut_creation_for_tests && extension->is_platform_app()) |
| 50 InstallApplicationShortcuts(extension); | 64 InstallApplicationShortcuts(extension); |
| 51 } | 65 } |
| 52 | 66 |
| 53 // static | 67 // static |
| 54 void AppShortcutManager::SetShortcutCreationDisabledForTesting(bool disabled) { | 68 void AppShortcutManager::SetShortcutCreationDisabledForTesting(bool disabled) { |
| 55 disable_shortcut_creation_for_tests = disabled; | 69 disable_shortcut_creation_for_tests = disabled; |
| 56 } | 70 } |
| 57 | 71 |
| 58 void AppShortcutManager::InstallApplicationShortcuts( | 72 void AppShortcutManager::InstallApplicationShortcuts( |
| 59 const Extension* extension) { | 73 const Extension* extension) { |
| 60 #if defined(OS_MACOSX) | 74 #if defined(OS_MACOSX) |
| 61 // TODO(sail): For now only install shortcuts if enable platform apps is true. | 75 // TODO(sail): For now only install shortcuts if enable platform apps is true. |
| 62 if (!CommandLine::ForCurrentProcess()->HasSwitch( | 76 if (!CommandLine::ForCurrentProcess()->HasSwitch( |
| 63 switches::kEnablePlatformApps)) { | 77 switches::kEnablePlatformApps)) { |
| 64 return; | 78 return; |
| 65 } | 79 } |
| 66 #endif | 80 #endif |
| 67 | 81 |
| 68 const int kAppIconSize = 32; | |
| 69 | |
| 70 shortcut_info_.extension_id = extension->id(); | 82 shortcut_info_.extension_id = extension->id(); |
| 71 shortcut_info_.url = GURL(extension->launch_web_url()); | 83 shortcut_info_.url = GURL(extension->launch_web_url()); |
| 72 shortcut_info_.title = UTF8ToUTF16(extension->name()); | 84 shortcut_info_.title = UTF8ToUTF16(extension->name()); |
| 73 shortcut_info_.description = UTF8ToUTF16(extension->description()); | 85 shortcut_info_.description = UTF8ToUTF16(extension->description()); |
| 74 shortcut_info_.extension_path = extension->path(); | 86 shortcut_info_.extension_path = extension->path(); |
| 75 shortcut_info_.create_in_applications_menu = true; | 87 shortcut_info_.create_in_applications_menu = true; |
| 76 shortcut_info_.create_in_quick_launch_bar = true; | 88 shortcut_info_.create_in_quick_launch_bar = true; |
| 77 shortcut_info_.create_on_desktop = true; | 89 shortcut_info_.create_on_desktop = true; |
| 78 | 90 |
| 79 // The icon will be resized to |max_size|. | 91 std::vector<ImageLoadingTracker::ImageInfo> info_list; |
| 80 const gfx::Size max_size(kAppIconSize, kAppIconSize); | 92 for (size_t i = 0; i < arraysize(kDesiredSizes); ++i) { |
| 81 | 93 int size = kDesiredSizes[i]; |
| 82 // Look for an icon. If there is no icon at the ideal size, we will resize | 94 ExtensionResource resource = extension->GetIconResource( |
| 83 // whatever we can get. Making a large icon smaller is prefered to making a | 95 size, ExtensionIconSet::MATCH_EXACTLY); |
| 84 // small icon larger, so look for a larger icon first: | 96 if (!resource.empty()) { |
| 85 ExtensionResource icon_resource = extension->GetIconResource( | 97 info_list.push_back( |
| 86 kAppIconSize, | 98 ImageLoadingTracker::ImageInfo(resource, gfx::Size(size, size))); |
| 87 ExtensionIconSet::MATCH_BIGGER); | 99 } |
| 88 | |
| 89 // If no icon exists that is the desired size or larger, get the | |
| 90 // largest icon available: | |
| 91 if (icon_resource.empty()) { | |
| 92 icon_resource = extension->GetIconResource( | |
| 93 kAppIconSize, | |
| 94 ExtensionIconSet::MATCH_SMALLER); | |
| 95 } | 100 } |
| 96 | 101 |
| 97 // icon_resource may still be empty at this point, in which case LoadImage | 102 if (info_list.empty()) { |
| 103 size_t i = arraysize(kDesiredSizes) - 1; | |
| 104 int size = kDesiredSizes[i]; | |
| 105 | |
| 106 // If there is no icon at the desired sizes, we will resize what we can get. | |
| 107 // Making a large icon smaller is prefered to making a small icon larger, so | |
| 108 // look for a larger icon first: | |
| 109 ExtensionResource resource = extension->GetIconResource( | |
| 110 size, ExtensionIconSet::MATCH_BIGGER); | |
| 111 if (resource.empty()) { | |
| 112 resource = extension->GetIconResource( | |
| 113 size, ExtensionIconSet::MATCH_SMALLER); | |
| 114 } | |
| 115 info_list.push_back( | |
| 116 ImageLoadingTracker::ImageInfo(resource, gfx::Size(size, size))); | |
| 117 } | |
| 118 | |
| 119 // icon_resources may still be empty at this point, in which case LoadImage | |
| 98 // which call the OnImageLoaded callback with a NULL image and exit | 120 // which call the OnImageLoaded callback with a NULL image and exit |
| 99 // immediately. | 121 // immediately. |
| 100 tracker_.LoadImage(extension, | 122 tracker_.LoadImages(extension, info_list, ImageLoadingTracker::DONT_CACHE); |
| 101 icon_resource, | |
| 102 max_size, | |
| 103 ImageLoadingTracker::DONT_CACHE); | |
| 104 } | 123 } |
| OLD | NEW |