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

Side by Side Diff: chrome/browser/profiles/profile_shortcut_manager_win.cc

Issue 8502033: Add Windows desktop shortcut for multiple profiles. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: fix install.cc Created 9 years, 1 month 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 | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(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(
35 L"%ls%ls%ls%ls%ls",
36 L"--",
37 UTF8ToUTF16(std::string(switches::kProfileDirectory)).c_str(),
38 L"=\"",
grt (UTC plus 2) 2011/11/18 20:02:54 base::StringPrintf(L"--%ls=\"%ls\"", ASCIIToUTF16(
Miranda Callahan 2011/11/18 22:41:33 :-) Done.
39 profile_base_dir.c_str(),
40 L"\"");
41 return profile_directory;
42 }
43
44 // Wrap a ShellUtil function that returns a bool so it can be posted in a
45 // task to the FILE thread.
46 void CallShellUtilBoolFunction(
47 const base::Callback<bool(void)>& bool_function) {
48 bool_function.Run();
49 }
50
51 } // namespace
52
53 ProfileShortcutManagerWin::ProfileShortcutManagerWin() {
54 }
55
56 ProfileShortcutManagerWin::~ProfileShortcutManagerWin() {
57 }
58
59 void ProfileShortcutManagerWin::OnProfileAdded(
60 const string16& profile_name,
61 const string16& profile_base_dir) {
62 // Launch task to add shortcut to desktop on Windows. If this is the very
63 // first profile created, don't add the user name to the shortcut.
64 // TODO(mirandac): respect master_preferences choice to create no shortcuts
65 // (see http://crbug.com/104463)
66 if (g_browser_process->profile_manager()->GetNumberOfProfiles() > 1) {
67 string16 profile_directory =
68 CreateProfileShortcutSwitch(profile_base_dir);
69 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
70 base::Bind(&CreateChromeDesktopShortcutForProfile,
71 profile_name, profile_directory, true));
72
73 // If this is the very first multi-user account created, change the
74 // original shortcut to launch with the First User profile.
75 PrefService* local_state = g_browser_process->local_state();
76 if (local_state->GetInteger(prefs::kProfilesNumCreated) == 2) {
77 string16 default_name = l10n_util::GetStringUTF16(
78 IDS_DEFAULT_PROFILE_NAME);
79 string16 default_directory =
80 CreateProfileShortcutSwitch(UTF8ToUTF16(chrome::kInitialProfile));
81 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
82
83 string16 old_shortcut;
84 string16 new_shortcut;
85 if (ShellUtil::GetChromeShortcutName(dist, false, L"", &old_shortcut) &&
86 ShellUtil::GetChromeShortcutName(dist, false, default_name,
87 &new_shortcut)) {
88 // Update doesn't allow changing the target, so rename first.
89 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
90 base::Bind(&RenameChromeDesktopShortcutForProfile,
91 old_shortcut, new_shortcut));
92 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
93 base::Bind(&UpdateChromeDesktopShortcutForProfile,
94 new_shortcut, default_directory));
95 }
96 }
97 } else { // Only one profile, so create original shortcut.
98 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
99 base::Bind(&CreateChromeDesktopShortcutForProfile,
100 L"", L"", true));
101 }
102 }
103
104 void ProfileShortcutManagerWin::OnProfileRemoved(
105 const string16& profile_name) {
106 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
107 string16 shortcut;
108 if (ShellUtil::GetChromeShortcutName(dist, false, profile_name, &shortcut)) {
109 std::vector<string16> shortcuts(1, shortcut);
110 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
111 base::Bind(&CallShellUtilBoolFunction,
112 base::Bind(
113 &ShellUtil::RemoveChromeDesktopShortcutsWithAppendedNames,
114 shortcuts)));
115 }
116 }
117
118 void ProfileShortcutManagerWin::OnProfileNameChanged(
119 const string16& old_profile_name,
120 const string16& new_profile_name) {
121 // Launch task to change name of desktop shortcut on Windows.
122 // TODO(mirandac): respect master_preferences choice to create no shortcuts
123 // (see http://crbug.com/104463)
124 string16 old_shortcut;
125 string16 new_shortcut;
126 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
127 if (ShellUtil::GetChromeShortcutName(
128 dist, false, old_profile_name, &old_shortcut) &&
129 ShellUtil::GetChromeShortcutName(
130 dist, false, new_profile_name, &new_shortcut)) {
131 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
132 base::Bind(&RenameChromeDesktopShortcutForProfile,
133 old_shortcut,
134 new_shortcut));
135 }
136 }
137
138 // static
139 std::vector<string16> ProfileShortcutManagerWin::GenerateShortcutsFromProfiles(
140 std::vector<string16> profile_names) {
141 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
142 std::vector<string16> shortcuts;
143 shortcuts.reserve(profile_names.size());
144 for (std::vector<string16>::const_iterator it = profile_names.begin();
145 it != profile_names.end();
146 ++it) {
147 string16 shortcut;
148 if (ShellUtil::GetChromeShortcutName(dist, false, *it, &shortcut))
149 shortcuts.push_back(shortcut);
150 }
151 return shortcuts;
152 }
153
154 // static
155 void ProfileShortcutManagerWin::CreateChromeDesktopShortcutForProfile(
156 const string16& profile_name, const string16& directory, bool create) {
grt (UTC plus 2) 2011/11/18 20:02:54 break into three lines
Miranda Callahan 2011/11/18 22:41:33 Done.
157 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
158 FilePath chrome_exe;
159 if (!PathService::Get(base::FILE_EXE, &chrome_exe))
160 return;
161 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
162 string16 description;
163 if (!dist)
164 return;
165 else
166 description = WideToUTF16(dist->GetAppDescription());
167 ShellUtil::CreateChromeDesktopShortcut(
168 dist,
169 chrome_exe.value(),
170 description,
171 profile_name,
172 directory,
173 ShellUtil::CURRENT_USER,
174 false, // Use alternate text.
175 create); // Create if it doesn't already exist.
176 }
177
178 // static
179 void ProfileShortcutManagerWin::RenameChromeDesktopShortcutForProfile(
180 const string16& old_shortcut, const string16& new_shortcut) {
grt (UTC plus 2) 2011/11/18 20:02:54 break into three lines
Miranda Callahan 2011/11/18 22:41:33 would you settle for two? :-)
grt (UTC plus 2) 2011/11/21 15:21:18 uh, yeah, that's the ticket. :-)
181 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
182 FilePath shortcut_path;
183 if (ShellUtil::GetDesktopPath(false, // User's directory instead of system.
184 &shortcut_path)) {
185 FilePath old_profile = shortcut_path.Append(old_shortcut);
186 FilePath new_profile = shortcut_path.Append(new_shortcut);
187 file_util::Move(old_profile, new_profile);
188 }
189 }
190
191 // static
192 void ProfileShortcutManagerWin::UpdateChromeDesktopShortcutForProfile(
193 const string16& shortcut, const string16& arguments) {
grt (UTC plus 2) 2011/11/18 20:02:54 break into two lines
Miranda Callahan 2011/11/18 22:41:33 Done.
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 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698