| 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/profiles/profile_shortcut_manager_win.h" | 5 #include "chrome/browser/profiles/profile_shortcut_manager.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/file_path.h" | 8 #include "base/command_line.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| 11 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
| 12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 13 #include "chrome/browser/app_icon_win.h" | 13 #include "chrome/browser/app_icon_win.h" |
| 14 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/prefs/pref_service.h" | 15 #include "chrome/browser/prefs/pref_service.h" |
| 16 #include "chrome/browser/profiles/profile_info_cache.h" | 16 #include "chrome/browser/profiles/profile_info_cache.h" |
| 17 #include "chrome/browser/profiles/profile_info_util.h" | 17 #include "chrome/browser/profiles/profile_info_util.h" |
| 18 #include "chrome/browser/profiles/profile_manager.h" | 18 #include "chrome/browser/profiles/profile_manager.h" |
| 19 #include "chrome/common/chrome_constants.h" | 19 #include "chrome/common/chrome_constants.h" |
| 20 #include "chrome/common/chrome_switches.h" | 20 #include "chrome/common/chrome_switches.h" |
| 21 #include "chrome/common/pref_names.h" | 21 #include "chrome/common/pref_names.h" |
| 22 #include "chrome/installer/util/auto_launch_util.h" | 22 #include "chrome/installer/util/auto_launch_util.h" |
| 23 #include "chrome/installer/util/browser_distribution.h" | 23 #include "chrome/installer/util/browser_distribution.h" |
| 24 #include "chrome/installer/util/shell_util.h" | 24 #include "chrome/installer/util/shell_util.h" |
| 25 #include "content/public/browser/browser_thread.h" | 25 #include "content/public/browser/browser_thread.h" |
| 26 #include "grit/generated_resources.h" | 26 #include "grit/generated_resources.h" |
| 27 #include "skia/ext/image_operations.h" | 27 #include "skia/ext/image_operations.h" |
| 28 #include "skia/ext/platform_canvas.h" | 28 #include "skia/ext/platform_canvas.h" |
| 29 #include "ui/base/l10n/l10n_util.h" | 29 #include "ui/base/l10n/l10n_util.h" |
| 30 #include "ui/base/resource/resource_bundle.h" | 30 #include "ui/base/resource/resource_bundle.h" |
| 31 #include "ui/gfx/icon_util.h" | 31 #include "ui/gfx/icon_util.h" |
| 32 #include "ui/gfx/image/image.h" | |
| 33 | 32 |
| 34 using content::BrowserThread; | 33 using content::BrowserThread; |
| 35 | 34 |
| 36 namespace { | 35 namespace { |
| 37 | 36 |
| 38 const char kProfileIconFileName[] = "Google Profile.ico"; | 37 const char kProfileIconFileName[] = "Google Profile.ico"; |
| 39 const int kProfileAvatarShortcutBadgeWidth = 28; | 38 const int kProfileAvatarShortcutBadgeWidth = 28; |
| 40 const int kProfileAvatarShortcutBadgeHeight = 28; | 39 const int kProfileAvatarShortcutBadgeHeight = 28; |
| 41 const int kShortcutIconSize = 48; | 40 const int kShortcutIconSize = 48; |
| 42 | 41 |
| 43 // Creates the argument to pass to the Windows executable that launches Chrome | 42 struct ProfileShortcutInfo { |
| 44 // with the profile in |profile_base_dir|. | 43 string16 flags; |
| 45 // For example: --profile-directory="Profile 2" | 44 string16 profile_name; |
| 46 string16 CreateProfileShortcutSwitch(const string16& profile_base_dir) { | 45 gfx::Image avatar_image; |
| 47 return base::StringPrintf(L"--%ls=\"%ls\"", | |
| 48 ASCIIToUTF16(switches::kProfileDirectory).c_str(), | |
| 49 profile_base_dir.c_str()); | |
| 50 } | |
| 51 | 46 |
| 52 // Wrap a ShellUtil function that returns a bool so it can be posted in a | 47 ProfileShortcutInfo() : flags(string16()), profile_name(string16()), |
| 53 // task to the FILE thread. | 48 avatar_image(gfx::Image()) { |
| 54 void CallShellUtilBoolFunction( | 49 } |
| 55 const base::Callback<bool(void)>& bool_function) { | 50 |
| 56 bool_function.Run(); | 51 ProfileShortcutInfo(string16 p_flags, string16 p_profile_name, |
| 57 } | 52 gfx::Image p_avatar_image) : flags(p_flags), |
| 53 profile_name(p_profile_name), avatar_image(p_avatar_image) { |
| 54 } |
| 55 }; |
| 56 |
| 57 typedef std::map<FilePath, ProfileShortcutInfo> ProfileShortcutsMap; |
| 58 ProfileShortcutsMap profile_shortcuts_; |
| 58 | 59 |
| 59 // Creates a desktop shortcut icon file (.ico) on the disk for a given profile, | 60 // Creates a desktop shortcut icon file (.ico) on the disk for a given profile, |
| 60 // badging the browser distribution icon with the profile avatar. | 61 // badging the browser distribution icon with the profile avatar. |
| 61 // |profile_base_dir| is the base directory (and key) of the profile. Returns | 62 // |profile_base_dir| is the base directory (and key) of the profile. Returns |
| 62 // a path to the shortcut icon file on disk, which is empty if this fails. | 63 // a path to the shortcut icon file on disk, which is empty if this fails. |
| 63 // Use index 0 when assigning the resulting file as the icon. | 64 // Use index 0 when assigning the resulting file as the icon. |
| 64 FilePath CreateChromeDesktopShortcutIconForProfile( | 65 FilePath CreateChromeDesktopShortcutIconForProfile( |
| 65 const FilePath& profile_path, | 66 const FilePath& profile_path, |
| 66 const gfx::Image& avatar_image) { | 67 const gfx::Image& avatar_image) { |
| 67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 68 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 68 const SkBitmap* avatar_bitmap = avatar_image.ToSkBitmap(); | 69 const SkBitmap* avatar_bitmap = avatar_image.ToSkBitmap(); |
| 69 HICON app_icon_handle = GetAppIconForSize(kShortcutIconSize); | 70 HICON app_icon_handle = GetAppIconForSize(kShortcutIconSize); |
| 70 scoped_ptr<SkBitmap> app_icon_bitmap( | 71 scoped_ptr<SkBitmap> app_icon_bitmap( |
| 71 IconUtil::CreateSkBitmapFromHICON(app_icon_handle)); | 72 IconUtil::CreateSkBitmapFromHICON(app_icon_handle)); |
| 72 DestroyIcon(app_icon_handle); | 73 DestroyIcon(app_icon_handle); |
| 73 if (!app_icon_bitmap.get()) | 74 if (!app_icon_bitmap.get()) |
| 74 return FilePath(); | 75 return FilePath(); |
| 75 | 76 |
| 76 // TODO(stevet): Share this chunk of code with | 77 // TODO: Share this chunk of code with |
| 77 // avatar_menu_button::DrawTaskBarDecoration. | 78 // avatar_menu_button::DrawTaskBarDecoration. |
| 78 const SkBitmap* source_bitmap = NULL; | 79 const SkBitmap* source_bitmap = NULL; |
| 79 SkBitmap squarer_bitmap; | 80 SkBitmap squarer_bitmap; |
| 80 if ((avatar_bitmap->width() == profiles::kAvatarIconWidth) && | 81 if ((avatar_bitmap->width() == profiles::kAvatarIconWidth) && |
| 81 (avatar_bitmap->height() == profiles::kAvatarIconHeight)) { | 82 (avatar_bitmap->height() == profiles::kAvatarIconHeight)) { |
| 82 // Shave a couple of columns so the bitmap is more square. So when | 83 // Shave a couple of columns so the bitmap is more square. So when |
| 83 // resized to a square aspect ratio it looks pretty. | 84 // resized to a square aspect ratio it looks pretty. |
| 84 int x = 2; | 85 int x = 2; |
| 85 avatar_bitmap->extractSubset(&squarer_bitmap, SkIRect::MakeXYWH(x, 0, | 86 avatar_bitmap->extractSubset(&squarer_bitmap, SkIRect::MakeXYWH(x, 0, |
| 86 profiles::kAvatarIconWidth - x * 2, profiles::kAvatarIconHeight)); | 87 profiles::kAvatarIconWidth - x * 2, profiles::kAvatarIconHeight)); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 110 offscreen_canvas->getDevice()->accessBitmap(false); | 111 offscreen_canvas->getDevice()->accessBitmap(false); |
| 111 | 112 |
| 112 // Finally, write the .ico file containing this new bitmap. | 113 // Finally, write the .ico file containing this new bitmap. |
| 113 FilePath icon_path = profile_path.AppendASCII(kProfileIconFileName); | 114 FilePath icon_path = profile_path.AppendASCII(kProfileIconFileName); |
| 114 if (!IconUtil::CreateIconFileFromSkBitmap(final_bitmap, icon_path)) | 115 if (!IconUtil::CreateIconFileFromSkBitmap(final_bitmap, icon_path)) |
| 115 return FilePath(); | 116 return FilePath(); |
| 116 | 117 |
| 117 return icon_path; | 118 return icon_path; |
| 118 } | 119 } |
| 119 | 120 |
| 120 // Creates a desktop shortcut to open Chrome with the given profile name and | 121 string16 CreateProfileShortcutFlags(const FilePath& profile_path) { |
| 121 // base directory. Iff |create|, create shortcut if it doesn't already exist. | 122 return base::StringPrintf(L"--%ls=\"%ls\"", |
| 122 // Must be called on the FILE thread. | 123 ASCIIToUTF16(switches::kProfileDirectory).c_str(), |
| 123 void CreateChromeDesktopShortcutForProfile( | 124 profile_path.BaseName().value().c_str()); |
| 124 const string16& profile_name, | |
| 125 const string16& profile_base_dir, | |
| 126 const FilePath& profile_path, | |
| 127 const gfx::Image* avatar_image, | |
| 128 bool create) { | |
| 129 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 130 FilePath chrome_exe; | |
| 131 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) | |
| 132 return; | |
| 133 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | |
| 134 string16 description; | |
| 135 if (!dist) | |
| 136 return; | |
| 137 else | |
| 138 description = WideToUTF16(dist->GetAppDescription()); | |
| 139 const string16& directory = CreateProfileShortcutSwitch(profile_base_dir); | |
| 140 FilePath icon_path = avatar_image ? | |
| 141 CreateChromeDesktopShortcutIconForProfile(profile_path, *avatar_image) : | |
| 142 FilePath(); | |
| 143 | |
| 144 ShellUtil::CreateChromeDesktopShortcut( | |
| 145 dist, | |
| 146 chrome_exe.value(), | |
| 147 description, | |
| 148 profile_name, | |
| 149 directory, | |
| 150 icon_path.empty() ? chrome_exe.value() : icon_path.value(), | |
| 151 icon_path.empty() ? dist->GetIconIndex() : 0, | |
| 152 ShellUtil::CURRENT_USER, | |
| 153 create ? ShellUtil::SHORTCUT_CREATE_ALWAYS : | |
| 154 ShellUtil::SHORTCUT_NO_OPTIONS); | |
| 155 } | 125 } |
| 156 | 126 |
| 157 // Renames an existing Chrome desktop profile shortcut. Must be called on the | 127 // Wrap a ShellUtil function that returns a bool so it can be posted in a |
| 158 // FILE thread. | 128 // task to the FILE thread. |
| 159 void RenameChromeDesktopShortcutForProfile( | 129 void CallShellUtilBoolFunction( |
| 160 const string16& old_shortcut, | 130 const base::Callback<bool(void)>& bool_function) { |
| 161 const string16& new_shortcut) { | 131 bool_function.Run(); |
| 162 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 132 } |
| 163 FilePath shortcut_path; | 133 } // namespace |
| 164 if (ShellUtil::GetDesktopPath(false, // User's directory instead of system. | 134 |
| 165 &shortcut_path)) { | 135 ProfileShortcutManager::ProfileShortcutManager() { |
| 166 FilePath old_profile = shortcut_path.Append(old_shortcut); | |
| 167 FilePath new_profile = shortcut_path.Append(new_shortcut); | |
| 168 file_util::Move(old_profile, new_profile); | |
| 169 } | |
| 170 } | 136 } |
| 171 | 137 |
| 172 // Updates the arguments to a Chrome desktop shortcut for a profile. Must be | 138 ProfileShortcutManager::~ProfileShortcutManager() { |
| 173 // called on the FILE thread. | |
| 174 void UpdateChromeDesktopShortcutForProfile( | |
| 175 const string16& shortcut, | |
| 176 const string16& arguments, | |
| 177 const FilePath& profile_path, | |
| 178 const gfx::Image* avatar_image) { | |
| 179 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 180 FilePath shortcut_path; | |
| 181 if (!ShellUtil::GetDesktopPath(false, &shortcut_path)) | |
| 182 return; | |
| 183 | |
| 184 shortcut_path = shortcut_path.Append(shortcut); | |
| 185 FilePath chrome_exe; | |
| 186 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) | |
| 187 return; | |
| 188 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | |
| 189 string16 description; | |
| 190 if (!dist) | |
| 191 return; | |
| 192 else | |
| 193 description = WideToUTF16(dist->GetAppDescription()); | |
| 194 FilePath icon_path = avatar_image ? | |
| 195 CreateChromeDesktopShortcutIconForProfile(profile_path, *avatar_image) : | |
| 196 FilePath(); | |
| 197 | |
| 198 ShellUtil::UpdateChromeShortcut( | |
| 199 dist, | |
| 200 chrome_exe.value(), | |
| 201 shortcut_path.value(), | |
| 202 arguments, | |
| 203 description, | |
| 204 icon_path.empty() ? chrome_exe.value() : icon_path.value(), | |
| 205 icon_path.empty() ? dist->GetIconIndex() : 0, | |
| 206 ShellUtil::SHORTCUT_NO_OPTIONS); | |
| 207 } | 139 } |
| 208 | 140 |
| 209 void DeleteAutoLaunchValueForProfile( | 141 // static |
| 210 const FilePath& profile_path) { | 142 bool ProfileShortcutManager::IsFeatureEnabled() { |
| 211 if (auto_launch_util::AutoStartRequested(profile_path.BaseName().value(), | 143 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 212 true, // Window requested. | 144 switches::kProfileDesktopShortcuts)) { |
| 213 FilePath())) { | 145 return true; |
| 214 auto_launch_util::DisableForegroundStartAtLogin( | |
| 215 profile_path.BaseName().value()); | |
| 216 } | 146 } |
| 147 return false; |
| 217 } | 148 } |
| 218 | 149 |
| 219 } // namespace | 150 // static |
| 151 void ProfileShortcutManager::CreateChromeDesktopShortcut( |
| 152 const FilePath& profile_path, string16 profile_name, |
| 153 const gfx::Image& avatar_image) { |
| 154 if (ProfileShortcutManager::IsFeatureEnabled()) { |
| 155 FilePath shortcut_icon = CreateChromeDesktopShortcutIconForProfile( |
| 156 profile_path, avatar_image); |
| 157 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 158 FilePath chrome_exe; |
| 159 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) |
| 160 return; |
| 161 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 162 string16 description; |
| 163 if (!dist) |
| 164 return; |
| 165 else |
| 166 description = WideToUTF16(dist->GetAppDescription()); |
| 220 | 167 |
| 221 ProfileShortcutManagerWin::ProfileShortcutManagerWin() { | 168 // Add the profile to the map if it doesn't exist already |
| 222 } | 169 if (!profile_shortcuts_.count(profile_path)) { |
| 223 | 170 string16 flags = CreateProfileShortcutFlags(profile_path); |
| 224 ProfileShortcutManagerWin::~ProfileShortcutManagerWin() { | 171 profile_shortcuts_.insert(std::make_pair(profile_path, |
| 225 } | 172 ProfileShortcutInfo(flags, profile_name, |
| 226 | 173 avatar_image))); |
| 227 void ProfileShortcutManagerWin::AddProfileShortcut( | |
| 228 const FilePath& profile_path) { | |
| 229 ProfileInfoCache& cache = | |
| 230 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
| 231 size_t index = cache.GetIndexOfProfileWithPath(profile_path); | |
| 232 if (index == std::string::npos) | |
| 233 return; | |
| 234 | |
| 235 // Launch task to add shortcut to desktop on Windows. If this is the very | |
| 236 // first profile created, don't add the user name to the shortcut. | |
| 237 // TODO(mirandac): respect master_preferences choice to create no shortcuts | |
| 238 // (see http://crbug.com/104463) | |
| 239 if (g_browser_process->profile_manager()->GetNumberOfProfiles() > 1) { | |
| 240 { | |
| 241 // We make a copy of the Image to ensure that the underlying image data is | |
| 242 // AddRef'd, in case the original copy gets deleted. | |
| 243 gfx::Image* avatar_copy = | |
| 244 new gfx::Image(cache.GetAvatarIconOfProfileAtIndex(index)); | |
| 245 string16 profile_name = cache.GetNameOfProfileAtIndex(index); | |
| 246 string16 profile_base_dir = profile_path.BaseName().value(); | |
| 247 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | |
| 248 base::Bind(&CreateChromeDesktopShortcutForProfile, | |
| 249 profile_name, profile_base_dir, profile_path, | |
| 250 base::Owned(avatar_copy), true)); | |
| 251 } | 174 } |
| 252 | 175 |
| 253 // If this is the second existing multi-user account created, change the | 176 ShellUtil::CreateChromeDesktopShortcut( |
| 254 // original shortcut use the first profile's details (name, badge, | 177 dist, |
| 255 // argument). | 178 chrome_exe.value(), |
| 256 if (cache.GetNumberOfProfiles() == 2) { | 179 description, |
| 257 // Get the index of the first profile, based on the index of the second | 180 profile_shortcuts_[profile_path].profile_name, |
| 258 // profile. It's either 0 or 1, whichever the second profile isn't. | 181 profile_shortcuts_[profile_path].flags, |
| 259 size_t first_index = 0; | 182 shortcut_icon.empty() ? chrome_exe.value() : shortcut_icon.value(), |
| 260 if (cache.GetIndexOfProfileWithPath(profile_path) == 0) | 183 shortcut_icon.empty() ? dist->GetIconIndex() : 0, |
| 261 first_index = 1; | 184 ShellUtil::CURRENT_USER, |
| 262 string16 first_name = cache.GetNameOfProfileAtIndex(first_index); | 185 ShellUtil::SHORTCUT_CREATE_ALWAYS); |
| 263 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | |
| 264 | |
| 265 string16 old_shortcut; | |
| 266 string16 new_shortcut; | |
| 267 if (ShellUtil::GetChromeShortcutName(dist, false, L"", &old_shortcut) && | |
| 268 ShellUtil::GetChromeShortcutName(dist, false, first_name, | |
| 269 &new_shortcut)) { | |
| 270 // Update doesn't allow changing the target, so rename first. | |
| 271 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | |
| 272 base::Bind(&RenameChromeDesktopShortcutForProfile, | |
| 273 old_shortcut, new_shortcut)); | |
| 274 | |
| 275 // Fetch the avatar for the first profile and make a copy of the Image | |
| 276 // to ensure that the underlying image data is AddRef'd, in case the | |
| 277 // original copy is deleted. | |
| 278 gfx::Image& first_avatar = | |
| 279 ResourceBundle::GetSharedInstance().GetNativeImageNamed( | |
| 280 ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex( | |
| 281 cache.GetAvatarIconIndexOfProfileAtIndex(first_index))); | |
| 282 gfx::Image* first_avatar_copy = new gfx::Image(first_avatar); | |
| 283 FilePath first_path = cache.GetPathOfProfileAtIndex(first_index); | |
| 284 | |
| 285 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | |
| 286 base::Bind(&UpdateChromeDesktopShortcutForProfile, | |
| 287 new_shortcut, | |
| 288 CreateProfileShortcutSwitch(UTF8ToUTF16( | |
| 289 first_path.BaseName().MaybeAsASCII())), | |
| 290 first_path, | |
| 291 base::Owned(first_avatar_copy))); | |
| 292 } | |
| 293 } | |
| 294 } else { // Only one profile, so create original shortcut, with no avatar. | |
| 295 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | |
| 296 base::Bind(&CreateChromeDesktopShortcutForProfile, | |
| 297 string16(), string16(), FilePath(), | |
| 298 static_cast<gfx::Image*>(NULL), true)); | |
| 299 } | |
| 300 } | |
| 301 | |
| 302 void ProfileShortcutManagerWin::OnProfileAdded( | |
| 303 const FilePath& profile_path) { | |
| 304 } | |
| 305 | |
| 306 void ProfileShortcutManagerWin::OnProfileWillBeRemoved( | |
| 307 const FilePath& profile_path) { | |
| 308 ProfileInfoCache& cache = | |
| 309 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
| 310 size_t index = cache.GetIndexOfProfileWithPath(profile_path); | |
| 311 if (index == std::string::npos) | |
| 312 return; | |
| 313 string16 profile_name = cache.GetNameOfProfileAtIndex(index); | |
| 314 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | |
| 315 string16 shortcut; | |
| 316 if (ShellUtil::GetChromeShortcutName(dist, false, profile_name, &shortcut)) { | |
| 317 std::vector<string16> shortcuts(1, shortcut); | |
| 318 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | |
| 319 base::Bind(&CallShellUtilBoolFunction, | |
| 320 base::Bind( | |
| 321 &ShellUtil::RemoveChromeDesktopShortcutsWithAppendedNames, | |
| 322 shortcuts))); | |
| 323 } | |
| 324 } | |
| 325 | |
| 326 void ProfileShortcutManagerWin::OnProfileWasRemoved( | |
| 327 const FilePath& profile_path, | |
| 328 const string16& profile_name) { | |
| 329 BrowserThread::PostTask( | |
| 330 BrowserThread::FILE, FROM_HERE, | |
| 331 base::Bind(&DeleteAutoLaunchValueForProfile, profile_path)); | |
| 332 | |
| 333 // If there is one profile left, we want to remove the badge and name from it. | |
| 334 ProfileInfoCache& cache = | |
| 335 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
| 336 if (cache.GetNumberOfProfiles() != 1) | |
| 337 return; | |
| 338 | |
| 339 FilePath last_profile_path = cache.GetPathOfProfileAtIndex(0); | |
| 340 string16 old_shortcut; | |
| 341 string16 new_shortcut; | |
| 342 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | |
| 343 if (ShellUtil::GetChromeShortcutName( | |
| 344 dist, false, cache.GetNameOfProfileAtIndex(0), &old_shortcut) && | |
| 345 ShellUtil::GetChromeShortcutName( | |
| 346 dist, false, L"", &new_shortcut)) { | |
| 347 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | |
| 348 base::Bind(&RenameChromeDesktopShortcutForProfile, | |
| 349 old_shortcut, | |
| 350 new_shortcut)); | |
| 351 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | |
| 352 base::Bind(&UpdateChromeDesktopShortcutForProfile, | |
| 353 new_shortcut, | |
| 354 CreateProfileShortcutSwitch(UTF8ToUTF16( | |
| 355 last_profile_path.BaseName().MaybeAsASCII())), | |
| 356 last_profile_path, | |
| 357 static_cast<gfx::Image*>(NULL))); | |
| 358 } | |
| 359 } | |
| 360 | |
| 361 void ProfileShortcutManagerWin::OnProfileNameChanged( | |
| 362 const FilePath& profile_path, | |
| 363 const string16& old_profile_name) { | |
| 364 // Launch task to change name of desktop shortcut on Windows. | |
| 365 // TODO(mirandac): respect master_preferences choice to create no shortcuts | |
| 366 // (see http://crbug.com/104463) | |
| 367 ProfileInfoCache& cache = | |
| 368 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
| 369 size_t index = cache.GetIndexOfProfileWithPath(profile_path); | |
| 370 if (index == std::string::npos) | |
| 371 return; | |
| 372 string16 new_profile_name = cache.GetNameOfProfileAtIndex(index); | |
| 373 | |
| 374 string16 old_shortcut; | |
| 375 string16 new_shortcut; | |
| 376 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | |
| 377 if (ShellUtil::GetChromeShortcutName( | |
| 378 dist, false, old_profile_name, &old_shortcut) && | |
| 379 ShellUtil::GetChromeShortcutName( | |
| 380 dist, false, new_profile_name, &new_shortcut)) { | |
| 381 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | |
| 382 base::Bind(&RenameChromeDesktopShortcutForProfile, | |
| 383 old_shortcut, | |
| 384 new_shortcut)); | |
| 385 } | |
| 386 } | |
| 387 | |
| 388 void ProfileShortcutManagerWin::OnProfileAvatarChanged( | |
| 389 const FilePath& profile_path) { | |
| 390 ProfileInfoCache& cache = | |
| 391 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
| 392 size_t index = cache.GetIndexOfProfileWithPath(profile_path); | |
| 393 if (index == std::string::npos) | |
| 394 return; | |
| 395 string16 profile_name = cache.GetNameOfProfileAtIndex(index); | |
| 396 string16 profile_base_dir = | |
| 397 UTF8ToUTF16(profile_path.BaseName().MaybeAsASCII()); | |
| 398 const gfx::Image& avatar_image = cache.GetAvatarIconOfProfileAtIndex(index); | |
| 399 | |
| 400 // Launch task to change the icon of the desktop shortcut on windows. | |
| 401 string16 new_shortcut; | |
| 402 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | |
| 403 if (ShellUtil::GetChromeShortcutName(dist, false, profile_name, | |
| 404 &new_shortcut)) { | |
| 405 // We make a copy of the Image to ensure that the underlying image data is | |
| 406 // AddRef'd, in case the original copy gets deleted. | |
| 407 gfx::Image* avatar_copy = new gfx::Image(avatar_image); | |
| 408 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | |
| 409 base::Bind(&UpdateChromeDesktopShortcutForProfile, | |
| 410 new_shortcut, | |
| 411 CreateProfileShortcutSwitch(profile_base_dir), | |
| 412 profile_path, | |
| 413 base::Owned(avatar_copy))); | |
| 414 } | 186 } |
| 415 } | 187 } |
| 416 | 188 |
| 417 // static | 189 // static |
| 418 std::vector<string16> ProfileShortcutManagerWin::GenerateShortcutsFromProfiles( | 190 void ProfileShortcutManager::DeleteChromeDesktopShortcut( |
| 419 const std::vector<string16>& profile_names) { | 191 const FilePath& profile_path) { |
| 420 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | 192 if (ProfileShortcutManager::IsFeatureEnabled()) { |
| 421 std::vector<string16> shortcuts; | 193 // If the profile to be deleted has a shortcut |
| 422 shortcuts.reserve(profile_names.size()); | 194 if (profile_shortcuts_.count(profile_path)) { |
| 423 for (std::vector<string16>::const_iterator it = profile_names.begin(); | 195 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 424 it != profile_names.end(); | 196 string16 shortcut; |
| 425 ++it) { | 197 // If we can find the shortcut, delete it |
| 426 string16 shortcut; | 198 if (ShellUtil::GetChromeShortcutName(dist, false, |
| 427 if (ShellUtil::GetChromeShortcutName(dist, false, *it, &shortcut)) | 199 profile_shortcuts_[profile_path].profile_name, &shortcut)) { |
| 428 shortcuts.push_back(shortcut); | 200 std::vector<string16> appended_names(1, shortcut); |
| 201 BrowserThread::PostTask( |
| 202 BrowserThread::FILE, FROM_HERE, |
| 203 base::Bind(&CallShellUtilBoolFunction, base::Bind( |
| 204 &ShellUtil::RemoveChromeDesktopShortcutsWithAppendedNames, |
| 205 appended_names))); |
| 206 profile_shortcuts_.erase(profile_path); |
| 207 } |
| 208 } |
| 429 } | 209 } |
| 430 return shortcuts; | |
| 431 } | 210 } |
| OLD | NEW |