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

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: refactor GetProfileNames and add Callback wrapper 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/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 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.
66 PrefService* local_state = g_browser_process->local_state();
67 if (local_state->GetInteger(prefs::kProfilesNumCreated) == 2) {
68 string16 default_name = l10n_util::GetStringUTF16(
69 IDS_DEFAULT_PROFILE_NAME);
70 string16 default_directory =
71 CreateProfileShortcutSwitch(UTF8ToUTF16(chrome::kInitialProfile));
72 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
73
74 string16 old_shortcut;
75 string16 new_shortcut;
76 if (ShellUtil::GetChromeShortcutName(dist, false, L"", &old_shortcut) &&
77 ShellUtil::GetChromeShortcutName(dist, false, default_name,
78 &new_shortcut)) {
79 // Update doesn't allow changing the target, so rename first.
80 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
81 base::Bind(&RenameChromeDesktopShortcutForProfile,
82 old_shortcut, new_shortcut));
83 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
84 base::Bind(&UpdateChromeDesktopShortcutForProfile,
85 new_shortcut, default_directory));
86 }
87 }
88 } else { // Only one profile, so create original shortcut.
Robert Sesek 2011/11/18 14:13:43 nit: two spaces before comment, or push to next li
Miranda Callahan 2011/11/18 19:00:36 Done.
89 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
90 base::Bind(&CreateChromeDesktopShortcutForProfile,
91 L"", L"", true));
92 }
93 }
94
95 void ProfileShortcutManagerWin::OnProfileRemoved(string16 profile_name) {
96 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
97 string16 shortcut;
98 if (ShellUtil::GetChromeShortcutName(dist, false, profile_name, &shortcut)) {
99 std::vector<string16> shortcuts;
grt (UTC plus 2) 2011/11/18 16:02:15 construct the vector with the one shortcut in it:
Miranda Callahan 2011/11/18 19:00:36 Done.
100 shortcuts.push_back(shortcut);
grt (UTC plus 2) 2011/11/18 16:02:15 remove this line.
Miranda Callahan 2011/11/18 19:00:36 Done.
101 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
102 base::Bind(&CallShellUtilBoolFunction,
103 base::Bind(
104 &ShellUtil::RemoveChromeDesktopShortcutsWithAppendedNames,
105 shortcuts)));
106 }
107 }
108
109 void ProfileShortcutManagerWin::OnProfileNameChanged(
110 string16 old_profile_name,
111 string16 new_profile_name) {
112 // Launch task to change name of desktop shortcut on Windows.
113 // TODO(mirandac): respect master_preferences choice to create no shortcuts
114 // (see http://crbug.com/104463)
115 string16 old_shortcut;
116 string16 new_shortcut;
117 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
118 if (ShellUtil::GetChromeShortcutName(
119 dist, false, old_profile_name, &old_shortcut) &&
120 ShellUtil::GetChromeShortcutName(
121 dist, false, new_profile_name, &new_shortcut)) {
122 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
123 base::Bind(&RenameChromeDesktopShortcutForProfile,
124 old_shortcut,
125 new_shortcut));
126 }
127 }
128
129 // static
130 void ProfileShortcutManagerWin::CallShellUtilBoolFunction(
131 const base::Callback<bool(void)>& bool_function) {
132 bool_function.Run();
133 }
134
135 // static
136 std::vector<string16> ProfileShortcutManagerWin::GenerateShortcutsFromProfiles(
137 std::vector<string16> profile_names) {
138 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
139 std::vector<string16> shortcuts;
140 for (std::vector<string16>::iterator it = profile_names.begin();
grt (UTC plus 2) 2011/11/18 16:02:15 insert "shortcuts.reserve(profile_names.size());"
grt (UTC plus 2) 2011/11/18 16:02:15 const_iterator
Miranda Callahan 2011/11/18 19:00:36 Done.
Miranda Callahan 2011/11/18 19:00:36 Done.
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 string16 profile_name, string16 directory, bool create) {
153 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
154 FilePath chrome_exe;
155 if (!PathService::Get(base::FILE_EXE, &chrome_exe))
156 return;
157 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
158 string16 description;
159 if (!dist)
160 return;
161 else
162 description = WideToUTF16(dist->GetAppDescription());
163 ShellUtil::CreateChromeDesktopShortcut(
164 dist,
165 chrome_exe.value(),
166 description,
167 profile_name,
168 directory,
169 ShellUtil::CURRENT_USER,
170 false, // Use alternate text.
171 create); // Create if it doesn't already exist.
172 }
173
174 // static
175 void ProfileShortcutManagerWin::RenameChromeDesktopShortcutForProfile(
176 string16 old_shortcut,
177 string16 new_shortcut) {
178 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
179 FilePath shortcut_path;
180 if (ShellUtil::GetDesktopPath(false, // User's directory instead of system.
181 &shortcut_path)) {
182 FilePath old_profile = shortcut_path.Append(old_shortcut);
183 FilePath new_profile = shortcut_path.Append(new_shortcut);
184 file_util::Move(old_profile, new_profile);
185 }
186 }
187
188 // static
189 void ProfileShortcutManagerWin::UpdateChromeDesktopShortcutForProfile(
190 string16 shortcut,
191 string16 arguments) {
192 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
193 FilePath shortcut_path;
194 if (!ShellUtil::GetDesktopPath(false, &shortcut_path))
195 return;
196
197 shortcut_path = shortcut_path.Append(shortcut);
198 FilePath chrome_exe;
199 if (!PathService::Get(base::FILE_EXE, &chrome_exe))
200 return;
201 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
202 string16 description;
203 if (!dist)
204 return;
205 else
206 description = WideToUTF16(dist->GetAppDescription());
207 ShellUtil::UpdateChromeShortcut(
208 dist,
209 chrome_exe.value(),
210 UTF8ToUTF16(shortcut_path.MaybeAsASCII()),
grt (UTC plus 2) 2011/11/18 16:02:15 MaybeAsASCII() is bad since shortcuts are localize
Miranda Callahan 2011/11/18 19:00:36 Done.
211 arguments,
212 description,
213 false);
214 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698