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.IsEmpty()) { |
38 image = ExtensionIconSource::LoadImageByResourceId(IDR_APP_DEFAULT_ICON); | 45 gfx::Image default_icon = |
| 46 ResourceBundle::GetSharedInstance().GetImageNamed(IDR_APP_DEFAULT_ICON); |
| 47 int size = kDesiredSizes[arraysize(kDesiredSizes) - 1]; |
| 48 SkBitmap bmp = skia::ImageOperations::Resize( |
| 49 *default_icon.ToSkBitmap(), skia::ImageOperations::RESIZE_BEST, |
| 50 size, size); |
| 51 shortcut_info_.favicon = gfx::Image(bmp); |
| 52 } else { |
| 53 shortcut_info_.favicon = image; |
| 54 } |
39 | 55 |
40 shortcut_info_.favicon = *image; | |
41 web_app::CreateShortcut(profile_->GetPath(), shortcut_info_); | 56 web_app::CreateShortcut(profile_->GetPath(), shortcut_info_); |
42 } | 57 } |
43 | 58 |
44 void AppShortcutManager::Observe(int type, | 59 void AppShortcutManager::Observe(int type, |
45 const content::NotificationSource& source, | 60 const content::NotificationSource& source, |
46 const content::NotificationDetails& details) { | 61 const content::NotificationDetails& details) { |
47 DCHECK(type == chrome::NOTIFICATION_EXTENSION_INSTALLED); | 62 DCHECK(type == chrome::NOTIFICATION_EXTENSION_INSTALLED); |
48 const Extension* extension = content::Details<const Extension>(details).ptr(); | 63 const Extension* extension = content::Details<const Extension>(details).ptr(); |
49 if (!disable_shortcut_creation_for_tests && extension->is_platform_app()) | 64 if (!disable_shortcut_creation_for_tests && extension->is_platform_app()) |
50 InstallApplicationShortcuts(extension); | 65 InstallApplicationShortcuts(extension); |
51 } | 66 } |
52 | 67 |
53 // static | 68 // static |
54 void AppShortcutManager::SetShortcutCreationDisabledForTesting(bool disabled) { | 69 void AppShortcutManager::SetShortcutCreationDisabledForTesting(bool disabled) { |
55 disable_shortcut_creation_for_tests = disabled; | 70 disable_shortcut_creation_for_tests = disabled; |
56 } | 71 } |
57 | 72 |
58 void AppShortcutManager::InstallApplicationShortcuts( | 73 void AppShortcutManager::InstallApplicationShortcuts( |
59 const Extension* extension) { | 74 const Extension* extension) { |
60 #if defined(OS_MACOSX) | 75 #if defined(OS_MACOSX) |
61 // TODO(sail): For now only install shortcuts if enable platform apps is true. | 76 // TODO(sail): For now only install shortcuts if enable platform apps is true. |
62 if (!CommandLine::ForCurrentProcess()->HasSwitch( | 77 if (!CommandLine::ForCurrentProcess()->HasSwitch( |
63 switches::kEnablePlatformApps)) { | 78 switches::kEnablePlatformApps)) { |
64 return; | 79 return; |
65 } | 80 } |
66 #endif | 81 #endif |
67 | 82 |
68 const int kAppIconSize = 32; | |
69 | |
70 shortcut_info_.extension_id = extension->id(); | 83 shortcut_info_.extension_id = extension->id(); |
71 shortcut_info_.url = GURL(extension->launch_web_url()); | 84 shortcut_info_.url = GURL(extension->launch_web_url()); |
72 shortcut_info_.title = UTF8ToUTF16(extension->name()); | 85 shortcut_info_.title = UTF8ToUTF16(extension->name()); |
73 shortcut_info_.description = UTF8ToUTF16(extension->description()); | 86 shortcut_info_.description = UTF8ToUTF16(extension->description()); |
74 shortcut_info_.extension_path = extension->path(); | 87 shortcut_info_.extension_path = extension->path(); |
75 shortcut_info_.create_in_applications_menu = true; | 88 shortcut_info_.create_in_applications_menu = true; |
76 shortcut_info_.create_in_quick_launch_bar = true; | 89 shortcut_info_.create_in_quick_launch_bar = true; |
77 shortcut_info_.create_on_desktop = true; | 90 shortcut_info_.create_on_desktop = true; |
78 | 91 |
79 // The icon will be resized to |max_size|. | 92 std::vector<ImageLoadingTracker::ImageInfo> info_list; |
80 const gfx::Size max_size(kAppIconSize, kAppIconSize); | 93 for (size_t i = 0; i < arraysize(kDesiredSizes); ++i) { |
81 | 94 int size = kDesiredSizes[i]; |
82 // Look for an icon. If there is no icon at the ideal size, we will resize | 95 ExtensionResource resource = extension->GetIconResource( |
83 // whatever we can get. Making a large icon smaller is prefered to making a | 96 size, ExtensionIconSet::MATCH_EXACTLY); |
84 // small icon larger, so look for a larger icon first: | 97 if (!resource.empty()) { |
85 ExtensionResource icon_resource = extension->GetIconResource( | 98 info_list.push_back( |
86 kAppIconSize, | 99 ImageLoadingTracker::ImageInfo(resource, gfx::Size(size, size))); |
87 ExtensionIconSet::MATCH_BIGGER); | 100 } |
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 } | 101 } |
96 | 102 |
97 // icon_resource may still be empty at this point, in which case LoadImage | 103 if (info_list.empty()) { |
98 // which call the OnImageLoaded callback with a NULL image and exit | 104 size_t i = arraysize(kDesiredSizes) - 1; |
| 105 int size = kDesiredSizes[i]; |
| 106 |
| 107 // If there is no icon at the desired sizes, we will resize what we can get. |
| 108 // Making a large icon smaller is prefered to making a small icon larger, so |
| 109 // look for a larger icon first: |
| 110 ExtensionResource resource = extension->GetIconResource( |
| 111 size, ExtensionIconSet::MATCH_BIGGER); |
| 112 if (resource.empty()) { |
| 113 resource = extension->GetIconResource( |
| 114 size, ExtensionIconSet::MATCH_SMALLER); |
| 115 } |
| 116 info_list.push_back( |
| 117 ImageLoadingTracker::ImageInfo(resource, gfx::Size(size, size))); |
| 118 } |
| 119 |
| 120 // |icon_resources| may still be empty at this point, in which case LoadImage |
| 121 // will call the OnImageLoaded callback with an empty image and exit |
99 // immediately. | 122 // immediately. |
100 tracker_.LoadImage(extension, | 123 tracker_.LoadImages(extension, info_list, ImageLoadingTracker::DONT_CACHE); |
101 icon_resource, | |
102 max_size, | |
103 ImageLoadingTracker::DONT_CACHE); | |
104 } | 124 } |
OLD | NEW |