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

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: address comments Created 7 years, 5 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
« no previous file with comments | « chrome/browser/profiles/profile_shortcut_manager_win.h ('k') | chrome/common/pref_names.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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>
11 11
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/command_line.h" 13 #include "base/command_line.h"
14 #include "base/file_util.h" 14 #include "base/file_util.h"
15 #include "base/files/file_enumerator.h" 15 #include "base/files/file_enumerator.h"
16 #include "base/path_service.h" 16 #include "base/path_service.h"
17 #include "base/prefs/pref_service.h"
17 #include "base/strings/string16.h" 18 #include "base/strings/string16.h"
18 #include "base/strings/string_util.h" 19 #include "base/strings/string_util.h"
19 #include "base/strings/stringprintf.h" 20 #include "base/strings/stringprintf.h"
20 #include "base/strings/utf_string_conversions.h" 21 #include "base/strings/utf_string_conversions.h"
21 #include "base/win/shortcut.h" 22 #include "base/win/shortcut.h"
22 #include "chrome/browser/app_icon_win.h" 23 #include "chrome/browser/app_icon_win.h"
23 #include "chrome/browser/browser_process.h" 24 #include "chrome/browser/browser_process.h"
25 #include "chrome/browser/chrome_notification_types.h"
24 #include "chrome/browser/profiles/profile_info_cache_observer.h" 26 #include "chrome/browser/profiles/profile_info_cache_observer.h"
25 #include "chrome/browser/profiles/profile_info_util.h" 27 #include "chrome/browser/profiles/profile_info_util.h"
26 #include "chrome/browser/profiles/profile_manager.h" 28 #include "chrome/browser/profiles/profile_manager.h"
27 #include "chrome/browser/shell_integration.h" 29 #include "chrome/browser/shell_integration.h"
28 #include "chrome/common/chrome_switches.h" 30 #include "chrome/common/chrome_switches.h"
31 #include "chrome/common/pref_names.h"
29 #include "chrome/installer/util/browser_distribution.h" 32 #include "chrome/installer/util/browser_distribution.h"
30 #include "chrome/installer/util/product.h" 33 #include "chrome/installer/util/product.h"
31 #include "chrome/installer/util/shell_util.h" 34 #include "chrome/installer/util/shell_util.h"
32 #include "content/public/browser/browser_thread.h" 35 #include "content/public/browser/browser_thread.h"
36 #include "content/public/browser/notification_service.h"
33 #include "grit/chrome_unscaled_resources.h" 37 #include "grit/chrome_unscaled_resources.h"
34 #include "grit/chromium_strings.h" 38 #include "grit/chromium_strings.h"
35 #include "skia/ext/image_operations.h" 39 #include "skia/ext/image_operations.h"
36 #include "skia/ext/platform_canvas.h" 40 #include "skia/ext/platform_canvas.h"
37 #include "ui/base/l10n/l10n_util.h" 41 #include "ui/base/l10n/l10n_util.h"
38 #include "ui/base/resource/resource_bundle.h" 42 #include "ui/base/resource/resource_bundle.h"
39 #include "ui/gfx/icon_util.h" 43 #include "ui/gfx/icon_util.h"
40 #include "ui/gfx/image/image.h" 44 #include "ui/gfx/image/image.h"
41 #include "ui/gfx/image/image_family.h" 45 #include "ui/gfx/image/image_family.h"
42 #include "ui/gfx/rect.h" 46 #include "ui/gfx/rect.h"
43 #include "ui/gfx/skia_util.h" 47 #include "ui/gfx/skia_util.h"
44 48
45 using content::BrowserThread; 49 using content::BrowserThread;
46 50
47 namespace { 51 namespace {
48 52
53 // Name of the badged icon file generated for a given profile.
54 const char kProfileIconFileName[] = "Google Profile.ico";
55
49 // Characters that are not allowed in Windows filenames. Taken from 56 // Characters that are not allowed in Windows filenames. Taken from
50 // http://msdn.microsoft.com/en-us/library/aa365247.aspx 57 // http://msdn.microsoft.com/en-us/library/aa365247.aspx
51 const char16 kReservedCharacters[] = L"<>:\"/\\|?*\x01\x02\x03\x04\x05\x06\x07" 58 const char16 kReservedCharacters[] = L"<>:\"/\\|?*\x01\x02\x03\x04\x05\x06\x07"
52 L"\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19" 59 L"\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19"
53 L"\x1A\x1B\x1C\x1D\x1E\x1F"; 60 L"\x1A\x1B\x1C\x1D\x1E\x1F";
54 61
55 // The maximum number of characters allowed in profile shortcuts' file names. 62 // The maximum number of characters allowed in profile shortcuts' file names.
56 // Warning: migration code will be needed if this is changed later, since 63 // Warning: migration code will be needed if this is changed later, since
57 // existing shortcuts might no longer be found if the name is generated 64 // existing shortcuts might no longer be found if the name is generated
58 // differently than it was when a shortcut was originally created. 65 // differently than it was when a shortcut was originally created.
59 const int kMaxProfileShortcutFileNameLength = 64; 66 const int kMaxProfileShortcutFileNameLength = 64;
60 67
61 const int kProfileAvatarBadgeSize = 28; 68 const int kProfileAvatarBadgeSize = 28;
62 const int kShortcutIconSize = 48; 69 const int kShortcutIconSize = 48;
63 70
71 const int kCurrentProfileIconVersion = 1;
72
64 // 2x sized profile avatar icons. Mirrors |kDefaultAvatarIconResources| in 73 // 2x sized profile avatar icons. Mirrors |kDefaultAvatarIconResources| in
65 // profile_info_cache.cc. 74 // profile_info_cache.cc.
66 const int kProfileAvatarIconResources2x[] = { 75 const int kProfileAvatarIconResources2x[] = {
67 IDR_PROFILE_AVATAR_2X_0, 76 IDR_PROFILE_AVATAR_2X_0,
68 IDR_PROFILE_AVATAR_2X_1, 77 IDR_PROFILE_AVATAR_2X_1,
69 IDR_PROFILE_AVATAR_2X_2, 78 IDR_PROFILE_AVATAR_2X_2,
70 IDR_PROFILE_AVATAR_2X_3, 79 IDR_PROFILE_AVATAR_2X_3,
71 IDR_PROFILE_AVATAR_2X_4, 80 IDR_PROFILE_AVATAR_2X_4,
72 IDR_PROFILE_AVATAR_2X_5, 81 IDR_PROFILE_AVATAR_2X_5,
73 IDR_PROFILE_AVATAR_2X_6, 82 IDR_PROFILE_AVATAR_2X_6,
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 const SkBitmap& badged_bitmap = 143 const SkBitmap& badged_bitmap =
135 offscreen_canvas->getDevice()->accessBitmap(false); 144 offscreen_canvas->getDevice()->accessBitmap(false);
136 SkBitmap badged_bitmap_copy; 145 SkBitmap badged_bitmap_copy;
137 badged_bitmap.deepCopyTo(&badged_bitmap_copy, badged_bitmap.getConfig()); 146 badged_bitmap.deepCopyTo(&badged_bitmap_copy, badged_bitmap.getConfig());
138 return badged_bitmap_copy; 147 return badged_bitmap_copy;
139 } 148 }
140 149
141 // Creates a desktop shortcut icon file (.ico) on the disk for a given profile, 150 // Creates a desktop shortcut icon file (.ico) on the disk for a given profile,
142 // badging the browser distribution icon with the profile avatar. 151 // badging the browser distribution icon with the profile avatar.
143 // Returns a path to the shortcut icon file on disk, which is empty if this 152 // Returns a path to the shortcut icon file on disk, which is empty if this
144 // fails. Use index 0 when assigning the resulting file as the icon. 153 // fails. Use index 0 when assigning the resulting file as the icon. If both
145 base::FilePath CreateChromeDesktopShortcutIconForProfile( 154 // given bitmaps are empty, an unbadged icon is created.
155 // |callback| will be run on successful icon creation.
156 // Returns the path to the created icon on success and an empty base::FilePath
157 // on failure.
158 // TODO(calamity): Ideally we'd just copy the app icon verbatim from the exe's
159 // resources in the case of an unbadged icon.
160 base::FilePath CreateOrUpdateShortcutIconForProfile(
146 const base::FilePath& profile_path, 161 const base::FilePath& profile_path,
147 const SkBitmap& avatar_bitmap_1x, 162 const SkBitmap& avatar_bitmap_1x,
148 const SkBitmap& avatar_bitmap_2x) { 163 const SkBitmap& avatar_bitmap_2x,
164 const base::Closure& callback) {
149 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 165 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
166
167 if (!base::PathExists(profile_path)) {
168 LOG(ERROR) << "Profile directory " << profile_path.value()
169 << " did not exist when trying to create profile icon";
170 return base::FilePath();
171 }
172
150 scoped_ptr<SkBitmap> app_icon_bitmap(GetAppIconForSize(kShortcutIconSize)); 173 scoped_ptr<SkBitmap> app_icon_bitmap(GetAppIconForSize(kShortcutIconSize));
151 if (!app_icon_bitmap) 174 if (!app_icon_bitmap)
152 return base::FilePath(); 175 return base::FilePath();
153 176
154 gfx::ImageFamily badged_bitmaps; 177 gfx::ImageFamily badged_bitmaps;
155 badged_bitmaps.Add(gfx::Image::CreateFrom1xBitmap( 178 if (!avatar_bitmap_1x.empty()) {
156 BadgeIcon(*app_icon_bitmap, avatar_bitmap_1x, 1)));
157
158 app_icon_bitmap = GetAppIconForSize(IconUtil::kLargeIconSize);
159 if (app_icon_bitmap) {
160 badged_bitmaps.Add(gfx::Image::CreateFrom1xBitmap( 179 badged_bitmaps.Add(gfx::Image::CreateFrom1xBitmap(
161 BadgeIcon(*app_icon_bitmap, avatar_bitmap_2x, 2))); 180 BadgeIcon(*app_icon_bitmap, avatar_bitmap_1x, 1)));
162 } 181 }
163 182
183 scoped_ptr<SkBitmap> large_app_icon_bitmap(
184 GetAppIconForSize(IconUtil::kLargeIconSize));
185 if (large_app_icon_bitmap && !avatar_bitmap_2x.empty()) {
186 badged_bitmaps.Add(gfx::Image::CreateFrom1xBitmap(
187 BadgeIcon(*large_app_icon_bitmap, avatar_bitmap_2x, 2)));
188 }
189
190 // If we have no badged bitmaps, we should just use the default chrome icon.
191 if (badged_bitmaps.empty()) {
192 badged_bitmaps.Add(gfx::Image::CreateFrom1xBitmap(*app_icon_bitmap));
193 if (large_app_icon_bitmap) {
194 badged_bitmaps.Add(
195 gfx::Image::CreateFrom1xBitmap(*large_app_icon_bitmap));
196 }
197 }
164 // Finally, write the .ico file containing this new bitmap. 198 // Finally, write the .ico file containing this new bitmap.
165 const base::FilePath icon_path = 199 const base::FilePath icon_path =
166 profile_path.AppendASCII(profiles::internal::kProfileIconFileName); 200 profiles::internal::GetProfileIconPath(profile_path);
167 if (!IconUtil::CreateIconFileFromImageFamily(badged_bitmaps, icon_path)) 201 const bool had_icon = base::PathExists(icon_path);
202
203 if (!IconUtil::CreateIconFileFromImageFamily(badged_bitmaps, icon_path)) {
204 // This can happen if the profile directory is deleted by another process
205 // between the beginning of the function and here.
gab 2013/07/18 16:51:02 I would change this to something that justifies ha
calamity 2013/07/19 04:23:17 Done.
206 NOTREACHED();
168 return base::FilePath(); 207 return base::FilePath();
208 }
169 209
210 if (had_icon) {
211 // This invalidates the Windows icon cache and causes the icon changes to
212 // register with the taskbar and desktop. SHCNE_ASSOCCHANGED will cause a
213 // desktop flash and we would like to avoid that if possible.
214 SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL);
215 } else {
216 SHChangeNotify(SHCNE_CREATE, SHCNF_PATH, icon_path.value().c_str(), NULL);
217 }
218 if (!callback.is_null())
219 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback);
170 return icon_path; 220 return icon_path;
171 } 221 }
172 222
223 // Updates the preferences with the current icon version on icon creation
224 // success.
225 void OnProfileIconCreateSuccess(base::FilePath profile_path) {
226 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
227 if (!g_browser_process->profile_manager())
228 return;
229 Profile* profile =
230 g_browser_process->profile_manager()->GetProfileByPath(profile_path);
231 if (profile) {
232 profile->GetPrefs()->SetInteger(prefs::kProfileIconVersion,
233 kCurrentProfileIconVersion);
234 }
235 }
236
173 // Gets the user and system directories for desktop shortcuts. Parameters may 237 // Gets the user and system directories for desktop shortcuts. Parameters may
174 // be NULL if a directory type is not needed. Returns true on success. 238 // be NULL if a directory type is not needed. Returns true on success.
175 bool GetDesktopShortcutsDirectories( 239 bool GetDesktopShortcutsDirectories(
176 base::FilePath* user_shortcuts_directory, 240 base::FilePath* user_shortcuts_directory,
177 base::FilePath* system_shortcuts_directory) { 241 base::FilePath* system_shortcuts_directory) {
178 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); 242 BrowserDistribution* distribution = BrowserDistribution::GetDistribution();
179 if (user_shortcuts_directory && 243 if (user_shortcuts_directory &&
180 !ShellUtil::GetShortcutPath(ShellUtil::SHORTCUT_LOCATION_DESKTOP, 244 !ShellUtil::GetShortcutPath(ShellUtil::SHORTCUT_LOCATION_DESKTOP,
181 distribution, ShellUtil::CURRENT_USER, 245 distribution, ShellUtil::CURRENT_USER,
182 user_shortcuts_directory)) { 246 user_shortcuts_directory)) {
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 if (base::PathExists(possible_new_system_shortcut)) 358 if (base::PathExists(possible_new_system_shortcut))
295 base::Delete(old_shortcut_path, false); 359 base::Delete(old_shortcut_path, false);
296 else if (!RenameDesktopShortcut(old_shortcut_path, new_shortcut_path)) 360 else if (!RenameDesktopShortcut(old_shortcut_path, new_shortcut_path))
297 DLOG(ERROR) << "Could not rename Windows profile desktop shortcut."; 361 DLOG(ERROR) << "Could not rename Windows profile desktop shortcut.";
298 } else { 362 } else {
299 // If the shortcut does not exist, it may have been renamed by the user. In 363 // If the shortcut does not exist, it may have been renamed by the user. In
300 // that case, its name should not be changed. 364 // that case, its name should not be changed.
301 // It's also possible that a system-level shortcut exists instead - this 365 // It's also possible that a system-level shortcut exists instead - this
302 // should only be the case for the original Chrome shortcut from an 366 // should only be the case for the original Chrome shortcut from an
303 // installation. If that's the case, copy that one over - it will get its 367 // installation. If that's the case, copy that one over - it will get its
304 // properties updated by |CreateOrUpdateDesktopShortcutsForProfile()|. 368 // properties updated by
369 // |CreateOrUpdateDesktopShortcutsAndIconForProfile()|.
305 const base::FilePath possible_old_system_shortcut = 370 const base::FilePath possible_old_system_shortcut =
306 system_shortcuts_directory.Append(old_shortcut_filename); 371 system_shortcuts_directory.Append(old_shortcut_filename);
307 if (base::PathExists(possible_old_system_shortcut)) 372 if (base::PathExists(possible_old_system_shortcut))
308 base::CopyFile(possible_old_system_shortcut, new_shortcut_path); 373 base::CopyFile(possible_old_system_shortcut, new_shortcut_path);
309 } 374 }
310 } 375 }
311 376
377 struct CreateOrUpdateShortcutsParams {
378 CreateOrUpdateShortcutsParams(
379 base::FilePath profile_path,
380 ProfileShortcutManagerWin::CreateOrUpdateMode create_mode,
381 ProfileShortcutManagerWin::NonProfileShortcutAction action)
382 : profile_path(profile_path), create_mode(create_mode), action(action) {}
383 ~CreateOrUpdateShortcutsParams() {}
384
385 ProfileShortcutManagerWin::CreateOrUpdateMode create_mode;
386 ProfileShortcutManagerWin::NonProfileShortcutAction action;
387
388 // The path for this profile.
389 base::FilePath profile_path;
390 // The profile name before this update. Empty on create.
391 string16 old_profile_name;
392 // The new profile name.
393 string16 profile_name;
394 // Avatar images for this profile.
395 SkBitmap avatar_image_1x;
396 SkBitmap avatar_image_2x;
397 };
398
312 // Updates all desktop shortcuts for the given profile to have the specified 399 // Updates all desktop shortcuts for the given profile to have the specified
313 // parameters. If |create_mode| is CREATE_WHEN_NONE_FOUND, a new shortcut is 400 // parameters. If |params.create_mode| is CREATE_WHEN_NONE_FOUND, a new shortcut
314 // created if no existing ones were found. Whether non-profile shortcuts should 401 // is created if no existing ones were found. Whether non-profile shortcuts
315 // be updated is specified by |action|. Must be called on the FILE thread. 402 // should be updated is specified by |params.action|. Must be called on the FILE
316 void CreateOrUpdateDesktopShortcutsForProfile( 403 // thread. |callback| is called on successful icon creation.
317 const base::FilePath& profile_path, 404 void CreateOrUpdateDesktopShortcutsAndIconForProfile(
318 const string16& old_profile_name, 405 const CreateOrUpdateShortcutsParams& params,
319 const string16& profile_name, 406 const base::Closure& callback) {
320 const SkBitmap& avatar_image_1x,
321 const SkBitmap& avatar_image_2x,
322 ProfileShortcutManagerWin::CreateOrUpdateMode create_mode,
323 ProfileShortcutManagerWin::NonProfileShortcutAction action) {
324 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 407 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
325 408
409 const base::FilePath shortcut_icon =
410 CreateOrUpdateShortcutIconForProfile(params.profile_path,
411 params.avatar_image_1x,
412 params.avatar_image_2x,
413 callback);
414 if (shortcut_icon.empty() ||
415 params.create_mode ==
416 ProfileShortcutManagerWin::CREATE_OR_UPDATE_ICON_ONLY) {
417 return;
418 }
419
326 base::FilePath chrome_exe; 420 base::FilePath chrome_exe;
327 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { 421 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
328 NOTREACHED(); 422 NOTREACHED();
329 return; 423 return;
330 } 424 }
331 425
332 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); 426 BrowserDistribution* distribution = BrowserDistribution::GetDistribution();
333 // Ensure that the distribution supports creating shortcuts. If it doesn't, 427 // Ensure that the distribution supports creating shortcuts. If it doesn't,
334 // the following code may result in NOTREACHED() being hit. 428 // the following code may result in NOTREACHED() being hit.
335 DCHECK(distribution->CanCreateDesktopShortcuts()); 429 DCHECK(distribution->CanCreateDesktopShortcuts());
336 430
337 if (old_profile_name != profile_name) { 431 if (params.old_profile_name != params.profile_name) {
338 const string16 old_shortcut_filename = 432 const string16 old_shortcut_filename =
339 profiles::internal::GetShortcutFilenameForProfile(old_profile_name, 433 profiles::internal::GetShortcutFilenameForProfile(
340 distribution); 434 params.old_profile_name,
435 distribution);
341 const string16 new_shortcut_filename = 436 const string16 new_shortcut_filename =
342 profiles::internal::GetShortcutFilenameForProfile(profile_name, 437 profiles::internal::GetShortcutFilenameForProfile(params.profile_name,
343 distribution); 438 distribution);
344 RenameChromeDesktopShortcutForProfile(old_shortcut_filename, 439 RenameChromeDesktopShortcutForProfile(old_shortcut_filename,
345 new_shortcut_filename); 440 new_shortcut_filename);
346 } 441 }
347 442
348 ShellUtil::ShortcutProperties properties(ShellUtil::CURRENT_USER); 443 ShellUtil::ShortcutProperties properties(ShellUtil::CURRENT_USER);
349 installer::Product product(distribution); 444 installer::Product product(distribution);
350 product.AddDefaultShortcutProperties(chrome_exe, &properties); 445 product.AddDefaultShortcutProperties(chrome_exe, &properties);
351 446
352 const string16 command_line = 447 const string16 command_line =
353 profiles::internal::CreateProfileShortcutFlags(profile_path); 448 profiles::internal::CreateProfileShortcutFlags(params.profile_path);
354 449
355 // Only set the profile-specific properties when |profile_name| is non empty. 450 // Only set the profile-specific properties when |profile_name| is non empty.
356 // If it is empty, it means the shortcut being created should be a regular, 451 // If it is empty, it means the shortcut being created should be a regular,
357 // non-profile Chrome shortcut. 452 // non-profile Chrome shortcut.
358 if (!profile_name.empty()) { 453 if (!params.profile_name.empty()) {
359 const base::FilePath shortcut_icon =
360 CreateChromeDesktopShortcutIconForProfile(profile_path,
361 avatar_image_1x,
362 avatar_image_2x);
363 if (!shortcut_icon.empty())
364 properties.set_icon(shortcut_icon, 0);
365 properties.set_arguments(command_line); 454 properties.set_arguments(command_line);
455 properties.set_icon(shortcut_icon, 0);
366 } else { 456 } else {
367 // Set the arguments explicitly to the empty string to ensure that 457 // Set the arguments explicitly to the empty string to ensure that
368 // |ShellUtil::CreateOrUpdateShortcut| updates that part of the shortcut. 458 // |ShellUtil::CreateOrUpdateShortcut| updates that part of the shortcut.
369 properties.set_arguments(string16()); 459 properties.set_arguments(string16());
370 } 460 }
371 461
372 properties.set_app_id( 462 properties.set_app_id(
373 ShellIntegration::GetChromiumModelIdForProfile(profile_path)); 463 ShellIntegration::GetChromiumModelIdForProfile(params.profile_path));
374 464
375 ShellUtil::ShortcutOperation operation = 465 ShellUtil::ShortcutOperation operation =
376 ShellUtil::SHELL_SHORTCUT_REPLACE_EXISTING; 466 ShellUtil::SHELL_SHORTCUT_REPLACE_EXISTING;
377 467
378 std::vector<base::FilePath> shortcuts; 468 std::vector<base::FilePath> shortcuts;
379 ListDesktopShortcutsWithCommandLine(chrome_exe, command_line, 469 ListDesktopShortcutsWithCommandLine(chrome_exe, command_line,
380 action == ProfileShortcutManagerWin::UPDATE_NON_PROFILE_SHORTCUTS, 470 params.action == ProfileShortcutManagerWin::UPDATE_NON_PROFILE_SHORTCUTS,
381 &shortcuts); 471 &shortcuts);
382 if (create_mode == ProfileShortcutManagerWin::CREATE_WHEN_NONE_FOUND && 472 if (params.create_mode == ProfileShortcutManagerWin::CREATE_WHEN_NONE_FOUND &&
383 shortcuts.empty()) { 473 shortcuts.empty()) {
384 const string16 shortcut_name = 474 const string16 shortcut_name =
385 profiles::internal::GetShortcutFilenameForProfile(profile_name, 475 profiles::internal::GetShortcutFilenameForProfile(params.profile_name,
386 distribution); 476 distribution);
387 shortcuts.push_back(base::FilePath(shortcut_name)); 477 shortcuts.push_back(base::FilePath(shortcut_name));
388 operation = ShellUtil::SHELL_SHORTCUT_CREATE_IF_NO_SYSTEM_LEVEL; 478 operation = ShellUtil::SHELL_SHORTCUT_CREATE_IF_NO_SYSTEM_LEVEL;
389 } 479 }
390 480
391 for (size_t i = 0; i < shortcuts.size(); ++i) { 481 for (size_t i = 0; i < shortcuts.size(); ++i) {
392 const base::FilePath shortcut_name = 482 const base::FilePath shortcut_name =
393 shortcuts[i].BaseName().RemoveExtension(); 483 shortcuts[i].BaseName().RemoveExtension();
394 properties.set_shortcut_name(shortcut_name.value()); 484 properties.set_shortcut_name(shortcut_name.value());
395 ShellUtil::CreateOrUpdateShortcut(ShellUtil::SHORTCUT_LOCATION_DESKTOP, 485 ShellUtil::CreateOrUpdateShortcut(ShellUtil::SHORTCUT_LOCATION_DESKTOP,
(...skipping 12 matching lines...) Expand all
408 base::FileEnumerator::FILES); 498 base::FileEnumerator::FILES);
409 for (base::FilePath path = enumerator.Next(); !path.empty(); 499 for (base::FilePath path = enumerator.Next(); !path.empty();
410 path = enumerator.Next()) { 500 path = enumerator.Next()) {
411 if (IsChromeShortcut(path, chrome_exe, NULL)) 501 if (IsChromeShortcut(path, chrome_exe, NULL))
412 return true; 502 return true;
413 } 503 }
414 504
415 return false; 505 return false;
416 } 506 }
417 507
418 // Deletes all desktop shortcuts for the specified profile and also removes the 508 // Deletes all desktop shortcuts for the specified profile. If
419 // corresponding icon file. If |ensure_shortcuts_remain| is true, then a regular 509 // |ensure_shortcuts_remain| is true, then a regular non-profile shortcut will
420 // non-profile shortcut will be created if this function would otherwise delete 510 // be created if this function would otherwise delete the last Chrome desktop
421 // the last Chrome desktop shortcut(s). Must be called on the FILE thread. 511 // shortcut(s). Must be called on the FILE thread.
422 void DeleteDesktopShortcutsAndIconFile(const base::FilePath& profile_path, 512 void DeleteDesktopShortcuts(const base::FilePath& profile_path,
423 bool ensure_shortcuts_remain) { 513 bool ensure_shortcuts_remain) {
424 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 514 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
425 515
426 base::FilePath chrome_exe; 516 base::FilePath chrome_exe;
427 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { 517 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
428 NOTREACHED(); 518 NOTREACHED();
429 return; 519 return;
430 } 520 }
431 521
432 const string16 command_line = 522 const string16 command_line =
433 profiles::internal::CreateProfileShortcutFlags(profile_path); 523 profiles::internal::CreateProfileShortcutFlags(profile_path);
434 std::vector<base::FilePath> shortcuts; 524 std::vector<base::FilePath> shortcuts;
435 ListDesktopShortcutsWithCommandLine(chrome_exe, command_line, false, 525 ListDesktopShortcutsWithCommandLine(chrome_exe, command_line, false,
436 &shortcuts); 526 &shortcuts);
437 527
438 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); 528 BrowserDistribution* distribution = BrowserDistribution::GetDistribution();
439 for (size_t i = 0; i < shortcuts.size(); ++i) { 529 for (size_t i = 0; i < shortcuts.size(); ++i) {
440 // Use base::Delete() instead of ShellUtil::RemoveShortcut(), as the 530 // Use base::Delete() instead of ShellUtil::RemoveShortcut(), as the
441 // latter causes non-profile taskbar shortcuts to be unpinned. 531 // latter causes non-profile taskbar shortcuts to be unpinned.
442 base::Delete(shortcuts[i], false); 532 base::Delete(shortcuts[i], false);
443 // Notify the shell that the shortcut was deleted to ensure desktop refresh. 533 // Notify the shell that the shortcut was deleted to ensure desktop refresh.
444 SHChangeNotify(SHCNE_DELETE, SHCNF_PATH, shortcuts[i].value().c_str(), 534 SHChangeNotify(SHCNE_DELETE, SHCNF_PATH, shortcuts[i].value().c_str(),
445 NULL); 535 NULL);
446 } 536 }
447 537
448 const base::FilePath icon_path =
449 profile_path.AppendASCII(profiles::internal::kProfileIconFileName);
450 base::Delete(icon_path, false);
451
452 // If |ensure_shortcuts_remain| is true and deleting this profile caused the 538 // If |ensure_shortcuts_remain| is true and deleting this profile caused the
453 // last shortcuts to be removed, re-create a regular non-profile shortcut. 539 // last shortcuts to be removed, re-create a regular non-profile shortcut.
454 const bool had_shortcuts = !shortcuts.empty(); 540 const bool had_shortcuts = !shortcuts.empty();
455 if (ensure_shortcuts_remain && had_shortcuts && 541 if (ensure_shortcuts_remain && had_shortcuts &&
456 !ChromeDesktopShortcutsExist(chrome_exe)) { 542 !ChromeDesktopShortcutsExist(chrome_exe)) {
457 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); 543 BrowserDistribution* distribution = BrowserDistribution::GetDistribution();
458 // Ensure that the distribution supports creating shortcuts. If it doesn't, 544 // Ensure that the distribution supports creating shortcuts. If it doesn't,
459 // the following code may result in NOTREACHED() being hit. 545 // the following code may result in NOTREACHED() being hit.
460 DCHECK(distribution->CanCreateDesktopShortcuts()); 546 DCHECK(distribution->CanCreateDesktopShortcuts());
461 installer::Product product(distribution); 547 installer::Product product(distribution);
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
523 SkBitmap bitmap_copy; 609 SkBitmap bitmap_copy;
524 image_bitmap->deepCopyTo(&bitmap_copy, image_bitmap->getConfig()); 610 image_bitmap->deepCopyTo(&bitmap_copy, image_bitmap->getConfig());
525 return bitmap_copy; 611 return bitmap_copy;
526 } 612 }
527 613
528 } // namespace 614 } // namespace
529 615
530 namespace profiles { 616 namespace profiles {
531 namespace internal { 617 namespace internal {
532 618
533 const char kProfileIconFileName[] = "Google Profile.ico"; 619 base::FilePath GetProfileIconPath(const base::FilePath& profile_path) {
620 return profile_path.AppendASCII(kProfileIconFileName);
621 }
534 622
535 string16 GetShortcutFilenameForProfile(const string16& profile_name, 623 string16 GetShortcutFilenameForProfile(const string16& profile_name,
536 BrowserDistribution* distribution) { 624 BrowserDistribution* distribution) {
537 string16 shortcut_name; 625 string16 shortcut_name;
538 if (!profile_name.empty()) { 626 if (!profile_name.empty()) {
539 shortcut_name.append(SanitizeShortcutProfileNameString(profile_name)); 627 shortcut_name.append(SanitizeShortcutProfileNameString(profile_name));
540 shortcut_name.append(L" - "); 628 shortcut_name.append(L" - ");
541 shortcut_name.append(l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME)); 629 shortcut_name.append(l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME));
542 } else { 630 } else {
543 shortcut_name.append(distribution->GetAppShortCutName()); 631 shortcut_name.append(distribution->GetAppShortCutName());
(...skipping 21 matching lines...) Expand all
565 ProfileManager* manager) { 653 ProfileManager* manager) {
566 return new ProfileShortcutManagerWin(manager); 654 return new ProfileShortcutManagerWin(manager);
567 } 655 }
568 656
569 ProfileShortcutManagerWin::ProfileShortcutManagerWin(ProfileManager* manager) 657 ProfileShortcutManagerWin::ProfileShortcutManagerWin(ProfileManager* manager)
570 : profile_manager_(manager) { 658 : profile_manager_(manager) {
571 DCHECK_EQ( 659 DCHECK_EQ(
572 arraysize(kProfileAvatarIconResources2x), 660 arraysize(kProfileAvatarIconResources2x),
573 profile_manager_->GetProfileInfoCache().GetDefaultAvatarIconCount()); 661 profile_manager_->GetProfileInfoCache().GetDefaultAvatarIconCount());
574 662
663 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CREATED,
664 content::NotificationService::AllSources());
665
575 profile_manager_->GetProfileInfoCache().AddObserver(this); 666 profile_manager_->GetProfileInfoCache().AddObserver(this);
576 } 667 }
577 668
578 ProfileShortcutManagerWin::~ProfileShortcutManagerWin() { 669 ProfileShortcutManagerWin::~ProfileShortcutManagerWin() {
579 profile_manager_->GetProfileInfoCache().RemoveObserver(this); 670 profile_manager_->GetProfileInfoCache().RemoveObserver(this);
580 } 671 }
581 672
673 void ProfileShortcutManagerWin::CreateOrUpdateProfileIcon(
674 const base::FilePath& profile_path,
675 const base::Closure& callback) {
676 CreateOrUpdateShortcutsForProfileAtPath(profile_path,
677 CREATE_OR_UPDATE_ICON_ONLY,
678 IGNORE_NON_PROFILE_SHORTCUTS,
679 callback);
680 }
681
582 void ProfileShortcutManagerWin::CreateProfileShortcut( 682 void ProfileShortcutManagerWin::CreateProfileShortcut(
583 const base::FilePath& profile_path) { 683 const base::FilePath& profile_path) {
584 CreateOrUpdateShortcutsForProfileAtPath(profile_path, CREATE_WHEN_NONE_FOUND, 684 CreateOrUpdateShortcutsForProfileAtPath(profile_path, CREATE_WHEN_NONE_FOUND,
585 IGNORE_NON_PROFILE_SHORTCUTS); 685 IGNORE_NON_PROFILE_SHORTCUTS,
686 base::Closure());
586 } 687 }
587 688
588 void ProfileShortcutManagerWin::RemoveProfileShortcuts( 689 void ProfileShortcutManagerWin::RemoveProfileShortcuts(
589 const base::FilePath& profile_path) { 690 const base::FilePath& profile_path) {
590 BrowserThread::PostTask( 691 BrowserThread::PostTask(
591 BrowserThread::FILE, FROM_HERE, 692 BrowserThread::FILE, FROM_HERE,
592 base::Bind(&DeleteDesktopShortcutsAndIconFile, profile_path, false)); 693 base::Bind(&DeleteDesktopShortcuts, profile_path, false));
593 } 694 }
594 695
595 void ProfileShortcutManagerWin::HasProfileShortcuts( 696 void ProfileShortcutManagerWin::HasProfileShortcuts(
596 const base::FilePath& profile_path, 697 const base::FilePath& profile_path,
597 const base::Callback<void(bool)>& callback) { 698 const base::Callback<void(bool)>& callback) {
598 BrowserThread::PostTaskAndReplyWithResult( 699 BrowserThread::PostTaskAndReplyWithResult(
599 BrowserThread::FILE, FROM_HERE, 700 BrowserThread::FILE, FROM_HERE,
600 base::Bind(&HasAnyProfileShortcuts, profile_path), callback); 701 base::Bind(&HasAnyProfileShortcuts, profile_path), callback);
601 } 702 }
602 703
603 void ProfileShortcutManagerWin::OnProfileAdded( 704 void ProfileShortcutManagerWin::OnProfileAdded(
604 const base::FilePath& profile_path) { 705 const base::FilePath& profile_path) {
706 CreateOrUpdateProfileIcon(profile_path, base::Closure());
605 if (profile_manager_->GetProfileInfoCache().GetNumberOfProfiles() == 2) { 707 if (profile_manager_->GetProfileInfoCache().GetNumberOfProfiles() == 2) {
606 // When the second profile is added, make existing non-profile shortcuts 708 // When the second profile is added, make existing non-profile shortcuts
607 // point to the first profile and be badged/named appropriately. 709 // point to the first profile and be badged/named appropriately.
608 CreateOrUpdateShortcutsForProfileAtPath(GetOtherProfilePath(profile_path), 710 CreateOrUpdateShortcutsForProfileAtPath(GetOtherProfilePath(profile_path),
609 UPDATE_EXISTING_ONLY, 711 UPDATE_EXISTING_ONLY,
610 UPDATE_NON_PROFILE_SHORTCUTS); 712 UPDATE_NON_PROFILE_SHORTCUTS,
713 base::Closure());
611 } 714 }
612 } 715 }
613 716
614 void ProfileShortcutManagerWin::OnProfileWasRemoved( 717 void ProfileShortcutManagerWin::OnProfileWasRemoved(
615 const base::FilePath& profile_path, 718 const base::FilePath& profile_path,
616 const string16& profile_name) { 719 const string16& profile_name) {
617 const ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache(); 720 const ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache();
618 // If there is only one profile remaining, remove the badging information 721 // If there is only one profile remaining, remove the badging information
619 // from an existing shortcut. 722 // from an existing shortcut.
620 const bool deleting_down_to_last_profile = (cache.GetNumberOfProfiles() == 1); 723 const bool deleting_down_to_last_profile = (cache.GetNumberOfProfiles() == 1);
621 if (deleting_down_to_last_profile) { 724 if (deleting_down_to_last_profile) {
725 // This is needed to unbadge the icon.
622 CreateOrUpdateShortcutsForProfileAtPath(cache.GetPathOfProfileAtIndex(0), 726 CreateOrUpdateShortcutsForProfileAtPath(cache.GetPathOfProfileAtIndex(0),
623 UPDATE_EXISTING_ONLY, 727 UPDATE_EXISTING_ONLY,
624 IGNORE_NON_PROFILE_SHORTCUTS); 728 IGNORE_NON_PROFILE_SHORTCUTS,
729 base::Closure());
625 } 730 }
626 731
627 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, 732 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
628 base::Bind(&DeleteDesktopShortcutsAndIconFile, 733 base::Bind(&DeleteDesktopShortcuts,
629 profile_path, 734 profile_path,
630 deleting_down_to_last_profile)); 735 deleting_down_to_last_profile));
631 } 736 }
632 737
633 void ProfileShortcutManagerWin::OnProfileNameChanged( 738 void ProfileShortcutManagerWin::OnProfileNameChanged(
634 const base::FilePath& profile_path, 739 const base::FilePath& profile_path,
635 const string16& old_profile_name) { 740 const string16& old_profile_name) {
636 CreateOrUpdateShortcutsForProfileAtPath(profile_path, UPDATE_EXISTING_ONLY, 741 CreateOrUpdateShortcutsForProfileAtPath(profile_path, UPDATE_EXISTING_ONLY,
637 IGNORE_NON_PROFILE_SHORTCUTS); 742 IGNORE_NON_PROFILE_SHORTCUTS,
743 base::Closure());
638 } 744 }
639 745
640 void ProfileShortcutManagerWin::OnProfileAvatarChanged( 746 void ProfileShortcutManagerWin::OnProfileAvatarChanged(
641 const base::FilePath& profile_path) { 747 const base::FilePath& profile_path) {
642 CreateOrUpdateShortcutsForProfileAtPath(profile_path, UPDATE_EXISTING_ONLY, 748 CreateOrUpdateProfileIcon(profile_path, base::Closure());
643 IGNORE_NON_PROFILE_SHORTCUTS);
644 } 749 }
645 750
646 base::FilePath ProfileShortcutManagerWin::GetOtherProfilePath( 751 base::FilePath ProfileShortcutManagerWin::GetOtherProfilePath(
647 const base::FilePath& profile_path) { 752 const base::FilePath& profile_path) {
648 const ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache(); 753 const ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache();
649 DCHECK_EQ(2U, cache.GetNumberOfProfiles()); 754 DCHECK_EQ(2U, cache.GetNumberOfProfiles());
650 // Get the index of the current profile, in order to find the index of the 755 // Get the index of the current profile, in order to find the index of the
651 // other profile. 756 // other profile.
652 size_t current_profile_index = cache.GetIndexOfProfileWithPath(profile_path); 757 size_t current_profile_index = cache.GetIndexOfProfileWithPath(profile_path);
653 size_t other_profile_index = (current_profile_index == 0) ? 1 : 0; 758 size_t other_profile_index = (current_profile_index == 0) ? 1 : 0;
654 return cache.GetPathOfProfileAtIndex(other_profile_index); 759 return cache.GetPathOfProfileAtIndex(other_profile_index);
655 } 760 }
656 761
657 void ProfileShortcutManagerWin::CreateOrUpdateShortcutsForProfileAtPath( 762 void ProfileShortcutManagerWin::CreateOrUpdateShortcutsForProfileAtPath(
658 const base::FilePath& profile_path, 763 const base::FilePath& profile_path,
659 CreateOrUpdateMode create_mode, 764 CreateOrUpdateMode create_mode,
660 NonProfileShortcutAction action) { 765 NonProfileShortcutAction action,
766 const base::Closure& callback) {
767 DCHECK(!BrowserThread::IsWellKnownThread(BrowserThread::UI) ||
768 BrowserThread::CurrentlyOn(BrowserThread::UI));
769 CreateOrUpdateShortcutsParams params(profile_path, create_mode, action);
770
661 ProfileInfoCache* cache = &profile_manager_->GetProfileInfoCache(); 771 ProfileInfoCache* cache = &profile_manager_->GetProfileInfoCache();
662 size_t profile_index = cache->GetIndexOfProfileWithPath(profile_path); 772 size_t profile_index = cache->GetIndexOfProfileWithPath(profile_path);
663 if (profile_index == std::string::npos) 773 if (profile_index == std::string::npos)
664 return; 774 return;
665 bool remove_badging = cache->GetNumberOfProfiles() == 1; 775 bool remove_badging = cache->GetNumberOfProfiles() == 1;
666 776
667 string16 old_shortcut_appended_name = 777 params.old_profile_name =
668 cache->GetShortcutNameOfProfileAtIndex(profile_index); 778 cache->GetShortcutNameOfProfileAtIndex(profile_index);
669 779
670 // Exit early if the mode is to update existing profile shortcuts only and 780 // Exit early if the mode is to update existing profile shortcuts only and
671 // none were ever created for this profile, per the shortcut name not being 781 // none were ever created for this profile, per the shortcut name not being
672 // set in the profile info cache. 782 // set in the profile info cache.
673 if (old_shortcut_appended_name.empty() && 783 if (params.old_profile_name.empty() &&
674 create_mode == UPDATE_EXISTING_ONLY && 784 create_mode == UPDATE_EXISTING_ONLY &&
675 action == IGNORE_NON_PROFILE_SHORTCUTS) { 785 action == IGNORE_NON_PROFILE_SHORTCUTS) {
676 return; 786 return;
677 } 787 }
678 788
679 string16 new_shortcut_appended_name; 789 if (!remove_badging) {
680 if (!remove_badging) 790 params.profile_name = cache->GetNameOfProfileAtIndex(profile_index);
681 new_shortcut_appended_name = cache->GetNameOfProfileAtIndex(profile_index);
682 791
683 SkBitmap avatar_bitmap_copy_1x;
684 SkBitmap avatar_bitmap_copy_2x;
685 if (!remove_badging) {
686 const size_t icon_index = 792 const size_t icon_index =
687 cache->GetAvatarIconIndexOfProfileAtIndex(profile_index); 793 cache->GetAvatarIconIndexOfProfileAtIndex(profile_index);
688 const int resource_id_1x = 794 const int resource_id_1x =
689 cache->GetDefaultAvatarIconResourceIDAtIndex(icon_index); 795 cache->GetDefaultAvatarIconResourceIDAtIndex(icon_index);
690 const int resource_id_2x = kProfileAvatarIconResources2x[icon_index]; 796 const int resource_id_2x = kProfileAvatarIconResources2x[icon_index];
691 // Make a copy of the SkBitmaps to ensure that we can safely use the image 797 // Make a copy of the SkBitmaps to ensure that we can safely use the image
692 // data on the FILE thread. 798 // data on the FILE thread.
693 avatar_bitmap_copy_1x = GetImageResourceSkBitmapCopy(resource_id_1x); 799 params.avatar_image_1x = GetImageResourceSkBitmapCopy(resource_id_1x);
694 avatar_bitmap_copy_2x = GetImageResourceSkBitmapCopy(resource_id_2x); 800 params.avatar_image_2x = GetImageResourceSkBitmapCopy(resource_id_2x);
695 } 801 }
696 BrowserThread::PostTask( 802 BrowserThread::PostTask(
697 BrowserThread::FILE, FROM_HERE, 803 BrowserThread::FILE, FROM_HERE,
698 base::Bind(&CreateOrUpdateDesktopShortcutsForProfile, profile_path, 804 base::Bind(&CreateOrUpdateDesktopShortcutsAndIconForProfile, params,
699 old_shortcut_appended_name, new_shortcut_appended_name, 805 callback));
700 avatar_bitmap_copy_1x, avatar_bitmap_copy_2x, create_mode,
701 action));
702 806
703 cache->SetShortcutNameOfProfileAtIndex(profile_index, 807 cache->SetShortcutNameOfProfileAtIndex(profile_index,
704 new_shortcut_appended_name); 808 params.profile_name);
705 } 809 }
810
811 void ProfileShortcutManagerWin::Observe(
812 int type,
813 const content::NotificationSource& source,
814 const content::NotificationDetails& details) {
815 switch (type) {
816 // This notification is triggered when a profile is loaded.
817 case chrome::NOTIFICATION_PROFILE_CREATED: {
818 Profile* profile =
819 content::Source<Profile>(source).ptr()->GetOriginalProfile();
820 if (profile->GetPrefs()->GetInteger(prefs::kProfileIconVersion) <
821 kCurrentProfileIconVersion) {
822 // Ensure the profile's icon file has been created.
823 CreateOrUpdateProfileIcon(
824 profile->GetPath(),
825 base::Bind(&OnProfileIconCreateSuccess, profile->GetPath()));
826 }
827 break;
828 }
829 default:
830 NOTREACHED();
831 break;
832 }
833 }
OLDNEW
« no previous file with comments | « chrome/browser/profiles/profile_shortcut_manager_win.h ('k') | chrome/common/pref_names.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698