Chromium Code Reviews| Index: chrome/browser/profiles/profile_shortcut_manager_win.cc |
| =================================================================== |
| --- chrome/browser/profiles/profile_shortcut_manager_win.cc (revision 169477) |
| +++ chrome/browser/profiles/profile_shortcut_manager_win.cc (working copy) |
| @@ -98,13 +98,6 @@ |
| return icon_path; |
| } |
| -// Returns the command-line flags to launch Chrome with the given profile. |
| -string16 CreateProfileShortcutFlags(const FilePath& profile_path) { |
| - return base::StringPrintf(L"--%ls=\"%ls\"", |
| - ASCIIToUTF16(switches::kProfileDirectory).c_str(), |
| - profile_path.BaseName().value().c_str()); |
| -} |
| - |
| // Gets the directory where to create desktop shortcuts. |
| bool GetDesktopShortcutsDirectory(FilePath* directory) { |
| const bool result = |
| @@ -115,32 +108,27 @@ |
| return result; |
| } |
| -// Returns true if the file at |path| is a Chrome shortcut with the given |
| -// |command_line|. |
| -bool IsChromeShortcutWithCommandLine(const FilePath& path, |
| - const FilePath& chrome_exe, |
| - const string16& command_line) { |
| +// Returns true if the file at |path| is a Chrome shortcut and returns its |
| +// command line in output parameter |command_line|. |
| +bool IsChromeShortcut(const FilePath& path, |
| + const FilePath& chrome_exe, |
| + string16* command_line) { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| if (path.Extension() != installer::kLnkExt) |
| return false; |
| FilePath target_path; |
| - string16 command_line_args; |
| - if (!base::win::ResolveShortcut(path, &target_path, &command_line_args)) |
| + if (!base::win::ResolveShortcut(path, &target_path, command_line)) |
| return false; |
| - |
| - // TODO(asvitkine): Change this to build a CommandLine object and ensure all |
| - // args from |command_line| are present in the shortcut's CommandLine. This |
| - // will be more robust when |command_line| contains multiple args. |
| - return target_path == chrome_exe && |
| - command_line_args.find(command_line) != string16::npos; |
| + return target_path == chrome_exe; |
| } |
| // Populates |paths| with the file paths of Chrome desktop shortcuts that have |
| // the specified |command_line|. |
| void ListDesktopShortcutsWithCommandLine(const FilePath& chrome_exe, |
| const string16& command_line, |
| + bool include_empty_command_lines, |
|
sail
2012/11/28 05:12:31
Yea, I think plumbing the enums here would be grea
Alexei Svitkine (slow)
2012/11/28 21:27:12
I've plumbed them through to CreateOrUpdateDesktop
|
| std::vector<FilePath>* paths) { |
| FilePath shortcuts_directory; |
| if (!GetDesktopShortcutsDirectory(&shortcuts_directory)) |
| @@ -150,8 +138,17 @@ |
| file_util::FileEnumerator::FILES); |
| for (FilePath path = enumerator.Next(); !path.empty(); |
| path = enumerator.Next()) { |
| - if (IsChromeShortcutWithCommandLine(path, chrome_exe, command_line)) |
| + string16 shortcut_command_line; |
| + if (!IsChromeShortcut(path, chrome_exe, &shortcut_command_line)) |
| + continue; |
| + |
| + // TODO(asvitkine): Change this to build a CommandLine object and ensure all |
| + // args from |command_line| are present in the shortcut's CommandLine. This |
| + // will be more robust when |command_line| contains multiple args. |
| + if ((shortcut_command_line.empty() && include_empty_command_lines) || |
| + (shortcut_command_line.find(command_line) != string16::npos)) { |
| paths->push_back(path); |
| + } |
| } |
| } |
| @@ -183,6 +180,7 @@ |
| void CreateOrUpdateDesktopShortcutsForProfile(const FilePath& profile_path, |
| const string16& profile_name, |
| const SkBitmap& avatar_image, |
| + bool include_empty_command_lines, |
| bool create) { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| @@ -202,14 +200,16 @@ |
| if (!shortcut_icon.empty()) |
| properties.set_icon(shortcut_icon, 0); |
| - const string16 command_line = CreateProfileShortcutFlags(profile_path); |
| + const string16 command_line = |
| + profiles::internal::CreateProfileShortcutFlags(profile_path); |
| properties.set_arguments(command_line); |
| ShellUtil::ShortcutOperation operation = |
| ShellUtil::SHELL_SHORTCUT_REPLACE_EXISTING; |
| std::vector<FilePath> shortcuts; |
| - ListDesktopShortcutsWithCommandLine(chrome_exe, command_line, &shortcuts); |
| + ListDesktopShortcutsWithCommandLine(chrome_exe, command_line, |
| + include_empty_command_lines, &shortcuts); |
| if (create && shortcuts.empty()) { |
| const string16 shortcut_name = |
| profiles::internal::GetShortcutFilenameForProfile(profile_name, |
| @@ -238,9 +238,11 @@ |
| return; |
| } |
| - const string16 command_line = CreateProfileShortcutFlags(profile_path); |
| + const string16 command_line = |
| + profiles::internal::CreateProfileShortcutFlags(profile_path); |
| std::vector<FilePath> shortcuts; |
| - ListDesktopShortcutsWithCommandLine(chrome_exe, command_line, &shortcuts); |
| + ListDesktopShortcutsWithCommandLine(chrome_exe, command_line, false, |
| + &shortcuts); |
| BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); |
| for (size_t i = 0; i < shortcuts.size(); ++i) { |
| @@ -271,6 +273,12 @@ |
| return shortcut_name + installer::kLnkExt; |
| } |
| +string16 CreateProfileShortcutFlags(const FilePath& profile_path) { |
| + return base::StringPrintf(L"--%ls=\"%ls\"", |
| + ASCIIToUTF16(switches::kProfileDirectory).c_str(), |
| + profile_path.BaseName().value().c_str()); |
| +} |
| + |
| } // namespace internal |
| } // namespace profiles |
| @@ -296,16 +304,21 @@ |
| void ProfileShortcutManagerWin::CreateProfileShortcut( |
| const FilePath& profile_path) { |
| - UpdateShortcutsForProfileAtPath(profile_path, true); |
| + UpdateShortcutsForProfileAtPath(profile_path, CREATE_WHEN_NONE_FOUND, |
| + IGNORE_NON_PROFILE_SHORTCUTS); |
| } |
| void ProfileShortcutManagerWin::OnProfileAdded(const FilePath& profile_path) { |
| const size_t profile_count = |
| profile_manager_->GetProfileInfoCache().GetNumberOfProfiles(); |
| if (profile_count == 1) { |
| - UpdateShortcutsForProfileAtPath(profile_path, true); |
| + UpdateShortcutsForProfileAtPath(profile_path, |
| + CREATE_WHEN_NONE_FOUND, |
| + UPDATE_NON_PROFILE_SHORTCUTS); |
| } else if (profile_count == 2) { |
| - UpdateShortcutsForProfileAtPath(GetOtherProfilePath(profile_path), false); |
| + UpdateShortcutsForProfileAtPath(GetOtherProfilePath(profile_path), |
| + UPDATE_EXISTING_ONLY, |
| + UPDATE_NON_PROFILE_SHORTCUTS); |
| } |
| } |
| @@ -319,8 +332,11 @@ |
| const ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache(); |
| // If there is only one profile remaining, remove the badging information |
| // from an existing shortcut. |
| - if (cache.GetNumberOfProfiles() == 1) |
| - UpdateShortcutsForProfileAtPath(cache.GetPathOfProfileAtIndex(0), false); |
| + if (cache.GetNumberOfProfiles() == 1) { |
| + UpdateShortcutsForProfileAtPath(cache.GetPathOfProfileAtIndex(0), |
| + UPDATE_EXISTING_ONLY, |
| + IGNORE_NON_PROFILE_SHORTCUTS); |
| + } |
| const FilePath icon_path = profile_path.AppendASCII(kProfileIconFileName); |
| BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| @@ -331,12 +347,14 @@ |
| void ProfileShortcutManagerWin::OnProfileNameChanged( |
| const FilePath& profile_path, |
| const string16& old_profile_name) { |
| - UpdateShortcutsForProfileAtPath(profile_path, false); |
| + UpdateShortcutsForProfileAtPath(profile_path, UPDATE_EXISTING_ONLY, |
| + IGNORE_NON_PROFILE_SHORTCUTS); |
| } |
| void ProfileShortcutManagerWin::OnProfileAvatarChanged( |
| const FilePath& profile_path) { |
| - UpdateShortcutsForProfileAtPath(profile_path, false); |
| + UpdateShortcutsForProfileAtPath(profile_path, UPDATE_EXISTING_ONLY, |
| + IGNORE_NON_PROFILE_SHORTCUTS); |
| } |
| void ProfileShortcutManagerWin::StartProfileShortcutNameChange( |
| @@ -379,7 +397,8 @@ |
| void ProfileShortcutManagerWin::UpdateShortcutsForProfileAtPath( |
| const FilePath& profile_path, |
| - bool create_always) { |
| + CreateOrUpdateMode create_mode, |
| + NonProfileShortcutAction action) { |
| ProfileInfoCache* cache = &profile_manager_->GetProfileInfoCache(); |
| size_t profile_index = cache->GetIndexOfProfileWithPath(profile_path); |
| if (profile_index == std::string::npos) |
| @@ -393,7 +412,7 @@ |
| if (!remove_badging) |
| new_shortcut_appended_name = cache->GetNameOfProfileAtIndex(profile_index); |
| - if (!create_always && |
| + if (create_mode == UPDATE_EXISTING_ONLY && |
| new_shortcut_appended_name != old_shortcut_appended_name) { |
| // TODO(asvitkine): Fold this into |UpdateDesktopShortcutsForProfile()|. |
| StartProfileShortcutNameChange(profile_path, old_shortcut_appended_name); |
| @@ -418,7 +437,9 @@ |
| BrowserThread::FILE, FROM_HERE, |
| base::Bind(&CreateOrUpdateDesktopShortcutsForProfile, |
| profile_path, new_shortcut_appended_name, |
| - profile_avatar_bitmap_copy, create_always)); |
| + profile_avatar_bitmap_copy, |
| + action == UPDATE_NON_PROFILE_SHORTCUTS, |
| + create_mode == CREATE_WHEN_NONE_FOUND)); |
| cache->SetShortcutNameOfProfileAtIndex(profile_index, |
| new_shortcut_appended_name); |