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

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 rsesek's comments. 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
36 const char kProfileIconFileName[] = "Google Profile.ico";
37 const int kProfileAvatarShortcutBadgeWidth = 28;
38 const int kProfileAvatarShortcutBadgeHeight = 28;
39 const int kShortcutIconSize = 48;
40
30 // Creates the argument to pass to the Windows executable that launches Chrome 41 // Creates the argument to pass to the Windows executable that launches Chrome
31 // with the profile in |profile_base_dir|. 42 // with the profile in |profile_base_dir|.
32 // For example: --profile-directory="Profile 2" 43 // For example: --profile-directory="Profile 2"
33 string16 CreateProfileShortcutSwitch(string16 profile_base_dir) { 44 string16 CreateProfileShortcutSwitch(string16 profile_base_dir) {
34 string16 profile_directory = base::StringPrintf(L"--%ls=\"%ls\"", 45 string16 profile_directory = base::StringPrintf(L"--%ls=\"%ls\"",
35 ASCIIToUTF16(switches::kProfileDirectory).c_str(), 46 ASCIIToUTF16(switches::kProfileDirectory).c_str(),
36 profile_base_dir.c_str()); 47 profile_base_dir.c_str());
37 return profile_directory; 48 return profile_directory;
38 } 49 }
39 50
40 // Wrap a ShellUtil function that returns a bool so it can be posted in a 51 // Wrap a ShellUtil function that returns a bool so it can be posted in a
41 // task to the FILE thread. 52 // task to the FILE thread.
42 void CallShellUtilBoolFunction( 53 void CallShellUtilBoolFunction(
43 const base::Callback<bool(void)>& bool_function) { 54 const base::Callback<bool(void)>& bool_function) {
44 bool_function.Run(); 55 bool_function.Run();
45 } 56 }
46 57
58 // Creates a desktop shortcut icon file (.ico) on the disk for a given profile,
59 // badging the browser distribution icon with the profile avatar.
60 // |profile_base_dir| is the base directory (and key) of the profile. Returns
61 // a path to the shortcut icon file on disk, which is empty if this fails.
62 // Use index 0 when assigning the resulting file as the icon.
63 FilePath CreateChromeDesktopShortcutIconForProfile(
64 const FilePath& profile_path,
65 const gfx::Image* avatar_image) {
cpu_(ooo_6.6-7.5) 2011/12/05 21:37:34 why not pass as const gfx:Image& ?
SteveT 2011/12/05 22:58:10 Sure. Do note that the methods that subsequently t
66 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
67 if (!avatar_image)
68 return FilePath();
69 const SkBitmap* avatar_bitmap = avatar_image->ToSkBitmap();
70 HICON app_icon_handle = GetAppIconForSize(kShortcutIconSize);
71 scoped_ptr<SkBitmap> app_icon_bitmap(
72 IconUtil::CreateSkBitmapFromHICON(app_icon_handle));
73 DestroyIcon(app_icon_handle);
74 if (!app_icon_bitmap.get())
75 return FilePath();
76
77 // TODO(stevet): Share this chunk of code with
78 // avatar_menu_button::DrawTaskBarDecoration.
79 const SkBitmap* source_bitmap = NULL;
80 SkBitmap squarer_bitmap;
81 if ((avatar_bitmap->width() == profiles::kAvatarIconWidth) &&
82 (avatar_bitmap->height() == profiles::kAvatarIconHeight)) {
83 // Shave a couple of columns so the bitmap is more square. So when
84 // resized to a square aspect ratio it looks pretty.
85 int x = 2;
86 avatar_bitmap->extractSubset(&squarer_bitmap, SkIRect::MakeXYWH(x, 0,
87 profiles::kAvatarIconWidth - x * 2, profiles::kAvatarIconHeight));
88 source_bitmap = &squarer_bitmap;
89 } else {
90 source_bitmap = avatar_bitmap;
91 }
92 SkBitmap sk_icon = skia::ImageOperations::Resize(
93 *source_bitmap,
94 skia::ImageOperations::RESIZE_LANCZOS3,
95 kProfileAvatarShortcutBadgeWidth,
96 kProfileAvatarShortcutBadgeHeight);
97
98 // Overlay the avatar on the icon, anchoring it to the bottom-right of the
99 // icon.
100 scoped_ptr<SkCanvas> offscreen_canvas(
101 skia::CreateBitmapCanvas(app_icon_bitmap->width(),
102 app_icon_bitmap->height(),
103 false));
104 DCHECK(offscreen_canvas.get());
105 offscreen_canvas->drawBitmap(*app_icon_bitmap, 0, 0);
106 offscreen_canvas->drawBitmap(
107 sk_icon,
108 app_icon_bitmap->width() - kProfileAvatarShortcutBadgeWidth,
109 app_icon_bitmap->height() - kProfileAvatarShortcutBadgeHeight);
110 const SkBitmap& final_bitmap =
111 offscreen_canvas->getDevice()->accessBitmap(false);
112
113 // Finally, write the .ico file containing this new bitmap.
114 FilePath icon_path = profile_path.AppendASCII(kProfileIconFileName);
115 if (!IconUtil::CreateIconFileFromSkBitmap(final_bitmap, icon_path))
116 return FilePath();
117
118 return icon_path;
119 }
120
121 // Creates a desktop shortcut to open Chrome with the given profile name and
122 // base directory. Iff |create|, create shortcut if it doesn't already exist.
123 // Must be called on the FILE thread.
124 void CreateChromeDesktopShortcutForProfile(
125 const string16& profile_name,
126 const string16& profile_base_dir,
127 const FilePath& profile_path,
128 const gfx::Image* avatar_image,
129 bool create) {
130 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
131 FilePath chrome_exe;
132 if (!PathService::Get(base::FILE_EXE, &chrome_exe))
133 return;
134 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
135 string16 description;
136 if (!dist)
137 return;
138 else
139 description = WideToUTF16(dist->GetAppDescription());
140 const string16& directory = CreateProfileShortcutSwitch(profile_base_dir);
141 FilePath icon_path =
142 CreateChromeDesktopShortcutIconForProfile(profile_path, avatar_image);
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 false, // Use alternate text.
154 create); // Create if it doesn't already exist.
155 }
156
157 // Renames an existing Chrome desktop profile shortcut. Must be called on the
158 // FILE thread.
159 void RenameChromeDesktopShortcutForProfile(
160 const string16& old_shortcut,
161 const string16& new_shortcut) {
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 =
195 CreateChromeDesktopShortcutIconForProfile(profile_path, avatar_image);
196
197 ShellUtil::UpdateChromeShortcut(
198 dist,
199 chrome_exe.value(),
200 shortcut_path.value(),
201 arguments,
202 description,
203 icon_path.empty() ? chrome_exe.value() : icon_path.value(),
204 icon_path.empty() ? dist->GetIconIndex() : 0,
205 false);
206 }
207
47 } // namespace 208 } // namespace
48 209
49 ProfileShortcutManagerWin::ProfileShortcutManagerWin() { 210 ProfileShortcutManagerWin::ProfileShortcutManagerWin() {
50 } 211 }
51 212
52 ProfileShortcutManagerWin::~ProfileShortcutManagerWin() { 213 ProfileShortcutManagerWin::~ProfileShortcutManagerWin() {
53 } 214 }
54 215
55 void ProfileShortcutManagerWin::OnProfileAdded( 216 void ProfileShortcutManagerWin::OnProfileAdded(
56 const string16& profile_name, 217 const string16& profile_name,
57 const string16& profile_base_dir) { 218 const string16& profile_base_dir,
219 const FilePath& profile_path,
220 const gfx::Image* avatar_image) {
58 // Launch task to add shortcut to desktop on Windows. If this is the very 221 // 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. 222 // first profile created, don't add the user name to the shortcut.
60 // TODO(mirandac): respect master_preferences choice to create no shortcuts 223 // TODO(mirandac): respect master_preferences choice to create no shortcuts
61 // (see http://crbug.com/104463) 224 // (see http://crbug.com/104463)
62 if (g_browser_process->profile_manager()->GetNumberOfProfiles() > 1) { 225 if (g_browser_process->profile_manager()->GetNumberOfProfiles() > 1) {
63 string16 profile_directory = 226 {
64 CreateProfileShortcutSwitch(profile_base_dir); 227 // We make a copy of the Image to ensure that the underlying image data is
65 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, 228 // AddRef'd, in case the original copy gets deleted.
66 base::Bind(&CreateChromeDesktopShortcutForProfile, 229 gfx::Image* avatar_copy = avatar_image ?
67 profile_name, profile_directory, true)); 230 new gfx::Image(*avatar_image) : NULL;
231 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
232 base::Bind(&CreateChromeDesktopShortcutForProfile,
233 profile_name, profile_base_dir, profile_path,
234 base::Owned(avatar_copy), true));
235 }
68 236
69 // If this is the very first multi-user account created, change the 237 // If this is the very first multi-user account created, change the
70 // original shortcut to launch with the First User profile. 238 // original shortcut to launch with the First User profile.
71 PrefService* local_state = g_browser_process->local_state(); 239 PrefService* local_state = g_browser_process->local_state();
72 if (local_state->GetInteger(prefs::kProfilesNumCreated) == 2) { 240 if (local_state->GetInteger(prefs::kProfilesNumCreated) == 2) {
73 string16 default_name = l10n_util::GetStringUTF16( 241 string16 default_name = l10n_util::GetStringUTF16(
74 IDS_DEFAULT_PROFILE_NAME); 242 IDS_DEFAULT_PROFILE_NAME);
75 string16 default_directory =
76 CreateProfileShortcutSwitch(UTF8ToUTF16(chrome::kInitialProfile));
77 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); 243 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
78 244
79 string16 old_shortcut; 245 string16 old_shortcut;
80 string16 new_shortcut; 246 string16 new_shortcut;
81 if (ShellUtil::GetChromeShortcutName(dist, false, L"", &old_shortcut) && 247 if (ShellUtil::GetChromeShortcutName(dist, false, L"", &old_shortcut) &&
82 ShellUtil::GetChromeShortcutName(dist, false, default_name, 248 ShellUtil::GetChromeShortcutName(dist, false, default_name,
83 &new_shortcut)) { 249 &new_shortcut)) {
84 // Update doesn't allow changing the target, so rename first. 250 // Update doesn't allow changing the target, so rename first.
85 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, 251 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
86 base::Bind(&RenameChromeDesktopShortcutForProfile, 252 base::Bind(&RenameChromeDesktopShortcutForProfile,
87 old_shortcut, new_shortcut)); 253 old_shortcut, new_shortcut));
254 // TODO(stevet): We actually need to retrieve the newly assigned avatar
255 // icon for the original profile here and update it with that.
88 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, 256 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
89 base::Bind(&UpdateChromeDesktopShortcutForProfile, 257 base::Bind(&UpdateChromeDesktopShortcutForProfile,
90 new_shortcut, default_directory)); 258 new_shortcut, UTF8ToUTF16(chrome::kInitialProfile),
259 profile_path, static_cast<gfx::Image*>(NULL)));
91 } 260 }
92 } 261 }
93 } else { // Only one profile, so create original shortcut. 262 } else { // Only one profile, so create original shortcut, with no avatar.
94 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, 263 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
95 base::Bind(&CreateChromeDesktopShortcutForProfile, 264 base::Bind(&CreateChromeDesktopShortcutForProfile,
96 L"", L"", true)); 265 L"", L"", FilePath(), static_cast<gfx::Image*>(NULL), true));
97 } 266 }
98 } 267 }
99 268
100 void ProfileShortcutManagerWin::OnProfileRemoved( 269 void ProfileShortcutManagerWin::OnProfileRemoved(
101 const string16& profile_name) { 270 const string16& profile_name) {
102 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); 271 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
103 string16 shortcut; 272 string16 shortcut;
104 if (ShellUtil::GetChromeShortcutName(dist, false, profile_name, &shortcut)) { 273 if (ShellUtil::GetChromeShortcutName(dist, false, profile_name, &shortcut)) {
105 std::vector<string16> shortcuts(1, shortcut); 274 std::vector<string16> shortcuts(1, shortcut);
106 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, 275 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
(...skipping 17 matching lines...) Expand all
124 dist, false, old_profile_name, &old_shortcut) && 293 dist, false, old_profile_name, &old_shortcut) &&
125 ShellUtil::GetChromeShortcutName( 294 ShellUtil::GetChromeShortcutName(
126 dist, false, new_profile_name, &new_shortcut)) { 295 dist, false, new_profile_name, &new_shortcut)) {
127 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, 296 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
128 base::Bind(&RenameChromeDesktopShortcutForProfile, 297 base::Bind(&RenameChromeDesktopShortcutForProfile,
129 old_shortcut, 298 old_shortcut,
130 new_shortcut)); 299 new_shortcut));
131 } 300 }
132 } 301 }
133 302
303 void ProfileShortcutManagerWin::OnProfileAvatarChanged(
304 const string16& profile_name,
305 const string16& profile_base_dir,
306 const FilePath& profile_path,
307 const gfx::Image* avatar_image) {
cpu_(ooo_6.6-7.5) 2011/12/05 21:37:34 same comment, use const reference? I mean, passin
SteveT 2011/12/05 22:58:10 So here I'd rather keep the pointer, as we can sti
308 // Launch task to change the icon of the desktop shortcut on windows.
309 string16 new_shortcut;
310 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
311 if (ShellUtil::GetChromeShortcutName(dist, false, profile_name,
312 &new_shortcut)) {
313 // We make a copy of the Image to ensure that the underlying image data is
314 // AddRef'd, in case the original copy gets deleted.
315 gfx::Image* avatar_copy = avatar_image ?
316 new gfx::Image(*avatar_image) : NULL;
317 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
318 base::Bind(&UpdateChromeDesktopShortcutForProfile,
319 new_shortcut,
320 CreateProfileShortcutSwitch(profile_base_dir),
321 profile_path,
322 base::Owned(avatar_copy)));
323 }
324 }
325
134 // static 326 // static
135 std::vector<string16> ProfileShortcutManagerWin::GenerateShortcutsFromProfiles( 327 std::vector<string16> ProfileShortcutManagerWin::GenerateShortcutsFromProfiles(
136 const std::vector<string16>& profile_names) { 328 const std::vector<string16>& profile_names) {
137 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); 329 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
138 std::vector<string16> shortcuts; 330 std::vector<string16> shortcuts;
139 shortcuts.reserve(profile_names.size()); 331 shortcuts.reserve(profile_names.size());
140 for (std::vector<string16>::const_iterator it = profile_names.begin(); 332 for (std::vector<string16>::const_iterator it = profile_names.begin();
141 it != profile_names.end(); 333 it != profile_names.end();
142 ++it) { 334 ++it) {
143 string16 shortcut; 335 string16 shortcut;
144 if (ShellUtil::GetChromeShortcutName(dist, false, *it, &shortcut)) 336 if (ShellUtil::GetChromeShortcutName(dist, false, *it, &shortcut))
145 shortcuts.push_back(shortcut); 337 shortcuts.push_back(shortcut);
146 } 338 }
147 return shortcuts; 339 return shortcuts;
148 } 340 }
149
150 // static
151 void ProfileShortcutManagerWin::CreateChromeDesktopShortcutForProfile(
152 const string16& profile_name,
153 const string16& directory,
154 bool create) {
155 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
156 FilePath chrome_exe;
157 if (!PathService::Get(base::FILE_EXE, &chrome_exe))
158 return;
159 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
160 string16 description;
161 if (!dist)
162 return;
163 else
164 description = WideToUTF16(dist->GetAppDescription());
165 ShellUtil::CreateChromeDesktopShortcut(
166 dist,
167 chrome_exe.value(),
168 description,
169 profile_name,
170 directory,
171 ShellUtil::CURRENT_USER,
172 false, // Use alternate text.
173 create); // Create if it doesn't already exist.
174 }
175
176 // static
177 void ProfileShortcutManagerWin::RenameChromeDesktopShortcutForProfile(
178 const string16& old_shortcut,
179 const string16& new_shortcut) {
180 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
181 FilePath shortcut_path;
182 if (ShellUtil::GetDesktopPath(false, // User's directory instead of system.
183 &shortcut_path)) {
184 FilePath old_profile = shortcut_path.Append(old_shortcut);
185 FilePath new_profile = shortcut_path.Append(new_shortcut);
186 file_util::Move(old_profile, new_profile);
187 }
188 }
189
190 // static
191 void ProfileShortcutManagerWin::UpdateChromeDesktopShortcutForProfile(
192 const string16& shortcut,
193 const string16& arguments) {
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