Chromium Code Reviews| OLD | NEW |
|---|---|
| 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" |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 208 | 208 |
| 209 } // namespace | 209 } // namespace |
| 210 | 210 |
| 211 ProfileShortcutManagerWin::ProfileShortcutManagerWin() { | 211 ProfileShortcutManagerWin::ProfileShortcutManagerWin() { |
| 212 } | 212 } |
| 213 | 213 |
| 214 ProfileShortcutManagerWin::~ProfileShortcutManagerWin() { | 214 ProfileShortcutManagerWin::~ProfileShortcutManagerWin() { |
| 215 } | 215 } |
| 216 | 216 |
| 217 void ProfileShortcutManagerWin::OnProfileAdded( | 217 void ProfileShortcutManagerWin::OnProfileAdded( |
| 218 const string16& profile_name, | 218 const FilePath& profile_path) { |
| 219 const string16& profile_base_dir, | 219 ProfileInfoCache& cache = |
| 220 const FilePath& profile_path, | 220 g_browser_process->profile_manager()->GetProfileInfoCache(); |
| 221 const gfx::Image* avatar_image) { | 221 size_t index = cache.GetIndexOfProfileWithPath(profile_path); |
| 222 | |
| 222 // Launch task to add shortcut to desktop on Windows. If this is the very | 223 // Launch task to add shortcut to desktop on Windows. If this is the very |
| 223 // first profile created, don't add the user name to the shortcut. | 224 // first profile created, don't add the user name to the shortcut. |
| 224 // TODO(mirandac): respect master_preferences choice to create no shortcuts | 225 // TODO(mirandac): respect master_preferences choice to create no shortcuts |
| 225 // (see http://crbug.com/104463) | 226 // (see http://crbug.com/104463) |
| 226 if (g_browser_process->profile_manager()->GetNumberOfProfiles() > 1) { | 227 if (g_browser_process->profile_manager()->GetNumberOfProfiles() > 1) { |
| 227 { | 228 { |
| 228 // We make a copy of the Image to ensure that the underlying image data is | 229 // We make a copy of the Image to ensure that the underlying image data is |
| 229 // AddRef'd, in case the original copy gets deleted. | 230 // AddRef'd, in case the original copy gets deleted. |
| 230 gfx::Image* avatar_copy = avatar_image ? | 231 gfx::Image* avatar_copy = |
| 231 new gfx::Image(*avatar_image) : NULL; | 232 new gfx::Image(cache.GetAvatarIconOfProfileAtIndex(index)); |
| 233 string16 profile_name = cache.GetNameOfProfileAtIndex(index); | |
| 234 string16 profile_base_dir = | |
| 235 UTF8ToUTF16(profile_path.BaseName().MaybeAsASCII()); | |
|
sail
2011/12/21 21:03:12
since this code is windows only you don't need the
| |
| 232 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 236 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 233 base::Bind(&CreateChromeDesktopShortcutForProfile, | 237 base::Bind(&CreateChromeDesktopShortcutForProfile, |
| 234 profile_name, profile_base_dir, profile_path, | 238 profile_name, profile_base_dir, profile_path, |
| 235 base::Owned(avatar_copy), true)); | 239 base::Owned(avatar_copy), true)); |
| 236 } | 240 } |
| 237 | 241 |
| 238 // If this is the second existing multi-user account created, change the | 242 // If this is the second existing multi-user account created, change the |
| 239 // original shortcut use the first profile's details (name, badge, | 243 // original shortcut use the first profile's details (name, badge, |
| 240 // argument). | 244 // argument). |
| 241 ProfileInfoCache& cache = | |
| 242 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
| 243 if (cache.GetNumberOfProfiles() == 2) { | 245 if (cache.GetNumberOfProfiles() == 2) { |
| 244 // Get the index of the first profile, based on the index of the second | 246 // Get the index of the first profile, based on the index of the second |
| 245 // profile. It's either 0 or 1, whichever the second profile isn't. | 247 // profile. It's either 0 or 1, whichever the second profile isn't. |
| 246 size_t first_index = 0; | 248 size_t first_index = 0; |
| 247 if (cache.GetIndexOfProfileWithPath(profile_path) == 0) | 249 if (cache.GetIndexOfProfileWithPath(profile_path) == 0) |
| 248 first_index = 1; | 250 first_index = 1; |
| 249 string16 first_name = cache.GetNameOfProfileAtIndex(first_index); | 251 string16 first_name = cache.GetNameOfProfileAtIndex(first_index); |
| 250 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | 252 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 251 | 253 |
| 252 string16 old_shortcut; | 254 string16 old_shortcut; |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 279 } | 281 } |
| 280 } | 282 } |
| 281 } else { // Only one profile, so create original shortcut, with no avatar. | 283 } else { // Only one profile, so create original shortcut, with no avatar. |
| 282 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 284 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 283 base::Bind(&CreateChromeDesktopShortcutForProfile, | 285 base::Bind(&CreateChromeDesktopShortcutForProfile, |
| 284 L"", L"", FilePath(), static_cast<gfx::Image*>(NULL), true)); | 286 L"", L"", FilePath(), static_cast<gfx::Image*>(NULL), true)); |
| 285 } | 287 } |
| 286 } | 288 } |
| 287 | 289 |
| 288 void ProfileShortcutManagerWin::OnProfileWillBeRemoved( | 290 void ProfileShortcutManagerWin::OnProfileWillBeRemoved( |
| 289 const string16& profile_name) { | 291 const FilePath& profile_path) { |
| 292 ProfileInfoCache& cache = | |
| 293 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
| 294 string16 profile_name = cache.GetNameOfProfileAtIndex( | |
| 295 cache.GetIndexOfProfileWithPath(profile_path)); | |
| 290 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | 296 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 291 string16 shortcut; | 297 string16 shortcut; |
| 292 if (ShellUtil::GetChromeShortcutName(dist, false, profile_name, &shortcut)) { | 298 if (ShellUtil::GetChromeShortcutName(dist, false, profile_name, &shortcut)) { |
| 293 std::vector<string16> shortcuts(1, shortcut); | 299 std::vector<string16> shortcuts(1, shortcut); |
| 294 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 300 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 295 base::Bind(&CallShellUtilBoolFunction, | 301 base::Bind(&CallShellUtilBoolFunction, |
| 296 base::Bind( | 302 base::Bind( |
| 297 &ShellUtil::RemoveChromeDesktopShortcutsWithAppendedNames, | 303 &ShellUtil::RemoveChromeDesktopShortcutsWithAppendedNames, |
| 298 shortcuts))); | 304 shortcuts))); |
| 299 } | 305 } |
| 300 } | 306 } |
| 301 | 307 |
| 302 void ProfileShortcutManagerWin::OnProfileWasRemoved( | 308 void ProfileShortcutManagerWin::OnProfileWasRemoved( |
| 309 const FilePath& profile_path, | |
| 303 const string16& profile_name) { | 310 const string16& profile_name) { |
| 304 // If there is one profile left, we want to remove the badge and name from it. | 311 // If there is one profile left, we want to remove the badge and name from it. |
| 305 ProfileInfoCache& cache = | 312 ProfileInfoCache& cache = |
| 306 g_browser_process->profile_manager()->GetProfileInfoCache(); | 313 g_browser_process->profile_manager()->GetProfileInfoCache(); |
| 307 if (cache.GetNumberOfProfiles() != 1) | 314 if (cache.GetNumberOfProfiles() != 1) |
| 308 return; | 315 return; |
| 309 | 316 |
| 310 // TODO(stevet): Now that we've sunk our fangs onto ProfileInfoCache, we | 317 FilePath last_profile_path = cache.GetPathOfProfileAtIndex(0); |
| 311 // should clean up the ProfileInfoCacheObserver interface and its users | |
| 312 // (including us) to not pass every parameter through and instead query the | |
| 313 // cache when needed. | |
| 314 FilePath profile_path = cache.GetPathOfProfileAtIndex(0); | |
| 315 string16 old_shortcut; | 318 string16 old_shortcut; |
| 316 string16 new_shortcut; | 319 string16 new_shortcut; |
| 317 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | 320 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 318 if (ShellUtil::GetChromeShortcutName( | 321 if (ShellUtil::GetChromeShortcutName( |
| 319 dist, false, cache.GetNameOfProfileAtIndex(0), &old_shortcut) && | 322 dist, false, cache.GetNameOfProfileAtIndex(0), &old_shortcut) && |
| 320 ShellUtil::GetChromeShortcutName( | 323 ShellUtil::GetChromeShortcutName( |
| 321 dist, false, L"", &new_shortcut)) { | 324 dist, false, L"", &new_shortcut)) { |
| 322 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 325 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 323 base::Bind(&RenameChromeDesktopShortcutForProfile, | 326 base::Bind(&RenameChromeDesktopShortcutForProfile, |
| 324 old_shortcut, | 327 old_shortcut, |
| 325 new_shortcut)); | 328 new_shortcut)); |
| 326 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 329 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 327 base::Bind(&UpdateChromeDesktopShortcutForProfile, | 330 base::Bind(&UpdateChromeDesktopShortcutForProfile, |
| 328 new_shortcut, | 331 new_shortcut, |
| 329 CreateProfileShortcutSwitch(UTF8ToUTF16( | 332 CreateProfileShortcutSwitch(UTF8ToUTF16( |
| 330 profile_path.BaseName().MaybeAsASCII())), | 333 last_profile_path.BaseName().MaybeAsASCII())), |
| 331 profile_path, | 334 last_profile_path, |
| 332 static_cast<gfx::Image*>(NULL))); | 335 static_cast<gfx::Image*>(NULL))); |
| 333 } | 336 } |
| 334 } | 337 } |
| 335 | 338 |
| 336 void ProfileShortcutManagerWin::OnProfileNameChanged( | 339 void ProfileShortcutManagerWin::OnProfileNameChanged( |
| 337 const string16& old_profile_name, | 340 const FilePath& profile_path, |
| 338 const string16& new_profile_name) { | 341 const string16& old_profile_name) { |
| 339 // Launch task to change name of desktop shortcut on Windows. | 342 // Launch task to change name of desktop shortcut on Windows. |
| 340 // TODO(mirandac): respect master_preferences choice to create no shortcuts | 343 // TODO(mirandac): respect master_preferences choice to create no shortcuts |
| 341 // (see http://crbug.com/104463) | 344 // (see http://crbug.com/104463) |
| 345 ProfileInfoCache& cache = | |
| 346 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
| 347 string16 new_profile_name = cache.GetNameOfProfileAtIndex( | |
| 348 cache.GetIndexOfProfileWithPath(profile_path)); | |
| 349 | |
| 342 string16 old_shortcut; | 350 string16 old_shortcut; |
| 343 string16 new_shortcut; | 351 string16 new_shortcut; |
| 344 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | 352 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 345 if (ShellUtil::GetChromeShortcutName( | 353 if (ShellUtil::GetChromeShortcutName( |
| 346 dist, false, old_profile_name, &old_shortcut) && | 354 dist, false, old_profile_name, &old_shortcut) && |
| 347 ShellUtil::GetChromeShortcutName( | 355 ShellUtil::GetChromeShortcutName( |
| 348 dist, false, new_profile_name, &new_shortcut)) { | 356 dist, false, new_profile_name, &new_shortcut)) { |
| 349 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 357 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 350 base::Bind(&RenameChromeDesktopShortcutForProfile, | 358 base::Bind(&RenameChromeDesktopShortcutForProfile, |
| 351 old_shortcut, | 359 old_shortcut, |
| 352 new_shortcut)); | 360 new_shortcut)); |
| 353 } | 361 } |
| 354 } | 362 } |
| 355 | 363 |
| 356 void ProfileShortcutManagerWin::OnProfileAvatarChanged( | 364 void ProfileShortcutManagerWin::OnProfileAvatarChanged( |
| 357 const string16& profile_name, | 365 const FilePath& profile_path) { |
| 358 const string16& profile_base_dir, | 366 ProfileInfoCache& cache = |
| 359 const FilePath& profile_path, | 367 g_browser_process->profile_manager()->GetProfileInfoCache(); |
| 360 const gfx::Image* avatar_image) { | 368 size_t index = cache.GetIndexOfProfileWithPath(profile_path); |
| 369 string16 profile_name = cache.GetNameOfProfileAtIndex(index); | |
| 370 string16 profile_base_dir = | |
| 371 UTF8ToUTF16(profile_path.BaseName().MaybeAsASCII()); | |
| 372 const gfx::Image& avatar_image = cache.GetAvatarIconOfProfileAtIndex(index); | |
| 373 | |
| 361 // Launch task to change the icon of the desktop shortcut on windows. | 374 // Launch task to change the icon of the desktop shortcut on windows. |
| 362 string16 new_shortcut; | 375 string16 new_shortcut; |
| 363 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | 376 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 364 if (ShellUtil::GetChromeShortcutName(dist, false, profile_name, | 377 if (ShellUtil::GetChromeShortcutName(dist, false, profile_name, |
| 365 &new_shortcut)) { | 378 &new_shortcut)) { |
| 366 // We make a copy of the Image to ensure that the underlying image data is | 379 // We make a copy of the Image to ensure that the underlying image data is |
| 367 // AddRef'd, in case the original copy gets deleted. | 380 // AddRef'd, in case the original copy gets deleted. |
| 368 gfx::Image* avatar_copy = avatar_image ? | 381 gfx::Image* avatar_copy = new gfx::Image(avatar_image); |
| 369 new gfx::Image(*avatar_image) : NULL; | |
| 370 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 382 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 371 base::Bind(&UpdateChromeDesktopShortcutForProfile, | 383 base::Bind(&UpdateChromeDesktopShortcutForProfile, |
| 372 new_shortcut, | 384 new_shortcut, |
| 373 CreateProfileShortcutSwitch(profile_base_dir), | 385 CreateProfileShortcutSwitch(profile_base_dir), |
| 374 profile_path, | 386 profile_path, |
| 375 base::Owned(avatar_copy))); | 387 base::Owned(avatar_copy))); |
| 376 } | 388 } |
| 377 } | 389 } |
| 378 | 390 |
| 379 // static | 391 // static |
| 380 std::vector<string16> ProfileShortcutManagerWin::GenerateShortcutsFromProfiles( | 392 std::vector<string16> ProfileShortcutManagerWin::GenerateShortcutsFromProfiles( |
| 381 const std::vector<string16>& profile_names) { | 393 const std::vector<string16>& profile_names) { |
| 382 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | 394 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 383 std::vector<string16> shortcuts; | 395 std::vector<string16> shortcuts; |
| 384 shortcuts.reserve(profile_names.size()); | 396 shortcuts.reserve(profile_names.size()); |
| 385 for (std::vector<string16>::const_iterator it = profile_names.begin(); | 397 for (std::vector<string16>::const_iterator it = profile_names.begin(); |
| 386 it != profile_names.end(); | 398 it != profile_names.end(); |
| 387 ++it) { | 399 ++it) { |
| 388 string16 shortcut; | 400 string16 shortcut; |
| 389 if (ShellUtil::GetChromeShortcutName(dist, false, *it, &shortcut)) | 401 if (ShellUtil::GetChromeShortcutName(dist, false, *it, &shortcut)) |
| 390 shortcuts.push_back(shortcut); | 402 shortcuts.push_back(shortcut); |
| 391 } | 403 } |
| 392 return shortcuts; | 404 return shortcuts; |
| 393 } | 405 } |
| OLD | NEW |