Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(204)

Side by Side Diff: chrome/browser/profiles/profile_shortcut_manager_win.cc

Issue 14137032: Create profile .ico file on profile creation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rework Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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
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(
350 base::FilePath profile_path_in,
351 ProfileShortcutManagerWin::CreateOrUpdateMode create_mode_in,
352 ProfileShortcutManagerWin::NonProfileShortcutAction action_in)
353 : profile_path(profile_path_in), create_mode(create_mode_in),
Alexei Svitkine (slow) 2013/06/10 17:44:52 I'm not sure you need the _in suffix for this to w
calamity 2013/06/13 06:26:03 Done.
354 action(action_in) {}
355 ~CreateOrUpdateShortcutsParams() {}
356
357 ProfileShortcutManagerWin::CreateOrUpdateMode create_mode;
358 ProfileShortcutManagerWin::NonProfileShortcutAction action;
359
360 // The path for this profile.
361 base::FilePath profile_path;
362 // The profile name before this update. Empty on create.
363 string16 old_profile_name;
364 // The new profile name.
365 string16 profile_name;
366 // Avatar images for this profile.
367 SkBitmap avatar_image_1x;
368 SkBitmap avatar_image_2x;
369 };
370
311 // Updates all desktop shortcuts for the given profile to have the specified 371 // 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 372 // 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 373 // 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. 374 // be updated is specified by |action|. Must be called on the FILE thread.
315 void CreateOrUpdateDesktopShortcutsForProfile( 375 void CreateOrUpdateDesktopShortcutsAndIconForProfile(
316 const base::FilePath& profile_path, 376 const CreateOrUpdateShortcutsParams& params,
317 const string16& old_profile_name, 377 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)); 378 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
324 379
380 const base::FilePath shortcut_icon =
381 CreateOrUpdateShortcutIconForProfile(params.profile_path,
382 params.avatar_image_1x,
383 params.avatar_image_2x,
384 callback);
385 if (shortcut_icon.empty()) {
386 NOTREACHED();
387 return;
388 }
389 if (params.create_mode == ProfileShortcutManagerWin::CREATE_ICON_ONLY)
390 return;
391
325 base::FilePath chrome_exe; 392 base::FilePath chrome_exe;
326 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { 393 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
327 NOTREACHED(); 394 NOTREACHED();
328 return; 395 return;
329 } 396 }
330 397
331 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); 398 BrowserDistribution* distribution = BrowserDistribution::GetDistribution();
332 // Ensure that the distribution supports creating shortcuts. If it doesn't, 399 // Ensure that the distribution supports creating shortcuts. If it doesn't,
333 // the following code may result in NOTREACHED() being hit. 400 // the following code may result in NOTREACHED() being hit.
334 DCHECK(distribution->CanCreateDesktopShortcuts()); 401 DCHECK(distribution->CanCreateDesktopShortcuts());
335 402
336 if (old_profile_name != profile_name) { 403 if (params.old_profile_name != params.profile_name) {
337 const string16 old_shortcut_filename = 404 const string16 old_shortcut_filename =
338 profiles::internal::GetShortcutFilenameForProfile(old_profile_name, 405 profiles::internal::GetShortcutFilenameForProfile(
339 distribution); 406 params.old_profile_name,
407 distribution);
340 const string16 new_shortcut_filename = 408 const string16 new_shortcut_filename =
341 profiles::internal::GetShortcutFilenameForProfile(profile_name, 409 profiles::internal::GetShortcutFilenameForProfile(params.profile_name,
342 distribution); 410 distribution);
343 RenameChromeDesktopShortcutForProfile(old_shortcut_filename, 411 RenameChromeDesktopShortcutForProfile(old_shortcut_filename,
344 new_shortcut_filename); 412 new_shortcut_filename);
345 } 413 }
346 414
347 ShellUtil::ShortcutProperties properties(ShellUtil::CURRENT_USER); 415 ShellUtil::ShortcutProperties properties(ShellUtil::CURRENT_USER);
348 installer::Product product(distribution); 416 installer::Product product(distribution);
349 product.AddDefaultShortcutProperties(chrome_exe, &properties); 417 product.AddDefaultShortcutProperties(chrome_exe, &properties);
350 418
351 const string16 command_line = 419 const string16 command_line =
352 profiles::internal::CreateProfileShortcutFlags(profile_path); 420 profiles::internal::CreateProfileShortcutFlags(params.profile_path);
353 421
354 // Only set the profile-specific properties when |profile_name| is non empty. 422 // 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, 423 // If it is empty, it means the shortcut being created should be a regular,
356 // non-profile Chrome shortcut. 424 // non-profile Chrome shortcut.
357 if (!profile_name.empty()) { 425 properties.set_icon(shortcut_icon, 0);
358 const base::FilePath shortcut_icon = 426 if (!params.profile_name.empty()) {
359 CreateChromeDesktopShortcutIconForProfile(profile_path,
360 avatar_image_1x,
361 avatar_image_2x);
362 if (!shortcut_icon.empty())
363 properties.set_icon(shortcut_icon, 0);
364 properties.set_arguments(command_line); 427 properties.set_arguments(command_line);
365 } else { 428 } else {
366 // Set the arguments explicitly to the empty string to ensure that 429 // Set the arguments explicitly to the empty string to ensure that
367 // |ShellUtil::CreateOrUpdateShortcut| updates that part of the shortcut. 430 // |ShellUtil::CreateOrUpdateShortcut| updates that part of the shortcut.
368 properties.set_arguments(string16()); 431 properties.set_arguments(string16());
369 } 432 }
370 433
371 properties.set_app_id( 434 properties.set_app_id(
372 ShellIntegration::GetChromiumModelIdForProfile(profile_path)); 435 ShellIntegration::GetChromiumModelIdForProfile(params.profile_path));
373 436
374 ShellUtil::ShortcutOperation operation = 437 ShellUtil::ShortcutOperation operation =
375 ShellUtil::SHELL_SHORTCUT_REPLACE_EXISTING; 438 ShellUtil::SHELL_SHORTCUT_REPLACE_EXISTING;
376 439
377 std::vector<base::FilePath> shortcuts; 440 std::vector<base::FilePath> shortcuts;
378 ListDesktopShortcutsWithCommandLine(chrome_exe, command_line, 441 ListDesktopShortcutsWithCommandLine(chrome_exe, command_line,
379 action == ProfileShortcutManagerWin::UPDATE_NON_PROFILE_SHORTCUTS, 442 params.action == ProfileShortcutManagerWin::UPDATE_NON_PROFILE_SHORTCUTS,
380 &shortcuts); 443 &shortcuts);
381 if (create_mode == ProfileShortcutManagerWin::CREATE_WHEN_NONE_FOUND && 444 if (params.create_mode == ProfileShortcutManagerWin::CREATE_WHEN_NONE_FOUND &&
382 shortcuts.empty()) { 445 shortcuts.empty()) {
383 const string16 shortcut_name = 446 const string16 shortcut_name =
384 profiles::internal::GetShortcutFilenameForProfile(profile_name, 447 profiles::internal::GetShortcutFilenameForProfile(params.profile_name,
385 distribution); 448 distribution);
386 shortcuts.push_back(base::FilePath(shortcut_name)); 449 shortcuts.push_back(base::FilePath(shortcut_name));
387 operation = ShellUtil::SHELL_SHORTCUT_CREATE_IF_NO_SYSTEM_LEVEL; 450 operation = ShellUtil::SHELL_SHORTCUT_CREATE_IF_NO_SYSTEM_LEVEL;
388 } 451 }
389 452
390 for (size_t i = 0; i < shortcuts.size(); ++i) { 453 for (size_t i = 0; i < shortcuts.size(); ++i) {
391 const base::FilePath shortcut_name = 454 const base::FilePath shortcut_name =
392 shortcuts[i].BaseName().RemoveExtension(); 455 shortcuts[i].BaseName().RemoveExtension();
393 properties.set_shortcut_name(shortcut_name.value()); 456 properties.set_shortcut_name(shortcut_name.value());
394 ShellUtil::CreateOrUpdateShortcut(ShellUtil::SHORTCUT_LOCATION_DESKTOP, 457 ShellUtil::CreateOrUpdateShortcut(ShellUtil::SHORTCUT_LOCATION_DESKTOP,
(...skipping 12 matching lines...) Expand all
407 file_util::FileEnumerator::FILES); 470 file_util::FileEnumerator::FILES);
408 for (base::FilePath path = enumerator.Next(); !path.empty(); 471 for (base::FilePath path = enumerator.Next(); !path.empty();
409 path = enumerator.Next()) { 472 path = enumerator.Next()) {
410 if (IsChromeShortcut(path, chrome_exe, NULL)) 473 if (IsChromeShortcut(path, chrome_exe, NULL))
411 return true; 474 return true;
412 } 475 }
413 476
414 return false; 477 return false;
415 } 478 }
416 479
417 // Deletes all desktop shortcuts for the specified profile and also removes the 480 // Deletes all desktop shortcuts for the specified profile. If
418 // corresponding icon file. If |ensure_shortcuts_remain| is true, then a regular 481 // |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 482 // 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. 483 // shortcut(s). Must be called on the FILE thread.
421 void DeleteDesktopShortcutsAndIconFile(const base::FilePath& profile_path, 484 void DeleteDesktopShortcuts(const base::FilePath& profile_path,
422 bool ensure_shortcuts_remain) { 485 bool ensure_shortcuts_remain) {
423 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 486 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
424 487
425 base::FilePath chrome_exe; 488 base::FilePath chrome_exe;
426 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { 489 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
427 NOTREACHED(); 490 NOTREACHED();
428 return; 491 return;
429 } 492 }
430 493
431 const string16 command_line = 494 const string16 command_line =
432 profiles::internal::CreateProfileShortcutFlags(profile_path); 495 profiles::internal::CreateProfileShortcutFlags(profile_path);
433 std::vector<base::FilePath> shortcuts; 496 std::vector<base::FilePath> shortcuts;
434 ListDesktopShortcutsWithCommandLine(chrome_exe, command_line, false, 497 ListDesktopShortcutsWithCommandLine(chrome_exe, command_line, false,
435 &shortcuts); 498 &shortcuts);
436 499
437 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); 500 BrowserDistribution* distribution = BrowserDistribution::GetDistribution();
438 for (size_t i = 0; i < shortcuts.size(); ++i) { 501 for (size_t i = 0; i < shortcuts.size(); ++i) {
439 // Use file_util::Delete() instead of ShellUtil::RemoveShortcut(), as the 502 // Use file_util::Delete() instead of ShellUtil::RemoveShortcut(), as the
440 // latter causes non-profile taskbar shortcuts to be unpinned. 503 // latter causes non-profile taskbar shortcuts to be unpinned.
441 file_util::Delete(shortcuts[i], false); 504 file_util::Delete(shortcuts[i], false);
442 // Notify the shell that the shortcut was deleted to ensure desktop refresh. 505 // Notify the shell that the shortcut was deleted to ensure desktop refresh.
443 SHChangeNotify(SHCNE_DELETE, SHCNF_PATH, shortcuts[i].value().c_str(), 506 SHChangeNotify(SHCNE_DELETE, SHCNF_PATH, shortcuts[i].value().c_str(),
444 NULL); 507 NULL);
445 } 508 }
446 509
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 510 // 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. 511 // last shortcuts to be removed, re-create a regular non-profile shortcut.
453 const bool had_shortcuts = !shortcuts.empty(); 512 const bool had_shortcuts = !shortcuts.empty();
454 if (ensure_shortcuts_remain && had_shortcuts && 513 if (ensure_shortcuts_remain && had_shortcuts &&
455 !ChromeDesktopShortcutsExist(chrome_exe)) { 514 !ChromeDesktopShortcutsExist(chrome_exe)) {
456 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); 515 BrowserDistribution* distribution = BrowserDistribution::GetDistribution();
457 // Ensure that the distribution supports creating shortcuts. If it doesn't, 516 // Ensure that the distribution supports creating shortcuts. If it doesn't,
458 // the following code may result in NOTREACHED() being hit. 517 // the following code may result in NOTREACHED() being hit.
459 DCHECK(distribution->CanCreateDesktopShortcuts()); 518 DCHECK(distribution->CanCreateDesktopShortcuts());
460 installer::Product product(distribution); 519 installer::Product product(distribution);
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
522 SkBitmap bitmap_copy; 581 SkBitmap bitmap_copy;
523 image_bitmap->deepCopyTo(&bitmap_copy, image_bitmap->getConfig()); 582 image_bitmap->deepCopyTo(&bitmap_copy, image_bitmap->getConfig());
524 return bitmap_copy; 583 return bitmap_copy;
525 } 584 }
526 585
527 } // namespace 586 } // namespace
528 587
529 namespace profiles { 588 namespace profiles {
530 namespace internal { 589 namespace internal {
531 590
532 const char kProfileIconFileName[] = "Google Profile.ico"; 591 base::FilePath GetProfileIconPath(const base::FilePath& profile_path) {
592 return profile_path.AppendASCII(kProfileIconFileName);
593 }
533 594
534 string16 GetShortcutFilenameForProfile(const string16& profile_name, 595 string16 GetShortcutFilenameForProfile(const string16& profile_name,
535 BrowserDistribution* distribution) { 596 BrowserDistribution* distribution) {
536 string16 shortcut_name; 597 string16 shortcut_name;
537 if (!profile_name.empty()) { 598 if (!profile_name.empty()) {
538 shortcut_name.append(SanitizeShortcutProfileNameString(profile_name)); 599 shortcut_name.append(SanitizeShortcutProfileNameString(profile_name));
539 shortcut_name.append(L" - "); 600 shortcut_name.append(L" - ");
540 shortcut_name.append(l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME)); 601 shortcut_name.append(l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME));
541 } else { 602 } else {
542 shortcut_name.append(distribution->GetAppShortCutName()); 603 shortcut_name.append(distribution->GetAppShortCutName());
543 } 604 }
544 return shortcut_name + installer::kLnkExt; 605 return shortcut_name + installer::kLnkExt;
545 } 606 }
546 607
547 string16 CreateProfileShortcutFlags(const base::FilePath& profile_path) { 608 string16 CreateProfileShortcutFlags(const base::FilePath& profile_path) {
548 return base::StringPrintf(L"--%ls=\"%ls\"", 609 return base::StringPrintf(L"--%ls=\"%ls\"",
549 ASCIIToUTF16(switches::kProfileDirectory).c_str(), 610 ASCIIToUTF16(switches::kProfileDirectory).c_str(),
550 profile_path.BaseName().value().c_str()); 611 profile_path.BaseName().value().c_str());
551 } 612 }
552 613
553 } // namespace internal 614 } // namespace internal
554 } // namespace profiles 615 } // namespace profiles
555 616
556 // static 617 // static
557 bool ProfileShortcutManager::IsFeatureEnabled() { 618 bool ProfileShortcutManager::IsFeatureEnabled() {
558 return BrowserDistribution::GetDistribution()->CanCreateDesktopShortcuts() && 619 return BrowserDistribution::GetDistribution()->CanCreateDesktopShortcuts() &&
559 !CommandLine::ForCurrentProcess()->HasSwitch(switches::kUserDataDir) && 620 !CommandLine::ForCurrentProcess()->HasSwitch(switches::kUserDataDir);
560 !CommandLine::ForCurrentProcess()->HasSwitch(switches::kShowAppList);
561 } 621 }
562 622
563 // static 623 // static
564 ProfileShortcutManager* ProfileShortcutManager::Create( 624 ProfileShortcutManager* ProfileShortcutManager::Create(
565 ProfileManager* manager) { 625 ProfileManager* manager) {
566 return new ProfileShortcutManagerWin(manager); 626 return new ProfileShortcutManagerWin(manager);
567 } 627 }
568 628
569 ProfileShortcutManagerWin::ProfileShortcutManagerWin(ProfileManager* manager) 629 ProfileShortcutManagerWin::ProfileShortcutManagerWin(ProfileManager* manager)
570 : profile_manager_(manager) { 630 : profile_manager_(manager) {
571 DCHECK_EQ( 631 DCHECK_EQ(
572 arraysize(kProfileAvatarIconResources2x), 632 arraysize(kProfileAvatarIconResources2x),
573 profile_manager_->GetProfileInfoCache().GetDefaultAvatarIconCount()); 633 profile_manager_->GetProfileInfoCache().GetDefaultAvatarIconCount());
574 634
575 profile_manager_->GetProfileInfoCache().AddObserver(this); 635 profile_manager_->GetProfileInfoCache().AddObserver(this);
576 } 636 }
577 637
578 ProfileShortcutManagerWin::~ProfileShortcutManagerWin() { 638 ProfileShortcutManagerWin::~ProfileShortcutManagerWin() {
579 profile_manager_->GetProfileInfoCache().RemoveObserver(this); 639 profile_manager_->GetProfileInfoCache().RemoveObserver(this);
580 } 640 }
581 641
642 void ProfileShortcutManagerWin::CreateProfileIcon(
643 const base::FilePath& profile_path,
644 const base::Closure& callback) {
645 CreateOrUpdateShortcutsForProfileAtPath(profile_path, CREATE_ICON_ONLY,
646 IGNORE_NON_PROFILE_SHORTCUTS,
647 callback);
648 }
649
582 void ProfileShortcutManagerWin::CreateProfileShortcut( 650 void ProfileShortcutManagerWin::CreateProfileShortcut(
583 const base::FilePath& profile_path) { 651 const base::FilePath& profile_path) {
584 CreateOrUpdateShortcutsForProfileAtPath(profile_path, CREATE_WHEN_NONE_FOUND, 652 CreateOrUpdateShortcutsForProfileAtPath(profile_path, CREATE_WHEN_NONE_FOUND,
585 IGNORE_NON_PROFILE_SHORTCUTS); 653 IGNORE_NON_PROFILE_SHORTCUTS,
654 base::Closure());
586 } 655 }
587 656
588 void ProfileShortcutManagerWin::RemoveProfileShortcuts( 657 void ProfileShortcutManagerWin::RemoveProfileShortcuts(
589 const base::FilePath& profile_path) { 658 const base::FilePath& profile_path) {
590 BrowserThread::PostTask( 659 BrowserThread::PostTask(
591 BrowserThread::FILE, FROM_HERE, 660 BrowserThread::FILE, FROM_HERE,
592 base::Bind(&DeleteDesktopShortcutsAndIconFile, profile_path, false)); 661 base::Bind(&DeleteDesktopShortcuts, profile_path, false));
593 } 662 }
594 663
595 void ProfileShortcutManagerWin::HasProfileShortcuts( 664 void ProfileShortcutManagerWin::HasProfileShortcuts(
596 const base::FilePath& profile_path, 665 const base::FilePath& profile_path,
597 const base::Callback<void(bool)>& callback) { 666 const base::Callback<void(bool)>& callback) {
598 BrowserThread::PostTaskAndReplyWithResult( 667 BrowserThread::PostTaskAndReplyWithResult(
599 BrowserThread::FILE, FROM_HERE, 668 BrowserThread::FILE, FROM_HERE,
600 base::Bind(&HasAnyProfileShortcuts, profile_path), callback); 669 base::Bind(&HasAnyProfileShortcuts, profile_path), callback);
601 } 670 }
602 671
603 void ProfileShortcutManagerWin::OnProfileAdded( 672 void ProfileShortcutManagerWin::OnProfileAdded(
604 const base::FilePath& profile_path) { 673 const base::FilePath& profile_path) {
674 CreateProfileIcon(profile_path, base::Closure());
Alexei Svitkine (slow) 2013/06/10 17:44:52 Hmm, I would like to see a test that checks that t
calamity 2013/06/13 06:26:03 Done.
605 if (profile_manager_->GetProfileInfoCache().GetNumberOfProfiles() == 2) { 675 if (profile_manager_->GetProfileInfoCache().GetNumberOfProfiles() == 2) {
606 // When the second profile is added, make existing non-profile shortcuts 676 // When the second profile is added, make existing non-profile shortcuts
607 // point to the first profile and be badged/named appropriately. 677 // point to the first profile and be badged/named appropriately.
608 CreateOrUpdateShortcutsForProfileAtPath(GetOtherProfilePath(profile_path), 678 CreateOrUpdateShortcutsForProfileAtPath(GetOtherProfilePath(profile_path),
609 UPDATE_EXISTING_ONLY, 679 UPDATE_EXISTING_ONLY,
610 UPDATE_NON_PROFILE_SHORTCUTS); 680 UPDATE_NON_PROFILE_SHORTCUTS,
681 base::Closure());
611 } 682 }
612 } 683 }
613 684
614 void ProfileShortcutManagerWin::OnProfileWillBeRemoved( 685 void ProfileShortcutManagerWin::OnProfileWillBeRemoved(
615 const base::FilePath& profile_path) { 686 const base::FilePath& profile_path) {
616 } 687 }
617 688
618 void ProfileShortcutManagerWin::OnProfileWasRemoved( 689 void ProfileShortcutManagerWin::OnProfileWasRemoved(
619 const base::FilePath& profile_path, 690 const base::FilePath& profile_path,
620 const string16& profile_name) { 691 const string16& profile_name) {
621 const ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache(); 692 const ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache();
622 // If there is only one profile remaining, remove the badging information 693 // If there is only one profile remaining, remove the badging information
623 // from an existing shortcut. 694 // from an existing shortcut.
624 const bool deleting_down_to_last_profile = (cache.GetNumberOfProfiles() == 1); 695 const bool deleting_down_to_last_profile = (cache.GetNumberOfProfiles() == 1);
625 if (deleting_down_to_last_profile) { 696 if (deleting_down_to_last_profile) {
697 // This is needed to unbadge the icon.
626 CreateOrUpdateShortcutsForProfileAtPath(cache.GetPathOfProfileAtIndex(0), 698 CreateOrUpdateShortcutsForProfileAtPath(cache.GetPathOfProfileAtIndex(0),
627 UPDATE_EXISTING_ONLY, 699 UPDATE_EXISTING_ONLY,
628 IGNORE_NON_PROFILE_SHORTCUTS); 700 IGNORE_NON_PROFILE_SHORTCUTS,
701 base::Closure());
629 } 702 }
630 703
631 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, 704 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
632 base::Bind(&DeleteDesktopShortcutsAndIconFile, 705 base::Bind(&DeleteDesktopShortcuts,
633 profile_path, 706 profile_path,
634 deleting_down_to_last_profile)); 707 deleting_down_to_last_profile));
635 } 708 }
636 709
637 void ProfileShortcutManagerWin::OnProfileNameChanged( 710 void ProfileShortcutManagerWin::OnProfileNameChanged(
638 const base::FilePath& profile_path, 711 const base::FilePath& profile_path,
639 const string16& old_profile_name) { 712 const string16& old_profile_name) {
640 CreateOrUpdateShortcutsForProfileAtPath(profile_path, UPDATE_EXISTING_ONLY, 713 CreateOrUpdateShortcutsForProfileAtPath(profile_path, UPDATE_EXISTING_ONLY,
641 IGNORE_NON_PROFILE_SHORTCUTS); 714 IGNORE_NON_PROFILE_SHORTCUTS,
715 base::Closure());
642 } 716 }
643 717
644 void ProfileShortcutManagerWin::OnProfileAvatarChanged( 718 void ProfileShortcutManagerWin::OnProfileAvatarChanged(
645 const base::FilePath& profile_path) { 719 const base::FilePath& profile_path) {
646 CreateOrUpdateShortcutsForProfileAtPath(profile_path, UPDATE_EXISTING_ONLY, 720 CreateProfileIcon(profile_path, base::Closure());
647 IGNORE_NON_PROFILE_SHORTCUTS);
648 } 721 }
649 722
650 base::FilePath ProfileShortcutManagerWin::GetOtherProfilePath( 723 base::FilePath ProfileShortcutManagerWin::GetOtherProfilePath(
651 const base::FilePath& profile_path) { 724 const base::FilePath& profile_path) {
652 const ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache(); 725 const ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache();
653 DCHECK_EQ(2U, cache.GetNumberOfProfiles()); 726 DCHECK_EQ(2U, cache.GetNumberOfProfiles());
654 // Get the index of the current profile, in order to find the index of the 727 // Get the index of the current profile, in order to find the index of the
655 // other profile. 728 // other profile.
656 size_t current_profile_index = cache.GetIndexOfProfileWithPath(profile_path); 729 size_t current_profile_index = cache.GetIndexOfProfileWithPath(profile_path);
657 size_t other_profile_index = (current_profile_index == 0) ? 1 : 0; 730 size_t other_profile_index = (current_profile_index == 0) ? 1 : 0;
658 return cache.GetPathOfProfileAtIndex(other_profile_index); 731 return cache.GetPathOfProfileAtIndex(other_profile_index);
659 } 732 }
660 733
661 void ProfileShortcutManagerWin::CreateOrUpdateShortcutsForProfileAtPath( 734 void ProfileShortcutManagerWin::CreateOrUpdateShortcutsForProfileAtPath(
662 const base::FilePath& profile_path, 735 const base::FilePath& profile_path,
663 CreateOrUpdateMode create_mode, 736 CreateOrUpdateMode create_mode,
664 NonProfileShortcutAction action) { 737 NonProfileShortcutAction action,
738 const base::Closure& callback) {
739 CreateOrUpdateShortcutsParams params(profile_path, create_mode, action);
740
665 ProfileInfoCache* cache = &profile_manager_->GetProfileInfoCache(); 741 ProfileInfoCache* cache = &profile_manager_->GetProfileInfoCache();
666 size_t profile_index = cache->GetIndexOfProfileWithPath(profile_path); 742 size_t profile_index = cache->GetIndexOfProfileWithPath(profile_path);
667 if (profile_index == std::string::npos) 743 if (profile_index == std::string::npos)
668 return; 744 return;
669 bool remove_badging = cache->GetNumberOfProfiles() == 1; 745 bool remove_badging = cache->GetNumberOfProfiles() == 1;
670 746
671 string16 old_shortcut_appended_name = 747 params.old_profile_name =
672 cache->GetShortcutNameOfProfileAtIndex(profile_index); 748 cache->GetShortcutNameOfProfileAtIndex(profile_index);
673 749
674 // Exit early if the mode is to update existing profile shortcuts only and 750 // 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 751 // none were ever created for this profile, per the shortcut name not being
676 // set in the profile info cache. 752 // set in the profile info cache.
677 if (old_shortcut_appended_name.empty() && 753 if (params.old_profile_name.empty() &&
678 create_mode == UPDATE_EXISTING_ONLY && 754 create_mode == UPDATE_EXISTING_ONLY &&
679 action == IGNORE_NON_PROFILE_SHORTCUTS) { 755 action == IGNORE_NON_PROFILE_SHORTCUTS) {
680 return; 756 return;
681 } 757 }
682 758
683 string16 new_shortcut_appended_name;
684 if (!remove_badging) 759 if (!remove_badging)
685 new_shortcut_appended_name = cache->GetNameOfProfileAtIndex(profile_index); 760 params.profile_name = cache->GetNameOfProfileAtIndex(profile_index);
686 761
687 SkBitmap avatar_bitmap_copy_1x;
688 SkBitmap avatar_bitmap_copy_2x;
689 if (!remove_badging) { 762 if (!remove_badging) {
690 const size_t icon_index = 763 const size_t icon_index =
691 cache->GetAvatarIconIndexOfProfileAtIndex(profile_index); 764 cache->GetAvatarIconIndexOfProfileAtIndex(profile_index);
692 const int resource_id_1x = 765 const int resource_id_1x =
693 cache->GetDefaultAvatarIconResourceIDAtIndex(icon_index); 766 cache->GetDefaultAvatarIconResourceIDAtIndex(icon_index);
694 const int resource_id_2x = kProfileAvatarIconResources2x[icon_index]; 767 const int resource_id_2x = kProfileAvatarIconResources2x[icon_index];
695 // Make a copy of the SkBitmaps to ensure that we can safely use the image 768 // Make a copy of the SkBitmaps to ensure that we can safely use the image
696 // data on the FILE thread. 769 // data on the FILE thread.
697 avatar_bitmap_copy_1x = GetImageResourceSkBitmapCopy(resource_id_1x); 770 params.avatar_image_1x = GetImageResourceSkBitmapCopy(resource_id_1x);
698 avatar_bitmap_copy_2x = GetImageResourceSkBitmapCopy(resource_id_2x); 771 params.avatar_image_2x = GetImageResourceSkBitmapCopy(resource_id_2x);
699 } 772 }
700 BrowserThread::PostTask( 773 BrowserThread::PostTask(
701 BrowserThread::FILE, FROM_HERE, 774 BrowserThread::FILE, FROM_HERE,
702 base::Bind(&CreateOrUpdateDesktopShortcutsForProfile, profile_path, 775 base::Bind(&CreateOrUpdateDesktopShortcutsAndIconForProfile, params,
703 old_shortcut_appended_name, new_shortcut_appended_name, 776 callback));
704 avatar_bitmap_copy_1x, avatar_bitmap_copy_2x, create_mode,
705 action));
706 777
707 cache->SetShortcutNameOfProfileAtIndex(profile_index, 778 cache->SetShortcutNameOfProfileAtIndex(profile_index,
708 new_shortcut_appended_name); 779 params.profile_name);
709 } 780 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698