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/profiles/profile_shortcut_manager_win.h" | 5 #include "chrome/browser/profiles/profile_shortcut_manager_win.h" |
| 6 | 6 |
| 7 #include <shlobj.h> // For SHChangeNotify(). | 7 #include <shlobj.h> // For SHChangeNotify(). |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 38 #include "ui/gfx/icon_util.h" | 38 #include "ui/gfx/icon_util.h" |
| 39 #include "ui/gfx/image/image.h" | 39 #include "ui/gfx/image/image.h" |
| 40 #include "ui/gfx/image/image_family.h" | 40 #include "ui/gfx/image/image_family.h" |
| 41 #include "ui/gfx/rect.h" | 41 #include "ui/gfx/rect.h" |
| 42 #include "ui/gfx/skia_util.h" | 42 #include "ui/gfx/skia_util.h" |
| 43 | 43 |
| 44 using content::BrowserThread; | 44 using content::BrowserThread; |
| 45 | 45 |
| 46 namespace { | 46 namespace { |
| 47 | 47 |
| 48 // Name of the badged icon file generated for a given profile. | |
| 49 const char kProfileIconFileName[] = "Google Profile.ico"; | |
| 50 | |
| 48 // Characters that are not allowed in Windows filenames. Taken from | 51 // Characters that are not allowed in Windows filenames. Taken from |
| 49 // http://msdn.microsoft.com/en-us/library/aa365247.aspx | 52 // http://msdn.microsoft.com/en-us/library/aa365247.aspx |
| 50 const char16 kReservedCharacters[] = L"<>:\"/\\|?*\x01\x02\x03\x04\x05\x06\x07" | 53 const char16 kReservedCharacters[] = L"<>:\"/\\|?*\x01\x02\x03\x04\x05\x06\x07" |
| 51 L"\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19" | 54 L"\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19" |
| 52 L"\x1A\x1B\x1C\x1D\x1E\x1F"; | 55 L"\x1A\x1B\x1C\x1D\x1E\x1F"; |
| 53 | 56 |
| 54 // The maximum number of characters allowed in profile shortcuts' file names. | 57 // The maximum number of characters allowed in profile shortcuts' file names. |
| 55 // Warning: migration code will be needed if this is changed later, since | 58 // Warning: migration code will be needed if this is changed later, since |
| 56 // existing shortcuts might no longer be found if the name is generated | 59 // existing shortcuts might no longer be found if the name is generated |
| 57 // differently than it was when a shortcut was originally created. | 60 // differently than it was when a shortcut was originally created. |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 133 const SkBitmap& badged_bitmap = | 136 const SkBitmap& badged_bitmap = |
| 134 offscreen_canvas->getDevice()->accessBitmap(false); | 137 offscreen_canvas->getDevice()->accessBitmap(false); |
| 135 SkBitmap badged_bitmap_copy; | 138 SkBitmap badged_bitmap_copy; |
| 136 badged_bitmap.deepCopyTo(&badged_bitmap_copy, badged_bitmap.getConfig()); | 139 badged_bitmap.deepCopyTo(&badged_bitmap_copy, badged_bitmap.getConfig()); |
| 137 return badged_bitmap_copy; | 140 return badged_bitmap_copy; |
| 138 } | 141 } |
| 139 | 142 |
| 140 // Creates a desktop shortcut icon file (.ico) on the disk for a given profile, | 143 // Creates a desktop shortcut icon file (.ico) on the disk for a given profile, |
| 141 // badging the browser distribution icon with the profile avatar. | 144 // badging the browser distribution icon with the profile avatar. |
| 142 // Returns a path to the shortcut icon file on disk, which is empty if this | 145 // Returns a path to the shortcut icon file on disk, which is empty if this |
| 143 // fails. Use index 0 when assigning the resulting file as the icon. | 146 // fails. Use index 0 when assigning the resulting file as the icon. If both |
| 144 base::FilePath CreateChromeDesktopShortcutIconForProfile( | 147 // given bitmaps are empty, an unbadged icon is created. |
| 148 // |is_create| should be true if we are creating the icon at a time when it | |
| 149 // is not possibly already in use. If it is false, we will refresh the Windows | |
| 150 // icon cache. | |
| 151 // TODO(calamity): Ideally we'd just copy the app icon verbatim from the exe's | |
| 152 // resources in the case of an unbadged icon. | |
| 153 base::FilePath CreateOrUpdateShortcutIconForProfile( | |
| 145 const base::FilePath& profile_path, | 154 const base::FilePath& profile_path, |
| 146 const SkBitmap& avatar_bitmap_1x, | 155 const SkBitmap& avatar_bitmap_1x, |
| 147 const SkBitmap& avatar_bitmap_2x) { | 156 const SkBitmap& avatar_bitmap_2x, |
| 157 const base::Closure& callback) { | |
| 148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 158 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 149 scoped_ptr<SkBitmap> app_icon_bitmap(GetAppIconForSize(kShortcutIconSize)); | 159 scoped_ptr<SkBitmap> app_icon_bitmap(GetAppIconForSize(kShortcutIconSize)); |
| 150 if (!app_icon_bitmap) | 160 if (!app_icon_bitmap) |
| 151 return base::FilePath(); | 161 return base::FilePath(); |
| 152 | 162 |
| 153 gfx::ImageFamily badged_bitmaps; | 163 gfx::ImageFamily badged_bitmaps; |
| 154 badged_bitmaps.Add(gfx::Image::CreateFrom1xBitmap( | 164 if (!avatar_bitmap_1x.empty()) { |
| 155 BadgeIcon(*app_icon_bitmap, avatar_bitmap_1x, 1))); | |
| 156 | |
| 157 app_icon_bitmap = GetAppIconForSize(IconUtil::kLargeIconSize); | |
| 158 if (app_icon_bitmap) { | |
| 159 badged_bitmaps.Add(gfx::Image::CreateFrom1xBitmap( | 165 badged_bitmaps.Add(gfx::Image::CreateFrom1xBitmap( |
| 160 BadgeIcon(*app_icon_bitmap, avatar_bitmap_2x, 2))); | 166 BadgeIcon(*app_icon_bitmap, avatar_bitmap_1x, 1))); |
| 161 } | 167 } |
| 162 | 168 |
| 169 scoped_ptr<SkBitmap> large_app_icon_bitmap( | |
| 170 GetAppIconForSize(IconUtil::kLargeIconSize)); | |
| 171 if (large_app_icon_bitmap && !avatar_bitmap_2x.empty()) { | |
| 172 badged_bitmaps.Add(gfx::Image::CreateFrom1xBitmap( | |
| 173 BadgeIcon(*large_app_icon_bitmap, avatar_bitmap_2x, 2))); | |
| 174 } | |
| 175 | |
| 176 // If we have no badged bitmaps, we should just use the default chrome icon. | |
| 177 if (badged_bitmaps.empty()) { | |
| 178 badged_bitmaps.Add(gfx::Image::CreateFrom1xBitmap(*app_icon_bitmap)); | |
| 179 if (large_app_icon_bitmap) { | |
| 180 badged_bitmaps.Add( | |
| 181 gfx::Image::CreateFrom1xBitmap(*large_app_icon_bitmap)); | |
| 182 } | |
| 183 } | |
| 163 // Finally, write the .ico file containing this new bitmap. | 184 // Finally, write the .ico file containing this new bitmap. |
| 164 const base::FilePath icon_path = | 185 const base::FilePath icon_path = |
| 165 profile_path.AppendASCII(profiles::internal::kProfileIconFileName); | 186 profiles::internal::GetProfileIconPath(profile_path); |
| 166 if (!IconUtil::CreateIconFileFromImageFamily(badged_bitmaps, icon_path)) | 187 const bool had_icon = file_util::PathExists(icon_path); |
| 188 | |
| 189 if (!IconUtil::CreateIconFileFromImageFamily(badged_bitmaps, icon_path)) { | |
| 190 NOTREACHED(); | |
| 167 return base::FilePath(); | 191 return base::FilePath(); |
| 192 } | |
| 168 | 193 |
| 194 if (had_icon) { | |
| 195 // This invalidates the Windows icon cache and causes the icon changes to | |
| 196 // register with the taskbar and desktop. SHCNE_ASSOCCHANGED will cause a | |
| 197 // desktop flash and we would like to avoid that if possible. | |
| 198 SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST | SHCNF_FLUSHNOWAIT, | |
| 199 NULL, NULL); | |
| 200 } else { | |
| 201 SHChangeNotify(SHCNE_CREATE, SHCNF_PATH, icon_path.value().c_str(), NULL); | |
| 202 } | |
| 203 if (!callback.is_null()) | |
| 204 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback); | |
| 169 return icon_path; | 205 return icon_path; |
| 170 } | 206 } |
| 171 | 207 |
| 172 // Gets the user and system directories for desktop shortcuts. Parameters may | 208 // Gets the user and system directories for desktop shortcuts. Parameters may |
| 173 // be NULL if a directory type is not needed. Returns true on success. | 209 // be NULL if a directory type is not needed. Returns true on success. |
| 174 bool GetDesktopShortcutsDirectories( | 210 bool GetDesktopShortcutsDirectories( |
| 175 base::FilePath* user_shortcuts_directory, | 211 base::FilePath* user_shortcuts_directory, |
| 176 base::FilePath* system_shortcuts_directory) { | 212 base::FilePath* system_shortcuts_directory) { |
| 177 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); | 213 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); |
| 178 if (user_shortcuts_directory && | 214 if (user_shortcuts_directory && |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 293 if (file_util::PathExists(possible_new_system_shortcut)) | 329 if (file_util::PathExists(possible_new_system_shortcut)) |
| 294 file_util::Delete(old_shortcut_path, false); | 330 file_util::Delete(old_shortcut_path, false); |
| 295 else if (!RenameDesktopShortcut(old_shortcut_path, new_shortcut_path)) | 331 else if (!RenameDesktopShortcut(old_shortcut_path, new_shortcut_path)) |
| 296 DLOG(ERROR) << "Could not rename Windows profile desktop shortcut."; | 332 DLOG(ERROR) << "Could not rename Windows profile desktop shortcut."; |
| 297 } else { | 333 } else { |
| 298 // If the shortcut does not exist, it may have been renamed by the user. In | 334 // If the shortcut does not exist, it may have been renamed by the user. In |
| 299 // that case, its name should not be changed. | 335 // that case, its name should not be changed. |
| 300 // It's also possible that a system-level shortcut exists instead - this | 336 // It's also possible that a system-level shortcut exists instead - this |
| 301 // should only be the case for the original Chrome shortcut from an | 337 // should only be the case for the original Chrome shortcut from an |
| 302 // installation. If that's the case, copy that one over - it will get its | 338 // installation. If that's the case, copy that one over - it will get its |
| 303 // properties updated by |CreateOrUpdateDesktopShortcutsForProfile()|. | 339 // properties updated by |
| 340 // |CreateOrUpdateDesktopShortcutsAndIconForProfile()|. | |
| 304 const base::FilePath possible_old_system_shortcut = | 341 const base::FilePath possible_old_system_shortcut = |
| 305 system_shortcuts_directory.Append(old_shortcut_filename); | 342 system_shortcuts_directory.Append(old_shortcut_filename); |
| 306 if (file_util::PathExists(possible_old_system_shortcut)) | 343 if (file_util::PathExists(possible_old_system_shortcut)) |
| 307 file_util::CopyFile(possible_old_system_shortcut, new_shortcut_path); | 344 file_util::CopyFile(possible_old_system_shortcut, new_shortcut_path); |
| 308 } | 345 } |
| 309 } | 346 } |
| 310 | 347 |
| 348 struct CreateOrUpdateShortcutsParams { | |
| 349 CreateOrUpdateShortcutsParams() {} | |
|
Alexei Svitkine (slow)
2013/06/05 14:06:03
This needs to initialize |create_mode| and |action
calamity
2013/06/07 04:26:26
Done.
| |
| 350 ~CreateOrUpdateShortcutsParams() {} | |
| 351 | |
| 352 base::FilePath profile_path; | |
|
Alexei Svitkine (slow)
2013/06/05 14:06:03
Please comment the fields.
calamity
2013/06/07 04:26:26
Done.
| |
| 353 string16 old_profile_name; | |
| 354 string16 profile_name; | |
| 355 SkBitmap avatar_image_1x; | |
| 356 SkBitmap avatar_image_2x; | |
| 357 ProfileShortcutManagerWin::CreateOrUpdateMode create_mode; | |
| 358 ProfileShortcutManagerWin::NonProfileShortcutAction action; | |
| 359 }; | |
| 360 | |
| 311 // Updates all desktop shortcuts for the given profile to have the specified | 361 // Updates all desktop shortcuts for the given profile to have the specified |
| 312 // parameters. If |create_mode| is CREATE_WHEN_NONE_FOUND, a new shortcut is | 362 // parameters. If |create_mode| is CREATE_WHEN_NONE_FOUND, a new shortcut is |
| 313 // created if no existing ones were found. Whether non-profile shortcuts should | 363 // created if no existing ones were found. Whether non-profile shortcuts should |
| 314 // be updated is specified by |action|. Must be called on the FILE thread. | 364 // be updated is specified by |action|. Must be called on the FILE thread. |
| 315 void CreateOrUpdateDesktopShortcutsForProfile( | 365 void CreateOrUpdateDesktopShortcutsAndIconForProfile( |
| 316 const base::FilePath& profile_path, | 366 const CreateOrUpdateShortcutsParams& params, |
| 317 const string16& old_profile_name, | 367 const base::Closure& callback) { |
| 318 const string16& profile_name, | |
| 319 const SkBitmap& avatar_image_1x, | |
| 320 const SkBitmap& avatar_image_2x, | |
| 321 ProfileShortcutManagerWin::CreateOrUpdateMode create_mode, | |
| 322 ProfileShortcutManagerWin::NonProfileShortcutAction action) { | |
| 323 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 368 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 324 | 369 |
| 370 base::FilePath shortcut_icon = | |
|
Alexei Svitkine (slow)
2013/06/05 14:06:03
Nit: Make this const.
calamity
2013/06/07 04:26:26
Done.
| |
| 371 CreateOrUpdateShortcutIconForProfile(params.profile_path, | |
| 372 params.avatar_image_1x, | |
| 373 params.avatar_image_2x, | |
| 374 callback); | |
| 375 if (shortcut_icon.empty()) { | |
| 376 NOTREACHED(); | |
| 377 return; | |
| 378 } | |
| 379 if (params.create_mode == ProfileShortcutManagerWin::CREATE_ICON_ONLY) | |
| 380 return; | |
| 381 | |
| 325 base::FilePath chrome_exe; | 382 base::FilePath chrome_exe; |
| 326 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { | 383 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
| 327 NOTREACHED(); | 384 NOTREACHED(); |
| 328 return; | 385 return; |
| 329 } | 386 } |
| 330 | 387 |
| 331 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); | 388 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); |
| 332 // Ensure that the distribution supports creating shortcuts. If it doesn't, | 389 // Ensure that the distribution supports creating shortcuts. If it doesn't, |
| 333 // the following code may result in NOTREACHED() being hit. | 390 // the following code may result in NOTREACHED() being hit. |
| 334 DCHECK(distribution->CanCreateDesktopShortcuts()); | 391 DCHECK(distribution->CanCreateDesktopShortcuts()); |
| 335 | 392 |
| 336 if (old_profile_name != profile_name) { | 393 if (params.old_profile_name != params.profile_name) { |
| 337 const string16 old_shortcut_filename = | 394 const string16 old_shortcut_filename = |
| 338 profiles::internal::GetShortcutFilenameForProfile(old_profile_name, | 395 profiles::internal::GetShortcutFilenameForProfile( |
| 339 distribution); | 396 params.old_profile_name, |
| 397 distribution); | |
| 340 const string16 new_shortcut_filename = | 398 const string16 new_shortcut_filename = |
| 341 profiles::internal::GetShortcutFilenameForProfile(profile_name, | 399 profiles::internal::GetShortcutFilenameForProfile(params.profile_name, |
| 342 distribution); | 400 distribution); |
| 343 RenameChromeDesktopShortcutForProfile(old_shortcut_filename, | 401 RenameChromeDesktopShortcutForProfile(old_shortcut_filename, |
| 344 new_shortcut_filename); | 402 new_shortcut_filename); |
| 345 } | 403 } |
| 346 | 404 |
| 347 ShellUtil::ShortcutProperties properties(ShellUtil::CURRENT_USER); | 405 ShellUtil::ShortcutProperties properties(ShellUtil::CURRENT_USER); |
| 348 installer::Product product(distribution); | 406 installer::Product product(distribution); |
| 349 product.AddDefaultShortcutProperties(chrome_exe, &properties); | 407 product.AddDefaultShortcutProperties(chrome_exe, &properties); |
| 350 | 408 |
| 351 const string16 command_line = | 409 const string16 command_line = |
| 352 profiles::internal::CreateProfileShortcutFlags(profile_path); | 410 profiles::internal::CreateProfileShortcutFlags(params.profile_path); |
| 353 | 411 |
| 354 // Only set the profile-specific properties when |profile_name| is non empty. | 412 // Only set the profile-specific properties when |profile_name| is non empty. |
| 355 // If it is empty, it means the shortcut being created should be a regular, | 413 // If it is empty, it means the shortcut being created should be a regular, |
| 356 // non-profile Chrome shortcut. | 414 // non-profile Chrome shortcut. |
| 357 if (!profile_name.empty()) { | 415 if (!params.profile_name.empty()) { |
| 358 const base::FilePath shortcut_icon = | |
| 359 CreateChromeDesktopShortcutIconForProfile(profile_path, | |
| 360 avatar_image_1x, | |
| 361 avatar_image_2x); | |
| 362 if (!shortcut_icon.empty()) | 416 if (!shortcut_icon.empty()) |
|
Alexei Svitkine (slow)
2013/06/05 14:06:03
Since you're doing an early return on shortcut_ico
calamity
2013/06/07 04:26:26
Done. In fact, since we always want to use the ico
Alexei Svitkine (slow)
2013/06/10 17:44:52
I disagree, we shouldn't move it outside the if.
calamity
2013/06/13 06:26:03
Done.
| |
| 363 properties.set_icon(shortcut_icon, 0); | 417 properties.set_icon(shortcut_icon, 0); |
| 364 properties.set_arguments(command_line); | 418 properties.set_arguments(command_line); |
| 365 } else { | 419 } else { |
| 366 // Set the arguments explicitly to the empty string to ensure that | 420 // Set the arguments explicitly to the empty string to ensure that |
| 367 // |ShellUtil::CreateOrUpdateShortcut| updates that part of the shortcut. | 421 // |ShellUtil::CreateOrUpdateShortcut| updates that part of the shortcut. |
| 368 properties.set_arguments(string16()); | 422 properties.set_arguments(string16()); |
| 369 } | 423 } |
| 370 | 424 |
| 371 properties.set_app_id( | 425 properties.set_app_id( |
| 372 ShellIntegration::GetChromiumModelIdForProfile(profile_path)); | 426 ShellIntegration::GetChromiumModelIdForProfile(params.profile_path)); |
| 373 | 427 |
| 374 ShellUtil::ShortcutOperation operation = | 428 ShellUtil::ShortcutOperation operation = |
| 375 ShellUtil::SHELL_SHORTCUT_REPLACE_EXISTING; | 429 ShellUtil::SHELL_SHORTCUT_REPLACE_EXISTING; |
| 376 | 430 |
| 377 std::vector<base::FilePath> shortcuts; | 431 std::vector<base::FilePath> shortcuts; |
| 378 ListDesktopShortcutsWithCommandLine(chrome_exe, command_line, | 432 ListDesktopShortcutsWithCommandLine(chrome_exe, command_line, |
| 379 action == ProfileShortcutManagerWin::UPDATE_NON_PROFILE_SHORTCUTS, | 433 params.action == ProfileShortcutManagerWin::UPDATE_NON_PROFILE_SHORTCUTS, |
| 380 &shortcuts); | 434 &shortcuts); |
| 381 if (create_mode == ProfileShortcutManagerWin::CREATE_WHEN_NONE_FOUND && | 435 if (params.create_mode == ProfileShortcutManagerWin::CREATE_WHEN_NONE_FOUND && |
| 382 shortcuts.empty()) { | 436 shortcuts.empty()) { |
| 383 const string16 shortcut_name = | 437 const string16 shortcut_name = |
| 384 profiles::internal::GetShortcutFilenameForProfile(profile_name, | 438 profiles::internal::GetShortcutFilenameForProfile(params.profile_name, |
| 385 distribution); | 439 distribution); |
| 386 shortcuts.push_back(base::FilePath(shortcut_name)); | 440 shortcuts.push_back(base::FilePath(shortcut_name)); |
| 387 operation = ShellUtil::SHELL_SHORTCUT_CREATE_IF_NO_SYSTEM_LEVEL; | 441 operation = ShellUtil::SHELL_SHORTCUT_CREATE_IF_NO_SYSTEM_LEVEL; |
| 388 } | 442 } |
| 389 | 443 |
| 390 for (size_t i = 0; i < shortcuts.size(); ++i) { | 444 for (size_t i = 0; i < shortcuts.size(); ++i) { |
| 391 const base::FilePath shortcut_name = | 445 const base::FilePath shortcut_name = |
| 392 shortcuts[i].BaseName().RemoveExtension(); | 446 shortcuts[i].BaseName().RemoveExtension(); |
| 393 properties.set_shortcut_name(shortcut_name.value()); | 447 properties.set_shortcut_name(shortcut_name.value()); |
| 394 ShellUtil::CreateOrUpdateShortcut(ShellUtil::SHORTCUT_LOCATION_DESKTOP, | 448 ShellUtil::CreateOrUpdateShortcut(ShellUtil::SHORTCUT_LOCATION_DESKTOP, |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 407 file_util::FileEnumerator::FILES); | 461 file_util::FileEnumerator::FILES); |
| 408 for (base::FilePath path = enumerator.Next(); !path.empty(); | 462 for (base::FilePath path = enumerator.Next(); !path.empty(); |
| 409 path = enumerator.Next()) { | 463 path = enumerator.Next()) { |
| 410 if (IsChromeShortcut(path, chrome_exe, NULL)) | 464 if (IsChromeShortcut(path, chrome_exe, NULL)) |
| 411 return true; | 465 return true; |
| 412 } | 466 } |
| 413 | 467 |
| 414 return false; | 468 return false; |
| 415 } | 469 } |
| 416 | 470 |
| 417 // Deletes all desktop shortcuts for the specified profile and also removes the | 471 // Deletes all desktop shortcuts for the specified profile. If |
| 418 // corresponding icon file. If |ensure_shortcuts_remain| is true, then a regular | 472 // |ensure_shortcuts_remain| is true, then a regular non-profile shortcut will |
| 419 // non-profile shortcut will be created if this function would otherwise delete | 473 // be created if this function would otherwise delete the last Chrome desktop |
| 420 // the last Chrome desktop shortcut(s). Must be called on the FILE thread. | 474 // shortcut(s). Must be called on the FILE thread. |
| 421 void DeleteDesktopShortcutsAndIconFile(const base::FilePath& profile_path, | 475 void DeleteDesktopShortcuts(const base::FilePath& profile_path, |
| 422 bool ensure_shortcuts_remain) { | 476 bool ensure_shortcuts_remain) { |
| 423 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 477 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 424 | 478 |
| 425 base::FilePath chrome_exe; | 479 base::FilePath chrome_exe; |
| 426 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { | 480 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
| 427 NOTREACHED(); | 481 NOTREACHED(); |
| 428 return; | 482 return; |
| 429 } | 483 } |
| 430 | 484 |
| 431 const string16 command_line = | 485 const string16 command_line = |
| 432 profiles::internal::CreateProfileShortcutFlags(profile_path); | 486 profiles::internal::CreateProfileShortcutFlags(profile_path); |
| 433 std::vector<base::FilePath> shortcuts; | 487 std::vector<base::FilePath> shortcuts; |
| 434 ListDesktopShortcutsWithCommandLine(chrome_exe, command_line, false, | 488 ListDesktopShortcutsWithCommandLine(chrome_exe, command_line, false, |
| 435 &shortcuts); | 489 &shortcuts); |
| 436 | 490 |
| 437 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); | 491 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); |
| 438 for (size_t i = 0; i < shortcuts.size(); ++i) { | 492 for (size_t i = 0; i < shortcuts.size(); ++i) { |
| 439 // Use file_util::Delete() instead of ShellUtil::RemoveShortcut(), as the | 493 // Use file_util::Delete() instead of ShellUtil::RemoveShortcut(), as the |
| 440 // latter causes non-profile taskbar shortcuts to be unpinned. | 494 // latter causes non-profile taskbar shortcuts to be unpinned. |
| 441 file_util::Delete(shortcuts[i], false); | 495 file_util::Delete(shortcuts[i], false); |
| 442 // Notify the shell that the shortcut was deleted to ensure desktop refresh. | 496 // Notify the shell that the shortcut was deleted to ensure desktop refresh. |
| 443 SHChangeNotify(SHCNE_DELETE, SHCNF_PATH, shortcuts[i].value().c_str(), | 497 SHChangeNotify(SHCNE_DELETE, SHCNF_PATH, shortcuts[i].value().c_str(), |
| 444 NULL); | 498 NULL); |
| 445 } | 499 } |
| 446 | 500 |
| 447 const base::FilePath icon_path = | |
| 448 profile_path.AppendASCII(profiles::internal::kProfileIconFileName); | |
| 449 file_util::Delete(icon_path, false); | |
| 450 | |
| 451 // If |ensure_shortcuts_remain| is true and deleting this profile caused the | 501 // If |ensure_shortcuts_remain| is true and deleting this profile caused the |
| 452 // last shortcuts to be removed, re-create a regular non-profile shortcut. | 502 // last shortcuts to be removed, re-create a regular non-profile shortcut. |
| 453 const bool had_shortcuts = !shortcuts.empty(); | 503 const bool had_shortcuts = !shortcuts.empty(); |
| 454 if (ensure_shortcuts_remain && had_shortcuts && | 504 if (ensure_shortcuts_remain && had_shortcuts && |
| 455 !ChromeDesktopShortcutsExist(chrome_exe)) { | 505 !ChromeDesktopShortcutsExist(chrome_exe)) { |
| 456 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); | 506 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); |
| 457 // Ensure that the distribution supports creating shortcuts. If it doesn't, | 507 // Ensure that the distribution supports creating shortcuts. If it doesn't, |
| 458 // the following code may result in NOTREACHED() being hit. | 508 // the following code may result in NOTREACHED() being hit. |
| 459 DCHECK(distribution->CanCreateDesktopShortcuts()); | 509 DCHECK(distribution->CanCreateDesktopShortcuts()); |
| 460 installer::Product product(distribution); | 510 installer::Product product(distribution); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 522 SkBitmap bitmap_copy; | 572 SkBitmap bitmap_copy; |
| 523 image_bitmap->deepCopyTo(&bitmap_copy, image_bitmap->getConfig()); | 573 image_bitmap->deepCopyTo(&bitmap_copy, image_bitmap->getConfig()); |
| 524 return bitmap_copy; | 574 return bitmap_copy; |
| 525 } | 575 } |
| 526 | 576 |
| 527 } // namespace | 577 } // namespace |
| 528 | 578 |
| 529 namespace profiles { | 579 namespace profiles { |
| 530 namespace internal { | 580 namespace internal { |
| 531 | 581 |
| 532 const char kProfileIconFileName[] = "Google Profile.ico"; | 582 base::FilePath GetProfileIconPath(const base::FilePath& profile_path) { |
| 583 return profile_path.AppendASCII(kProfileIconFileName); | |
| 584 } | |
| 533 | 585 |
| 534 string16 GetShortcutFilenameForProfile(const string16& profile_name, | 586 string16 GetShortcutFilenameForProfile(const string16& profile_name, |
| 535 BrowserDistribution* distribution) { | 587 BrowserDistribution* distribution) { |
| 536 string16 shortcut_name; | 588 string16 shortcut_name; |
| 537 if (!profile_name.empty()) { | 589 if (!profile_name.empty()) { |
| 538 shortcut_name.append(SanitizeShortcutProfileNameString(profile_name)); | 590 shortcut_name.append(SanitizeShortcutProfileNameString(profile_name)); |
| 539 shortcut_name.append(L" - "); | 591 shortcut_name.append(L" - "); |
| 540 shortcut_name.append(l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME)); | 592 shortcut_name.append(l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME)); |
| 541 } else { | 593 } else { |
| 542 shortcut_name.append(distribution->GetAppShortCutName()); | 594 shortcut_name.append(distribution->GetAppShortCutName()); |
| 543 } | 595 } |
| 544 return shortcut_name + installer::kLnkExt; | 596 return shortcut_name + installer::kLnkExt; |
| 545 } | 597 } |
| 546 | 598 |
| 547 string16 CreateProfileShortcutFlags(const base::FilePath& profile_path) { | 599 string16 CreateProfileShortcutFlags(const base::FilePath& profile_path) { |
| 548 return base::StringPrintf(L"--%ls=\"%ls\"", | 600 return base::StringPrintf(L"--%ls=\"%ls\"", |
| 549 ASCIIToUTF16(switches::kProfileDirectory).c_str(), | 601 ASCIIToUTF16(switches::kProfileDirectory).c_str(), |
| 550 profile_path.BaseName().value().c_str()); | 602 profile_path.BaseName().value().c_str()); |
| 551 } | 603 } |
| 552 | 604 |
| 553 } // namespace internal | 605 } // namespace internal |
| 554 } // namespace profiles | 606 } // namespace profiles |
| 555 | 607 |
| 556 // static | 608 // static |
| 557 bool ProfileShortcutManager::IsFeatureEnabled() { | 609 bool ProfileShortcutManager::IsFeatureEnabled() { |
| 558 return BrowserDistribution::GetDistribution()->CanCreateDesktopShortcuts() && | 610 return BrowserDistribution::GetDistribution()->CanCreateDesktopShortcuts() && |
| 559 !CommandLine::ForCurrentProcess()->HasSwitch(switches::kUserDataDir) && | 611 !CommandLine::ForCurrentProcess()->HasSwitch(switches::kUserDataDir); |
| 560 !CommandLine::ForCurrentProcess()->HasSwitch(switches::kShowAppList); | |
| 561 } | 612 } |
| 562 | 613 |
| 563 // static | 614 // static |
| 564 ProfileShortcutManager* ProfileShortcutManager::Create( | 615 ProfileShortcutManager* ProfileShortcutManager::Create( |
| 565 ProfileManager* manager) { | 616 ProfileManager* manager) { |
| 566 return new ProfileShortcutManagerWin(manager); | 617 return new ProfileShortcutManagerWin(manager); |
| 567 } | 618 } |
| 568 | 619 |
| 569 ProfileShortcutManagerWin::ProfileShortcutManagerWin(ProfileManager* manager) | 620 ProfileShortcutManagerWin::ProfileShortcutManagerWin(ProfileManager* manager) |
| 570 : profile_manager_(manager) { | 621 : profile_manager_(manager) { |
| 571 DCHECK_EQ( | 622 DCHECK_EQ( |
| 572 arraysize(kProfileAvatarIconResources2x), | 623 arraysize(kProfileAvatarIconResources2x), |
| 573 profile_manager_->GetProfileInfoCache().GetDefaultAvatarIconCount()); | 624 profile_manager_->GetProfileInfoCache().GetDefaultAvatarIconCount()); |
| 574 | 625 |
| 575 profile_manager_->GetProfileInfoCache().AddObserver(this); | 626 profile_manager_->GetProfileInfoCache().AddObserver(this); |
| 576 } | 627 } |
| 577 | 628 |
| 578 ProfileShortcutManagerWin::~ProfileShortcutManagerWin() { | 629 ProfileShortcutManagerWin::~ProfileShortcutManagerWin() { |
| 579 profile_manager_->GetProfileInfoCache().RemoveObserver(this); | 630 profile_manager_->GetProfileInfoCache().RemoveObserver(this); |
| 580 } | 631 } |
| 581 | 632 |
| 633 void ProfileShortcutManagerWin::CreateProfileIcon( | |
| 634 const base::FilePath& profile_path, | |
| 635 const base::Closure& callback) { | |
| 636 CreateOrUpdateShortcutsForProfileAtPath(profile_path, CREATE_ICON_ONLY, | |
| 637 IGNORE_NON_PROFILE_SHORTCUTS, | |
| 638 callback); | |
| 639 } | |
| 640 | |
| 582 void ProfileShortcutManagerWin::CreateProfileShortcut( | 641 void ProfileShortcutManagerWin::CreateProfileShortcut( |
| 583 const base::FilePath& profile_path) { | 642 const base::FilePath& profile_path) { |
| 584 CreateOrUpdateShortcutsForProfileAtPath(profile_path, CREATE_WHEN_NONE_FOUND, | 643 CreateOrUpdateShortcutsForProfileAtPath(profile_path, CREATE_WHEN_NONE_FOUND, |
| 585 IGNORE_NON_PROFILE_SHORTCUTS); | 644 IGNORE_NON_PROFILE_SHORTCUTS, |
| 645 base::Closure()); | |
| 586 } | 646 } |
| 587 | 647 |
| 588 void ProfileShortcutManagerWin::RemoveProfileShortcuts( | 648 void ProfileShortcutManagerWin::RemoveProfileShortcuts( |
| 589 const base::FilePath& profile_path) { | 649 const base::FilePath& profile_path) { |
| 590 BrowserThread::PostTask( | 650 BrowserThread::PostTask( |
| 591 BrowserThread::FILE, FROM_HERE, | 651 BrowserThread::FILE, FROM_HERE, |
| 592 base::Bind(&DeleteDesktopShortcutsAndIconFile, profile_path, false)); | 652 base::Bind(&DeleteDesktopShortcuts, profile_path, false)); |
| 593 } | 653 } |
| 594 | 654 |
| 595 void ProfileShortcutManagerWin::HasProfileShortcuts( | 655 void ProfileShortcutManagerWin::HasProfileShortcuts( |
| 596 const base::FilePath& profile_path, | 656 const base::FilePath& profile_path, |
| 597 const base::Callback<void(bool)>& callback) { | 657 const base::Callback<void(bool)>& callback) { |
| 598 BrowserThread::PostTaskAndReplyWithResult( | 658 BrowserThread::PostTaskAndReplyWithResult( |
| 599 BrowserThread::FILE, FROM_HERE, | 659 BrowserThread::FILE, FROM_HERE, |
| 600 base::Bind(&HasAnyProfileShortcuts, profile_path), callback); | 660 base::Bind(&HasAnyProfileShortcuts, profile_path), callback); |
| 601 } | 661 } |
| 602 | 662 |
| 603 void ProfileShortcutManagerWin::OnProfileAdded( | 663 void ProfileShortcutManagerWin::OnProfileAdded( |
| 604 const base::FilePath& profile_path) { | 664 const base::FilePath& profile_path) { |
| 665 CreateProfileIcon(profile_path, base::Closure()); | |
| 605 if (profile_manager_->GetProfileInfoCache().GetNumberOfProfiles() == 2) { | 666 if (profile_manager_->GetProfileInfoCache().GetNumberOfProfiles() == 2) { |
| 606 // When the second profile is added, make existing non-profile shortcuts | 667 // When the second profile is added, make existing non-profile shortcuts |
| 607 // point to the first profile and be badged/named appropriately. | 668 // point to the first profile and be badged/named appropriately. |
| 608 CreateOrUpdateShortcutsForProfileAtPath(GetOtherProfilePath(profile_path), | 669 CreateOrUpdateShortcutsForProfileAtPath(GetOtherProfilePath(profile_path), |
| 609 UPDATE_EXISTING_ONLY, | 670 UPDATE_EXISTING_ONLY, |
| 610 UPDATE_NON_PROFILE_SHORTCUTS); | 671 UPDATE_NON_PROFILE_SHORTCUTS, |
| 672 base::Closure()); | |
| 611 } | 673 } |
| 612 } | 674 } |
| 613 | 675 |
| 614 void ProfileShortcutManagerWin::OnProfileWillBeRemoved( | 676 void ProfileShortcutManagerWin::OnProfileWillBeRemoved( |
| 615 const base::FilePath& profile_path) { | 677 const base::FilePath& profile_path) { |
| 616 } | 678 } |
| 617 | 679 |
| 618 void ProfileShortcutManagerWin::OnProfileWasRemoved( | 680 void ProfileShortcutManagerWin::OnProfileWasRemoved( |
| 619 const base::FilePath& profile_path, | 681 const base::FilePath& profile_path, |
| 620 const string16& profile_name) { | 682 const string16& profile_name) { |
| 621 const ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache(); | 683 const ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache(); |
| 622 // If there is only one profile remaining, remove the badging information | 684 // If there is only one profile remaining, remove the badging information |
| 623 // from an existing shortcut. | 685 // from an existing shortcut. |
| 624 const bool deleting_down_to_last_profile = (cache.GetNumberOfProfiles() == 1); | 686 const bool deleting_down_to_last_profile = (cache.GetNumberOfProfiles() == 1); |
| 625 if (deleting_down_to_last_profile) { | 687 if (deleting_down_to_last_profile) { |
| 688 // This is needed to unbadge the icon. | |
| 626 CreateOrUpdateShortcutsForProfileAtPath(cache.GetPathOfProfileAtIndex(0), | 689 CreateOrUpdateShortcutsForProfileAtPath(cache.GetPathOfProfileAtIndex(0), |
| 627 UPDATE_EXISTING_ONLY, | 690 UPDATE_EXISTING_ONLY, |
| 628 IGNORE_NON_PROFILE_SHORTCUTS); | 691 IGNORE_NON_PROFILE_SHORTCUTS, |
| 692 base::Closure()); | |
| 629 } | 693 } |
| 630 | 694 |
| 631 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 695 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 632 base::Bind(&DeleteDesktopShortcutsAndIconFile, | 696 base::Bind(&DeleteDesktopShortcuts, |
| 633 profile_path, | 697 profile_path, |
| 634 deleting_down_to_last_profile)); | 698 deleting_down_to_last_profile)); |
| 635 } | 699 } |
| 636 | 700 |
| 637 void ProfileShortcutManagerWin::OnProfileNameChanged( | 701 void ProfileShortcutManagerWin::OnProfileNameChanged( |
| 638 const base::FilePath& profile_path, | 702 const base::FilePath& profile_path, |
| 639 const string16& old_profile_name) { | 703 const string16& old_profile_name) { |
| 640 CreateOrUpdateShortcutsForProfileAtPath(profile_path, UPDATE_EXISTING_ONLY, | 704 CreateOrUpdateShortcutsForProfileAtPath(profile_path, UPDATE_EXISTING_ONLY, |
| 641 IGNORE_NON_PROFILE_SHORTCUTS); | 705 IGNORE_NON_PROFILE_SHORTCUTS, |
| 706 base::Closure()); | |
| 642 } | 707 } |
| 643 | 708 |
| 644 void ProfileShortcutManagerWin::OnProfileAvatarChanged( | 709 void ProfileShortcutManagerWin::OnProfileAvatarChanged( |
| 645 const base::FilePath& profile_path) { | 710 const base::FilePath& profile_path) { |
| 646 CreateOrUpdateShortcutsForProfileAtPath(profile_path, UPDATE_EXISTING_ONLY, | 711 CreateProfileIcon(profile_path, base::Closure()); |
| 647 IGNORE_NON_PROFILE_SHORTCUTS); | |
| 648 } | 712 } |
| 649 | 713 |
| 650 base::FilePath ProfileShortcutManagerWin::GetOtherProfilePath( | 714 base::FilePath ProfileShortcutManagerWin::GetOtherProfilePath( |
| 651 const base::FilePath& profile_path) { | 715 const base::FilePath& profile_path) { |
| 652 const ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache(); | 716 const ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache(); |
| 653 DCHECK_EQ(2U, cache.GetNumberOfProfiles()); | 717 DCHECK_EQ(2U, cache.GetNumberOfProfiles()); |
| 654 // Get the index of the current profile, in order to find the index of the | 718 // Get the index of the current profile, in order to find the index of the |
| 655 // other profile. | 719 // other profile. |
| 656 size_t current_profile_index = cache.GetIndexOfProfileWithPath(profile_path); | 720 size_t current_profile_index = cache.GetIndexOfProfileWithPath(profile_path); |
| 657 size_t other_profile_index = (current_profile_index == 0) ? 1 : 0; | 721 size_t other_profile_index = (current_profile_index == 0) ? 1 : 0; |
| 658 return cache.GetPathOfProfileAtIndex(other_profile_index); | 722 return cache.GetPathOfProfileAtIndex(other_profile_index); |
| 659 } | 723 } |
| 660 | 724 |
| 661 void ProfileShortcutManagerWin::CreateOrUpdateShortcutsForProfileAtPath( | 725 void ProfileShortcutManagerWin::CreateOrUpdateShortcutsForProfileAtPath( |
| 662 const base::FilePath& profile_path, | 726 const base::FilePath& profile_path, |
| 663 CreateOrUpdateMode create_mode, | 727 CreateOrUpdateMode create_mode, |
| 664 NonProfileShortcutAction action) { | 728 NonProfileShortcutAction action, |
| 729 const base::Closure& callback) { | |
| 730 CreateOrUpdateShortcutsParams params; | |
| 731 params.profile_path = profile_path; | |
| 732 params.create_mode = create_mode; | |
| 733 params.action = action; | |
| 734 | |
| 665 ProfileInfoCache* cache = &profile_manager_->GetProfileInfoCache(); | 735 ProfileInfoCache* cache = &profile_manager_->GetProfileInfoCache(); |
| 666 size_t profile_index = cache->GetIndexOfProfileWithPath(profile_path); | 736 size_t profile_index = cache->GetIndexOfProfileWithPath(profile_path); |
| 667 if (profile_index == std::string::npos) | 737 if (profile_index == std::string::npos) |
| 668 return; | 738 return; |
| 669 bool remove_badging = cache->GetNumberOfProfiles() == 1; | 739 bool remove_badging = cache->GetNumberOfProfiles() == 1; |
| 670 | 740 |
| 671 string16 old_shortcut_appended_name = | 741 params.old_profile_name = |
| 672 cache->GetShortcutNameOfProfileAtIndex(profile_index); | 742 cache->GetShortcutNameOfProfileAtIndex(profile_index); |
| 673 | 743 |
| 674 // Exit early if the mode is to update existing profile shortcuts only and | 744 // Exit early if the mode is to update existing profile shortcuts only and |
| 675 // none were ever created for this profile, per the shortcut name not being | 745 // none were ever created for this profile, per the shortcut name not being |
| 676 // set in the profile info cache. | 746 // set in the profile info cache. |
| 677 if (old_shortcut_appended_name.empty() && | 747 if (params.old_profile_name.empty() && |
| 678 create_mode == UPDATE_EXISTING_ONLY && | 748 create_mode == UPDATE_EXISTING_ONLY && |
| 679 action == IGNORE_NON_PROFILE_SHORTCUTS) { | 749 action == IGNORE_NON_PROFILE_SHORTCUTS) { |
| 680 return; | 750 return; |
| 681 } | 751 } |
| 682 | 752 |
| 683 string16 new_shortcut_appended_name; | |
| 684 if (!remove_badging) | 753 if (!remove_badging) |
| 685 new_shortcut_appended_name = cache->GetNameOfProfileAtIndex(profile_index); | 754 params.profile_name = cache->GetNameOfProfileAtIndex(profile_index); |
| 686 | 755 |
| 687 SkBitmap avatar_bitmap_copy_1x; | |
| 688 SkBitmap avatar_bitmap_copy_2x; | |
| 689 if (!remove_badging) { | 756 if (!remove_badging) { |
| 690 const size_t icon_index = | 757 const size_t icon_index = |
| 691 cache->GetAvatarIconIndexOfProfileAtIndex(profile_index); | 758 cache->GetAvatarIconIndexOfProfileAtIndex(profile_index); |
| 692 const int resource_id_1x = | 759 const int resource_id_1x = |
| 693 cache->GetDefaultAvatarIconResourceIDAtIndex(icon_index); | 760 cache->GetDefaultAvatarIconResourceIDAtIndex(icon_index); |
| 694 const int resource_id_2x = kProfileAvatarIconResources2x[icon_index]; | 761 const int resource_id_2x = kProfileAvatarIconResources2x[icon_index]; |
| 695 // Make a copy of the SkBitmaps to ensure that we can safely use the image | 762 // Make a copy of the SkBitmaps to ensure that we can safely use the image |
| 696 // data on the FILE thread. | 763 // data on the FILE thread. |
| 697 avatar_bitmap_copy_1x = GetImageResourceSkBitmapCopy(resource_id_1x); | 764 params.avatar_image_1x = GetImageResourceSkBitmapCopy(resource_id_1x); |
| 698 avatar_bitmap_copy_2x = GetImageResourceSkBitmapCopy(resource_id_2x); | 765 params.avatar_image_2x = GetImageResourceSkBitmapCopy(resource_id_2x); |
| 699 } | 766 } |
| 700 BrowserThread::PostTask( | 767 BrowserThread::PostTask( |
| 701 BrowserThread::FILE, FROM_HERE, | 768 BrowserThread::FILE, FROM_HERE, |
| 702 base::Bind(&CreateOrUpdateDesktopShortcutsForProfile, profile_path, | 769 base::Bind(&CreateOrUpdateDesktopShortcutsAndIconForProfile, params, |
| 703 old_shortcut_appended_name, new_shortcut_appended_name, | 770 callback)); |
| 704 avatar_bitmap_copy_1x, avatar_bitmap_copy_2x, create_mode, | |
| 705 action)); | |
| 706 | 771 |
| 707 cache->SetShortcutNameOfProfileAtIndex(profile_index, | 772 cache->SetShortcutNameOfProfileAtIndex(profile_index, |
| 708 new_shortcut_appended_name); | 773 params.profile_name); |
| 709 } | 774 } |
| OLD | NEW |