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_win.h" |
6 | 6 |
7 #include <shlobj.h> // For SHChangeNotify(). | 7 #include <shlobj.h> // For SHChangeNotify(). |
8 | 8 |
9 #include <algorithm> | |
10 #include <set> | |
9 #include <string> | 11 #include <string> |
10 #include <vector> | 12 #include <vector> |
11 | 13 |
12 #include "base/bind.h" | 14 #include "base/bind.h" |
13 #include "base/command_line.h" | 15 #include "base/command_line.h" |
14 #include "base/files/file_enumerator.h" | 16 #include "base/files/file_enumerator.h" |
15 #include "base/files/file_util.h" | 17 #include "base/files/file_util.h" |
16 #include "base/path_service.h" | 18 #include "base/path_service.h" |
17 #include "base/prefs/pref_service.h" | 19 #include "base/prefs/pref_service.h" |
18 #include "base/strings/string16.h" | 20 #include "base/strings/string16.h" |
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
282 return false; | 284 return false; |
283 | 285 |
284 base::FilePath target_path; | 286 base::FilePath target_path; |
285 if (!base::win::ResolveShortcut(path, &target_path, command_line)) | 287 if (!base::win::ResolveShortcut(path, &target_path, command_line)) |
286 return false; | 288 return false; |
287 // One of the paths may be in short (elided) form. Compare long paths to | 289 // One of the paths may be in short (elided) form. Compare long paths to |
288 // ensure these are still properly matched. | 290 // ensure these are still properly matched. |
289 return ConvertToLongPath(target_path) == ConvertToLongPath(chrome_exe); | 291 return ConvertToLongPath(target_path) == ConvertToLongPath(chrome_exe); |
290 } | 292 } |
291 | 293 |
292 // Populates |paths| with the file paths of Chrome desktop shortcuts that have | 294 // A functor checks if |path| is the Chrome desktop shortcut (|chrome_exe|) |
293 // the specified |command_line|. If |include_empty_command_lines| is true, | 295 // that have the specified |command_line|. If |include_empty_command_lines| is |
294 // Chrome desktop shortcuts with empty command lines will also be included. | 296 // true Chrome desktop shortcuts with empty command lines will also be included. |
295 void ListDesktopShortcutsWithCommandLine(const base::FilePath& chrome_exe, | 297 struct ChromeCommandLineFilter { |
296 const base::string16& command_line, | 298 const base::FilePath& chrome_exe; |
297 bool include_empty_command_lines, | 299 const base::string16& command_line; |
298 std::vector<base::FilePath>* paths) { | 300 bool include_empty_command_lines; |
299 base::FilePath user_shortcuts_directory; | |
300 if (!GetDesktopShortcutsDirectories(&user_shortcuts_directory, NULL)) | |
301 return; | |
302 | 301 |
303 base::FileEnumerator enumerator(user_shortcuts_directory, false, | 302 ChromeCommandLineFilter(const base::FilePath& chrome_exe, |
304 base::FileEnumerator::FILES); | 303 const base::string16& command_line, |
305 for (base::FilePath path = enumerator.Next(); !path.empty(); | 304 bool include_empty_command_lines) |
306 path = enumerator.Next()) { | 305 : chrome_exe(chrome_exe), |
306 command_line(command_line), | |
307 include_empty_command_lines(include_empty_command_lines) {} | |
308 | |
309 bool operator()(const base::FilePath& path) const { | |
307 base::string16 shortcut_command_line; | 310 base::string16 shortcut_command_line; |
308 if (!IsChromeShortcut(path, chrome_exe, &shortcut_command_line)) | 311 if (!IsChromeShortcut(path, chrome_exe, &shortcut_command_line)) |
309 continue; | 312 return false; |
310 | 313 |
311 // TODO(asvitkine): Change this to build a CommandLine object and ensure all | 314 // TODO(asvitkine): Change this to build a CommandLine object and ensure all |
312 // args from |command_line| are present in the shortcut's CommandLine. This | 315 // args from |command_line| are present in the shortcut's CommandLine. This |
313 // will be more robust when |command_line| contains multiple args. | 316 // will be more robust when |command_line| contains multiple args. |
314 if ((shortcut_command_line.empty() && include_empty_command_lines) || | 317 if ((shortcut_command_line.empty() && include_empty_command_lines) || |
315 (shortcut_command_line.find(command_line) != base::string16::npos)) { | 318 (shortcut_command_line.find(command_line) != base::string16::npos)) { |
316 paths->push_back(path); | 319 return true; |
317 } | 320 } |
321 return false; | |
318 } | 322 } |
323 }; | |
324 | |
325 // Get the file paths of desktop files and folders optionally filtered | |
326 // by |filter|. | |
327 std::set<base::FilePath> ListUserDesktopContents( | |
328 const ChromeCommandLineFilter* filter) { | |
329 std::set<base::FilePath> result; | |
330 | |
331 base::FilePath user_shortcuts_directory; | |
332 if (!GetDesktopShortcutsDirectories(&user_shortcuts_directory, nullptr)) | |
333 return result; | |
334 | |
335 base::FileEnumerator enumerator( | |
336 user_shortcuts_directory, false, | |
337 base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES); | |
338 for (base::FilePath path = enumerator.Next(); !path.empty(); | |
339 path = enumerator.Next()) { | |
340 if (!filter || (*filter)(path)) | |
341 result.insert(path); | |
342 } | |
343 return result; | |
319 } | 344 } |
320 | 345 |
321 // Renames the given desktop shortcut and informs the shell of this change. | 346 // Renames the given desktop shortcut and informs the shell of this change. |
322 bool RenameDesktopShortcut(const base::FilePath& old_shortcut_path, | 347 bool RenameDesktopShortcut(const base::FilePath& old_shortcut_path, |
323 const base::FilePath& new_shortcut_path) { | 348 const base::FilePath& new_shortcut_path) { |
324 if (!base::Move(old_shortcut_path, new_shortcut_path)) | 349 if (!base::Move(old_shortcut_path, new_shortcut_path)) |
325 return false; | 350 return false; |
326 | 351 |
327 // Notify the shell of the rename, which allows the icon to keep its position | 352 // Notify the shell of the rename, which allows the icon to keep its position |
328 // on the desktop when renamed. Note: This only works if either SHCNF_FLUSH or | 353 // on the desktop when renamed. Note: This only works if either SHCNF_FLUSH or |
329 // SHCNF_FLUSHNOWAIT is specified as a flag. | 354 // SHCNF_FLUSHNOWAIT is specified as a flag. |
330 SHChangeNotify(SHCNE_RENAMEITEM, SHCNF_PATH | SHCNF_FLUSHNOWAIT, | 355 SHChangeNotify(SHCNE_RENAMEITEM, SHCNF_PATH | SHCNF_FLUSHNOWAIT, |
331 old_shortcut_path.value().c_str(), | 356 old_shortcut_path.value().c_str(), |
332 new_shortcut_path.value().c_str()); | 357 new_shortcut_path.value().c_str()); |
333 return true; | 358 return true; |
334 } | 359 } |
335 | 360 |
336 // Renames an existing Chrome desktop profile shortcut. Must be called on the | 361 // Renames an existing Chrome desktop profile shortcut. Must be called on the |
337 // FILE thread. | 362 // FILE thread. |
363 // |profile_shortcuts| are desktop shortcuts that run Chrome under | |
364 // that profile (there can be several shortcuts for profile). | |
Alexei Svitkine (slow)
2016/01/18 16:43:08
Nit: "are Chrome desktop shortcuts for the profile
| |
365 // |desktop_contents| is the collection of all user desktop shortcuts | |
366 // (not only Chrome). It is used to make an unique shortcut for the | |
367 // |new_profile_name| among all shortcuts. | |
368 // This function updates |profile_shortcuts| and |desktop_contents| respectively | |
369 // when renaming occurs. | |
338 void RenameChromeDesktopShortcutForProfile( | 370 void RenameChromeDesktopShortcutForProfile( |
339 const base::string16& old_shortcut_filename, | 371 const base::string16& old_profile_name, |
340 const base::string16& new_shortcut_filename) { | 372 const base::string16& new_profile_name, |
373 std::set<base::FilePath>* profile_shortcuts, | |
374 std::set<base::FilePath>* desktop_contents) { | |
375 DCHECK(profile_shortcuts); | |
376 DCHECK(desktop_contents); | |
341 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 377 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
342 | 378 |
343 base::FilePath user_shortcuts_directory; | 379 base::FilePath user_shortcuts_directory; |
344 base::FilePath system_shortcuts_directory; | 380 base::FilePath system_shortcuts_directory; |
345 if (!GetDesktopShortcutsDirectories(&user_shortcuts_directory, | 381 if (!GetDesktopShortcutsDirectories(&user_shortcuts_directory, |
346 &system_shortcuts_directory)) { | 382 &system_shortcuts_directory)) { |
347 return; | 383 return; |
348 } | 384 } |
349 | 385 |
350 const base::FilePath old_shortcut_path = | 386 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); |
351 user_shortcuts_directory.Append(old_shortcut_filename); | 387 |
388 // Get a new unique shortcut name. | |
389 const base::string16 new_shortcut_filename = | |
390 profiles::internal::GetUniqueShortcutFilenameForProfile( | |
391 new_profile_name, distribution, *desktop_contents); | |
352 const base::FilePath new_shortcut_path = | 392 const base::FilePath new_shortcut_path = |
353 user_shortcuts_directory.Append(new_shortcut_filename); | 393 user_shortcuts_directory.Append(new_shortcut_filename); |
354 | 394 |
355 if (base::PathExists(old_shortcut_path)) { | 395 if (!profile_shortcuts->empty()) { |
396 // From all profile_shortcuts choose only with known (canonical) name. | |
Alexei Svitkine (slow)
2016/01/18 16:43:08
Nit: "choose the one with a known (canonical) name
| |
397 profiles::internal::ShortcutFilenameMatcher matcher(old_profile_name, | |
398 distribution); | |
399 auto it = std::find_if(profile_shortcuts->begin(), profile_shortcuts->end(), | |
400 [&matcher](const base::FilePath& p) { | |
401 return matcher.IsCanonical(p.BaseName().value()); | |
402 }); | |
403 // If all profile_shortcuts were renamed by user, respect it and do not | |
404 // rename. | |
405 if (it == profile_shortcuts->end()) | |
406 return; | |
407 const base::FilePath old_shortcut_path = *it; | |
408 | |
356 // Rename the old shortcut unless a system-level shortcut exists at the | 409 // Rename the old shortcut unless a system-level shortcut exists at the |
357 // destination, in which case the old shortcut is simply deleted. | 410 // destination, in which case the old shortcut is simply deleted. |
358 const base::FilePath possible_new_system_shortcut = | 411 const base::FilePath possible_new_system_shortcut = |
359 system_shortcuts_directory.Append(new_shortcut_filename); | 412 system_shortcuts_directory.Append(new_shortcut_filename); |
360 if (base::PathExists(possible_new_system_shortcut)) | 413 if (base::PathExists(possible_new_system_shortcut)) { |
361 base::DeleteFile(old_shortcut_path, false); | 414 if (base::DeleteFile(old_shortcut_path, false)) { |
362 else if (!RenameDesktopShortcut(old_shortcut_path, new_shortcut_path)) | 415 profile_shortcuts->erase(old_shortcut_path); |
363 DLOG(ERROR) << "Could not rename Windows profile desktop shortcut."; | 416 desktop_contents->erase(old_shortcut_path); |
417 } else { | |
418 DLOG(ERROR) << "Could not delete Windows profile desktop shortcut."; | |
419 } | |
420 } else { | |
421 if (RenameDesktopShortcut(old_shortcut_path, new_shortcut_path)) { | |
422 profile_shortcuts->erase(old_shortcut_path); | |
423 desktop_contents->erase(old_shortcut_path); | |
424 profile_shortcuts->insert(new_shortcut_path); | |
425 desktop_contents->insert(new_shortcut_path); | |
426 } else { | |
427 DLOG(ERROR) << "Could not rename Windows profile desktop shortcut."; | |
428 } | |
429 } | |
364 } else { | 430 } else { |
365 // If the shortcut does not exist, it may have been renamed by the user. In | 431 // If the shortcut does not exist, it may have been deleted by the user. |
366 // that case, its name should not be changed. | |
367 // It's also possible that a system-level shortcut exists instead - this | 432 // It's also possible that a system-level shortcut exists instead - this |
368 // should only be the case for the original Chrome shortcut from an | 433 // should only be the case for the original Chrome shortcut from an |
369 // installation. If that's the case, copy that one over - it will get its | 434 // installation. If that's the case, copy that one over - it will get its |
370 // properties updated by | 435 // properties updated by |
371 // |CreateOrUpdateDesktopShortcutsAndIconForProfile()|. | 436 // |CreateOrUpdateDesktopShortcutsAndIconForProfile()|. |
437 const auto old_shortcut_filename = | |
438 profiles::internal::GetShortcutFilenameForProfile(old_profile_name, | |
439 distribution); | |
372 const base::FilePath possible_old_system_shortcut = | 440 const base::FilePath possible_old_system_shortcut = |
373 system_shortcuts_directory.Append(old_shortcut_filename); | 441 system_shortcuts_directory.Append(old_shortcut_filename); |
374 if (base::PathExists(possible_old_system_shortcut)) | 442 if (base::PathExists(possible_old_system_shortcut)) { |
375 base::CopyFile(possible_old_system_shortcut, new_shortcut_path); | 443 if (base::CopyFile(possible_old_system_shortcut, new_shortcut_path)) { |
444 profile_shortcuts->insert(new_shortcut_path); | |
445 desktop_contents->insert(new_shortcut_path); | |
446 } else { | |
447 DLOG(ERROR) << "Could not copy Windows profile desktop shortcut."; | |
448 } | |
449 } | |
376 } | 450 } |
377 } | 451 } |
378 | 452 |
379 struct CreateOrUpdateShortcutsParams { | 453 struct CreateOrUpdateShortcutsParams { |
380 CreateOrUpdateShortcutsParams( | 454 CreateOrUpdateShortcutsParams( |
381 base::FilePath profile_path, | 455 base::FilePath profile_path, |
382 ProfileShortcutManagerWin::CreateOrUpdateMode create_mode, | 456 ProfileShortcutManagerWin::CreateOrUpdateMode create_mode, |
383 ProfileShortcutManagerWin::NonProfileShortcutAction action) | 457 ProfileShortcutManagerWin::NonProfileShortcutAction action) |
384 : create_mode(create_mode), action(action), profile_path(profile_path) {} | 458 : create_mode(create_mode), action(action), profile_path(profile_path) {} |
385 ~CreateOrUpdateShortcutsParams() {} | 459 ~CreateOrUpdateShortcutsParams() {} |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
421 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { | 495 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
422 NOTREACHED(); | 496 NOTREACHED(); |
423 return; | 497 return; |
424 } | 498 } |
425 | 499 |
426 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); | 500 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); |
427 // Ensure that the distribution supports creating shortcuts. If it doesn't, | 501 // Ensure that the distribution supports creating shortcuts. If it doesn't, |
428 // the following code may result in NOTREACHED() being hit. | 502 // the following code may result in NOTREACHED() being hit. |
429 DCHECK(distribution->CanCreateDesktopShortcuts()); | 503 DCHECK(distribution->CanCreateDesktopShortcuts()); |
430 | 504 |
505 std::set<base::FilePath> desktop_contents = ListUserDesktopContents(nullptr); | |
506 | |
507 const base::string16 command_line = | |
508 profiles::internal::CreateProfileShortcutFlags(params.profile_path); | |
509 ChromeCommandLineFilter filter( | |
510 chrome_exe, command_line, | |
511 params.action == ProfileShortcutManagerWin::UPDATE_NON_PROFILE_SHORTCUTS); | |
512 | |
513 std::set<base::FilePath> shortcuts; | |
514 // Do not call ListUserDesktopContents again (but with filter) to avoid | |
515 // excess work inside it. Just reuse non-filtered desktop_contents. | |
516 // We need both of them (desktop_contents and shortcuts) later. | |
517 std::copy_if(desktop_contents.begin(), desktop_contents.end(), | |
518 std::inserter(shortcuts, shortcuts.begin()), filter); | |
519 | |
431 if (params.old_profile_name != params.profile_name) { | 520 if (params.old_profile_name != params.profile_name) { |
432 const base::string16 old_shortcut_filename = | 521 RenameChromeDesktopShortcutForProfile(params.old_profile_name, |
433 profiles::internal::GetShortcutFilenameForProfile( | 522 params.profile_name, &shortcuts, |
434 params.old_profile_name, | 523 &desktop_contents); |
435 distribution); | |
436 const base::string16 new_shortcut_filename = | |
437 profiles::internal::GetShortcutFilenameForProfile(params.profile_name, | |
438 distribution); | |
439 RenameChromeDesktopShortcutForProfile(old_shortcut_filename, | |
440 new_shortcut_filename); | |
441 } | 524 } |
442 | 525 |
443 ShellUtil::ShortcutProperties properties(ShellUtil::CURRENT_USER); | 526 ShellUtil::ShortcutProperties properties(ShellUtil::CURRENT_USER); |
444 installer::Product product(distribution); | 527 installer::Product product(distribution); |
445 product.AddDefaultShortcutProperties(chrome_exe, &properties); | 528 product.AddDefaultShortcutProperties(chrome_exe, &properties); |
446 | 529 |
447 const base::string16 command_line = | |
448 profiles::internal::CreateProfileShortcutFlags(params.profile_path); | |
449 | 530 |
450 // Only set the profile-specific properties when |profile_name| is non empty. | 531 // Only set the profile-specific properties when |profile_name| is non empty. |
451 // If it is empty, it means the shortcut being created should be a regular, | 532 // If it is empty, it means the shortcut being created should be a regular, |
452 // non-profile Chrome shortcut. | 533 // non-profile Chrome shortcut. |
453 if (!params.profile_name.empty()) { | 534 if (!params.profile_name.empty()) { |
454 properties.set_arguments(command_line); | 535 properties.set_arguments(command_line); |
455 properties.set_icon(shortcut_icon, 0); | 536 properties.set_icon(shortcut_icon, 0); |
456 } else { | 537 } else { |
457 // Set the arguments explicitly to the empty string to ensure that | 538 // Set the arguments explicitly to the empty string to ensure that |
458 // |ShellUtil::CreateOrUpdateShortcut| updates that part of the shortcut. | 539 // |ShellUtil::CreateOrUpdateShortcut| updates that part of the shortcut. |
459 properties.set_arguments(base::string16()); | 540 properties.set_arguments(base::string16()); |
460 } | 541 } |
461 | 542 |
462 properties.set_app_id( | 543 properties.set_app_id( |
463 ShellIntegration::GetChromiumModelIdForProfile(params.profile_path)); | 544 ShellIntegration::GetChromiumModelIdForProfile(params.profile_path)); |
464 | 545 |
465 ShellUtil::ShortcutOperation operation = | 546 ShellUtil::ShortcutOperation operation = |
466 ShellUtil::SHELL_SHORTCUT_REPLACE_EXISTING; | 547 ShellUtil::SHELL_SHORTCUT_REPLACE_EXISTING; |
467 | 548 |
468 std::vector<base::FilePath> shortcuts; | |
469 ListDesktopShortcutsWithCommandLine(chrome_exe, command_line, | |
470 params.action == ProfileShortcutManagerWin::UPDATE_NON_PROFILE_SHORTCUTS, | |
471 &shortcuts); | |
472 if (params.create_mode == ProfileShortcutManagerWin::CREATE_WHEN_NONE_FOUND && | 549 if (params.create_mode == ProfileShortcutManagerWin::CREATE_WHEN_NONE_FOUND && |
473 shortcuts.empty()) { | 550 shortcuts.empty()) { |
474 const base::string16 shortcut_name = | 551 const base::string16 shortcut_name = |
475 profiles::internal::GetShortcutFilenameForProfile(params.profile_name, | 552 profiles::internal::GetUniqueShortcutFilenameForProfile( |
476 distribution); | 553 params.profile_name, distribution, desktop_contents); |
477 shortcuts.push_back(base::FilePath(shortcut_name)); | 554 shortcuts.insert(base::FilePath(shortcut_name)); |
478 operation = ShellUtil::SHELL_SHORTCUT_CREATE_IF_NO_SYSTEM_LEVEL; | 555 operation = ShellUtil::SHELL_SHORTCUT_CREATE_IF_NO_SYSTEM_LEVEL; |
479 } | 556 } |
480 | 557 |
481 for (size_t i = 0; i < shortcuts.size(); ++i) { | 558 for (const auto& shortcut : shortcuts) { |
482 const base::FilePath shortcut_name = | 559 const base::FilePath shortcut_name = shortcut.BaseName().RemoveExtension(); |
483 shortcuts[i].BaseName().RemoveExtension(); | |
484 properties.set_shortcut_name(shortcut_name.value()); | 560 properties.set_shortcut_name(shortcut_name.value()); |
485 ShellUtil::CreateOrUpdateShortcut(ShellUtil::SHORTCUT_LOCATION_DESKTOP, | 561 ShellUtil::CreateOrUpdateShortcut(ShellUtil::SHORTCUT_LOCATION_DESKTOP, |
486 distribution, properties, operation); | 562 distribution, properties, operation); |
487 } | 563 } |
488 } | 564 } |
489 | 565 |
490 // Returns true if any desktop shortcuts exist with target |chrome_exe|, | 566 // Returns true if any desktop shortcuts exist with target |chrome_exe|, |
491 // regardless of their command line arguments. | 567 // regardless of their command line arguments. |
492 bool ChromeDesktopShortcutsExist(const base::FilePath& chrome_exe) { | 568 bool ChromeDesktopShortcutsExist(const base::FilePath& chrome_exe) { |
493 base::FilePath user_shortcuts_directory; | 569 base::FilePath user_shortcuts_directory; |
(...skipping 20 matching lines...) Expand all Loading... | |
514 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 590 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
515 | 591 |
516 base::FilePath chrome_exe; | 592 base::FilePath chrome_exe; |
517 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { | 593 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
518 NOTREACHED(); | 594 NOTREACHED(); |
519 return; | 595 return; |
520 } | 596 } |
521 | 597 |
522 const base::string16 command_line = | 598 const base::string16 command_line = |
523 profiles::internal::CreateProfileShortcutFlags(profile_path); | 599 profiles::internal::CreateProfileShortcutFlags(profile_path); |
524 std::vector<base::FilePath> shortcuts; | 600 ChromeCommandLineFilter filter(chrome_exe, command_line, false); |
525 ListDesktopShortcutsWithCommandLine(chrome_exe, command_line, false, | 601 const std::set<base::FilePath> shortcuts = ListUserDesktopContents(&filter); |
526 &shortcuts); | |
527 | 602 |
528 for (size_t i = 0; i < shortcuts.size(); ++i) { | 603 for (const auto& shortcut : shortcuts) { |
529 // Use base::DeleteFile() instead of ShellUtil::RemoveShortcuts(), as the | 604 // Use base::DeleteFile() instead of ShellUtil::RemoveShortcuts(), as the |
530 // latter causes non-profile taskbar shortcuts to be removed since it | 605 // latter causes non-profile taskbar shortcuts to be removed since it |
531 // doesn't consider the command-line of the shortcuts it deletes. | 606 // doesn't consider the command-line of the shortcuts it deletes. |
532 // TODO(huangs): Refactor with ShellUtil::RemoveShortcuts(). | 607 // TODO(huangs): Refactor with ShellUtil::RemoveShortcuts(). |
533 base::win::UnpinShortcutFromTaskbar(shortcuts[i]); | 608 base::win::UnpinShortcutFromTaskbar(shortcut); |
534 base::DeleteFile(shortcuts[i], false); | 609 base::DeleteFile(shortcut, false); |
535 // Notify the shell that the shortcut was deleted to ensure desktop refresh. | 610 // Notify the shell that the shortcut was deleted to ensure desktop refresh. |
536 SHChangeNotify(SHCNE_DELETE, SHCNF_PATH, shortcuts[i].value().c_str(), | 611 SHChangeNotify(SHCNE_DELETE, SHCNF_PATH, shortcut.value().c_str(), nullptr); |
537 NULL); | |
538 } | 612 } |
539 | 613 |
540 // If |ensure_shortcuts_remain| is true and deleting this profile caused the | 614 // If |ensure_shortcuts_remain| is true and deleting this profile caused the |
541 // last shortcuts to be removed, re-create a regular non-profile shortcut. | 615 // last shortcuts to be removed, re-create a regular non-profile shortcut. |
542 const bool had_shortcuts = !shortcuts.empty(); | 616 const bool had_shortcuts = !shortcuts.empty(); |
543 if (ensure_shortcuts_remain && had_shortcuts && | 617 if (ensure_shortcuts_remain && had_shortcuts && |
544 !ChromeDesktopShortcutsExist(chrome_exe)) { | 618 !ChromeDesktopShortcutsExist(chrome_exe)) { |
545 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); | 619 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); |
546 // Ensure that the distribution supports creating shortcuts. If it doesn't, | 620 // Ensure that the distribution supports creating shortcuts. If it doesn't, |
547 // the following code may result in NOTREACHED() being hit. | 621 // the following code may result in NOTREACHED() being hit. |
(...skipping 17 matching lines...) Expand all Loading... | |
565 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 639 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
566 | 640 |
567 base::FilePath chrome_exe; | 641 base::FilePath chrome_exe; |
568 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { | 642 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
569 NOTREACHED(); | 643 NOTREACHED(); |
570 return false; | 644 return false; |
571 } | 645 } |
572 | 646 |
573 const base::string16 command_line = | 647 const base::string16 command_line = |
574 profiles::internal::CreateProfileShortcutFlags(profile_path); | 648 profiles::internal::CreateProfileShortcutFlags(profile_path); |
575 std::vector<base::FilePath> shortcuts; | 649 ChromeCommandLineFilter filter(chrome_exe, command_line, false); |
576 ListDesktopShortcutsWithCommandLine(chrome_exe, command_line, false, | 650 const std::set<base::FilePath> shortcuts = ListUserDesktopContents(&filter); |
Alexei Svitkine (slow)
2016/01/18 16:43:08
Nit: combine this with the next line, no need for
| |
577 &shortcuts); | |
578 return !shortcuts.empty(); | 651 return !shortcuts.empty(); |
579 } | 652 } |
580 | 653 |
581 // Replaces any reserved characters with spaces, and trims the resulting string | 654 // Replaces any reserved characters with spaces, and trims the resulting string |
582 // to prevent any leading and trailing spaces. Also makes sure that the | 655 // to prevent any leading and trailing spaces. Also makes sure that the |
583 // resulting filename doesn't exceed |kMaxProfileShortcutFileNameLength|. | 656 // resulting filename doesn't exceed |kMaxProfileShortcutFileNameLength|. |
584 // TODO(macourteau): find a way to limit the total path's length to MAX_PATH | 657 // TODO(macourteau): find a way to limit the total path's length to MAX_PATH |
585 // instead of limiting the profile's name to |kMaxProfileShortcutFileNameLength| | 658 // instead of limiting the profile's name to |kMaxProfileShortcutFileNameLength| |
586 // characters. | 659 // characters. |
587 base::string16 SanitizeShortcutProfileNameString( | 660 base::string16 SanitizeShortcutProfileNameString( |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
636 shortcut_name.append(SanitizeShortcutProfileNameString(profile_name)); | 709 shortcut_name.append(SanitizeShortcutProfileNameString(profile_name)); |
637 shortcut_name.append(L" - "); | 710 shortcut_name.append(L" - "); |
638 shortcut_name.append(l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME)); | 711 shortcut_name.append(l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME)); |
639 } else { | 712 } else { |
640 shortcut_name.append( | 713 shortcut_name.append( |
641 distribution->GetShortcutName(BrowserDistribution::SHORTCUT_CHROME)); | 714 distribution->GetShortcutName(BrowserDistribution::SHORTCUT_CHROME)); |
642 } | 715 } |
643 return shortcut_name + installer::kLnkExt; | 716 return shortcut_name + installer::kLnkExt; |
644 } | 717 } |
645 | 718 |
719 base::string16 GetUniqueShortcutFilenameForProfile( | |
720 const base::string16& profile_name, | |
721 BrowserDistribution* distribution, | |
722 const std::set<base::FilePath>& excludes) { | |
723 std::set<base::string16> excludes_names; | |
724 std::transform(excludes.begin(), excludes.end(), | |
725 std::inserter(excludes_names, excludes_names.begin()), | |
726 [](const base::FilePath& e) { return e.BaseName().value(); }); | |
727 | |
728 const auto base_name = | |
729 GetShortcutFilenameForProfile(profile_name, distribution); | |
730 auto name = base_name; | |
731 const base::FilePath base_path(base_name); | |
732 for (int uniquifier = 1; excludes_names.count(name) > 0; ++uniquifier) { | |
733 name = base_path | |
734 .InsertBeforeExtensionASCII(base::StringPrintf(" (%d)", uniquifier)) | |
735 .value(); | |
Alexei Svitkine (slow)
2016/01/18 16:43:08
Nit:
const auto suffix = base::StringPrintf(" (%d
| |
736 } | |
737 return name; | |
738 } | |
739 | |
740 // Corresponds to GetUniqueShortcutFilenameForProfile. | |
741 ShortcutFilenameMatcher::ShortcutFilenameMatcher( | |
742 const base::string16& profile_name, | |
743 BrowserDistribution* distribution) | |
744 : profile_shortcut_filename_( | |
745 GetShortcutFilenameForProfile(profile_name, distribution)), | |
746 lnk_ext_(installer::kLnkExt), | |
747 profile_shortcut_name_(profile_shortcut_filename_) { | |
748 DCHECK(profile_shortcut_name_.ends_with(lnk_ext_)); | |
749 profile_shortcut_name_.remove_suffix(lnk_ext_.size()); | |
750 } | |
751 | |
752 bool ShortcutFilenameMatcher::IsCanonical( | |
753 const base::string16& filename) const { | |
754 if (filename == profile_shortcut_filename_) | |
755 return true; | |
756 | |
757 base::StringPiece16 shortcut_suffix(filename); | |
758 if (!shortcut_suffix.starts_with(profile_shortcut_name_)) | |
759 return false; | |
760 shortcut_suffix.remove_prefix(profile_shortcut_name_.size()); | |
761 | |
762 if (!shortcut_suffix.ends_with(lnk_ext_)) | |
763 return false; | |
764 shortcut_suffix.remove_suffix(lnk_ext_.size()); | |
765 | |
766 if (shortcut_suffix.size() < 4 || !shortcut_suffix.starts_with(L" (") || | |
767 !shortcut_suffix.ends_with(L")")) { | |
768 return false; | |
769 } | |
770 return std::all_of(shortcut_suffix.begin() + 2, shortcut_suffix.end() - 1, | |
771 iswdigit); | |
772 } | |
773 | |
646 base::string16 CreateProfileShortcutFlags(const base::FilePath& profile_path) { | 774 base::string16 CreateProfileShortcutFlags(const base::FilePath& profile_path) { |
647 return base::StringPrintf(L"--%ls=\"%ls\"", | 775 return base::StringPrintf(L"--%ls=\"%ls\"", |
648 base::ASCIIToUTF16( | 776 base::ASCIIToUTF16( |
649 switches::kProfileDirectory).c_str(), | 777 switches::kProfileDirectory).c_str(), |
650 profile_path.BaseName().value().c_str()); | 778 profile_path.BaseName().value().c_str()); |
651 } | 779 } |
652 | 780 |
653 } // namespace internal | 781 } // namespace internal |
654 } // namespace profiles | 782 } // namespace profiles |
655 | 783 |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
877 // Ensure the profile's icon file has been created. | 1005 // Ensure the profile's icon file has been created. |
878 CreateOrUpdateProfileIcon(profile->GetPath()); | 1006 CreateOrUpdateProfileIcon(profile->GetPath()); |
879 } | 1007 } |
880 break; | 1008 break; |
881 } | 1009 } |
882 default: | 1010 default: |
883 NOTREACHED(); | 1011 NOTREACHED(); |
884 break; | 1012 break; |
885 } | 1013 } |
886 } | 1014 } |
OLD | NEW |