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

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

Powered by Google App Engine
This is Rietveld 408576698