Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/profiles/profile_shortcut_manager_win.h" | 5 #include "chrome/browser/profiles/profile_shortcut_manager_win.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 178 if ((shortcut_command_line.empty() && include_empty_command_lines) || | 178 if ((shortcut_command_line.empty() && include_empty_command_lines) || |
| 179 (shortcut_command_line.find(command_line) != string16::npos)) { | 179 (shortcut_command_line.find(command_line) != string16::npos)) { |
| 180 paths->push_back(path); | 180 paths->push_back(path); |
| 181 } | 181 } |
| 182 } | 182 } |
| 183 } | 183 } |
| 184 | 184 |
| 185 // Renames an existing Chrome desktop profile shortcut. Must be called on the | 185 // Renames an existing Chrome desktop profile shortcut. Must be called on the |
| 186 // FILE thread. | 186 // FILE thread. |
| 187 void RenameChromeDesktopShortcutForProfile( | 187 void RenameChromeDesktopShortcutForProfile( |
| 188 const string16& old_shortcut_file, | 188 const string16& old_shortcut_filename, |
| 189 const string16& new_shortcut_file) { | 189 const string16& new_shortcut_filename) { |
| 190 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 190 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 191 | 191 |
| 192 FilePath shortcuts_directory; | 192 FilePath shortcuts_directory; |
|
gab
2013/01/15 23:09:14
Rename this to user_shortcuts_directory since you
Alexei Svitkine (slow)
2013/01/16 16:11:44
Done.
| |
| 193 if (!GetDesktopShortcutsDirectory(&shortcuts_directory)) | 193 if (!GetDesktopShortcutsDirectory(&shortcuts_directory)) |
|
gab
2013/01/15 23:09:14
I don't really see the value of this method, an if
Alexei Svitkine (slow)
2013/01/15 23:34:43
The method is called from other functions too, to
Alexei Svitkine (slow)
2013/01/16 16:11:44
I've now done this in the latest patchset.
gab
2013/01/16 17:44:00
sgtm.
| |
| 194 return; | 194 return; |
| 195 | 195 |
| 196 FilePath old_shortcut_path = shortcuts_directory.Append(old_shortcut_file); | 196 FilePath system_shortcuts_directory; |
| 197 if (!ShellUtil::GetShortcutPath(ShellUtil::SHORTCUT_LOCATION_DESKTOP, | |
| 198 BrowserDistribution::GetDistribution(), | |
| 199 ShellUtil::SYSTEM_LEVEL, | |
| 200 &system_shortcuts_directory)) { | |
| 201 NOTREACHED(); | |
| 202 return; | |
| 203 } | |
| 204 | |
| 205 const FilePath old_shortcut_path = | |
| 206 shortcuts_directory.Append(old_shortcut_filename); | |
| 207 const FilePath new_shortcut_path = | |
| 208 shortcuts_directory.Append(new_shortcut_filename); | |
| 209 | |
| 197 // If the shortcut does not exist, it may have been renamed by the user. In | 210 // If the shortcut does not exist, it may have been renamed by the user. In |
| 198 // that case, its name should not be changed. | 211 // that case, its name should not be changed. |
| 199 if (!file_util::PathExists(old_shortcut_path)) | 212 if (!file_util::PathExists(old_shortcut_path)) { |
| 213 // It's possible that a system level shortcut exists instead, for example | |
| 214 // the original Chrome shortcut from an installation. If that's the case, | |
| 215 // copy that one over - it will get its properties updated by the code in | |
| 216 // |CreateOrUpdateDesktopShortcutsForProfile()|. | |
| 217 const FilePath possible_old_system_shortcut = | |
| 218 system_shortcuts_directory.Append(old_shortcut_filename); | |
| 219 if (file_util::PathExists(possible_old_system_shortcut)) | |
| 220 file_util::CopyFile(possible_old_system_shortcut, new_shortcut_path); | |
| 200 return; | 221 return; |
| 222 } | |
| 201 | 223 |
| 202 FilePath new_shortcut_path = shortcuts_directory.Append(new_shortcut_file); | 224 // If a system level shortcut exists at the destination, then simply delete |
| 225 // the old shortcut. | |
| 226 const FilePath possible_new_system_shortcut = | |
| 227 system_shortcuts_directory.Append(new_shortcut_filename); | |
| 228 if (file_util::PathExists(possible_new_system_shortcut)) { | |
| 229 file_util::Delete(old_shortcut_path, false); | |
| 230 return; | |
|
gab
2013/01/15 23:09:14
I'm not a fan of the multiple early returns I feel
Alexei Svitkine (slow)
2013/01/16 16:11:44
I like your suggested flow better, done!
| |
| 231 } | |
| 232 | |
| 203 if (!file_util::Move(old_shortcut_path, new_shortcut_path)) | 233 if (!file_util::Move(old_shortcut_path, new_shortcut_path)) |
| 204 LOG(ERROR) << "Could not rename Windows profile desktop shortcut."; | 234 LOG(ERROR) << "Could not rename Windows profile desktop shortcut."; |
| 205 } | 235 } |
| 206 | 236 |
| 207 // Updates all desktop shortcuts for the given profile to have the specified | 237 // Updates all desktop shortcuts for the given profile to have the specified |
| 208 // parameters. If |create_mode| is CREATE_WHEN_NONE_FOUND, a new shortcut is | 238 // parameters. If |create_mode| is CREATE_WHEN_NONE_FOUND, a new shortcut is |
| 209 // created if no existing ones were found. Whether non-profile shortcuts should | 239 // created if no existing ones were found. Whether non-profile shortcuts should |
| 210 // be updated is specified by |action|. Must be called on the FILE thread. | 240 // be updated is specified by |action|. Must be called on the FILE thread. |
| 211 void CreateOrUpdateDesktopShortcutsForProfile( | 241 void CreateOrUpdateDesktopShortcutsForProfile( |
| 212 const FilePath& profile_path, | 242 const FilePath& profile_path, |
| 243 const string16& old_profile_name, | |
| 213 const string16& profile_name, | 244 const string16& profile_name, |
| 214 const SkBitmap& avatar_image, | 245 const SkBitmap& avatar_image, |
| 215 ProfileShortcutManagerWin::CreateOrUpdateMode create_mode, | 246 ProfileShortcutManagerWin::CreateOrUpdateMode create_mode, |
| 216 ProfileShortcutManagerWin::NonProfileShortcutAction action) { | 247 ProfileShortcutManagerWin::NonProfileShortcutAction action) { |
| 217 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 248 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 218 | 249 |
| 219 FilePath chrome_exe; | 250 FilePath chrome_exe; |
| 220 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { | 251 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
| 221 NOTREACHED(); | 252 NOTREACHED(); |
| 222 return; | 253 return; |
| 223 } | 254 } |
| 224 | 255 |
| 225 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); | 256 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); |
| 226 // Ensure that the distribution supports creating shortcuts. If it doesn't, | 257 // Ensure that the distribution supports creating shortcuts. If it doesn't, |
| 227 // the following code may result in NOTREACHED() being hit. | 258 // the following code may result in NOTREACHED() being hit. |
| 228 DCHECK(distribution->CanCreateDesktopShortcuts()); | 259 DCHECK(distribution->CanCreateDesktopShortcuts()); |
| 229 installer::Product product(distribution); | 260 |
| 261 if (old_profile_name != profile_name) { | |
| 262 const string16 old_shortcut_filename = | |
| 263 profiles::internal::GetShortcutFilenameForProfile(old_profile_name, | |
| 264 distribution); | |
| 265 const string16 new_shortcut_filename = | |
| 266 profiles::internal::GetShortcutFilenameForProfile(profile_name, | |
| 267 distribution); | |
| 268 RenameChromeDesktopShortcutForProfile(old_shortcut_filename, | |
| 269 new_shortcut_filename); | |
| 270 } | |
| 230 | 271 |
| 231 ShellUtil::ShortcutProperties properties(ShellUtil::CURRENT_USER); | 272 ShellUtil::ShortcutProperties properties(ShellUtil::CURRENT_USER); |
| 273 installer::Product product(distribution); | |
| 232 product.AddDefaultShortcutProperties(chrome_exe, &properties); | 274 product.AddDefaultShortcutProperties(chrome_exe, &properties); |
| 233 | 275 |
| 234 const string16 command_line = | 276 const string16 command_line = |
| 235 profiles::internal::CreateProfileShortcutFlags(profile_path); | 277 profiles::internal::CreateProfileShortcutFlags(profile_path); |
| 236 | 278 |
| 237 // Only set the profile-specific properties when |profile_name| is non empty. | 279 // Only set the profile-specific properties when |profile_name| is non empty. |
| 238 // If it is empty, it means the shortcut being created should be a regular, | 280 // If it is empty, it means the shortcut being created should be a regular, |
| 239 // non-profile Chrome shortcut. | 281 // non-profile Chrome shortcut. |
| 240 if (!profile_name.empty()) { | 282 if (!profile_name.empty()) { |
| 241 const FilePath shortcut_icon = | 283 const FilePath shortcut_icon = |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 255 std::vector<FilePath> shortcuts; | 297 std::vector<FilePath> shortcuts; |
| 256 ListDesktopShortcutsWithCommandLine(chrome_exe, command_line, | 298 ListDesktopShortcutsWithCommandLine(chrome_exe, command_line, |
| 257 action == ProfileShortcutManagerWin::UPDATE_NON_PROFILE_SHORTCUTS, | 299 action == ProfileShortcutManagerWin::UPDATE_NON_PROFILE_SHORTCUTS, |
| 258 &shortcuts); | 300 &shortcuts); |
| 259 if (create_mode == ProfileShortcutManagerWin::CREATE_WHEN_NONE_FOUND && | 301 if (create_mode == ProfileShortcutManagerWin::CREATE_WHEN_NONE_FOUND && |
| 260 shortcuts.empty()) { | 302 shortcuts.empty()) { |
| 261 const string16 shortcut_name = | 303 const string16 shortcut_name = |
| 262 profiles::internal::GetShortcutFilenameForProfile(profile_name, | 304 profiles::internal::GetShortcutFilenameForProfile(profile_name, |
| 263 distribution); | 305 distribution); |
| 264 shortcuts.push_back(FilePath(shortcut_name)); | 306 shortcuts.push_back(FilePath(shortcut_name)); |
| 265 operation = ShellUtil::SHELL_SHORTCUT_CREATE_ALWAYS; | 307 operation = ShellUtil::SHELL_SHORTCUT_CREATE_IF_NO_SYSTEM_LEVEL; |
| 266 } | 308 } |
| 267 | 309 |
| 268 for (size_t i = 0; i < shortcuts.size(); ++i) { | 310 for (size_t i = 0; i < shortcuts.size(); ++i) { |
| 269 const FilePath shortcut_name = shortcuts[i].BaseName().RemoveExtension(); | 311 const FilePath shortcut_name = shortcuts[i].BaseName().RemoveExtension(); |
| 270 properties.set_shortcut_name(shortcut_name.value()); | 312 properties.set_shortcut_name(shortcut_name.value()); |
| 271 ShellUtil::CreateOrUpdateShortcut(ShellUtil::SHORTCUT_LOCATION_DESKTOP, | 313 ShellUtil::CreateOrUpdateShortcut(ShellUtil::SHORTCUT_LOCATION_DESKTOP, |
| 272 distribution, properties, operation); | 314 distribution, properties, operation); |
| 273 } | 315 } |
| 274 } | 316 } |
| 275 | 317 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 333 // Ensure that the distribution supports creating shortcuts. If it doesn't, | 375 // Ensure that the distribution supports creating shortcuts. If it doesn't, |
| 334 // the following code may result in NOTREACHED() being hit. | 376 // the following code may result in NOTREACHED() being hit. |
| 335 DCHECK(distribution->CanCreateDesktopShortcuts()); | 377 DCHECK(distribution->CanCreateDesktopShortcuts()); |
| 336 installer::Product product(distribution); | 378 installer::Product product(distribution); |
| 337 | 379 |
| 338 ShellUtil::ShortcutProperties properties(ShellUtil::CURRENT_USER); | 380 ShellUtil::ShortcutProperties properties(ShellUtil::CURRENT_USER); |
| 339 product.AddDefaultShortcutProperties(chrome_exe, &properties); | 381 product.AddDefaultShortcutProperties(chrome_exe, &properties); |
| 340 properties.set_shortcut_name( | 382 properties.set_shortcut_name( |
| 341 profiles::internal::GetShortcutFilenameForProfile(string16(), | 383 profiles::internal::GetShortcutFilenameForProfile(string16(), |
| 342 distribution)); | 384 distribution)); |
| 343 ShellUtil::CreateOrUpdateShortcut(ShellUtil::SHORTCUT_LOCATION_DESKTOP, | 385 ShellUtil::CreateOrUpdateShortcut( |
| 344 distribution, properties, | 386 ShellUtil::SHORTCUT_LOCATION_DESKTOP, distribution, properties, |
| 345 ShellUtil::SHELL_SHORTCUT_CREATE_ALWAYS); | 387 ShellUtil::SHELL_SHORTCUT_CREATE_IF_NO_SYSTEM_LEVEL); |
| 346 } | 388 } |
| 347 } | 389 } |
| 348 | 390 |
| 349 // Returns true if profile at |profile_path| has any shortcuts. Does not | 391 // Returns true if profile at |profile_path| has any shortcuts. Does not |
| 350 // consider non-profile shortcuts. Must be called on the FILE thread. | 392 // consider non-profile shortcuts. Must be called on the FILE thread. |
| 351 bool HasAnyProfileShortcuts(const FilePath& profile_path) { | 393 bool HasAnyProfileShortcuts(const FilePath& profile_path) { |
| 352 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 394 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 353 | 395 |
| 354 FilePath chrome_exe; | 396 FilePath chrome_exe; |
| 355 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { | 397 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 498 CreateOrUpdateShortcutsForProfileAtPath(profile_path, UPDATE_EXISTING_ONLY, | 540 CreateOrUpdateShortcutsForProfileAtPath(profile_path, UPDATE_EXISTING_ONLY, |
| 499 IGNORE_NON_PROFILE_SHORTCUTS); | 541 IGNORE_NON_PROFILE_SHORTCUTS); |
| 500 } | 542 } |
| 501 | 543 |
| 502 void ProfileShortcutManagerWin::OnProfileAvatarChanged( | 544 void ProfileShortcutManagerWin::OnProfileAvatarChanged( |
| 503 const FilePath& profile_path) { | 545 const FilePath& profile_path) { |
| 504 CreateOrUpdateShortcutsForProfileAtPath(profile_path, UPDATE_EXISTING_ONLY, | 546 CreateOrUpdateShortcutsForProfileAtPath(profile_path, UPDATE_EXISTING_ONLY, |
| 505 IGNORE_NON_PROFILE_SHORTCUTS); | 547 IGNORE_NON_PROFILE_SHORTCUTS); |
| 506 } | 548 } |
| 507 | 549 |
| 508 void ProfileShortcutManagerWin::StartProfileShortcutNameChange( | |
| 509 const FilePath& profile_path, | |
| 510 const string16& old_profile_name) { | |
| 511 const ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache(); | |
| 512 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_path); | |
| 513 if (profile_index == std::string::npos) | |
| 514 return; | |
| 515 // If the shortcut will have an appended name, get the profile name. | |
| 516 string16 new_profile_name; | |
| 517 if (cache.GetNumberOfProfiles() != 1) | |
| 518 new_profile_name = cache.GetNameOfProfileAtIndex(profile_index); | |
| 519 | |
| 520 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); | |
| 521 const string16 old_shortcut_file = | |
| 522 profiles::internal::GetShortcutFilenameForProfile(old_profile_name, | |
| 523 distribution); | |
| 524 const string16 new_shortcut_file = | |
| 525 profiles::internal::GetShortcutFilenameForProfile(new_profile_name, | |
| 526 distribution); | |
| 527 BrowserThread::PostTask( | |
| 528 BrowserThread::FILE, FROM_HERE, | |
| 529 base::Bind(&RenameChromeDesktopShortcutForProfile, | |
| 530 old_shortcut_file, | |
| 531 new_shortcut_file)); | |
| 532 } | |
| 533 | |
| 534 FilePath ProfileShortcutManagerWin::GetOtherProfilePath( | 550 FilePath ProfileShortcutManagerWin::GetOtherProfilePath( |
| 535 const FilePath& profile_path) { | 551 const FilePath& profile_path) { |
| 536 const ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache(); | 552 const ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache(); |
| 537 DCHECK_EQ(2U, cache.GetNumberOfProfiles()); | 553 DCHECK_EQ(2U, cache.GetNumberOfProfiles()); |
| 538 // Get the index of the current profile, in order to find the index of the | 554 // Get the index of the current profile, in order to find the index of the |
| 539 // other profile. | 555 // other profile. |
| 540 size_t current_profile_index = cache.GetIndexOfProfileWithPath(profile_path); | 556 size_t current_profile_index = cache.GetIndexOfProfileWithPath(profile_path); |
| 541 size_t other_profile_index = (current_profile_index == 0) ? 1 : 0; | 557 size_t other_profile_index = (current_profile_index == 0) ? 1 : 0; |
| 542 return cache.GetPathOfProfileAtIndex(other_profile_index); | 558 return cache.GetPathOfProfileAtIndex(other_profile_index); |
| 543 } | 559 } |
| 544 | 560 |
| 545 void ProfileShortcutManagerWin::CreateOrUpdateShortcutsForProfileAtPath( | 561 void ProfileShortcutManagerWin::CreateOrUpdateShortcutsForProfileAtPath( |
| 546 const FilePath& profile_path, | 562 const FilePath& profile_path, |
| 547 CreateOrUpdateMode create_mode, | 563 CreateOrUpdateMode create_mode, |
| 548 NonProfileShortcutAction action) { | 564 NonProfileShortcutAction action) { |
| 549 ProfileInfoCache* cache = &profile_manager_->GetProfileInfoCache(); | 565 ProfileInfoCache* cache = &profile_manager_->GetProfileInfoCache(); |
| 550 size_t profile_index = cache->GetIndexOfProfileWithPath(profile_path); | 566 size_t profile_index = cache->GetIndexOfProfileWithPath(profile_path); |
| 551 if (profile_index == std::string::npos) | 567 if (profile_index == std::string::npos) |
| 552 return; | 568 return; |
| 553 bool remove_badging = cache->GetNumberOfProfiles() == 1; | 569 bool remove_badging = cache->GetNumberOfProfiles() == 1; |
| 554 | 570 |
| 555 string16 old_shortcut_appended_name = | 571 string16 old_shortcut_appended_name = |
| 556 cache->GetShortcutNameOfProfileAtIndex(profile_index); | 572 cache->GetShortcutNameOfProfileAtIndex(profile_index); |
| 557 | 573 |
| 558 string16 new_shortcut_appended_name; | 574 string16 new_shortcut_appended_name; |
| 559 if (!remove_badging) | 575 if (!remove_badging) |
| 560 new_shortcut_appended_name = cache->GetNameOfProfileAtIndex(profile_index); | 576 new_shortcut_appended_name = cache->GetNameOfProfileAtIndex(profile_index); |
| 561 | 577 |
| 562 if (create_mode == UPDATE_EXISTING_ONLY && | |
| 563 new_shortcut_appended_name != old_shortcut_appended_name) { | |
| 564 // TODO(asvitkine): Fold this into |UpdateDesktopShortcutsForProfile()|. | |
| 565 StartProfileShortcutNameChange(profile_path, old_shortcut_appended_name); | |
| 566 } | |
| 567 | |
| 568 SkBitmap profile_avatar_bitmap_copy; | 578 SkBitmap profile_avatar_bitmap_copy; |
| 569 if (!remove_badging) { | 579 if (!remove_badging) { |
| 570 size_t profile_icon_index = | 580 size_t profile_icon_index = |
| 571 cache->GetAvatarIconIndexOfProfileAtIndex(profile_index); | 581 cache->GetAvatarIconIndexOfProfileAtIndex(profile_index); |
| 572 gfx::Image profile_avatar_image = ResourceBundle::GetSharedInstance(). | 582 gfx::Image profile_avatar_image = ResourceBundle::GetSharedInstance(). |
| 573 GetNativeImageNamed( | 583 GetNativeImageNamed( |
| 574 cache->GetDefaultAvatarIconResourceIDAtIndex(profile_icon_index)); | 584 cache->GetDefaultAvatarIconResourceIDAtIndex(profile_icon_index)); |
| 575 | 585 |
| 576 DCHECK(!profile_avatar_image.IsEmpty()); | 586 DCHECK(!profile_avatar_image.IsEmpty()); |
| 577 const SkBitmap* profile_avatar_bitmap = profile_avatar_image.ToSkBitmap(); | 587 const SkBitmap* profile_avatar_bitmap = profile_avatar_image.ToSkBitmap(); |
| 578 // Make a copy of the SkBitmap to ensure that we can safely use the image | 588 // Make a copy of the SkBitmap to ensure that we can safely use the image |
| 579 // data on the FILE thread. | 589 // data on the FILE thread. |
| 580 profile_avatar_bitmap->deepCopyTo(&profile_avatar_bitmap_copy, | 590 profile_avatar_bitmap->deepCopyTo(&profile_avatar_bitmap_copy, |
| 581 profile_avatar_bitmap->getConfig()); | 591 profile_avatar_bitmap->getConfig()); |
| 582 } | 592 } |
| 583 BrowserThread::PostTask( | 593 BrowserThread::PostTask( |
| 584 BrowserThread::FILE, FROM_HERE, | 594 BrowserThread::FILE, FROM_HERE, |
| 585 base::Bind(&CreateOrUpdateDesktopShortcutsForProfile, | 595 base::Bind(&CreateOrUpdateDesktopShortcutsForProfile, |
| 586 profile_path, new_shortcut_appended_name, | 596 profile_path, old_shortcut_appended_name, |
| 587 profile_avatar_bitmap_copy, create_mode, action)); | 597 new_shortcut_appended_name, profile_avatar_bitmap_copy, |
| 598 create_mode, action)); | |
| 588 | 599 |
| 589 cache->SetShortcutNameOfProfileAtIndex(profile_index, | 600 cache->SetShortcutNameOfProfileAtIndex(profile_index, |
| 590 new_shortcut_appended_name); | 601 new_shortcut_appended_name); |
| 591 } | 602 } |
| OLD | NEW |