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

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

Issue 10823217: Create/Delete windows profile shortcuts (Closed) Base URL: https://src.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 4 months 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/profiles/profile_shortcut_manager_win.h" 5 #include "chrome/browser/profiles/profile_shortcut_manager.h"
6
7 #include <map>
6 8
7 #include "base/bind.h" 9 #include "base/bind.h"
8 #include "base/file_path.h" 10 #include "base/command_line.h"
9 #include "base/file_util.h" 11 #include "base/file_util.h"
10 #include "base/path_service.h" 12 #include "base/path_service.h"
11 #include "base/stringprintf.h" 13 #include "base/stringprintf.h"
12 #include "base/utf_string_conversions.h" 14 #include "base/utf_string_conversions.h"
13 #include "chrome/browser/app_icon_win.h" 15 #include "chrome/browser/app_icon_win.h"
14 #include "chrome/browser/browser_process.h" 16 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/prefs/pref_service.h" 17 #include "chrome/browser/prefs/pref_service.h"
16 #include "chrome/browser/profiles/profile_info_cache.h" 18 #include "chrome/browser/profiles/profile_info_cache.h"
17 #include "chrome/browser/profiles/profile_info_util.h" 19 #include "chrome/browser/profiles/profile_info_util.h"
18 #include "chrome/browser/profiles/profile_manager.h" 20 #include "chrome/browser/profiles/profile_manager.h"
19 #include "chrome/common/chrome_constants.h" 21 #include "chrome/common/chrome_constants.h"
20 #include "chrome/common/chrome_switches.h" 22 #include "chrome/common/chrome_switches.h"
21 #include "chrome/common/pref_names.h" 23 #include "chrome/common/pref_names.h"
22 #include "chrome/installer/util/auto_launch_util.h" 24 #include "chrome/installer/util/auto_launch_util.h"
23 #include "chrome/installer/util/browser_distribution.h" 25 #include "chrome/installer/util/browser_distribution.h"
24 #include "chrome/installer/util/shell_util.h" 26 #include "chrome/installer/util/shell_util.h"
25 #include "content/public/browser/browser_thread.h" 27 #include "content/public/browser/browser_thread.h"
26 #include "grit/generated_resources.h" 28 #include "grit/generated_resources.h"
27 #include "skia/ext/image_operations.h" 29 #include "skia/ext/image_operations.h"
28 #include "skia/ext/platform_canvas.h" 30 #include "skia/ext/platform_canvas.h"
29 #include "ui/base/l10n/l10n_util.h" 31 #include "ui/base/l10n/l10n_util.h"
30 #include "ui/base/resource/resource_bundle.h" 32 #include "ui/base/resource/resource_bundle.h"
31 #include "ui/gfx/icon_util.h" 33 #include "ui/gfx/icon_util.h"
32 #include "ui/gfx/image/image.h"
33 34
34 using content::BrowserThread; 35 using content::BrowserThread;
35 36
36 namespace { 37 namespace {
37 38
38 const char kProfileIconFileName[] = "Google Profile.ico"; 39 const char kProfileIconFileName[] = "Google Profile.ico";
39 const int kProfileAvatarShortcutBadgeWidth = 28; 40 const int kProfileAvatarShortcutBadgeWidth = 28;
40 const int kProfileAvatarShortcutBadgeHeight = 28; 41 const int kProfileAvatarShortcutBadgeHeight = 28;
41 const int kShortcutIconSize = 48; 42 const int kShortcutIconSize = 48;
42 43
43 // Creates the argument to pass to the Windows executable that launches Chrome 44 struct ProfileShortcutInfo {
44 // with the profile in |profile_base_dir|. 45 string16 flags;
45 // For example: --profile-directory="Profile 2" 46 string16 profile_name;
46 string16 CreateProfileShortcutSwitch(const string16& profile_base_dir) { 47 gfx::Image avatar_image;
47 return base::StringPrintf(L"--%ls=\"%ls\"",
48 ASCIIToUTF16(switches::kProfileDirectory).c_str(),
49 profile_base_dir.c_str());
50 }
51 48
52 // Wrap a ShellUtil function that returns a bool so it can be posted in a 49 ProfileShortcutInfo() : flags(string16()), profile_name(string16()),
53 // task to the FILE thread. 50 avatar_image(gfx::Image()) {
54 void CallShellUtilBoolFunction( 51 }
55 const base::Callback<bool(void)>& bool_function) { 52
56 bool_function.Run(); 53 ProfileShortcutInfo(string16 p_flags, string16 p_profile_name,
57 } 54 gfx::Image p_avatar_image) : flags(p_flags),
55 profile_name(p_profile_name), avatar_image(p_avatar_image) {
56 }
57 };
58
59 typedef std::map<FilePath, ProfileShortcutInfo> ProfileShortcutsMap;
60 ProfileShortcutsMap profile_shortcuts_;
sail 2012/08/14 20:53:17 this shouldn't be a global variable Also, I don't
Halli 2012/08/14 21:24:19 Added TODO for map and moved profile_shortcuts_ in
58 61
59 // Creates a desktop shortcut icon file (.ico) on the disk for a given profile, 62 // Creates a desktop shortcut icon file (.ico) on the disk for a given profile,
60 // badging the browser distribution icon with the profile avatar. 63 // badging the browser distribution icon with the profile avatar.
61 // |profile_base_dir| is the base directory (and key) of the profile. Returns 64 // |profile_base_dir| is the base directory (and key) of the profile. Returns
62 // a path to the shortcut icon file on disk, which is empty if this fails. 65 // a path to the shortcut icon file on disk, which is empty if this fails.
63 // Use index 0 when assigning the resulting file as the icon. 66 // Use index 0 when assigning the resulting file as the icon.
64 FilePath CreateChromeDesktopShortcutIconForProfile( 67 FilePath CreateChromeDesktopShortcutIconForProfile(
65 const FilePath& profile_path, 68 const FilePath& profile_path,
66 const gfx::Image& avatar_image) { 69 const gfx::Image& avatar_image) {
67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 70 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
68 const SkBitmap* avatar_bitmap = avatar_image.ToSkBitmap(); 71 const SkBitmap* avatar_bitmap = avatar_image.ToSkBitmap();
69 HICON app_icon_handle = GetAppIconForSize(kShortcutIconSize); 72 HICON app_icon_handle = GetAppIconForSize(kShortcutIconSize);
70 scoped_ptr<SkBitmap> app_icon_bitmap( 73 scoped_ptr<SkBitmap> app_icon_bitmap(
71 IconUtil::CreateSkBitmapFromHICON(app_icon_handle)); 74 IconUtil::CreateSkBitmapFromHICON(app_icon_handle));
72 DestroyIcon(app_icon_handle); 75 DestroyIcon(app_icon_handle);
73 if (!app_icon_bitmap.get()) 76 if (!app_icon_bitmap.get())
74 return FilePath(); 77 return FilePath();
75 78
76 // TODO(stevet): Share this chunk of code with 79 // TODO(hallielaine): Share this chunk of code with
77 // avatar_menu_button::DrawTaskBarDecoration. 80 // avatar_menu_button::DrawTaskBarDecoration.
78 const SkBitmap* source_bitmap = NULL; 81 const SkBitmap* source_bitmap = NULL;
79 SkBitmap squarer_bitmap; 82 SkBitmap squarer_bitmap;
80 if ((avatar_bitmap->width() == profiles::kAvatarIconWidth) && 83 if ((avatar_bitmap->width() == profiles::kAvatarIconWidth) &&
81 (avatar_bitmap->height() == profiles::kAvatarIconHeight)) { 84 (avatar_bitmap->height() == profiles::kAvatarIconHeight)) {
82 // Shave a couple of columns so the bitmap is more square. So when 85 // Shave a couple of columns so the bitmap is more square. So when
83 // resized to a square aspect ratio it looks pretty. 86 // resized to a square aspect ratio it looks pretty.
84 int x = 2; 87 int x = 2;
85 avatar_bitmap->extractSubset(&squarer_bitmap, SkIRect::MakeXYWH(x, 0, 88 avatar_bitmap->extractSubset(&squarer_bitmap, SkIRect::MakeXYWH(x, 0,
86 profiles::kAvatarIconWidth - x * 2, profiles::kAvatarIconHeight)); 89 profiles::kAvatarIconWidth - x * 2, profiles::kAvatarIconHeight));
(...skipping 23 matching lines...) Expand all
110 offscreen_canvas->getDevice()->accessBitmap(false); 113 offscreen_canvas->getDevice()->accessBitmap(false);
111 114
112 // Finally, write the .ico file containing this new bitmap. 115 // Finally, write the .ico file containing this new bitmap.
113 FilePath icon_path = profile_path.AppendASCII(kProfileIconFileName); 116 FilePath icon_path = profile_path.AppendASCII(kProfileIconFileName);
114 if (!IconUtil::CreateIconFileFromSkBitmap(final_bitmap, icon_path)) 117 if (!IconUtil::CreateIconFileFromSkBitmap(final_bitmap, icon_path))
115 return FilePath(); 118 return FilePath();
116 119
117 return icon_path; 120 return icon_path;
118 } 121 }
119 122
120 // Creates a desktop shortcut to open Chrome with the given profile name and 123 string16 CreateProfileShortcutFlags(const FilePath& profile_path) {
121 // base directory. Iff |create|, create shortcut if it doesn't already exist. 124 return base::StringPrintf(L"--%ls=\"%ls\"",
122 // Must be called on the FILE thread. 125 ASCIIToUTF16(switches::kProfileDirectory).c_str(),
123 void CreateChromeDesktopShortcutForProfile( 126 profile_path.BaseName().value().c_str());
124 const string16& profile_name,
125 const string16& profile_base_dir,
126 const FilePath& profile_path,
127 const gfx::Image* avatar_image,
128 bool create) {
129 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
130 FilePath chrome_exe;
131 if (!PathService::Get(base::FILE_EXE, &chrome_exe))
132 return;
133 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
134 string16 description;
135 if (!dist)
136 return;
137 else
138 description = WideToUTF16(dist->GetAppDescription());
139 const string16& directory = CreateProfileShortcutSwitch(profile_base_dir);
140 FilePath icon_path = avatar_image ?
141 CreateChromeDesktopShortcutIconForProfile(profile_path, *avatar_image) :
142 FilePath();
143
144 ShellUtil::CreateChromeDesktopShortcut(
145 dist,
146 chrome_exe.value(),
147 description,
148 profile_name,
149 directory,
150 icon_path.empty() ? chrome_exe.value() : icon_path.value(),
151 icon_path.empty() ? dist->GetIconIndex() : 0,
152 ShellUtil::CURRENT_USER,
153 create ? ShellUtil::SHORTCUT_CREATE_ALWAYS :
154 ShellUtil::SHORTCUT_NO_OPTIONS);
155 } 127 }
156 128
157 // Renames an existing Chrome desktop profile shortcut. Must be called on the 129 // Wrap a ShellUtil function that returns a bool so it can be posted in a
158 // FILE thread. 130 // task to the FILE thread.
159 void RenameChromeDesktopShortcutForProfile( 131 void CallShellUtilBoolFunction(
160 const string16& old_shortcut, 132 const base::Callback<bool(void)>& bool_function) {
161 const string16& new_shortcut) { 133 bool_function.Run();
162 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
163 FilePath shortcut_path;
164 if (ShellUtil::GetDesktopPath(false, // User's directory instead of system.
165 &shortcut_path)) {
166 FilePath old_profile = shortcut_path.Append(old_shortcut);
167 FilePath new_profile = shortcut_path.Append(new_shortcut);
168 file_util::Move(old_profile, new_profile);
169 }
170 }
171
172 // Updates the arguments to a Chrome desktop shortcut for a profile. Must be
173 // called on the FILE thread.
174 void UpdateChromeDesktopShortcutForProfile(
175 const string16& shortcut,
176 const string16& arguments,
177 const FilePath& profile_path,
178 const gfx::Image* avatar_image) {
179 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
180 FilePath shortcut_path;
181 if (!ShellUtil::GetDesktopPath(false, &shortcut_path))
182 return;
183
184 shortcut_path = shortcut_path.Append(shortcut);
185 FilePath chrome_exe;
186 if (!PathService::Get(base::FILE_EXE, &chrome_exe))
187 return;
188 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
189 string16 description;
190 if (!dist)
191 return;
192 else
193 description = WideToUTF16(dist->GetAppDescription());
194 FilePath icon_path = avatar_image ?
195 CreateChromeDesktopShortcutIconForProfile(profile_path, *avatar_image) :
196 FilePath();
197
198 ShellUtil::UpdateChromeShortcut(
199 dist,
200 chrome_exe.value(),
201 shortcut_path.value(),
202 arguments,
203 description,
204 icon_path.empty() ? chrome_exe.value() : icon_path.value(),
205 icon_path.empty() ? dist->GetIconIndex() : 0,
206 ShellUtil::SHORTCUT_NO_OPTIONS);
207 }
208
209 void DeleteAutoLaunchValueForProfile(
210 const FilePath& profile_path) {
211 if (auto_launch_util::AutoStartRequested(profile_path.BaseName().value(),
212 true, // Window requested.
213 FilePath())) {
214 auto_launch_util::DisableForegroundStartAtLogin(
215 profile_path.BaseName().value());
216 }
217 } 134 }
218 135
219 } // namespace 136 } // namespace
220 137
138 class ProfileShortcutManagerWin : public ProfileShortcutManager {
139 public:
140 ProfileShortcutManagerWin();
141 ~ProfileShortcutManagerWin();
142
143 virtual void CreateChromeDesktopShortcut(
144 const FilePath& profile_path, const string16& profile_name,
145 const gfx::Image& avatar_image);
sail 2012/08/14 20:53:17 need OVERRIDE here and below
Halli 2012/08/14 21:24:19 Done.
146 virtual void DeleteChromeDesktopShortcut(const FilePath& profile_path);
147 };
148
149 // static
150 bool ProfileShortcutManager::IsFeatureEnabled() {
151 if (CommandLine::ForCurrentProcess()->HasSwitch(
152 switches::kProfileDesktopShortcuts)) {
153 return true;
154 }
155 return false;
156 }
157
158 // static
159 ProfileShortcutManager* ProfileShortcutManager::Create() {
160 return new ProfileShortcutManagerWin();
161 }
162
221 ProfileShortcutManagerWin::ProfileShortcutManagerWin() { 163 ProfileShortcutManagerWin::ProfileShortcutManagerWin() {
222 } 164 }
223 165
224 ProfileShortcutManagerWin::~ProfileShortcutManagerWin() { 166 ProfileShortcutManagerWin::~ProfileShortcutManagerWin() {
225 } 167 }
226 168
227 void ProfileShortcutManagerWin::AddProfileShortcut( 169 void ProfileShortcutManagerWin::CreateChromeDesktopShortcut(
170 const FilePath& profile_path, const string16& profile_name,
171 const gfx::Image& avatar_image) {
172 FilePath shortcut_icon = CreateChromeDesktopShortcutIconForProfile(
173 profile_path, avatar_image);
174 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
175 FilePath chrome_exe;
176 if (!PathService::Get(base::FILE_EXE, &chrome_exe))
177 return;
178 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
179 string16 description;
180 if (!dist)
181 return;
182 else
sail 2012/08/14 20:53:17 no need for else
Halli 2012/08/14 21:24:19 Done.
183 description = WideToUTF16(dist->GetAppDescription());
184
185 // Add the profile to the map if it doesn't exist already
186 if (!profile_shortcuts_.count(profile_path)) {
187 string16 flags = CreateProfileShortcutFlags(profile_path);
188 profile_shortcuts_.insert(std::make_pair(profile_path,
189 ProfileShortcutInfo(flags, profile_name,
190 avatar_image)));
191 }
192
193 ShellUtil::CreateChromeDesktopShortcut(
194 dist,
195 chrome_exe.value(),
196 description,
197 profile_shortcuts_[profile_path].profile_name,
198 profile_shortcuts_[profile_path].flags,
199 shortcut_icon.empty() ? chrome_exe.value() : shortcut_icon.value(),
200 shortcut_icon.empty() ? dist->GetIconIndex() : 0,
201 ShellUtil::CURRENT_USER,
202 ShellUtil::SHORTCUT_CREATE_ALWAYS);
203 }
204
205 void ProfileShortcutManagerWin::DeleteChromeDesktopShortcut(
228 const FilePath& profile_path) { 206 const FilePath& profile_path) {
229 ProfileInfoCache& cache = 207 // If the profile to be deleted has a shortcut
230 g_browser_process->profile_manager()->GetProfileInfoCache(); 208 if (profile_shortcuts_.count(profile_path)) {
sail 2012/08/14 20:53:17 I don't think you need this if statement at all, j
Halli 2012/08/14 21:24:19 Done.
231 size_t index = cache.GetIndexOfProfileWithPath(profile_path); 209 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
232 if (index == std::string::npos) 210 string16 shortcut;
233 return; 211 // If we can find the shortcut, delete it
234 212 if (ShellUtil::GetChromeShortcutName(dist, false,
235 // Launch task to add shortcut to desktop on Windows. If this is the very 213 profile_shortcuts_[profile_path].profile_name, &shortcut)) {
236 // first profile created, don't add the user name to the shortcut. 214 std::vector<string16> appended_names(1, shortcut);
237 // TODO(mirandac): respect master_preferences choice to create no shortcuts 215 BrowserThread::PostTask(
238 // (see http://crbug.com/104463) 216 BrowserThread::FILE, FROM_HERE,
239 if (g_browser_process->profile_manager()->GetNumberOfProfiles() > 1) { 217 base::Bind(&CallShellUtilBoolFunction, base::Bind(
240 { 218 &ShellUtil::RemoveChromeDesktopShortcutsWithAppendedNames,
241 // We make a copy of the Image to ensure that the underlying image data is 219 appended_names)));
242 // AddRef'd, in case the original copy gets deleted. 220 profile_shortcuts_.erase(profile_path);
243 gfx::Image* avatar_copy =
244 new gfx::Image(cache.GetAvatarIconOfProfileAtIndex(index));
245 string16 profile_name = cache.GetNameOfProfileAtIndex(index);
246 string16 profile_base_dir = profile_path.BaseName().value();
247 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
248 base::Bind(&CreateChromeDesktopShortcutForProfile,
249 profile_name, profile_base_dir, profile_path,
250 base::Owned(avatar_copy), true));
251 } 221 }
252
253 // If this is the second existing multi-user account created, change the
254 // original shortcut use the first profile's details (name, badge,
255 // argument).
256 if (cache.GetNumberOfProfiles() == 2) {
257 // Get the index of the first profile, based on the index of the second
258 // profile. It's either 0 or 1, whichever the second profile isn't.
259 size_t first_index = 0;
260 if (cache.GetIndexOfProfileWithPath(profile_path) == 0)
261 first_index = 1;
262 string16 first_name = cache.GetNameOfProfileAtIndex(first_index);
263 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
264
265 string16 old_shortcut;
266 string16 new_shortcut;
267 if (ShellUtil::GetChromeShortcutName(dist, false, L"", &old_shortcut) &&
268 ShellUtil::GetChromeShortcutName(dist, false, first_name,
269 &new_shortcut)) {
270 // Update doesn't allow changing the target, so rename first.
271 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
272 base::Bind(&RenameChromeDesktopShortcutForProfile,
273 old_shortcut, new_shortcut));
274
275 // Fetch the avatar for the first profile and make a copy of the Image
276 // to ensure that the underlying image data is AddRef'd, in case the
277 // original copy is deleted.
278 gfx::Image& first_avatar =
279 ResourceBundle::GetSharedInstance().GetNativeImageNamed(
280 ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(
281 cache.GetAvatarIconIndexOfProfileAtIndex(first_index)));
282 gfx::Image* first_avatar_copy = new gfx::Image(first_avatar);
283 FilePath first_path = cache.GetPathOfProfileAtIndex(first_index);
284
285 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
286 base::Bind(&UpdateChromeDesktopShortcutForProfile,
287 new_shortcut,
288 CreateProfileShortcutSwitch(UTF8ToUTF16(
289 first_path.BaseName().MaybeAsASCII())),
290 first_path,
291 base::Owned(first_avatar_copy)));
292 }
293 }
294 } else { // Only one profile, so create original shortcut, with no avatar.
295 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
296 base::Bind(&CreateChromeDesktopShortcutForProfile,
297 string16(), string16(), FilePath(),
298 static_cast<gfx::Image*>(NULL), true));
299 } 222 }
300 } 223 }
301 224
302 void ProfileShortcutManagerWin::OnProfileAdded(
303 const FilePath& profile_path) {
304 }
305
306 void ProfileShortcutManagerWin::OnProfileWillBeRemoved(
307 const FilePath& profile_path) {
308 ProfileInfoCache& cache =
309 g_browser_process->profile_manager()->GetProfileInfoCache();
310 size_t index = cache.GetIndexOfProfileWithPath(profile_path);
311 if (index == std::string::npos)
312 return;
313 string16 profile_name = cache.GetNameOfProfileAtIndex(index);
314 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
315 string16 shortcut;
316 if (ShellUtil::GetChromeShortcutName(dist, false, profile_name, &shortcut)) {
317 std::vector<string16> shortcuts(1, shortcut);
318 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
319 base::Bind(&CallShellUtilBoolFunction,
320 base::Bind(
321 &ShellUtil::RemoveChromeDesktopShortcutsWithAppendedNames,
322 shortcuts)));
323 }
324 }
325
326 void ProfileShortcutManagerWin::OnProfileWasRemoved(
327 const FilePath& profile_path,
328 const string16& profile_name) {
329 BrowserThread::PostTask(
330 BrowserThread::FILE, FROM_HERE,
331 base::Bind(&DeleteAutoLaunchValueForProfile, profile_path));
332
333 // If there is one profile left, we want to remove the badge and name from it.
334 ProfileInfoCache& cache =
335 g_browser_process->profile_manager()->GetProfileInfoCache();
336 if (cache.GetNumberOfProfiles() != 1)
337 return;
338
339 FilePath last_profile_path = cache.GetPathOfProfileAtIndex(0);
340 string16 old_shortcut;
341 string16 new_shortcut;
342 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
343 if (ShellUtil::GetChromeShortcutName(
344 dist, false, cache.GetNameOfProfileAtIndex(0), &old_shortcut) &&
345 ShellUtil::GetChromeShortcutName(
346 dist, false, L"", &new_shortcut)) {
347 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
348 base::Bind(&RenameChromeDesktopShortcutForProfile,
349 old_shortcut,
350 new_shortcut));
351 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
352 base::Bind(&UpdateChromeDesktopShortcutForProfile,
353 new_shortcut,
354 CreateProfileShortcutSwitch(UTF8ToUTF16(
355 last_profile_path.BaseName().MaybeAsASCII())),
356 last_profile_path,
357 static_cast<gfx::Image*>(NULL)));
358 }
359 }
360
361 void ProfileShortcutManagerWin::OnProfileNameChanged(
362 const FilePath& profile_path,
363 const string16& old_profile_name) {
364 // Launch task to change name of desktop shortcut on Windows.
365 // TODO(mirandac): respect master_preferences choice to create no shortcuts
366 // (see http://crbug.com/104463)
367 ProfileInfoCache& cache =
368 g_browser_process->profile_manager()->GetProfileInfoCache();
369 size_t index = cache.GetIndexOfProfileWithPath(profile_path);
370 if (index == std::string::npos)
371 return;
372 string16 new_profile_name = cache.GetNameOfProfileAtIndex(index);
373
374 string16 old_shortcut;
375 string16 new_shortcut;
376 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
377 if (ShellUtil::GetChromeShortcutName(
378 dist, false, old_profile_name, &old_shortcut) &&
379 ShellUtil::GetChromeShortcutName(
380 dist, false, new_profile_name, &new_shortcut)) {
381 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
382 base::Bind(&RenameChromeDesktopShortcutForProfile,
383 old_shortcut,
384 new_shortcut));
385 }
386 }
387
388 void ProfileShortcutManagerWin::OnProfileAvatarChanged(
389 const FilePath& profile_path) {
390 ProfileInfoCache& cache =
391 g_browser_process->profile_manager()->GetProfileInfoCache();
392 size_t index = cache.GetIndexOfProfileWithPath(profile_path);
393 if (index == std::string::npos)
394 return;
395 string16 profile_name = cache.GetNameOfProfileAtIndex(index);
396 string16 profile_base_dir =
397 UTF8ToUTF16(profile_path.BaseName().MaybeAsASCII());
398 const gfx::Image& avatar_image = cache.GetAvatarIconOfProfileAtIndex(index);
399
400 // Launch task to change the icon of the desktop shortcut on windows.
401 string16 new_shortcut;
402 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
403 if (ShellUtil::GetChromeShortcutName(dist, false, profile_name,
404 &new_shortcut)) {
405 // We make a copy of the Image to ensure that the underlying image data is
406 // AddRef'd, in case the original copy gets deleted.
407 gfx::Image* avatar_copy = new gfx::Image(avatar_image);
408 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
409 base::Bind(&UpdateChromeDesktopShortcutForProfile,
410 new_shortcut,
411 CreateProfileShortcutSwitch(profile_base_dir),
412 profile_path,
413 base::Owned(avatar_copy)));
414 }
415 }
416
417 // static
418 std::vector<string16> ProfileShortcutManagerWin::GenerateShortcutsFromProfiles(
419 const std::vector<string16>& profile_names) {
420 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
421 std::vector<string16> shortcuts;
422 shortcuts.reserve(profile_names.size());
423 for (std::vector<string16>::const_iterator it = profile_names.begin();
424 it != profile_names.end();
425 ++it) {
426 string16 shortcut;
427 if (ShellUtil::GetChromeShortcutName(dist, false, *it, &shortcut))
428 shortcuts.push_back(shortcut);
429 }
430 return shortcuts;
431 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698