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

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: rebase 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 in theory if the profile directory is deleted between the
205 // beginning of this function and here; however this is extremely unlikely
206 // and this check will help catch any regression where this call would start
207 // failing constantly.
208 NOTREACHED();
168 return base::FilePath(); 209 return base::FilePath();
210 }
169 211
212 if (had_icon) {
213 // This invalidates the Windows icon cache and causes the icon changes to
214 // register with the taskbar and desktop. SHCNE_ASSOCCHANGED will cause a
215 // desktop flash and we would like to avoid that if possible.
216 SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL);
217 } else {
218 SHChangeNotify(SHCNE_CREATE, SHCNF_PATH, icon_path.value().c_str(), NULL);
219 }
220 if (!callback.is_null())
221 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback);
170 return icon_path; 222 return icon_path;
171 } 223 }
172 224
225 // Updates the preferences with the current icon version on icon creation
226 // success.
227 void OnProfileIconCreateSuccess(base::FilePath profile_path) {
228 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
229 if (!g_browser_process->profile_manager())
230 return;
231 Profile* profile =
232 g_browser_process->profile_manager()->GetProfileByPath(profile_path);
233 if (profile) {
234 profile->GetPrefs()->SetInteger(prefs::kProfileIconVersion,
235 kCurrentProfileIconVersion);
236 }
237 }
238
173 // Gets the user and system directories for desktop shortcuts. Parameters may 239 // 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. 240 // be NULL if a directory type is not needed. Returns true on success.
175 bool GetDesktopShortcutsDirectories( 241 bool GetDesktopShortcutsDirectories(
176 base::FilePath* user_shortcuts_directory, 242 base::FilePath* user_shortcuts_directory,
177 base::FilePath* system_shortcuts_directory) { 243 base::FilePath* system_shortcuts_directory) {
178 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); 244 BrowserDistribution* distribution = BrowserDistribution::GetDistribution();
179 if (user_shortcuts_directory && 245 if (user_shortcuts_directory &&
180 !ShellUtil::GetShortcutPath(ShellUtil::SHORTCUT_LOCATION_DESKTOP, 246 !ShellUtil::GetShortcutPath(ShellUtil::SHORTCUT_LOCATION_DESKTOP,
181 distribution, ShellUtil::CURRENT_USER, 247 distribution, ShellUtil::CURRENT_USER,
182 user_shortcuts_directory)) { 248 user_shortcuts_directory)) {
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 if (base::PathExists(possible_new_system_shortcut)) 360 if (base::PathExists(possible_new_system_shortcut))
295 base::DeleteFile(old_shortcut_path, false); 361 base::DeleteFile(old_shortcut_path, false);
296 else if (!RenameDesktopShortcut(old_shortcut_path, new_shortcut_path)) 362 else if (!RenameDesktopShortcut(old_shortcut_path, new_shortcut_path))
297 DLOG(ERROR) << "Could not rename Windows profile desktop shortcut."; 363 DLOG(ERROR) << "Could not rename Windows profile desktop shortcut.";
298 } else { 364 } else {
299 // If the shortcut does not exist, it may have been renamed by the user. In 365 // If the shortcut does not exist, it may have been renamed by the user. In
300 // that case, its name should not be changed. 366 // that case, its name should not be changed.
301 // It's also possible that a system-level shortcut exists instead - this 367 // 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 368 // 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 369 // installation. If that's the case, copy that one over - it will get its
304 // properties updated by |CreateOrUpdateDesktopShortcutsForProfile()|. 370 // properties updated by
371 // |CreateOrUpdateDesktopShortcutsAndIconForProfile()|.
305 const base::FilePath possible_old_system_shortcut = 372 const base::FilePath possible_old_system_shortcut =
306 system_shortcuts_directory.Append(old_shortcut_filename); 373 system_shortcuts_directory.Append(old_shortcut_filename);
307 if (base::PathExists(possible_old_system_shortcut)) 374 if (base::PathExists(possible_old_system_shortcut))
308 base::CopyFile(possible_old_system_shortcut, new_shortcut_path); 375 base::CopyFile(possible_old_system_shortcut, new_shortcut_path);
309 } 376 }
310 } 377 }
311 378
379 struct CreateOrUpdateShortcutsParams {
380 CreateOrUpdateShortcutsParams(
381 base::FilePath profile_path,
382 ProfileShortcutManagerWin::CreateOrUpdateMode create_mode,
383 ProfileShortcutManagerWin::NonProfileShortcutAction action)
384 : profile_path(profile_path), create_mode(create_mode), action(action) {}
385 ~CreateOrUpdateShortcutsParams() {}
386
387 ProfileShortcutManagerWin::CreateOrUpdateMode create_mode;
388 ProfileShortcutManagerWin::NonProfileShortcutAction action;
389
390 // The path for this profile.
391 base::FilePath profile_path;
392 // The profile name before this update. Empty on create.
393 string16 old_profile_name;
394 // The new profile name.
395 string16 profile_name;
396 // Avatar images for this profile.
397 SkBitmap avatar_image_1x;
398 SkBitmap avatar_image_2x;
399 };
400
312 // Updates all desktop shortcuts for the given profile to have the specified 401 // 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 402 // 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 403 // 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. 404 // should be updated is specified by |params.action|. Must be called on the FILE
316 void CreateOrUpdateDesktopShortcutsForProfile( 405 // thread. |callback| is called on successful icon creation.
317 const base::FilePath& profile_path, 406 void CreateOrUpdateDesktopShortcutsAndIconForProfile(
318 const string16& old_profile_name, 407 const CreateOrUpdateShortcutsParams& params,
319 const string16& profile_name, 408 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)); 409 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
325 410
411 const base::FilePath shortcut_icon =
412 CreateOrUpdateShortcutIconForProfile(params.profile_path,
413 params.avatar_image_1x,
414 params.avatar_image_2x,
415 callback);
416 if (shortcut_icon.empty() ||
417 params.create_mode ==
418 ProfileShortcutManagerWin::CREATE_OR_UPDATE_ICON_ONLY) {
419 return;
420 }
421
326 base::FilePath chrome_exe; 422 base::FilePath chrome_exe;
327 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { 423 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
328 NOTREACHED(); 424 NOTREACHED();
329 return; 425 return;
330 } 426 }
331 427
332 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); 428 BrowserDistribution* distribution = BrowserDistribution::GetDistribution();
333 // Ensure that the distribution supports creating shortcuts. If it doesn't, 429 // Ensure that the distribution supports creating shortcuts. If it doesn't,
334 // the following code may result in NOTREACHED() being hit. 430 // the following code may result in NOTREACHED() being hit.
335 DCHECK(distribution->CanCreateDesktopShortcuts()); 431 DCHECK(distribution->CanCreateDesktopShortcuts());
336 432
337 if (old_profile_name != profile_name) { 433 if (params.old_profile_name != params.profile_name) {
338 const string16 old_shortcut_filename = 434 const string16 old_shortcut_filename =
339 profiles::internal::GetShortcutFilenameForProfile(old_profile_name, 435 profiles::internal::GetShortcutFilenameForProfile(
340 distribution); 436 params.old_profile_name,
437 distribution);
341 const string16 new_shortcut_filename = 438 const string16 new_shortcut_filename =
342 profiles::internal::GetShortcutFilenameForProfile(profile_name, 439 profiles::internal::GetShortcutFilenameForProfile(params.profile_name,
343 distribution); 440 distribution);
344 RenameChromeDesktopShortcutForProfile(old_shortcut_filename, 441 RenameChromeDesktopShortcutForProfile(old_shortcut_filename,
345 new_shortcut_filename); 442 new_shortcut_filename);
346 } 443 }
347 444
348 ShellUtil::ShortcutProperties properties(ShellUtil::CURRENT_USER); 445 ShellUtil::ShortcutProperties properties(ShellUtil::CURRENT_USER);
349 installer::Product product(distribution); 446 installer::Product product(distribution);
350 product.AddDefaultShortcutProperties(chrome_exe, &properties); 447 product.AddDefaultShortcutProperties(chrome_exe, &properties);
351 448
352 const string16 command_line = 449 const string16 command_line =
353 profiles::internal::CreateProfileShortcutFlags(profile_path); 450 profiles::internal::CreateProfileShortcutFlags(params.profile_path);
354 451
355 // Only set the profile-specific properties when |profile_name| is non empty. 452 // 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, 453 // If it is empty, it means the shortcut being created should be a regular,
357 // non-profile Chrome shortcut. 454 // non-profile Chrome shortcut.
358 if (!profile_name.empty()) { 455 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); 456 properties.set_arguments(command_line);
457 properties.set_icon(shortcut_icon, 0);
366 } else { 458 } else {
367 // Set the arguments explicitly to the empty string to ensure that 459 // Set the arguments explicitly to the empty string to ensure that
368 // |ShellUtil::CreateOrUpdateShortcut| updates that part of the shortcut. 460 // |ShellUtil::CreateOrUpdateShortcut| updates that part of the shortcut.
369 properties.set_arguments(string16()); 461 properties.set_arguments(string16());
370 } 462 }
371 463
372 properties.set_app_id( 464 properties.set_app_id(
373 ShellIntegration::GetChromiumModelIdForProfile(profile_path)); 465 ShellIntegration::GetChromiumModelIdForProfile(params.profile_path));
374 466
375 ShellUtil::ShortcutOperation operation = 467 ShellUtil::ShortcutOperation operation =
376 ShellUtil::SHELL_SHORTCUT_REPLACE_EXISTING; 468 ShellUtil::SHELL_SHORTCUT_REPLACE_EXISTING;
377 469
378 std::vector<base::FilePath> shortcuts; 470 std::vector<base::FilePath> shortcuts;
379 ListDesktopShortcutsWithCommandLine(chrome_exe, command_line, 471 ListDesktopShortcutsWithCommandLine(chrome_exe, command_line,
380 action == ProfileShortcutManagerWin::UPDATE_NON_PROFILE_SHORTCUTS, 472 params.action == ProfileShortcutManagerWin::UPDATE_NON_PROFILE_SHORTCUTS,
381 &shortcuts); 473 &shortcuts);
382 if (create_mode == ProfileShortcutManagerWin::CREATE_WHEN_NONE_FOUND && 474 if (params.create_mode == ProfileShortcutManagerWin::CREATE_WHEN_NONE_FOUND &&
383 shortcuts.empty()) { 475 shortcuts.empty()) {
384 const string16 shortcut_name = 476 const string16 shortcut_name =
385 profiles::internal::GetShortcutFilenameForProfile(profile_name, 477 profiles::internal::GetShortcutFilenameForProfile(params.profile_name,
386 distribution); 478 distribution);
387 shortcuts.push_back(base::FilePath(shortcut_name)); 479 shortcuts.push_back(base::FilePath(shortcut_name));
388 operation = ShellUtil::SHELL_SHORTCUT_CREATE_IF_NO_SYSTEM_LEVEL; 480 operation = ShellUtil::SHELL_SHORTCUT_CREATE_IF_NO_SYSTEM_LEVEL;
389 } 481 }
390 482
391 for (size_t i = 0; i < shortcuts.size(); ++i) { 483 for (size_t i = 0; i < shortcuts.size(); ++i) {
392 const base::FilePath shortcut_name = 484 const base::FilePath shortcut_name =
393 shortcuts[i].BaseName().RemoveExtension(); 485 shortcuts[i].BaseName().RemoveExtension();
394 properties.set_shortcut_name(shortcut_name.value()); 486 properties.set_shortcut_name(shortcut_name.value());
395 ShellUtil::CreateOrUpdateShortcut(ShellUtil::SHORTCUT_LOCATION_DESKTOP, 487 ShellUtil::CreateOrUpdateShortcut(ShellUtil::SHORTCUT_LOCATION_DESKTOP,
(...skipping 12 matching lines...) Expand all
408 base::FileEnumerator::FILES); 500 base::FileEnumerator::FILES);
409 for (base::FilePath path = enumerator.Next(); !path.empty(); 501 for (base::FilePath path = enumerator.Next(); !path.empty();
410 path = enumerator.Next()) { 502 path = enumerator.Next()) {
411 if (IsChromeShortcut(path, chrome_exe, NULL)) 503 if (IsChromeShortcut(path, chrome_exe, NULL))
412 return true; 504 return true;
413 } 505 }
414 506
415 return false; 507 return false;
416 } 508 }
417 509
418 // Deletes all desktop shortcuts for the specified profile and also removes the 510 // Deletes all desktop shortcuts for the specified profile. If
419 // corresponding icon file. If |ensure_shortcuts_remain| is true, then a regular 511 // |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 512 // 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. 513 // shortcut(s). Must be called on the FILE thread.
422 void DeleteDesktopShortcutsAndIconFile(const base::FilePath& profile_path, 514 void DeleteDesktopShortcuts(const base::FilePath& profile_path,
423 bool ensure_shortcuts_remain) { 515 bool ensure_shortcuts_remain) {
424 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 516 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
425 517
426 base::FilePath chrome_exe; 518 base::FilePath chrome_exe;
427 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { 519 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
428 NOTREACHED(); 520 NOTREACHED();
429 return; 521 return;
430 } 522 }
431 523
432 const string16 command_line = 524 const string16 command_line =
433 profiles::internal::CreateProfileShortcutFlags(profile_path); 525 profiles::internal::CreateProfileShortcutFlags(profile_path);
434 std::vector<base::FilePath> shortcuts; 526 std::vector<base::FilePath> shortcuts;
435 ListDesktopShortcutsWithCommandLine(chrome_exe, command_line, false, 527 ListDesktopShortcutsWithCommandLine(chrome_exe, command_line, false,
436 &shortcuts); 528 &shortcuts);
437 529
438 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); 530 BrowserDistribution* distribution = BrowserDistribution::GetDistribution();
439 for (size_t i = 0; i < shortcuts.size(); ++i) { 531 for (size_t i = 0; i < shortcuts.size(); ++i) {
440 // Use base::DeleteFile() instead of ShellUtil::RemoveShortcut(), as the 532 // Use base::DeleteFile() instead of ShellUtil::RemoveShortcut(), as the
441 // latter causes non-profile taskbar shortcuts to be unpinned. 533 // latter causes non-profile taskbar shortcuts to be unpinned.
442 base::DeleteFile(shortcuts[i], false); 534 base::DeleteFile(shortcuts[i], false);
443 // Notify the shell that the shortcut was deleted to ensure desktop refresh. 535 // Notify the shell that the shortcut was deleted to ensure desktop refresh.
444 SHChangeNotify(SHCNE_DELETE, SHCNF_PATH, shortcuts[i].value().c_str(), 536 SHChangeNotify(SHCNE_DELETE, SHCNF_PATH, shortcuts[i].value().c_str(),
445 NULL); 537 NULL);
446 } 538 }
447 539
448 const base::FilePath icon_path =
449 profile_path.AppendASCII(profiles::internal::kProfileIconFileName);
450 base::DeleteFile(icon_path, false);
451
452 // If |ensure_shortcuts_remain| is true and deleting this profile caused the 540 // 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. 541 // last shortcuts to be removed, re-create a regular non-profile shortcut.
454 const bool had_shortcuts = !shortcuts.empty(); 542 const bool had_shortcuts = !shortcuts.empty();
455 if (ensure_shortcuts_remain && had_shortcuts && 543 if (ensure_shortcuts_remain && had_shortcuts &&
456 !ChromeDesktopShortcutsExist(chrome_exe)) { 544 !ChromeDesktopShortcutsExist(chrome_exe)) {
457 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); 545 BrowserDistribution* distribution = BrowserDistribution::GetDistribution();
458 // Ensure that the distribution supports creating shortcuts. If it doesn't, 546 // Ensure that the distribution supports creating shortcuts. If it doesn't,
459 // the following code may result in NOTREACHED() being hit. 547 // the following code may result in NOTREACHED() being hit.
460 DCHECK(distribution->CanCreateDesktopShortcuts()); 548 DCHECK(distribution->CanCreateDesktopShortcuts());
461 installer::Product product(distribution); 549 installer::Product product(distribution);
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
523 SkBitmap bitmap_copy; 611 SkBitmap bitmap_copy;
524 image_bitmap->deepCopyTo(&bitmap_copy, image_bitmap->getConfig()); 612 image_bitmap->deepCopyTo(&bitmap_copy, image_bitmap->getConfig());
525 return bitmap_copy; 613 return bitmap_copy;
526 } 614 }
527 615
528 } // namespace 616 } // namespace
529 617
530 namespace profiles { 618 namespace profiles {
531 namespace internal { 619 namespace internal {
532 620
533 const char kProfileIconFileName[] = "Google Profile.ico"; 621 base::FilePath GetProfileIconPath(const base::FilePath& profile_path) {
622 return profile_path.AppendASCII(kProfileIconFileName);
623 }
534 624
535 string16 GetShortcutFilenameForProfile(const string16& profile_name, 625 string16 GetShortcutFilenameForProfile(const string16& profile_name,
536 BrowserDistribution* distribution) { 626 BrowserDistribution* distribution) {
537 string16 shortcut_name; 627 string16 shortcut_name;
538 if (!profile_name.empty()) { 628 if (!profile_name.empty()) {
539 shortcut_name.append(SanitizeShortcutProfileNameString(profile_name)); 629 shortcut_name.append(SanitizeShortcutProfileNameString(profile_name));
540 shortcut_name.append(L" - "); 630 shortcut_name.append(L" - ");
541 shortcut_name.append(l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME)); 631 shortcut_name.append(l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME));
542 } else { 632 } else {
543 shortcut_name.append(distribution->GetAppShortCutName()); 633 shortcut_name.append(distribution->GetAppShortCutName());
(...skipping 21 matching lines...) Expand all
565 ProfileManager* manager) { 655 ProfileManager* manager) {
566 return new ProfileShortcutManagerWin(manager); 656 return new ProfileShortcutManagerWin(manager);
567 } 657 }
568 658
569 ProfileShortcutManagerWin::ProfileShortcutManagerWin(ProfileManager* manager) 659 ProfileShortcutManagerWin::ProfileShortcutManagerWin(ProfileManager* manager)
570 : profile_manager_(manager) { 660 : profile_manager_(manager) {
571 DCHECK_EQ( 661 DCHECK_EQ(
572 arraysize(kProfileAvatarIconResources2x), 662 arraysize(kProfileAvatarIconResources2x),
573 profile_manager_->GetProfileInfoCache().GetDefaultAvatarIconCount()); 663 profile_manager_->GetProfileInfoCache().GetDefaultAvatarIconCount());
574 664
665 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CREATED,
666 content::NotificationService::AllSources());
667
575 profile_manager_->GetProfileInfoCache().AddObserver(this); 668 profile_manager_->GetProfileInfoCache().AddObserver(this);
576 } 669 }
577 670
578 ProfileShortcutManagerWin::~ProfileShortcutManagerWin() { 671 ProfileShortcutManagerWin::~ProfileShortcutManagerWin() {
579 profile_manager_->GetProfileInfoCache().RemoveObserver(this); 672 profile_manager_->GetProfileInfoCache().RemoveObserver(this);
580 } 673 }
581 674
675 void ProfileShortcutManagerWin::CreateOrUpdateProfileIcon(
676 const base::FilePath& profile_path,
677 const base::Closure& callback) {
678 CreateOrUpdateShortcutsForProfileAtPath(profile_path,
679 CREATE_OR_UPDATE_ICON_ONLY,
680 IGNORE_NON_PROFILE_SHORTCUTS,
681 callback);
682 }
683
582 void ProfileShortcutManagerWin::CreateProfileShortcut( 684 void ProfileShortcutManagerWin::CreateProfileShortcut(
583 const base::FilePath& profile_path) { 685 const base::FilePath& profile_path) {
584 CreateOrUpdateShortcutsForProfileAtPath(profile_path, CREATE_WHEN_NONE_FOUND, 686 CreateOrUpdateShortcutsForProfileAtPath(profile_path, CREATE_WHEN_NONE_FOUND,
585 IGNORE_NON_PROFILE_SHORTCUTS); 687 IGNORE_NON_PROFILE_SHORTCUTS,
688 base::Closure());
586 } 689 }
587 690
588 void ProfileShortcutManagerWin::RemoveProfileShortcuts( 691 void ProfileShortcutManagerWin::RemoveProfileShortcuts(
589 const base::FilePath& profile_path) { 692 const base::FilePath& profile_path) {
590 BrowserThread::PostTask( 693 BrowserThread::PostTask(
591 BrowserThread::FILE, FROM_HERE, 694 BrowserThread::FILE, FROM_HERE,
592 base::Bind(&DeleteDesktopShortcutsAndIconFile, profile_path, false)); 695 base::Bind(&DeleteDesktopShortcuts, profile_path, false));
593 } 696 }
594 697
595 void ProfileShortcutManagerWin::HasProfileShortcuts( 698 void ProfileShortcutManagerWin::HasProfileShortcuts(
596 const base::FilePath& profile_path, 699 const base::FilePath& profile_path,
597 const base::Callback<void(bool)>& callback) { 700 const base::Callback<void(bool)>& callback) {
598 BrowserThread::PostTaskAndReplyWithResult( 701 BrowserThread::PostTaskAndReplyWithResult(
599 BrowserThread::FILE, FROM_HERE, 702 BrowserThread::FILE, FROM_HERE,
600 base::Bind(&HasAnyProfileShortcuts, profile_path), callback); 703 base::Bind(&HasAnyProfileShortcuts, profile_path), callback);
601 } 704 }
602 705
603 void ProfileShortcutManagerWin::OnProfileAdded( 706 void ProfileShortcutManagerWin::OnProfileAdded(
604 const base::FilePath& profile_path) { 707 const base::FilePath& profile_path) {
708 CreateOrUpdateProfileIcon(profile_path, base::Closure());
605 if (profile_manager_->GetProfileInfoCache().GetNumberOfProfiles() == 2) { 709 if (profile_manager_->GetProfileInfoCache().GetNumberOfProfiles() == 2) {
606 // When the second profile is added, make existing non-profile shortcuts 710 // When the second profile is added, make existing non-profile shortcuts
607 // point to the first profile and be badged/named appropriately. 711 // point to the first profile and be badged/named appropriately.
608 CreateOrUpdateShortcutsForProfileAtPath(GetOtherProfilePath(profile_path), 712 CreateOrUpdateShortcutsForProfileAtPath(GetOtherProfilePath(profile_path),
609 UPDATE_EXISTING_ONLY, 713 UPDATE_EXISTING_ONLY,
610 UPDATE_NON_PROFILE_SHORTCUTS); 714 UPDATE_NON_PROFILE_SHORTCUTS,
715 base::Closure());
611 } 716 }
612 } 717 }
613 718
614 void ProfileShortcutManagerWin::OnProfileWasRemoved( 719 void ProfileShortcutManagerWin::OnProfileWasRemoved(
615 const base::FilePath& profile_path, 720 const base::FilePath& profile_path,
616 const string16& profile_name) { 721 const string16& profile_name) {
617 const ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache(); 722 const ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache();
618 // If there is only one profile remaining, remove the badging information 723 // If there is only one profile remaining, remove the badging information
619 // from an existing shortcut. 724 // from an existing shortcut.
620 const bool deleting_down_to_last_profile = (cache.GetNumberOfProfiles() == 1); 725 const bool deleting_down_to_last_profile = (cache.GetNumberOfProfiles() == 1);
621 if (deleting_down_to_last_profile) { 726 if (deleting_down_to_last_profile) {
727 // This is needed to unbadge the icon.
622 CreateOrUpdateShortcutsForProfileAtPath(cache.GetPathOfProfileAtIndex(0), 728 CreateOrUpdateShortcutsForProfileAtPath(cache.GetPathOfProfileAtIndex(0),
623 UPDATE_EXISTING_ONLY, 729 UPDATE_EXISTING_ONLY,
624 IGNORE_NON_PROFILE_SHORTCUTS); 730 IGNORE_NON_PROFILE_SHORTCUTS,
731 base::Closure());
625 } 732 }
626 733
627 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, 734 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
628 base::Bind(&DeleteDesktopShortcutsAndIconFile, 735 base::Bind(&DeleteDesktopShortcuts,
629 profile_path, 736 profile_path,
630 deleting_down_to_last_profile)); 737 deleting_down_to_last_profile));
631 } 738 }
632 739
633 void ProfileShortcutManagerWin::OnProfileNameChanged( 740 void ProfileShortcutManagerWin::OnProfileNameChanged(
634 const base::FilePath& profile_path, 741 const base::FilePath& profile_path,
635 const string16& old_profile_name) { 742 const string16& old_profile_name) {
636 CreateOrUpdateShortcutsForProfileAtPath(profile_path, UPDATE_EXISTING_ONLY, 743 CreateOrUpdateShortcutsForProfileAtPath(profile_path, UPDATE_EXISTING_ONLY,
637 IGNORE_NON_PROFILE_SHORTCUTS); 744 IGNORE_NON_PROFILE_SHORTCUTS,
745 base::Closure());
638 } 746 }
639 747
640 void ProfileShortcutManagerWin::OnProfileAvatarChanged( 748 void ProfileShortcutManagerWin::OnProfileAvatarChanged(
641 const base::FilePath& profile_path) { 749 const base::FilePath& profile_path) {
642 CreateOrUpdateShortcutsForProfileAtPath(profile_path, UPDATE_EXISTING_ONLY, 750 CreateOrUpdateProfileIcon(profile_path, base::Closure());
643 IGNORE_NON_PROFILE_SHORTCUTS);
644 } 751 }
645 752
646 base::FilePath ProfileShortcutManagerWin::GetOtherProfilePath( 753 base::FilePath ProfileShortcutManagerWin::GetOtherProfilePath(
647 const base::FilePath& profile_path) { 754 const base::FilePath& profile_path) {
648 const ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache(); 755 const ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache();
649 DCHECK_EQ(2U, cache.GetNumberOfProfiles()); 756 DCHECK_EQ(2U, cache.GetNumberOfProfiles());
650 // Get the index of the current profile, in order to find the index of the 757 // Get the index of the current profile, in order to find the index of the
651 // other profile. 758 // other profile.
652 size_t current_profile_index = cache.GetIndexOfProfileWithPath(profile_path); 759 size_t current_profile_index = cache.GetIndexOfProfileWithPath(profile_path);
653 size_t other_profile_index = (current_profile_index == 0) ? 1 : 0; 760 size_t other_profile_index = (current_profile_index == 0) ? 1 : 0;
654 return cache.GetPathOfProfileAtIndex(other_profile_index); 761 return cache.GetPathOfProfileAtIndex(other_profile_index);
655 } 762 }
656 763
657 void ProfileShortcutManagerWin::CreateOrUpdateShortcutsForProfileAtPath( 764 void ProfileShortcutManagerWin::CreateOrUpdateShortcutsForProfileAtPath(
658 const base::FilePath& profile_path, 765 const base::FilePath& profile_path,
659 CreateOrUpdateMode create_mode, 766 CreateOrUpdateMode create_mode,
660 NonProfileShortcutAction action) { 767 NonProfileShortcutAction action,
768 const base::Closure& callback) {
769 DCHECK(!BrowserThread::IsWellKnownThread(BrowserThread::UI) ||
770 BrowserThread::CurrentlyOn(BrowserThread::UI));
771 CreateOrUpdateShortcutsParams params(profile_path, create_mode, action);
772
661 ProfileInfoCache* cache = &profile_manager_->GetProfileInfoCache(); 773 ProfileInfoCache* cache = &profile_manager_->GetProfileInfoCache();
662 size_t profile_index = cache->GetIndexOfProfileWithPath(profile_path); 774 size_t profile_index = cache->GetIndexOfProfileWithPath(profile_path);
663 if (profile_index == std::string::npos) 775 if (profile_index == std::string::npos)
664 return; 776 return;
665 bool remove_badging = cache->GetNumberOfProfiles() == 1; 777 bool remove_badging = cache->GetNumberOfProfiles() == 1;
666 778
667 string16 old_shortcut_appended_name = 779 params.old_profile_name =
668 cache->GetShortcutNameOfProfileAtIndex(profile_index); 780 cache->GetShortcutNameOfProfileAtIndex(profile_index);
669 781
670 // Exit early if the mode is to update existing profile shortcuts only and 782 // 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 783 // none were ever created for this profile, per the shortcut name not being
672 // set in the profile info cache. 784 // set in the profile info cache.
673 if (old_shortcut_appended_name.empty() && 785 if (params.old_profile_name.empty() &&
674 create_mode == UPDATE_EXISTING_ONLY && 786 create_mode == UPDATE_EXISTING_ONLY &&
675 action == IGNORE_NON_PROFILE_SHORTCUTS) { 787 action == IGNORE_NON_PROFILE_SHORTCUTS) {
676 return; 788 return;
677 } 789 }
678 790
679 string16 new_shortcut_appended_name; 791 if (!remove_badging) {
680 if (!remove_badging) 792 params.profile_name = cache->GetNameOfProfileAtIndex(profile_index);
681 new_shortcut_appended_name = cache->GetNameOfProfileAtIndex(profile_index);
682 793
683 SkBitmap avatar_bitmap_copy_1x;
684 SkBitmap avatar_bitmap_copy_2x;
685 if (!remove_badging) {
686 const size_t icon_index = 794 const size_t icon_index =
687 cache->GetAvatarIconIndexOfProfileAtIndex(profile_index); 795 cache->GetAvatarIconIndexOfProfileAtIndex(profile_index);
688 const int resource_id_1x = 796 const int resource_id_1x =
689 cache->GetDefaultAvatarIconResourceIDAtIndex(icon_index); 797 cache->GetDefaultAvatarIconResourceIDAtIndex(icon_index);
690 const int resource_id_2x = kProfileAvatarIconResources2x[icon_index]; 798 const int resource_id_2x = kProfileAvatarIconResources2x[icon_index];
691 // Make a copy of the SkBitmaps to ensure that we can safely use the image 799 // Make a copy of the SkBitmaps to ensure that we can safely use the image
692 // data on the FILE thread. 800 // data on the FILE thread.
693 avatar_bitmap_copy_1x = GetImageResourceSkBitmapCopy(resource_id_1x); 801 params.avatar_image_1x = GetImageResourceSkBitmapCopy(resource_id_1x);
694 avatar_bitmap_copy_2x = GetImageResourceSkBitmapCopy(resource_id_2x); 802 params.avatar_image_2x = GetImageResourceSkBitmapCopy(resource_id_2x);
695 } 803 }
696 BrowserThread::PostTask( 804 BrowserThread::PostTask(
697 BrowserThread::FILE, FROM_HERE, 805 BrowserThread::FILE, FROM_HERE,
698 base::Bind(&CreateOrUpdateDesktopShortcutsForProfile, profile_path, 806 base::Bind(&CreateOrUpdateDesktopShortcutsAndIconForProfile, params,
699 old_shortcut_appended_name, new_shortcut_appended_name, 807 callback));
700 avatar_bitmap_copy_1x, avatar_bitmap_copy_2x, create_mode,
701 action));
702 808
703 cache->SetShortcutNameOfProfileAtIndex(profile_index, 809 cache->SetShortcutNameOfProfileAtIndex(profile_index,
704 new_shortcut_appended_name); 810 params.profile_name);
705 } 811 }
812
813 void ProfileShortcutManagerWin::Observe(
814 int type,
815 const content::NotificationSource& source,
816 const content::NotificationDetails& details) {
817 switch (type) {
818 // This notification is triggered when a profile is loaded.
819 case chrome::NOTIFICATION_PROFILE_CREATED: {
820 Profile* profile =
821 content::Source<Profile>(source).ptr()->GetOriginalProfile();
822 if (profile->GetPrefs()->GetInteger(prefs::kProfileIconVersion) <
823 kCurrentProfileIconVersion) {
824 // Ensure the profile's icon file has been created.
825 CreateOrUpdateProfileIcon(
826 profile->GetPath(),
827 base::Bind(&OnProfileIconCreateSuccess, profile->GetPath()));
828 }
829 break;
830 }
831 default:
832 NOTREACHED();
833 break;
834 }
835 }
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