OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/profiles/profile_shortcut_manager_win.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/file_path.h" |
| 9 #include "base/file_util.h" |
| 10 #include "base/path_service.h" |
| 11 #include "base/utf_string_conversions.h" |
| 12 #include "chrome/browser/browser_process.h" |
| 13 #include "chrome/browser/prefs/pref_service.h" |
| 14 #include "chrome/browser/profiles/profile_info_cache.h" |
| 15 #include "chrome/browser/profiles/profile_manager.h" |
| 16 #include "chrome/common/chrome_constants.h" |
| 17 #include "chrome/common/chrome_switches.h" |
| 18 #include "chrome/common/pref_names.h" |
| 19 #include "chrome/installer/util/browser_distribution.h" |
| 20 #include "chrome/installer/util/shell_util.h" |
| 21 #include "content/public/browser/browser_thread.h" |
| 22 #include "grit/generated_resources.h" |
| 23 #include "ui/base/l10n/l10n_util.h" |
| 24 |
| 25 using content::BrowserThread; |
| 26 |
| 27 namespace { |
| 28 |
| 29 // Creates the argument to pass to the Windows executable that launches Chrome |
| 30 // with the profile in |profile_base_dir|. |
| 31 // For example: --profile-directory="Profile 2" |
| 32 string16 CreateProfileShortcutSwitch(string16 profile_base_dir) { |
| 33 string16 profile_directory; |
| 34 profile_directory = L"--"; |
| 35 profile_directory.append( |
| 36 UTF8ToUTF16(std::string(switches::kProfileDirectory))); |
| 37 profile_directory.append(L"=\""); |
| 38 profile_directory.append(profile_base_dir); |
| 39 profile_directory.append(L"\""); |
| 40 return profile_directory; |
| 41 } |
| 42 |
| 43 } // namespace |
| 44 |
| 45 ProfileShortcutManagerWin::ProfileShortcutManagerWin() { |
| 46 } |
| 47 |
| 48 ProfileShortcutManagerWin::~ProfileShortcutManagerWin() { |
| 49 } |
| 50 |
| 51 void ProfileShortcutManagerWin::OnProfileAdded(string16 profile_name, |
| 52 string16 profile_base_dir) { |
| 53 // Launch task to add shortcut to desktop on Windows. If this is the very |
| 54 // first profile created, don't add the user name to the shortcut. |
| 55 // TODO(mirandac): respect master_preferences choice to create no shortcuts |
| 56 // (see http://crbug.com/104463) |
| 57 if (g_browser_process->profile_manager()->GetNumberOfProfiles() > 1) { |
| 58 string16 profile_directory; |
| 59 profile_directory.assign(CreateProfileShortcutSwitch(profile_base_dir)); |
| 60 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 61 base::Bind(&CreateChromeDesktopShortcutForProfile, |
| 62 profile_name, profile_directory, true)); |
| 63 |
| 64 // If this is the very first multi-user account created, change the |
| 65 // original shortcut to launch with the First User profile as well. |
| 66 PrefService* local_state = g_browser_process->local_state(); |
| 67 if (local_state->GetInteger(prefs::kProfilesNumCreated) == 2) { |
| 68 string16 old_shortcut; |
| 69 string16 new_shortcut; |
| 70 string16 default_name = l10n_util::GetStringUTF16( |
| 71 IDS_DEFAULT_PROFILE_NAME); |
| 72 string16 default_directory = |
| 73 CreateProfileShortcutSwitch(UTF8ToUTF16(chrome::kInitialProfile)); |
| 74 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 75 if (ShellUtil::GetChromeShortcutName(dist, false, L"", &old_shortcut) && |
| 76 ShellUtil::GetChromeShortcutName(dist, false, default_name, |
| 77 &new_shortcut)) { |
| 78 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 79 base::Bind(&RenameChromeDesktopShortcutForProfile, |
| 80 old_shortcut, new_shortcut)); |
| 81 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 82 base::Bind(&UpdateChromeDesktopShortcutForProfile, |
| 83 new_shortcut, default_directory)); |
| 84 } |
| 85 } |
| 86 } else { |
| 87 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 88 base::Bind(&CreateChromeDesktopShortcutForProfile, |
| 89 L"", L"", true)); |
| 90 } |
| 91 } |
| 92 |
| 93 void ProfileShortcutManagerWin::OnProfileRemoved(string16 profile_name) { |
| 94 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 95 string16 shortcut; |
| 96 if (ShellUtil::GetChromeShortcutName(dist, false, profile_name, &shortcut)) { |
| 97 std::vector<string16> shortcuts; |
| 98 shortcuts.push_back(shortcut); |
| 99 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 100 base::Bind(&RemoveChromeDesktopShortcutsForProfiles, |
| 101 shortcuts)); |
| 102 } |
| 103 } |
| 104 |
| 105 void ProfileShortcutManagerWin::OnProfileNameChanged( |
| 106 string16 old_profile_name, |
| 107 string16 new_profile_name) { |
| 108 // Launch task to change name of desktop shortcut on Windows. |
| 109 // TODO(mirandac): respect master_preferences choice to create no shortcuts |
| 110 // (see http://crbug.com/104463) |
| 111 string16 old_shortcut; |
| 112 string16 new_shortcut; |
| 113 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 114 if (ShellUtil::GetChromeShortcutName( |
| 115 dist, false, old_profile_name, &old_shortcut) && |
| 116 ShellUtil::GetChromeShortcutName( |
| 117 dist, false, new_profile_name, &new_shortcut)) { |
| 118 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 119 base::Bind(&RenameChromeDesktopShortcutForProfile, |
| 120 old_shortcut, |
| 121 new_shortcut)); |
| 122 } |
| 123 } |
| 124 |
| 125 // static |
| 126 void ProfileShortcutManagerWin::RemoveChromeDesktopShortcutsForProfiles( |
| 127 std::vector<string16> shortcut_names) { |
| 128 bool not_used = ShellUtil::RemoveChromeDesktopShortcutsWithAppendedNames( |
| 129 shortcut_names); |
| 130 } |
| 131 |
| 132 // static |
| 133 void ProfileShortcutManagerWin::CreateChromeDesktopShortcutForProfile( |
| 134 string16 profile_name, string16 directory, bool create) { |
| 135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 136 FilePath chrome_exe; |
| 137 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) |
| 138 return; |
| 139 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 140 string16 description; |
| 141 if (!dist) |
| 142 return; |
| 143 else |
| 144 description = WideToUTF16(dist->GetAppDescription()); |
| 145 ShellUtil::CreateChromeDesktopShortcut( |
| 146 dist, |
| 147 chrome_exe.value(), |
| 148 description, |
| 149 profile_name, |
| 150 directory, |
| 151 ShellUtil::CURRENT_USER, |
| 152 false, // Use alternate text. |
| 153 create); // Create if it doesn't already exist. |
| 154 } |
| 155 |
| 156 // static |
| 157 void ProfileShortcutManagerWin::RenameChromeDesktopShortcutForProfile( |
| 158 string16 old_shortcut, |
| 159 string16 new_shortcut) { |
| 160 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 161 FilePath shortcut_path; |
| 162 if (ShellUtil::GetDesktopPath(false, // User's directory instead of system. |
| 163 &shortcut_path)) { |
| 164 FilePath old_profile = shortcut_path.Append(old_shortcut); |
| 165 FilePath new_profile = shortcut_path.Append(new_shortcut); |
| 166 file_util::Move(old_profile, new_profile); |
| 167 } |
| 168 } |
| 169 |
| 170 // static |
| 171 void ProfileShortcutManagerWin::UpdateChromeDesktopShortcutForProfile( |
| 172 string16 shortcut, |
| 173 string16 arguments) { |
| 174 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 175 FilePath shortcut_path; |
| 176 if (!ShellUtil::GetDesktopPath(false, &shortcut_path)) |
| 177 return; |
| 178 |
| 179 shortcut_path = shortcut_path.Append(shortcut); |
| 180 FilePath chrome_exe; |
| 181 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) |
| 182 return; |
| 183 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 184 string16 description; |
| 185 if (!dist) |
| 186 return; |
| 187 else |
| 188 description = WideToUTF16(dist->GetAppDescription()); |
| 189 ShellUtil::UpdateChromeShortcut( |
| 190 dist, |
| 191 chrome_exe.value(), |
| 192 UTF8ToUTF16(shortcut_path.MaybeAsASCII()), |
| 193 arguments, |
| 194 description, |
| 195 false); |
| 196 } |
OLD | NEW |