| 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/bind.h" |
| 7 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/compiler_specific.h" |
| 10 #include "base/logging.h" |
| 8 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/web_applications/web_app.h" | 13 #include "chrome/browser/web_applications/web_app.h" |
| 11 #include "chrome/common/chrome_notification_types.h" | 14 #include "chrome/common/chrome_notification_types.h" |
| 12 #include "chrome/common/chrome_switches.h" | 15 #include "chrome/common/chrome_switches.h" |
| 13 #include "chrome/common/extensions/extension_resource.h" | 16 #include "chrome/common/extensions/extension_resource.h" |
| 14 #include "content/public/browser/notification_details.h" | 17 #include "content/public/browser/notification_details.h" |
| 15 #include "content/public/browser/notification_source.h" | 18 #include "content/public/browser/notification_source.h" |
| 16 #include "grit/theme_resources.h" | 19 #include "grit/theme_resources.h" |
| 17 #include "skia/ext/image_operations.h" | 20 #include "skia/ext/image_operations.h" |
| 18 #include "ui/base/resource/resource_bundle.h" | 21 #include "ui/base/resource/resource_bundle.h" |
| 19 | 22 |
| 23 #if defined(OS_WIN) |
| 24 #include "chrome/browser/extensions/app_host_installer_win.h" |
| 25 #endif |
| 26 |
| 20 namespace extensions { | 27 namespace extensions { |
| 21 | 28 |
| 22 namespace { | 29 namespace { |
| 23 | 30 |
| 24 #if defined(OS_MACOSX) | 31 #if defined(OS_MACOSX) |
| 25 const int kDesiredSizes[] = {16, 32, 128, 256, 512}; | 32 const int kDesiredSizes[] = {16, 32, 128, 256, 512}; |
| 26 #else | 33 #else |
| 27 const int kDesiredSizes[] = {32}; | 34 const int kDesiredSizes[] = {32}; |
| 28 #endif | 35 #endif |
| 29 | 36 |
| 30 ShellIntegration::ShortcutInfo ShortcutInfoForExtensionAndProfile( | 37 ShellIntegration::ShortcutInfo ShortcutInfoForExtensionAndProfile( |
| 31 const Extension* extension, Profile* profile) { | 38 const Extension* extension, Profile* profile) { |
| 32 ShellIntegration::ShortcutInfo shortcut_info; | 39 ShellIntegration::ShortcutInfo shortcut_info; |
| 33 shortcut_info.extension_id = extension->id(); | 40 shortcut_info.extension_id = extension->id(); |
| 34 shortcut_info.url = GURL(extension->launch_web_url()); | 41 shortcut_info.url = GURL(extension->launch_web_url()); |
| 35 shortcut_info.title = UTF8ToUTF16(extension->name()); | 42 shortcut_info.title = UTF8ToUTF16(extension->name()); |
| 36 shortcut_info.description = UTF8ToUTF16(extension->description()); | 43 shortcut_info.description = UTF8ToUTF16(extension->description()); |
| 37 shortcut_info.extension_path = extension->path(); | 44 shortcut_info.extension_path = extension->path(); |
| 38 shortcut_info.create_in_applications_menu = true; | 45 shortcut_info.create_in_applications_menu = true; |
| 39 shortcut_info.create_in_quick_launch_bar = true; | 46 shortcut_info.create_in_quick_launch_bar = true; |
| 40 shortcut_info.create_on_desktop = true; | 47 shortcut_info.create_on_desktop = true; |
| 41 shortcut_info.profile_path = profile->GetPath(); | 48 shortcut_info.profile_path = profile->GetPath(); |
| 42 return shortcut_info; | 49 return shortcut_info; |
| 43 } | 50 } |
| 44 | 51 |
| 45 } // namespace | 52 } // namespace |
| 46 | 53 |
| 47 AppShortcutManager::AppShortcutManager(Profile* profile) | 54 AppShortcutManager::AppShortcutManager(Profile* profile) |
| 48 : profile_(profile), | 55 : profile_(profile), |
| 49 tracker_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 56 tracker_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 57 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
| 50 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED, | 58 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED, |
| 51 content::Source<Profile>(profile_)); | 59 content::Source<Profile>(profile_)); |
| 52 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED, | 60 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED, |
| 53 content::Source<Profile>(profile_)); | 61 content::Source<Profile>(profile_)); |
| 54 } | 62 } |
| 55 | 63 |
| 64 AppShortcutManager::~AppShortcutManager() {} |
| 65 |
| 56 void AppShortcutManager::OnImageLoaded(const gfx::Image& image, | 66 void AppShortcutManager::OnImageLoaded(const gfx::Image& image, |
| 57 const std::string& extension_id, | 67 const std::string& extension_id, |
| 58 int index) { | 68 int index) { |
| 59 // If the image failed to load (e.g. if the resource being loaded was empty) | 69 // If the image failed to load (e.g. if the resource being loaded was empty) |
| 60 // use the standard application icon. | 70 // use the standard application icon. |
| 61 if (image.IsEmpty()) { | 71 if (image.IsEmpty()) { |
| 62 gfx::Image default_icon = | 72 gfx::Image default_icon = |
| 63 ResourceBundle::GetSharedInstance().GetImageNamed(IDR_APP_DEFAULT_ICON); | 73 ResourceBundle::GetSharedInstance().GetImageNamed(IDR_APP_DEFAULT_ICON); |
| 64 int size = kDesiredSizes[arraysize(kDesiredSizes) - 1]; | 74 int size = kDesiredSizes[arraysize(kDesiredSizes) - 1]; |
| 65 SkBitmap bmp = skia::ImageOperations::Resize( | 75 SkBitmap bmp = skia::ImageOperations::Resize( |
| 66 *default_icon.ToSkBitmap(), skia::ImageOperations::RESIZE_BEST, | 76 *default_icon.ToSkBitmap(), skia::ImageOperations::RESIZE_BEST, |
| 67 size, size); | 77 size, size); |
| 68 shortcut_info_.favicon = gfx::Image(bmp); | 78 shortcut_info_.favicon = gfx::Image(bmp); |
| 69 } else { | 79 } else { |
| 70 shortcut_info_.favicon = image; | 80 shortcut_info_.favicon = image; |
| 71 } | 81 } |
| 72 | 82 |
| 73 web_app::UpdateAllShortcuts(shortcut_info_); | 83 web_app::UpdateAllShortcuts(shortcut_info_); |
| 74 } | 84 } |
| 75 | 85 |
| 76 void AppShortcutManager::Observe(int type, | 86 void AppShortcutManager::Observe(int type, |
| 77 const content::NotificationSource& source, | 87 const content::NotificationSource& source, |
| 78 const content::NotificationDetails& details) { | 88 const content::NotificationDetails& details) { |
| 79 #if !defined(OS_MACOSX) | 89 #if !defined(OS_MACOSX) |
| 80 switch (type) { | 90 switch (type) { |
| 81 case chrome::NOTIFICATION_EXTENSION_INSTALLED: { | 91 case chrome::NOTIFICATION_EXTENSION_INSTALLED: { |
| 82 const Extension* extension = content::Details<const Extension>( | 92 const Extension* extension = content::Details<const Extension>( |
| 83 details).ptr(); | 93 details).ptr(); |
| 84 if (extension->is_platform_app()) | 94 if (extension->is_platform_app()) { |
| 95 #if defined(OS_WIN) |
| 96 extensions::AppHostInstaller::EnsureAppHostInstalled( |
| 97 base::Bind(&AppShortcutManager::OnAppHostInstallationComplete, |
| 98 weak_factory_.GetWeakPtr(), extension)); |
| 99 #else |
| 85 UpdateApplicationShortcuts(extension); | 100 UpdateApplicationShortcuts(extension); |
| 101 #endif |
| 102 } |
| 86 break; | 103 break; |
| 87 } | 104 } |
| 88 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED: { | 105 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED: { |
| 89 const Extension* extension = content::Details<const Extension>( | 106 const Extension* extension = content::Details<const Extension>( |
| 90 details).ptr(); | 107 details).ptr(); |
| 91 DeleteApplicationShortcuts(extension); | 108 DeleteApplicationShortcuts(extension); |
| 92 break; | 109 break; |
| 93 } | 110 } |
| 94 default: | 111 default: |
| 95 NOTREACHED(); | 112 NOTREACHED(); |
| 96 } | 113 } |
| 97 #endif | 114 #endif |
| 98 } | 115 } |
| 99 | 116 |
| 117 #if defined(OS_WIN) |
| 118 void AppShortcutManager::OnAppHostInstallationComplete( |
| 119 const Extension* extension, bool app_host_install_success) { |
| 120 if (!app_host_install_success) { |
| 121 // Do not create shortcuts if App Host fails to install. |
| 122 LOG(ERROR) << "Application Runtime installation failed."; |
| 123 return; |
| 124 } |
| 125 UpdateApplicationShortcuts(extension); |
| 126 } |
| 127 #endif |
| 128 |
| 100 void AppShortcutManager::UpdateApplicationShortcuts( | 129 void AppShortcutManager::UpdateApplicationShortcuts( |
| 101 const Extension* extension) { | 130 const Extension* extension) { |
| 102 shortcut_info_ = ShortcutInfoForExtensionAndProfile(extension, profile_); | 131 shortcut_info_ = ShortcutInfoForExtensionAndProfile(extension, profile_); |
| 103 | 132 |
| 104 std::vector<ImageLoadingTracker::ImageRepresentation> info_list; | 133 std::vector<ImageLoadingTracker::ImageRepresentation> info_list; |
| 105 for (size_t i = 0; i < arraysize(kDesiredSizes); ++i) { | 134 for (size_t i = 0; i < arraysize(kDesiredSizes); ++i) { |
| 106 int size = kDesiredSizes[i]; | 135 int size = kDesiredSizes[i]; |
| 107 ExtensionResource resource = extension->GetIconResource( | 136 ExtensionResource resource = extension->GetIconResource( |
| 108 size, ExtensionIconSet::MATCH_EXACTLY); | 137 size, ExtensionIconSet::MATCH_EXACTLY); |
| 109 if (!resource.empty()) { | 138 if (!resource.empty()) { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 } | 171 } |
| 143 | 172 |
| 144 void AppShortcutManager::DeleteApplicationShortcuts( | 173 void AppShortcutManager::DeleteApplicationShortcuts( |
| 145 const Extension* extension) { | 174 const Extension* extension) { |
| 146 ShellIntegration::ShortcutInfo delete_info = | 175 ShellIntegration::ShortcutInfo delete_info = |
| 147 ShortcutInfoForExtensionAndProfile(extension, profile_); | 176 ShortcutInfoForExtensionAndProfile(extension, profile_); |
| 148 web_app::DeleteAllShortcuts(delete_info); | 177 web_app::DeleteAllShortcuts(delete_info); |
| 149 } | 178 } |
| 150 | 179 |
| 151 } // namespace extensions | 180 } // namespace extensions |
| OLD | NEW |