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

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

Issue 8785006: Badge Windows profile shortcuts with multi-user avatar. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: addressed sail's comment. removed fix for crbug.com/106261 Created 9 years 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
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 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_win.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/file_path.h" 8 #include "base/file_path.h"
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/path_service.h" 10 #include "base/path_service.h"
11 #include "base/stringprintf.h" 11 #include "base/stringprintf.h"
12 #include "base/utf_string_conversions.h" 12 #include "base/utf_string_conversions.h"
13 #include "chrome/browser/app_icon_win.h"
13 #include "chrome/browser/browser_process.h" 14 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/prefs/pref_service.h" 15 #include "chrome/browser/prefs/pref_service.h"
15 #include "chrome/browser/profiles/profile_info_cache.h" 16 #include "chrome/browser/profiles/profile_info_cache.h"
17 #include "chrome/browser/profiles/profile_info_util.h"
16 #include "chrome/browser/profiles/profile_manager.h" 18 #include "chrome/browser/profiles/profile_manager.h"
17 #include "chrome/common/chrome_constants.h" 19 #include "chrome/common/chrome_constants.h"
18 #include "chrome/common/chrome_switches.h" 20 #include "chrome/common/chrome_switches.h"
19 #include "chrome/common/pref_names.h" 21 #include "chrome/common/pref_names.h"
20 #include "chrome/installer/util/browser_distribution.h" 22 #include "chrome/installer/util/browser_distribution.h"
21 #include "chrome/installer/util/shell_util.h" 23 #include "chrome/installer/util/shell_util.h"
22 #include "content/public/browser/browser_thread.h" 24 #include "content/public/browser/browser_thread.h"
23 #include "grit/generated_resources.h" 25 #include "grit/generated_resources.h"
26 #include "skia/ext/image_operations.h"
27 #include "skia/ext/platform_canvas.h"
24 #include "ui/base/l10n/l10n_util.h" 28 #include "ui/base/l10n/l10n_util.h"
29 #include "ui/gfx/icon_util.h"
30 #include "ui/gfx/image/image.h"
25 31
26 using content::BrowserThread; 32 using content::BrowserThread;
27 33
28 namespace { 34 namespace {
29 35
30 // Creates the argument to pass to the Windows executable that launches Chrome 36 // Creates the argument to pass to the Windows executable that launches Chrome
31 // with the profile in |profile_base_dir|. 37 // with the profile in |profile_base_dir|.
32 // For example: --profile-directory="Profile 2" 38 // For example: --profile-directory="Profile 2"
33 string16 CreateProfileShortcutSwitch(string16 profile_base_dir) { 39 string16 CreateProfileShortcutSwitch(string16 profile_base_dir) {
34 string16 profile_directory = base::StringPrintf(L"--%ls=\"%ls\"", 40 string16 profile_directory = base::StringPrintf(L"--%ls=\"%ls\"",
35 ASCIIToUTF16(switches::kProfileDirectory).c_str(), 41 ASCIIToUTF16(switches::kProfileDirectory).c_str(),
36 profile_base_dir.c_str()); 42 profile_base_dir.c_str());
37 return profile_directory; 43 return profile_directory;
38 } 44 }
39 45
40 // Wrap a ShellUtil function that returns a bool so it can be posted in a 46 // Wrap a ShellUtil function that returns a bool so it can be posted in a
41 // task to the FILE thread. 47 // task to the FILE thread.
42 void CallShellUtilBoolFunction( 48 void CallShellUtilBoolFunction(
43 const base::Callback<bool(void)>& bool_function) { 49 const base::Callback<bool(void)>& bool_function) {
44 bool_function.Run(); 50 bool_function.Run();
45 } 51 }
46 52
53 const char kProfileIconFileName[] = "Google Profile.ico";
54 const int kProfileAvatarShortcutBadgeWidth = 28;
55 const int kProfileAvatarShortcutBadgeHeight = 28;
56 const int kShortcutIconSize = 48;
57
47 } // namespace 58 } // namespace
48 59
49 ProfileShortcutManagerWin::ProfileShortcutManagerWin() { 60 ProfileShortcutManagerWin::ProfileShortcutManagerWin() {
50 } 61 }
51 62
52 ProfileShortcutManagerWin::~ProfileShortcutManagerWin() { 63 ProfileShortcutManagerWin::~ProfileShortcutManagerWin() {
53 } 64 }
54 65
55 void ProfileShortcutManagerWin::OnProfileAdded( 66 void ProfileShortcutManagerWin::OnProfileAdded(
56 const string16& profile_name, 67 const string16& profile_name,
57 const string16& profile_base_dir) { 68 const string16& profile_base_dir,
69 const string16& profile_path,
70 const gfx::Image* avatar_image) {
58 // Launch task to add shortcut to desktop on Windows. If this is the very 71 // 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. 72 // first profile created, don't add the user name to the shortcut.
60 // TODO(mirandac): respect master_preferences choice to create no shortcuts 73 // TODO(mirandac): respect master_preferences choice to create no shortcuts
61 // (see http://crbug.com/104463) 74 // (see http://crbug.com/104463)
62 if (g_browser_process->profile_manager()->GetNumberOfProfiles() > 1) { 75 if (g_browser_process->profile_manager()->GetNumberOfProfiles() > 1) {
63 string16 profile_directory = 76 // There is no guarantee that avatar_image won't be deleted before it's
64 CreateProfileShortcutSwitch(profile_base_dir); 77 // used, so we'll make a deep copy of it and rely on the user to delete the
65 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, 78 // copy.
66 base::Bind(&CreateChromeDesktopShortcutForProfile, 79 gfx::Image* avatar_copy = avatar_image ?
67 profile_name, profile_directory, true)); 80 new gfx::Image(avatar_image->CopySkBitmap()) : NULL;
81 if (!BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
82 base::Bind(&CreateChromeDesktopShortcutForProfile,
83 profile_name, profile_base_dir, profile_path,
84 avatar_copy, true))) {
85 delete avatar_copy;
86 }
68 87
69 // If this is the very first multi-user account created, change the 88 // If this is the very first multi-user account created, change the
70 // original shortcut to launch with the First User profile. 89 // original shortcut to launch with the First User profile.
71 PrefService* local_state = g_browser_process->local_state(); 90 PrefService* local_state = g_browser_process->local_state();
72 if (local_state->GetInteger(prefs::kProfilesNumCreated) == 2) { 91 if (local_state->GetInteger(prefs::kProfilesNumCreated) == 2) {
73 string16 default_name = l10n_util::GetStringUTF16( 92 string16 default_name = l10n_util::GetStringUTF16(
74 IDS_DEFAULT_PROFILE_NAME); 93 IDS_DEFAULT_PROFILE_NAME);
75 string16 default_directory =
76 CreateProfileShortcutSwitch(UTF8ToUTF16(chrome::kInitialProfile));
77 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); 94 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
78 95
79 string16 old_shortcut; 96 string16 old_shortcut;
80 string16 new_shortcut; 97 string16 new_shortcut;
81 if (ShellUtil::GetChromeShortcutName(dist, false, L"", &old_shortcut) && 98 if (ShellUtil::GetChromeShortcutName(dist, false, L"", &old_shortcut) &&
82 ShellUtil::GetChromeShortcutName(dist, false, default_name, 99 ShellUtil::GetChromeShortcutName(dist, false, default_name,
83 &new_shortcut)) { 100 &new_shortcut)) {
84 // Update doesn't allow changing the target, so rename first. 101 // Update doesn't allow changing the target, so rename first.
85 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, 102 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
86 base::Bind(&RenameChromeDesktopShortcutForProfile, 103 base::Bind(&RenameChromeDesktopShortcutForProfile,
87 old_shortcut, new_shortcut)); 104 old_shortcut, new_shortcut));
105 // TODO(stevet): We actually need to retrieve the newly assigned avatar
106 // icon for the original profile here and update it with that.
88 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, 107 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
89 base::Bind(&UpdateChromeDesktopShortcutForProfile, 108 base::Bind(&UpdateChromeDesktopShortcutForProfile,
90 new_shortcut, default_directory)); 109 new_shortcut, UTF8ToUTF16(chrome::kInitialProfile),
110 profile_path, static_cast<gfx::Image*>(NULL)));
91 } 111 }
92 } 112 }
93 } else { // Only one profile, so create original shortcut. 113 } else { // Only one profile, so create original shortcut, with no avatar.
94 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, 114 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
95 base::Bind(&CreateChromeDesktopShortcutForProfile, 115 base::Bind(&CreateChromeDesktopShortcutForProfile,
96 L"", L"", true)); 116 L"", L"", L"", static_cast<gfx::Image*>(NULL), true));
97 } 117 }
98 } 118 }
99 119
100 void ProfileShortcutManagerWin::OnProfileRemoved( 120 void ProfileShortcutManagerWin::OnProfileRemoved(
101 const string16& profile_name) { 121 const string16& profile_name) {
102 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); 122 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
103 string16 shortcut; 123 string16 shortcut;
104 if (ShellUtil::GetChromeShortcutName(dist, false, profile_name, &shortcut)) { 124 if (ShellUtil::GetChromeShortcutName(dist, false, profile_name, &shortcut)) {
105 std::vector<string16> shortcuts(1, shortcut); 125 std::vector<string16> shortcuts(1, shortcut);
106 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, 126 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
(...skipping 17 matching lines...) Expand all
124 dist, false, old_profile_name, &old_shortcut) && 144 dist, false, old_profile_name, &old_shortcut) &&
125 ShellUtil::GetChromeShortcutName( 145 ShellUtil::GetChromeShortcutName(
126 dist, false, new_profile_name, &new_shortcut)) { 146 dist, false, new_profile_name, &new_shortcut)) {
127 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, 147 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
128 base::Bind(&RenameChromeDesktopShortcutForProfile, 148 base::Bind(&RenameChromeDesktopShortcutForProfile,
129 old_shortcut, 149 old_shortcut,
130 new_shortcut)); 150 new_shortcut));
131 } 151 }
132 } 152 }
133 153
154 void ProfileShortcutManagerWin::OnProfileAvatarChanged(
155 const string16& profile_name,
156 const string16& profile_base_dir,
157 const string16& profile_path,
158 const gfx::Image* avatar_image) {
159 // Launch task to change the icon of the desktop shortcut on windows.
160 string16 new_shortcut;
161 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
162 if (ShellUtil::GetChromeShortcutName(dist, false, profile_name,
163 &new_shortcut)) {
164 // There is no guarantee that avatar_image won't be deleted before it's
165 // used, so we'll make a deep copy of it and rely on the user to delete the
166 // copy.
167 gfx::Image* avatar_copy = avatar_image ?
168 new gfx::Image(avatar_image->CopySkBitmap()) : NULL;
169 if (!BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
170 base::Bind(&UpdateChromeDesktopShortcutForProfile,
171 new_shortcut,
172 CreateProfileShortcutSwitch(profile_base_dir),
173 profile_path,
174 avatar_copy))) {
175 delete avatar_copy;
176 }
177 }
178 }
179
134 // static 180 // static
135 std::vector<string16> ProfileShortcutManagerWin::GenerateShortcutsFromProfiles( 181 std::vector<string16> ProfileShortcutManagerWin::GenerateShortcutsFromProfiles(
136 const std::vector<string16>& profile_names) { 182 const std::vector<string16>& profile_names) {
137 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); 183 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
138 std::vector<string16> shortcuts; 184 std::vector<string16> shortcuts;
139 shortcuts.reserve(profile_names.size()); 185 shortcuts.reserve(profile_names.size());
140 for (std::vector<string16>::const_iterator it = profile_names.begin(); 186 for (std::vector<string16>::const_iterator it = profile_names.begin();
141 it != profile_names.end(); 187 it != profile_names.end();
142 ++it) { 188 ++it) {
143 string16 shortcut; 189 string16 shortcut;
144 if (ShellUtil::GetChromeShortcutName(dist, false, *it, &shortcut)) 190 if (ShellUtil::GetChromeShortcutName(dist, false, *it, &shortcut))
145 shortcuts.push_back(shortcut); 191 shortcuts.push_back(shortcut);
146 } 192 }
147 return shortcuts; 193 return shortcuts;
148 } 194 }
149 195
150 // static 196 // static
151 void ProfileShortcutManagerWin::CreateChromeDesktopShortcutForProfile( 197 void ProfileShortcutManagerWin::CreateChromeDesktopShortcutForProfile(
152 const string16& profile_name, 198 const string16& profile_name,
153 const string16& directory, 199 const string16& profile_base_dir,
200 const string16& profile_path,
201 const gfx::Image* avatar_image,
154 bool create) { 202 bool create) {
155 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 203 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
156 FilePath chrome_exe; 204 FilePath chrome_exe;
157 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) 205 if (!PathService::Get(base::FILE_EXE, &chrome_exe))
158 return; 206 return;
159 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); 207 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
160 string16 description; 208 string16 description;
161 if (!dist) 209 if (!dist)
162 return; 210 return;
163 else 211 else
164 description = WideToUTF16(dist->GetAppDescription()); 212 description = WideToUTF16(dist->GetAppDescription());
213 const string16& directory = CreateProfileShortcutSwitch(profile_base_dir);
214 FilePath icon_path =
215 CreateChromeDesktopShortcutIconForProfile(profile_path, avatar_image);
216
165 ShellUtil::CreateChromeDesktopShortcut( 217 ShellUtil::CreateChromeDesktopShortcut(
166 dist, 218 dist,
167 chrome_exe.value(), 219 chrome_exe.value(),
168 description, 220 description,
169 profile_name, 221 profile_name,
170 directory, 222 directory,
223 icon_path.empty() ? chrome_exe.value() : icon_path.value(),
224 icon_path.empty() ? dist->GetIconIndex() : 0,
171 ShellUtil::CURRENT_USER, 225 ShellUtil::CURRENT_USER,
172 false, // Use alternate text. 226 false, // Use alternate text.
173 create); // Create if it doesn't already exist. 227 create); // Create if it doesn't already exist.
228
229 // We have ownership of avatar_image, so clean it up here.
230 if (avatar_image) {
231 delete avatar_image;
232 avatar_image = NULL;
233 }
174 } 234 }
175 235
176 // static 236 // static
177 void ProfileShortcutManagerWin::RenameChromeDesktopShortcutForProfile( 237 void ProfileShortcutManagerWin::RenameChromeDesktopShortcutForProfile(
178 const string16& old_shortcut, 238 const string16& old_shortcut,
179 const string16& new_shortcut) { 239 const string16& new_shortcut) {
180 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 240 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
181 FilePath shortcut_path; 241 FilePath shortcut_path;
182 if (ShellUtil::GetDesktopPath(false, // User's directory instead of system. 242 if (ShellUtil::GetDesktopPath(false, // User's directory instead of system.
183 &shortcut_path)) { 243 &shortcut_path)) {
184 FilePath old_profile = shortcut_path.Append(old_shortcut); 244 FilePath old_profile = shortcut_path.Append(old_shortcut);
185 FilePath new_profile = shortcut_path.Append(new_shortcut); 245 FilePath new_profile = shortcut_path.Append(new_shortcut);
186 file_util::Move(old_profile, new_profile); 246 file_util::Move(old_profile, new_profile);
187 } 247 }
188 } 248 }
189 249
190 // static 250 // static
191 void ProfileShortcutManagerWin::UpdateChromeDesktopShortcutForProfile( 251 void ProfileShortcutManagerWin::UpdateChromeDesktopShortcutForProfile(
192 const string16& shortcut, 252 const string16& shortcut,
193 const string16& arguments) { 253 const string16& arguments,
254 const string16& profile_path,
255 const gfx::Image* avatar_image) {
194 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 256 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
195 FilePath shortcut_path; 257 FilePath shortcut_path;
196 if (!ShellUtil::GetDesktopPath(false, &shortcut_path)) 258 if (!ShellUtil::GetDesktopPath(false, &shortcut_path))
197 return; 259 return;
198 260
199 shortcut_path = shortcut_path.Append(shortcut); 261 shortcut_path = shortcut_path.Append(shortcut);
200 FilePath chrome_exe; 262 FilePath chrome_exe;
201 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) 263 if (!PathService::Get(base::FILE_EXE, &chrome_exe))
202 return; 264 return;
203 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); 265 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
204 string16 description; 266 string16 description;
205 if (!dist) 267 if (!dist)
206 return; 268 return;
207 else 269 else
208 description = WideToUTF16(dist->GetAppDescription()); 270 description = WideToUTF16(dist->GetAppDescription());
271 FilePath icon_path =
272 CreateChromeDesktopShortcutIconForProfile(profile_path, avatar_image);
273
209 ShellUtil::UpdateChromeShortcut( 274 ShellUtil::UpdateChromeShortcut(
210 dist, 275 dist,
211 chrome_exe.value(), 276 chrome_exe.value(),
212 shortcut_path.value(), 277 shortcut_path.value(),
213 arguments, 278 arguments,
214 description, 279 description,
280 icon_path.empty() ? chrome_exe.value() : icon_path.value(),
281 icon_path.empty() ? dist->GetIconIndex() : 0,
215 false); 282 false);
283
284 // We have ownership of avatar_image, so clean it up here.
285 if (avatar_image) {
286 delete avatar_image;
287 avatar_image = NULL;
288 }
216 } 289 }
290
291 // static
292 FilePath ProfileShortcutManagerWin::CreateChromeDesktopShortcutIconForProfile(
293 const string16& profile_path,
294 const gfx::Image* avatar_image) {
295 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
296 if (!avatar_image)
297 return FilePath();
298 const SkBitmap* avatar_bitmap = avatar_image->ToSkBitmap();
299 DCHECK(avatar_bitmap);
300 HICON app_icon_handle = GetAppIconForSize(kShortcutIconSize);
SteveT 2011/12/03 03:19:15 robertshield: Don't need it for this CL, but if yo
301 scoped_ptr<SkBitmap> app_icon_bitmap(
302 IconUtil::CreateSkBitmapFromHICON(app_icon_handle));
303 DestroyIcon(app_icon_handle);
304 if (!app_icon_bitmap.get())
305 return FilePath();
306
307 // TODO(stevet): Share this chunk of code with
308 // avatar_menu_button::DrawTaskBarDecoration.
309 const SkBitmap* source_bitmap = NULL;
310 SkBitmap squarer_bitmap;
311 if ((avatar_bitmap->width() == profiles::kAvatarIconWidth) &&
312 (avatar_bitmap->height() == profiles::kAvatarIconHeight)) {
SteveT 2011/12/03 03:19:15 robertshield: This feels fragile and special-casey
313 // Shave a couple of columns so the bitmap is more square. So when
314 // resized to a square aspect ratio it looks pretty.
315 int x = 2;
316 avatar_bitmap->extractSubset(&squarer_bitmap, SkIRect::MakeXYWH(x, 0,
317 profiles::kAvatarIconWidth - x * 2, profiles::kAvatarIconHeight));
318 source_bitmap = &squarer_bitmap;
319 } else {
320 source_bitmap = avatar_bitmap;
321 }
322 SkBitmap sk_icon = skia::ImageOperations::Resize(
323 *source_bitmap,
324 skia::ImageOperations::RESIZE_LANCZOS3,
325 kProfileAvatarShortcutBadgeWidth,
326 kProfileAvatarShortcutBadgeHeight);
327
328 // Overlay the avatar on the icon, anchoring it to the bottom-right of the
329 // icon.
330 scoped_ptr<SkCanvas> offscreen_canvas(
331 skia::CreateBitmapCanvas(app_icon_bitmap->width(),
332 app_icon_bitmap->height(),
333 false));
334 DCHECK(offscreen_canvas.get());
335 offscreen_canvas->drawBitmap(*app_icon_bitmap, 0, 0);
336 offscreen_canvas->drawBitmap(
337 sk_icon,
338 app_icon_bitmap->width() - kProfileAvatarShortcutBadgeWidth,
339 app_icon_bitmap->height() - kProfileAvatarShortcutBadgeHeight);
340 const SkBitmap& final_bitmap =
341 offscreen_canvas->getDevice()->accessBitmap(false);
342
343 // Finally, write the .ico file containing this new bitmap.
344 FilePath icon_path(profile_path);
345 icon_path = icon_path.AppendASCII(kProfileIconFileName);
346 if (!IconUtil::CreateIconFileFromSkBitmap(final_bitmap, icon_path))
347 return FilePath();
348
349 return icon_path;
350 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698