OLD | NEW |
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 // with the profile in |profile_base_dir|. | |
45 // For example: --profile-directory="Profile 2" | |
46 string16 CreateProfileShortcutSwitch(const string16& profile_base_dir) { | |
47 return base::StringPrintf(L"--%ls=\"%ls\"", | |
48 ASCIIToUTF16(switches::kProfileDirectory).c_str(), | |
49 profile_base_dir.c_str()); | |
50 } | |
51 | |
52 // Wrap a ShellUtil function that returns a bool so it can be posted in a | |
53 // task to the FILE thread. | |
54 void CallShellUtilBoolFunction( | |
55 const base::Callback<bool(void)>& bool_function) { | |
56 bool_function.Run(); | |
57 } | |
58 | |
59 // Creates a desktop shortcut icon file (.ico) on the disk for a given profile, | 44 // Creates a desktop shortcut icon file (.ico) on the disk for a given profile, |
60 // badging the browser distribution icon with the profile avatar. | 45 // badging the browser distribution icon with the profile avatar. |
61 // |profile_base_dir| is the base directory (and key) of the profile. Returns | 46 // |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. | 47 // 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. | 48 // Use index 0 when assigning the resulting file as the icon. |
64 FilePath CreateChromeDesktopShortcutIconForProfile( | 49 FilePath CreateChromeDesktopShortcutIconForProfile( |
65 const FilePath& profile_path, | 50 const FilePath& profile_path, |
66 const gfx::Image& avatar_image) { | 51 const gfx::Image& avatar_image) { |
67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
68 const SkBitmap* avatar_bitmap = avatar_image.ToSkBitmap(); | 53 const SkBitmap* avatar_bitmap = avatar_image.ToSkBitmap(); |
69 HICON app_icon_handle = GetAppIconForSize(kShortcutIconSize); | 54 HICON app_icon_handle = GetAppIconForSize(kShortcutIconSize); |
70 scoped_ptr<SkBitmap> app_icon_bitmap( | 55 scoped_ptr<SkBitmap> app_icon_bitmap( |
71 IconUtil::CreateSkBitmapFromHICON(app_icon_handle)); | 56 IconUtil::CreateSkBitmapFromHICON(app_icon_handle)); |
72 DestroyIcon(app_icon_handle); | 57 DestroyIcon(app_icon_handle); |
73 if (!app_icon_bitmap.get()) | 58 if (!app_icon_bitmap.get()) |
74 return FilePath(); | 59 return FilePath(); |
75 | 60 |
76 // TODO(stevet): Share this chunk of code with | 61 // TODO(hallielaine): Share this chunk of code with |
77 // avatar_menu_button::DrawTaskBarDecoration. | 62 // avatar_menu_button::DrawTaskBarDecoration. |
78 const SkBitmap* source_bitmap = NULL; | 63 const SkBitmap* source_bitmap = NULL; |
79 SkBitmap squarer_bitmap; | 64 SkBitmap squarer_bitmap; |
80 if ((avatar_bitmap->width() == profiles::kAvatarIconWidth) && | 65 if ((avatar_bitmap->width() == profiles::kAvatarIconWidth) && |
81 (avatar_bitmap->height() == profiles::kAvatarIconHeight)) { | 66 (avatar_bitmap->height() == profiles::kAvatarIconHeight)) { |
82 // Shave a couple of columns so the bitmap is more square. So when | 67 // Shave a couple of columns so the bitmap is more square. So when |
83 // resized to a square aspect ratio it looks pretty. | 68 // resized to a square aspect ratio it looks pretty. |
84 int x = 2; | 69 int x = 2; |
85 avatar_bitmap->extractSubset(&squarer_bitmap, SkIRect::MakeXYWH(x, 0, | 70 avatar_bitmap->extractSubset(&squarer_bitmap, SkIRect::MakeXYWH(x, 0, |
86 profiles::kAvatarIconWidth - x * 2, profiles::kAvatarIconHeight)); | 71 profiles::kAvatarIconWidth - x * 2, profiles::kAvatarIconHeight)); |
(...skipping 23 matching lines...) Expand all Loading... |
110 offscreen_canvas->getDevice()->accessBitmap(false); | 95 offscreen_canvas->getDevice()->accessBitmap(false); |
111 | 96 |
112 // Finally, write the .ico file containing this new bitmap. | 97 // Finally, write the .ico file containing this new bitmap. |
113 FilePath icon_path = profile_path.AppendASCII(kProfileIconFileName); | 98 FilePath icon_path = profile_path.AppendASCII(kProfileIconFileName); |
114 if (!IconUtil::CreateIconFileFromSkBitmap(final_bitmap, icon_path)) | 99 if (!IconUtil::CreateIconFileFromSkBitmap(final_bitmap, icon_path)) |
115 return FilePath(); | 100 return FilePath(); |
116 | 101 |
117 return icon_path; | 102 return icon_path; |
118 } | 103 } |
119 | 104 |
120 // Creates a desktop shortcut to open Chrome with the given profile name and | 105 string16 CreateProfileShortcutFlags(const FilePath& profile_path) { |
121 // base directory. Iff |create|, create shortcut if it doesn't already exist. | 106 return base::StringPrintf(L"--%ls=\"%ls\"", |
122 // Must be called on the FILE thread. | 107 ASCIIToUTF16(switches::kProfileDirectory).c_str(), |
123 void CreateChromeDesktopShortcutForProfile( | 108 profile_path.BaseName().value().c_str()); |
124 const string16& profile_name, | 109 } |
125 const string16& profile_base_dir, | 110 |
126 const FilePath& profile_path, | 111 // Wrap a ShellUtil function that returns a bool so it can be posted in a |
127 const gfx::Image* avatar_image, | 112 // task to the FILE thread. |
128 bool create) { | 113 void CallShellUtilBoolFunction( |
| 114 const base::Callback<bool(void)>& bool_function) { |
| 115 bool_function.Run(); |
| 116 } |
| 117 |
| 118 } // namespace |
| 119 |
| 120 class ProfileShortcutManagerWin : public ProfileShortcutManager { |
| 121 public: |
| 122 ProfileShortcutManagerWin(); |
| 123 virtual ~ProfileShortcutManagerWin(); |
| 124 |
| 125 virtual void CreateChromeDesktopShortcut( |
| 126 const FilePath& profile_path, const string16& profile_name, |
| 127 const gfx::Image& avatar_image) OVERRIDE; |
| 128 virtual void DeleteChromeDesktopShortcut(const FilePath& profile_path) |
| 129 OVERRIDE; |
| 130 |
| 131 private: |
| 132 struct ProfileShortcutInfo { |
| 133 string16 flags; |
| 134 string16 profile_name; |
| 135 gfx::Image avatar_image; |
| 136 |
| 137 ProfileShortcutInfo() |
| 138 : flags(string16()), |
| 139 profile_name(string16()), |
| 140 avatar_image(gfx::Image()) { |
| 141 } |
| 142 |
| 143 ProfileShortcutInfo( |
| 144 string16 p_flags, |
| 145 string16 p_profile_name, |
| 146 gfx::Image p_avatar_image) |
| 147 : flags(p_flags), |
| 148 profile_name(p_profile_name), |
| 149 avatar_image(p_avatar_image) { |
| 150 } |
| 151 }; |
| 152 |
| 153 // TODO(hallielaine): Repopulate this map on chrome session startup |
| 154 typedef std::map<FilePath, ProfileShortcutInfo> ProfileShortcutsMap; |
| 155 ProfileShortcutsMap profile_shortcuts_; |
| 156 }; |
| 157 |
| 158 // static |
| 159 bool ProfileShortcutManager::IsFeatureEnabled() { |
| 160 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 161 switches::kProfileDesktopShortcuts)) { |
| 162 return true; |
| 163 } |
| 164 return false; |
| 165 } |
| 166 |
| 167 // static |
| 168 ProfileShortcutManager* ProfileShortcutManager::Create() { |
| 169 return new ProfileShortcutManagerWin(); |
| 170 } |
| 171 |
| 172 ProfileShortcutManagerWin::ProfileShortcutManagerWin() { |
| 173 } |
| 174 |
| 175 ProfileShortcutManagerWin::~ProfileShortcutManagerWin() { |
| 176 } |
| 177 |
| 178 void ProfileShortcutManagerWin::CreateChromeDesktopShortcut( |
| 179 const FilePath& profile_path, const string16& profile_name, |
| 180 const gfx::Image& avatar_image) { |
| 181 FilePath shortcut_icon = CreateChromeDesktopShortcutIconForProfile( |
| 182 profile_path, avatar_image); |
129 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 183 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
130 FilePath chrome_exe; | 184 FilePath chrome_exe; |
131 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) | 185 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) |
132 return; | 186 return; |
133 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | 187 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
134 string16 description; | 188 string16 description; |
135 if (!dist) | 189 if (!dist) |
136 return; | 190 return; |
137 else | 191 description = WideToUTF16(dist->GetAppDescription()); |
138 description = WideToUTF16(dist->GetAppDescription()); | 192 |
139 const string16& directory = CreateProfileShortcutSwitch(profile_base_dir); | 193 // Add the profile to the map if it doesn't exist already |
140 FilePath icon_path = avatar_image ? | 194 if (!profile_shortcuts_.count(profile_path)) { |
141 CreateChromeDesktopShortcutIconForProfile(profile_path, *avatar_image) : | 195 string16 flags = CreateProfileShortcutFlags(profile_path); |
142 FilePath(); | 196 profile_shortcuts_.insert(std::make_pair(profile_path, |
| 197 ProfileShortcutInfo(flags, profile_name, |
| 198 avatar_image))); |
| 199 } |
143 | 200 |
144 ShellUtil::CreateChromeDesktopShortcut( | 201 ShellUtil::CreateChromeDesktopShortcut( |
145 dist, | 202 dist, |
146 chrome_exe.value(), | 203 chrome_exe.value(), |
147 description, | 204 description, |
148 profile_name, | 205 profile_shortcuts_[profile_path].profile_name, |
149 directory, | 206 profile_shortcuts_[profile_path].flags, |
150 icon_path.empty() ? chrome_exe.value() : icon_path.value(), | 207 shortcut_icon.empty() ? chrome_exe.value() : shortcut_icon.value(), |
151 icon_path.empty() ? dist->GetIconIndex() : 0, | 208 shortcut_icon.empty() ? dist->GetIconIndex() : 0, |
152 ShellUtil::CURRENT_USER, | 209 ShellUtil::CURRENT_USER, |
153 create ? ShellUtil::SHORTCUT_CREATE_ALWAYS : | 210 ShellUtil::SHORTCUT_CREATE_ALWAYS); |
154 ShellUtil::SHORTCUT_NO_OPTIONS); | |
155 } | 211 } |
156 | 212 |
157 // Renames an existing Chrome desktop profile shortcut. Must be called on the | 213 void ProfileShortcutManagerWin::DeleteChromeDesktopShortcut( |
158 // FILE thread. | 214 const FilePath& profile_path) { |
159 void RenameChromeDesktopShortcutForProfile( | 215 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
160 const string16& old_shortcut, | 216 string16 shortcut; |
161 const string16& new_shortcut) { | 217 // If we can find the shortcut, delete it |
162 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 218 if (ShellUtil::GetChromeShortcutName(dist, false, |
163 FilePath shortcut_path; | 219 profile_shortcuts_[profile_path].profile_name, &shortcut)) { |
164 if (ShellUtil::GetDesktopPath(false, // User's directory instead of system. | 220 std::vector<string16> appended_names(1, shortcut); |
165 &shortcut_path)) { | 221 BrowserThread::PostTask( |
166 FilePath old_profile = shortcut_path.Append(old_shortcut); | 222 BrowserThread::FILE, FROM_HERE, |
167 FilePath new_profile = shortcut_path.Append(new_shortcut); | 223 base::Bind(&CallShellUtilBoolFunction, base::Bind( |
168 file_util::Move(old_profile, new_profile); | 224 &ShellUtil::RemoveChromeDesktopShortcutsWithAppendedNames, |
| 225 appended_names))); |
| 226 profile_shortcuts_.erase(profile_path); |
169 } | 227 } |
170 } | 228 } |
171 | 229 |
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 } | |
218 | |
219 } // namespace | |
220 | |
221 ProfileShortcutManagerWin::ProfileShortcutManagerWin() { | |
222 } | |
223 | |
224 ProfileShortcutManagerWin::~ProfileShortcutManagerWin() { | |
225 } | |
226 | |
227 void ProfileShortcutManagerWin::AddProfileShortcut( | |
228 const FilePath& profile_path) { | |
229 ProfileInfoCache& cache = | |
230 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
231 size_t index = cache.GetIndexOfProfileWithPath(profile_path); | |
232 if (index == std::string::npos) | |
233 return; | |
234 | |
235 // Launch task to add shortcut to desktop on Windows. If this is the very | |
236 // first profile created, don't add the user name to the shortcut. | |
237 // TODO(mirandac): respect master_preferences choice to create no shortcuts | |
238 // (see http://crbug.com/104463) | |
239 if (g_browser_process->profile_manager()->GetNumberOfProfiles() > 1) { | |
240 { | |
241 // We make a copy of the Image to ensure that the underlying image data is | |
242 // AddRef'd, in case the original copy gets deleted. | |
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 } | |
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 } | |
300 } | |
301 | |
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 } | |
OLD | NEW |