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/stringprintf.h" |
| 12 #include "base/utf_string_conversions.h" |
| 13 #include "chrome/browser/browser_process.h" |
| 14 #include "chrome/browser/prefs/pref_service.h" |
| 15 #include "chrome/browser/profiles/profile_info_cache.h" |
| 16 #include "chrome/browser/profiles/profile_manager.h" |
| 17 #include "chrome/common/chrome_constants.h" |
| 18 #include "chrome/common/chrome_switches.h" |
| 19 #include "chrome/common/pref_names.h" |
| 20 #include "chrome/installer/util/browser_distribution.h" |
| 21 #include "chrome/installer/util/shell_util.h" |
| 22 #include "content/public/browser/browser_thread.h" |
| 23 #include "grit/generated_resources.h" |
| 24 #include "ui/base/l10n/l10n_util.h" |
| 25 |
| 26 using content::BrowserThread; |
| 27 |
| 28 namespace { |
| 29 |
| 30 // Creates the argument to pass to the Windows executable that launches Chrome |
| 31 // with the profile in |profile_base_dir|. |
| 32 // For example: --profile-directory="Profile 2" |
| 33 string16 CreateProfileShortcutSwitch(string16 profile_base_dir) { |
| 34 string16 profile_directory = base::StringPrintf(L"--%ls=\"%ls\"", |
| 35 ASCIIToUTF16(switches::kProfileDirectory).c_str(), |
| 36 profile_base_dir.c_str()); |
| 37 return profile_directory; |
| 38 } |
| 39 |
| 40 // Wrap a ShellUtil function that returns a bool so it can be posted in a |
| 41 // task to the FILE thread. |
| 42 void CallShellUtilBoolFunction( |
| 43 const base::Callback<bool(void)>& bool_function) { |
| 44 bool_function.Run(); |
| 45 } |
| 46 |
| 47 } // namespace |
| 48 |
| 49 ProfileShortcutManagerWin::ProfileShortcutManagerWin() { |
| 50 } |
| 51 |
| 52 ProfileShortcutManagerWin::~ProfileShortcutManagerWin() { |
| 53 } |
| 54 |
| 55 void ProfileShortcutManagerWin::OnProfileAdded( |
| 56 const string16& profile_name, |
| 57 const string16& profile_base_dir) { |
| 58 // Launch task to add shortcut to desktop on Windows. If this is the very |
| 59 // first profile created, don't add the user name to the shortcut. |
| 60 // TODO(mirandac): respect master_preferences choice to create no shortcuts |
| 61 // (see http://crbug.com/104463) |
| 62 if (g_browser_process->profile_manager()->GetNumberOfProfiles() > 1) { |
| 63 string16 profile_directory = |
| 64 CreateProfileShortcutSwitch(profile_base_dir); |
| 65 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 66 base::Bind(&CreateChromeDesktopShortcutForProfile, |
| 67 profile_name, profile_directory, true)); |
| 68 |
| 69 // If this is the very first multi-user account created, change the |
| 70 // original shortcut to launch with the First User profile. |
| 71 PrefService* local_state = g_browser_process->local_state(); |
| 72 if (local_state->GetInteger(prefs::kProfilesNumCreated) == 2) { |
| 73 string16 default_name = l10n_util::GetStringUTF16( |
| 74 IDS_DEFAULT_PROFILE_NAME); |
| 75 string16 default_directory = |
| 76 CreateProfileShortcutSwitch(UTF8ToUTF16(chrome::kInitialProfile)); |
| 77 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 78 |
| 79 string16 old_shortcut; |
| 80 string16 new_shortcut; |
| 81 if (ShellUtil::GetChromeShortcutName(dist, false, L"", &old_shortcut) && |
| 82 ShellUtil::GetChromeShortcutName(dist, false, default_name, |
| 83 &new_shortcut)) { |
| 84 // Update doesn't allow changing the target, so rename first. |
| 85 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 86 base::Bind(&RenameChromeDesktopShortcutForProfile, |
| 87 old_shortcut, new_shortcut)); |
| 88 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 89 base::Bind(&UpdateChromeDesktopShortcutForProfile, |
| 90 new_shortcut, default_directory)); |
| 91 } |
| 92 } |
| 93 } else { // Only one profile, so create original shortcut. |
| 94 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 95 base::Bind(&CreateChromeDesktopShortcutForProfile, |
| 96 L"", L"", true)); |
| 97 } |
| 98 } |
| 99 |
| 100 void ProfileShortcutManagerWin::OnProfileRemoved( |
| 101 const string16& profile_name) { |
| 102 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 103 string16 shortcut; |
| 104 if (ShellUtil::GetChromeShortcutName(dist, false, profile_name, &shortcut)) { |
| 105 std::vector<string16> shortcuts(1, shortcut); |
| 106 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 107 base::Bind(&CallShellUtilBoolFunction, |
| 108 base::Bind( |
| 109 &ShellUtil::RemoveChromeDesktopShortcutsWithAppendedNames, |
| 110 shortcuts))); |
| 111 } |
| 112 } |
| 113 |
| 114 void ProfileShortcutManagerWin::OnProfileNameChanged( |
| 115 const string16& old_profile_name, |
| 116 const string16& new_profile_name) { |
| 117 // Launch task to change name of desktop shortcut on Windows. |
| 118 // TODO(mirandac): respect master_preferences choice to create no shortcuts |
| 119 // (see http://crbug.com/104463) |
| 120 string16 old_shortcut; |
| 121 string16 new_shortcut; |
| 122 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 123 if (ShellUtil::GetChromeShortcutName( |
| 124 dist, false, old_profile_name, &old_shortcut) && |
| 125 ShellUtil::GetChromeShortcutName( |
| 126 dist, false, new_profile_name, &new_shortcut)) { |
| 127 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 128 base::Bind(&RenameChromeDesktopShortcutForProfile, |
| 129 old_shortcut, |
| 130 new_shortcut)); |
| 131 } |
| 132 } |
| 133 |
| 134 // static |
| 135 std::vector<string16> ProfileShortcutManagerWin::GenerateShortcutsFromProfiles( |
| 136 const std::vector<string16>& profile_names) { |
| 137 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 138 std::vector<string16> shortcuts; |
| 139 shortcuts.reserve(profile_names.size()); |
| 140 for (std::vector<string16>::const_iterator it = profile_names.begin(); |
| 141 it != profile_names.end(); |
| 142 ++it) { |
| 143 string16 shortcut; |
| 144 if (ShellUtil::GetChromeShortcutName(dist, false, *it, &shortcut)) |
| 145 shortcuts.push_back(shortcut); |
| 146 } |
| 147 return shortcuts; |
| 148 } |
| 149 |
| 150 // static |
| 151 void ProfileShortcutManagerWin::CreateChromeDesktopShortcutForProfile( |
| 152 const string16& profile_name, |
| 153 const string16& directory, |
| 154 bool create) { |
| 155 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 156 FilePath chrome_exe; |
| 157 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) |
| 158 return; |
| 159 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 160 string16 description; |
| 161 if (!dist) |
| 162 return; |
| 163 else |
| 164 description = WideToUTF16(dist->GetAppDescription()); |
| 165 ShellUtil::CreateChromeDesktopShortcut( |
| 166 dist, |
| 167 chrome_exe.value(), |
| 168 description, |
| 169 profile_name, |
| 170 directory, |
| 171 ShellUtil::CURRENT_USER, |
| 172 false, // Use alternate text. |
| 173 create); // Create if it doesn't already exist. |
| 174 } |
| 175 |
| 176 // static |
| 177 void ProfileShortcutManagerWin::RenameChromeDesktopShortcutForProfile( |
| 178 const string16& old_shortcut, |
| 179 const string16& new_shortcut) { |
| 180 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 181 FilePath shortcut_path; |
| 182 if (ShellUtil::GetDesktopPath(false, // User's directory instead of system. |
| 183 &shortcut_path)) { |
| 184 FilePath old_profile = shortcut_path.Append(old_shortcut); |
| 185 FilePath new_profile = shortcut_path.Append(new_shortcut); |
| 186 file_util::Move(old_profile, new_profile); |
| 187 } |
| 188 } |
| 189 |
| 190 // static |
| 191 void ProfileShortcutManagerWin::UpdateChromeDesktopShortcutForProfile( |
| 192 const string16& shortcut, |
| 193 const string16& arguments) { |
| 194 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 195 FilePath shortcut_path; |
| 196 if (!ShellUtil::GetDesktopPath(false, &shortcut_path)) |
| 197 return; |
| 198 |
| 199 shortcut_path = shortcut_path.Append(shortcut); |
| 200 FilePath chrome_exe; |
| 201 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) |
| 202 return; |
| 203 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 204 string16 description; |
| 205 if (!dist) |
| 206 return; |
| 207 else |
| 208 description = WideToUTF16(dist->GetAppDescription()); |
| 209 ShellUtil::UpdateChromeShortcut( |
| 210 dist, |
| 211 chrome_exe.value(), |
| 212 shortcut_path.value(), |
| 213 arguments, |
| 214 description, |
| 215 false); |
| 216 } |
OLD | NEW |