Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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/win/jumplist.h" | 5 #include "chrome/browser/win/jumplist.h" |
| 6 | 6 |
| 7 #include "base/base_paths.h" | 7 #include "base/base_paths.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/bind_helpers.h" | 9 #include "base/bind_helpers.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 69 | 69 |
| 70 // The delay before updating the JumpList to prevent update storms. | 70 // The delay before updating the JumpList to prevent update storms. |
| 71 constexpr base::TimeDelta kDelayForJumplistUpdate = | 71 constexpr base::TimeDelta kDelayForJumplistUpdate = |
| 72 base::TimeDelta::FromMilliseconds(3500); | 72 base::TimeDelta::FromMilliseconds(3500); |
| 73 | 73 |
| 74 // The maximum allowed time for JumpListUpdater::BeginUpdate. Updates taking | 74 // The maximum allowed time for JumpListUpdater::BeginUpdate. Updates taking |
| 75 // longer than this are discarded to prevent bogging down slow machines. | 75 // longer than this are discarded to prevent bogging down slow machines. |
| 76 constexpr base::TimeDelta kTimeOutForJumplistBeginUpdate = | 76 constexpr base::TimeDelta kTimeOutForJumplistBeginUpdate = |
| 77 base::TimeDelta::FromMilliseconds(500); | 77 base::TimeDelta::FromMilliseconds(500); |
| 78 | 78 |
| 79 // The maximum allowed time for updating both "most visited" and "recently | 79 // The maximum allowed time for updating the "most visited" category via |
|
grt (UTC plus 2)
2017/06/27 07:45:29
i think the old version of this comment was more c
chengx
2017/06/29 17:46:05
Done.
| |
| 80 // closed" categories via JumpListUpdater::AddCustomCategory. | 80 // JumpListUpdater::AddCustomCategory. |
| 81 constexpr base::TimeDelta kTimeOutForAddCustomCategory = | 81 constexpr base::TimeDelta kTimeOutForAddCustomCategory = |
| 82 base::TimeDelta::FromMilliseconds(500); | 82 base::TimeDelta::FromMilliseconds(500); |
| 83 | 83 |
| 84 // The maximum allowed time for JumpListUpdater::CommitUpdate. | 84 // The maximum allowed time for JumpListUpdater::CommitUpdate. |
| 85 constexpr base::TimeDelta kTimeOutForJumplistCommitUpdate = | 85 constexpr base::TimeDelta kTimeOutForJumplistCommitUpdate = |
| 86 base::TimeDelta::FromMilliseconds(1000); | 86 base::TimeDelta::FromMilliseconds(1000); |
| 87 | 87 |
| 88 // Appends the common switches to each shell link. | 88 // Appends the common switches to each shell link. |
| 89 void AppendCommonSwitches(ShellLinkItem* shell_link) { | 89 void AppendCommonSwitches(ShellLinkItem* shell_link) { |
| 90 const char* kSwitchNames[] = { switches::kUserDataDir }; | 90 const char* kSwitchNames[] = { switches::kUserDataDir }; |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 187 base::FilePath GenerateJumplistIconDirName( | 187 base::FilePath GenerateJumplistIconDirName( |
| 188 const base::FilePath& profile_dir, | 188 const base::FilePath& profile_dir, |
| 189 const base::FilePath::StringPieceType& suffix) { | 189 const base::FilePath::StringPieceType& suffix) { |
| 190 base::FilePath::StringType dir_name(chrome::kJumpListIconDirname); | 190 base::FilePath::StringType dir_name(chrome::kJumpListIconDirname); |
| 191 suffix.AppendToString(&dir_name); | 191 suffix.AppendToString(&dir_name); |
| 192 return profile_dir.Append(dir_name); | 192 return profile_dir.Append(dir_name); |
| 193 } | 193 } |
| 194 | 194 |
| 195 } // namespace | 195 } // namespace |
| 196 | 196 |
| 197 JumpList::UpdateResults::UpdateResults() {} | 197 JumpList::UpdateTransaction::UpdateTransaction() {} |
| 198 | 198 |
| 199 JumpList::UpdateResults::~UpdateResults() {} | 199 JumpList::UpdateTransaction::~UpdateTransaction() {} |
| 200 | 200 |
| 201 // static | 201 // static |
| 202 bool JumpList::Enabled() { | 202 bool JumpList::Enabled() { |
| 203 return JumpListUpdater::IsEnabled(); | 203 return JumpListUpdater::IsEnabled(); |
| 204 } | 204 } |
| 205 | 205 |
| 206 void JumpList::Shutdown() { | 206 void JumpList::Shutdown() { |
| 207 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | 207 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 208 Terminate(); | 208 Terminate(); |
| 209 } | 209 } |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 299 sessions::TabRestoreService* service) {} | 299 sessions::TabRestoreService* service) {} |
| 300 | 300 |
| 301 void JumpList::OnIncognitoAvailabilityChanged() { | 301 void JumpList::OnIncognitoAvailabilityChanged() { |
| 302 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | 302 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 303 | 303 |
| 304 if (icon_urls_.empty()) | 304 if (icon_urls_.empty()) |
| 305 PostRunUpdate(); | 305 PostRunUpdate(); |
| 306 } | 306 } |
| 307 | 307 |
| 308 void JumpList::InitializeTimerForUpdate() { | 308 void JumpList::InitializeTimerForUpdate() { |
| 309 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | |
| 310 | |
| 309 if (timer_.IsRunning()) { | 311 if (timer_.IsRunning()) { |
| 310 timer_.Reset(); | 312 timer_.Reset(); |
| 311 } else { | 313 } else { |
| 312 // base::Unretained is safe since |this| is guaranteed to outlive timer_. | 314 // base::Unretained is safe since |this| is guaranteed to outlive timer_. |
| 313 timer_.Start(FROM_HERE, kDelayForJumplistUpdate, | 315 timer_.Start(FROM_HERE, kDelayForJumplistUpdate, |
| 314 base::Bind(&JumpList::OnDelayTimer, base::Unretained(this))); | 316 base::Bind(&JumpList::OnDelayTimer, base::Unretained(this))); |
| 315 } | 317 } |
| 316 } | 318 } |
| 317 | 319 |
| 318 void JumpList::OnDelayTimer() { | 320 void JumpList::OnDelayTimer() { |
| 319 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | 321 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 322 DCHECK(!update_in_progress_); | |
| 320 | 323 |
| 321 if (updates_to_skip_ > 0) { | 324 if (updates_to_skip_ > 0) { |
| 322 --updates_to_skip_; | 325 --updates_to_skip_; |
| 323 return; | 326 return; |
| 324 } | 327 } |
| 325 | 328 |
| 326 // Retrieve the recently closed URLs synchronously. | 329 // Retrieve the recently closed URLs synchronously. |
| 327 if (tab_restore_has_pending_notification_) { | 330 if (tab_restore_has_pending_notification_) { |
| 328 tab_restore_has_pending_notification_ = false; | 331 tab_restore_has_pending_notification_ = false; |
| 329 ProcessTabRestoreServiceNotification(); | 332 ProcessTabRestoreServiceNotification(); |
| 330 } | 333 } |
| 331 | 334 |
| 332 // If TopSites has updates, retrieve the URLs asynchronously, and on its | 335 // If TopSites has updates, retrieve the URLs asynchronously, and on its |
| 333 // completion, trigger favicon loading. | 336 // completion, trigger favicon loading. |
| 334 // Otherwise, call StartLoadingFavicon directly to start favicon loading. | 337 // Otherwise, call StartLoadingFavicon directly to start favicon loading. |
| 335 if (top_sites_has_pending_notification_) | 338 if (top_sites_has_pending_notification_) |
| 336 ProcessTopSitesNotification(); | 339 ProcessTopSitesNotification(); |
| 337 else | 340 else |
| 338 StartLoadingFavicon(); | 341 StartLoadingFavicon(); |
| 339 } | 342 } |
| 340 | 343 |
| 341 void JumpList::ProcessTopSitesNotification() { | 344 void JumpList::ProcessTopSitesNotification() { |
| 342 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | 345 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 346 DCHECK(!update_in_progress_); | |
| 343 | 347 |
| 344 // Opening the first tab in one session triggers a TopSite history sync. | 348 // Opening the first tab in one session triggers a TopSite history sync. |
| 345 // Delay this sync till the first tab is closed to allow the "recently closed" | 349 // Delay this sync till the first tab is closed to allow the "recently closed" |
| 346 // category from last session to stay longer. All previous pending | 350 // category from last session to stay longer. All previous pending |
| 347 // notifications from TopSites are ignored. | 351 // notifications from TopSites are ignored. |
| 348 if (!has_tab_closed_) { | 352 if (!has_tab_closed_) { |
| 349 top_sites_has_pending_notification_ = false; | 353 top_sites_has_pending_notification_ = false; |
| 350 return; | 354 return; |
| 351 } | 355 } |
| 352 | 356 |
| 353 scoped_refptr<history::TopSites> top_sites = | 357 scoped_refptr<history::TopSites> top_sites = |
| 354 TopSitesFactory::GetForProfile(profile_); | 358 TopSitesFactory::GetForProfile(profile_); |
| 355 if (top_sites) { | 359 if (top_sites) { |
| 356 top_sites->GetMostVisitedURLs( | 360 top_sites->GetMostVisitedURLs( |
| 357 base::Bind(&JumpList::OnMostVisitedURLsAvailable, | 361 base::Bind(&JumpList::OnMostVisitedURLsAvailable, |
| 358 weak_ptr_factory_.GetWeakPtr()), | 362 weak_ptr_factory_.GetWeakPtr()), |
| 359 false); | 363 false); |
| 360 } | 364 } |
| 361 } | 365 } |
| 362 | 366 |
| 363 void JumpList::ProcessTabRestoreServiceNotification() { | 367 void JumpList::ProcessTabRestoreServiceNotification() { |
| 364 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | 368 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 369 DCHECK(!update_in_progress_); | |
| 365 | 370 |
| 366 // Create a list of ShellLinkItems from the "Recently Closed" pages. | 371 // Create a list of ShellLinkItems from the "Recently Closed" pages. |
| 367 // As noted above, we create a ShellLinkItem objects with the following | 372 // As noted above, we create a ShellLinkItem objects with the following |
| 368 // parameters. | 373 // parameters. |
| 369 // * arguments | 374 // * arguments |
| 370 // The last URL of the tab object. | 375 // The last URL of the tab object. |
| 371 // * title | 376 // * title |
| 372 // The title of the last URL. | 377 // The title of the last URL. |
| 373 // * icon | 378 // * icon |
| 374 // An empty string. This value is to be updated in OnFaviconDataAvailable(). | 379 // An empty string. This value is to be updated in OnFaviconDataAvailable(). |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 542 TRACE_EVENT0("browser", "JumpList::PostRunUpdate"); | 547 TRACE_EVENT0("browser", "JumpList::PostRunUpdate"); |
| 543 | 548 |
| 544 update_in_progress_ = true; | 549 update_in_progress_ = true; |
| 545 | 550 |
| 546 base::FilePath profile_dir = profile_->GetPath(); | 551 base::FilePath profile_dir = profile_->GetPath(); |
| 547 | 552 |
| 548 // Check if incognito windows (or normal windows) are disabled by policy. | 553 // Check if incognito windows (or normal windows) are disabled by policy. |
| 549 IncognitoModePrefs::Availability incognito_availability = | 554 IncognitoModePrefs::Availability incognito_availability = |
| 550 IncognitoModePrefs::GetAvailability(profile_->GetPrefs()); | 555 IncognitoModePrefs::GetAvailability(profile_->GetPrefs()); |
| 551 | 556 |
| 552 // Make local copies of JumpList member variables and use them for an update. | 557 // Make local copies of JumpList member variables and use them for an update. |
|
grt (UTC plus 2)
2017/06/27 07:45:29
are any of these locals actually needed?
chengx
2017/06/29 17:46:04
No, they are not needed any more. I've removed the
| |
| 553 ShellLinkItemList local_most_visited_pages = most_visited_pages_; | 558 ShellLinkItemList local_most_visited_pages = most_visited_pages_; |
| 554 ShellLinkItemList local_recently_closed_pages = recently_closed_pages_; | 559 ShellLinkItemList local_recently_closed_pages = recently_closed_pages_; |
| 555 | |
| 556 bool most_visited_should_update = most_visited_should_update_; | 560 bool most_visited_should_update = most_visited_should_update_; |
| 557 bool recently_closed_should_update = recently_closed_should_update_; | 561 bool recently_closed_should_update = recently_closed_should_update_; |
| 558 | 562 |
| 559 auto update_results = base::MakeUnique<UpdateResults>(); | 563 auto update_transaction = base::MakeUnique<UpdateTransaction>(); |
| 560 update_results->most_visited_icons_in_update = most_visited_icons_; | 564 update_transaction->most_visited_icons = most_visited_icons_; |
|
grt (UTC plus 2)
2017/06/27 07:45:29
to consider for another CL: does it make sense to
chengx
2017/06/29 17:46:04
Agreed that caches need to be set only when the co
| |
| 561 update_results->recently_closed_icons_in_update = recently_closed_icons_; | 565 update_transaction->recently_closed_icons = recently_closed_icons_; |
| 562 | 566 |
| 563 // Parameter evaluation order is unspecified in C++. Ensure the pointer value | 567 // Parameter evaluation order is unspecified in C++. Do the first bind and |
| 564 // is obtained before base::Passed() is called. | 568 // then move it into PostTaskAndReply to ensure the pointer value is obtained |
| 565 auto* update_results_raw = update_results.get(); | 569 // before base::Passed() is called. |
| 570 auto run_update = | |
| 571 base::Bind(&JumpList::RunUpdateJumpList, app_id_, profile_dir, | |
| 572 local_most_visited_pages, local_recently_closed_pages, | |
| 573 most_visited_should_update, recently_closed_should_update, | |
| 574 incognito_availability, update_transaction.get()); | |
| 566 | 575 |
| 567 // Post a task to update the JumpList, which consists of 1) create new icons, | 576 // Post a task to update the JumpList, which consists of 1) create new icons, |
| 568 // 2) delete old icons, 3) notify the OS. | 577 // 2) notify the OS, 3) delete old icons. |
| 569 if (!update_jumplist_task_runner_->PostTaskAndReply( | 578 if (!update_jumplist_task_runner_->PostTaskAndReply( |
| 570 FROM_HERE, | 579 FROM_HERE, std::move(run_update), |
| 571 base::Bind(&JumpList::RunUpdateJumpList, app_id_, profile_dir, | |
| 572 local_most_visited_pages, local_recently_closed_pages, | |
| 573 most_visited_should_update, recently_closed_should_update, | |
| 574 incognito_availability, update_results_raw), | |
| 575 base::Bind(&JumpList::OnRunUpdateCompletion, | 580 base::Bind(&JumpList::OnRunUpdateCompletion, |
| 576 weak_ptr_factory_.GetWeakPtr(), | 581 weak_ptr_factory_.GetWeakPtr(), |
| 577 base::Passed(std::move(update_results))))) { | 582 base::Passed(std::move(update_transaction))))) { |
| 578 OnRunUpdateCompletion(base::MakeUnique<UpdateResults>()); | 583 OnRunUpdateCompletion(base::MakeUnique<UpdateTransaction>()); |
| 579 } | 584 } |
| 580 } | 585 } |
| 581 | 586 |
| 582 void JumpList::OnRunUpdateCompletion( | 587 void JumpList::OnRunUpdateCompletion( |
| 583 std::unique_ptr<UpdateResults> update_results) { | 588 std::unique_ptr<UpdateTransaction> update_transaction) { |
| 584 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | 589 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 585 | 590 |
| 586 // Update JumpList member variables based on the results from the update run | 591 // Update JumpList member variables based on the results from the update run |
| 587 // just finished. | 592 // just finished. |
| 588 if (update_results->update_timeout) | 593 if (update_transaction->update_timeout) |
| 589 updates_to_skip_ = kUpdatesToSkipUnderHeavyLoad; | 594 updates_to_skip_ = kUpdatesToSkipUnderHeavyLoad; |
| 590 | 595 |
| 591 if (update_results->update_success) { | 596 if (update_transaction->update_success) { |
| 592 most_visited_icons_.swap(update_results->most_visited_icons_in_update); | 597 if (most_visited_should_update_) { |
| 593 recently_closed_icons_.swap( | 598 most_visited_icons_ = std::move(update_transaction->most_visited_icons); |
| 594 update_results->recently_closed_icons_in_update); | 599 most_visited_should_update_ = false; |
| 595 most_visited_should_update_ = false; | 600 } |
| 596 recently_closed_should_update_ = false; | 601 if (recently_closed_should_update_) { |
| 602 recently_closed_icons_ = | |
| 603 std::move(update_transaction->recently_closed_icons); | |
| 604 recently_closed_should_update_ = false; | |
| 605 } | |
| 597 } | 606 } |
| 598 | 607 |
| 599 update_in_progress_ = false; | 608 update_in_progress_ = false; |
| 600 | 609 |
| 601 // If there is any new notification during the update run just finished, start | 610 // If there is any new notification during the update run just finished, start |
| 602 // another JumpList update. | 611 // another JumpList update. |
| 603 // Otherwise, post tasks to delete the JumpListIcons and JumpListIconsOld | 612 // Otherwise, post tasks to delete the JumpListIcons and JumpListIconsOld |
| 604 // folders as they are no longer needed. Now we have the | 613 // folders as they are no longer needed. Now we have the |
| 605 // JumpListIcons{MostVisited, RecentClosed} folders instead. | 614 // JumpListIcons{MostVisited, RecentClosed} folders instead. |
| 606 if (top_sites_has_pending_notification_ || | 615 if (top_sites_has_pending_notification_ || |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 656 | 665 |
| 657 // static | 666 // static |
| 658 void JumpList::RunUpdateJumpList( | 667 void JumpList::RunUpdateJumpList( |
| 659 const base::string16& app_id, | 668 const base::string16& app_id, |
| 660 const base::FilePath& profile_dir, | 669 const base::FilePath& profile_dir, |
| 661 const ShellLinkItemList& most_visited_pages, | 670 const ShellLinkItemList& most_visited_pages, |
| 662 const ShellLinkItemList& recently_closed_pages, | 671 const ShellLinkItemList& recently_closed_pages, |
| 663 bool most_visited_should_update, | 672 bool most_visited_should_update, |
| 664 bool recently_closed_should_update, | 673 bool recently_closed_should_update, |
| 665 IncognitoModePrefs::Availability incognito_availability, | 674 IncognitoModePrefs::Availability incognito_availability, |
| 666 UpdateResults* update_results) { | 675 UpdateTransaction* update_transaction) { |
| 667 if (!JumpListUpdater::IsEnabled()) | 676 if (!JumpListUpdater::IsEnabled()) |
| 668 return; | 677 return; |
| 669 | 678 |
| 670 DCHECK(update_results); | 679 DCHECK(update_transaction); |
| 680 | |
| 681 base::FilePath most_visited_icon_dir = GenerateJumplistIconDirName( | |
| 682 profile_dir, FILE_PATH_LITERAL("MostVisited")); | |
| 683 base::FilePath recently_closed_icon_dir = GenerateJumplistIconDirName( | |
| 684 profile_dir, FILE_PATH_LITERAL("RecentClosed")); | |
| 685 | |
| 686 CreateNewJumpListAndNotifyOS( | |
| 687 app_id, most_visited_icon_dir, recently_closed_icon_dir, | |
| 688 most_visited_pages, recently_closed_pages, most_visited_should_update, | |
| 689 recently_closed_should_update, incognito_availability, | |
| 690 update_transaction); | |
| 691 | |
| 692 // Delete any obsolete icon files. | |
| 693 if (most_visited_should_update) { | |
| 694 DeleteIconFiles(most_visited_icon_dir, | |
| 695 update_transaction->most_visited_icons); | |
| 696 } | |
| 697 if (recently_closed_should_update) { | |
| 698 DeleteIconFiles(recently_closed_icon_dir, | |
| 699 update_transaction->recently_closed_icons); | |
| 700 } | |
| 701 } | |
| 702 | |
| 703 // static | |
| 704 void JumpList::CreateNewJumpListAndNotifyOS( | |
| 705 const base::string16& app_id, | |
| 706 const base::FilePath& most_visited_icon_dir, | |
| 707 const base::FilePath& recently_closed_icon_dir, | |
| 708 const ShellLinkItemList& most_visited_pages, | |
| 709 const ShellLinkItemList& recently_closed_pages, | |
| 710 bool most_visited_should_update, | |
| 711 bool recently_closed_should_update, | |
| 712 IncognitoModePrefs::Availability incognito_availability, | |
| 713 UpdateTransaction* update_transaction) { | |
| 714 DCHECK(update_transaction); | |
| 671 | 715 |
| 672 JumpListUpdater jumplist_updater(app_id); | 716 JumpListUpdater jumplist_updater(app_id); |
| 673 | 717 |
| 674 base::ElapsedTimer begin_update_timer; | 718 base::ElapsedTimer begin_update_timer; |
| 675 | 719 |
| 676 if (!jumplist_updater.BeginUpdate()) | 720 if (!jumplist_updater.BeginUpdate()) |
| 677 return; | 721 return; |
| 678 | 722 |
| 679 // Discard this JumpList update if JumpListUpdater::BeginUpdate takes longer | 723 // Discard this JumpList update if JumpListUpdater::BeginUpdate takes longer |
| 680 // than the maximum allowed time, as it's very likely the following update | 724 // than the maximum allowed time, as it's very likely the following update |
| 681 // steps will also take a long time. As we've not updated the icons on the | 725 // steps will also take a long time. |
| 682 // disk, discarding this update wont't affect the current JumpList used by OS. | |
| 683 if (begin_update_timer.Elapsed() >= kTimeOutForJumplistBeginUpdate) { | 726 if (begin_update_timer.Elapsed() >= kTimeOutForJumplistBeginUpdate) { |
| 684 update_results->update_timeout = true; | 727 update_transaction->update_timeout = true; |
| 685 return; | 728 return; |
| 686 } | 729 } |
| 687 | 730 |
| 688 // Record the desired number of icons created in this JumpList update. | 731 // Record the desired number of icons created in this JumpList update. |
| 689 int icons_created = 0; | 732 int icons_created = 0; |
| 690 | 733 |
| 734 URLIconCache most_visited_icons_next; | |
| 735 URLIconCache recently_closed_icons_next; | |
| 736 | |
| 691 // Update the icons for "Most Visisted" category of the JumpList if needed. | 737 // Update the icons for "Most Visisted" category of the JumpList if needed. |
| 692 if (most_visited_should_update) { | 738 if (most_visited_should_update) { |
| 693 base::FilePath icon_dir_most_visited = GenerateJumplistIconDirName( | |
| 694 profile_dir, FILE_PATH_LITERAL("MostVisited")); | |
| 695 | |
| 696 icons_created += UpdateIconFiles( | 739 icons_created += UpdateIconFiles( |
| 697 icon_dir_most_visited, most_visited_pages, kMostVisitedItems, | 740 most_visited_icon_dir, most_visited_pages, kMostVisitedItems, |
| 698 &update_results->most_visited_icons_in_update); | 741 &update_transaction->most_visited_icons, &most_visited_icons_next); |
| 699 } | 742 } |
| 700 | 743 |
| 701 // Update the icons for "Recently Closed" category of the JumpList if needed. | 744 // Update the icons for "Recently Closed" category of the JumpList if needed. |
| 702 if (recently_closed_should_update) { | 745 if (recently_closed_should_update) { |
| 703 base::FilePath icon_dir_recent_closed = GenerateJumplistIconDirName( | |
| 704 profile_dir, FILE_PATH_LITERAL("RecentClosed")); | |
| 705 | |
| 706 icons_created += UpdateIconFiles( | 746 icons_created += UpdateIconFiles( |
| 707 icon_dir_recent_closed, recently_closed_pages, kRecentlyClosedItems, | 747 recently_closed_icon_dir, recently_closed_pages, kRecentlyClosedItems, |
| 708 &update_results->recently_closed_icons_in_update); | 748 &update_transaction->recently_closed_icons, |
| 749 &recently_closed_icons_next); | |
| 709 } | 750 } |
| 710 | 751 |
| 711 // TODO(chengx): Remove the UMA histogram after fixing http://crbug.com/40407. | 752 // TODO(chengx): Remove the UMA histogram after fixing http://crbug.com/40407. |
| 712 UMA_HISTOGRAM_COUNTS_100("WinJumplist.CreateIconFilesCount", icons_created); | 753 UMA_HISTOGRAM_COUNTS_100("WinJumplist.CreateIconFilesCount", icons_created); |
| 713 | 754 |
| 714 // TODO(chengx): Remove the UMA histogram after fixing http://crbug.com/40407. | 755 // TODO(chengx): Remove the UMA histogram after fixing http://crbug.com/40407. |
| 715 SCOPED_UMA_HISTOGRAM_TIMER("WinJumplist.UpdateJumpListDuration"); | 756 SCOPED_UMA_HISTOGRAM_TIMER("WinJumplist.UpdateJumpListDuration"); |
| 716 | 757 |
| 717 base::ElapsedTimer add_custom_category_timer; | 758 base::ElapsedTimer add_custom_category_timer; |
| 718 | 759 |
| 719 // Update the "Most Visited" category of the JumpList if it exists. | 760 // Update the "Most Visited" category of the JumpList if it exists. |
| 720 // This update request is applied into the JumpList when we commit this | 761 // This update request is applied into the JumpList when we commit this |
| 721 // transaction. | 762 // transaction. |
| 722 if (!jumplist_updater.AddCustomCategory( | 763 if (!jumplist_updater.AddCustomCategory( |
| 723 l10n_util::GetStringUTF16(IDS_NEW_TAB_MOST_VISITED), | 764 l10n_util::GetStringUTF16(IDS_NEW_TAB_MOST_VISITED), |
| 724 most_visited_pages, kMostVisitedItems)) { | 765 most_visited_pages, kMostVisitedItems)) { |
| 725 return; | 766 return; |
| 726 } | 767 } |
| 727 | 768 |
| 728 // Update the "Recently Closed" category of the JumpList. | 769 // Update the "Recently Closed" category of the JumpList. |
| 729 if (!jumplist_updater.AddCustomCategory( | 770 if (!jumplist_updater.AddCustomCategory( |
| 730 l10n_util::GetStringUTF16(IDS_RECENTLY_CLOSED), recently_closed_pages, | 771 l10n_util::GetStringUTF16(IDS_RECENTLY_CLOSED), recently_closed_pages, |
| 731 kRecentlyClosedItems)) { | 772 kRecentlyClosedItems)) { |
| 732 return; | 773 return; |
| 733 } | 774 } |
| 734 | 775 |
| 735 // If JumpListUpdater::AddCustomCategory or JumpListUpdater::CommitUpdate | 776 // If AddCustomCategory takes longer than the maximum allowed time, abort the |
| 736 // takes longer than the maximum allowed time, skip the next | 777 // current update and skip the next |kUpdatesToSkipUnderHeavyLoad| updates. |
| 737 // |kUpdatesToSkipUnderHeavyLoad| updates. This update should be finished | 778 if (add_custom_category_timer.Elapsed() >= kTimeOutForAddCustomCategory) { |
| 738 // because we've already updated the icons on the disk. If discarding this | 779 update_transaction->update_timeout = true; |
| 739 // update from here, some items in the current JumpList may not have icons | 780 return; |
| 740 // as they've been delete from the disk. In this case, the background color of | 781 } |
| 741 // the JumpList panel is used instead, which doesn't look nice. | |
| 742 | |
| 743 if (add_custom_category_timer.Elapsed() >= kTimeOutForAddCustomCategory) | |
| 744 update_results->update_timeout = true; | |
| 745 | 782 |
| 746 // Update the "Tasks" category of the JumpList. | 783 // Update the "Tasks" category of the JumpList. |
| 747 if (!UpdateTaskCategory(&jumplist_updater, incognito_availability)) | 784 if (!UpdateTaskCategory(&jumplist_updater, incognito_availability)) |
| 748 return; | 785 return; |
| 749 | 786 |
| 750 base::ElapsedTimer commit_update_timer; | 787 base::ElapsedTimer commit_update_timer; |
| 751 | 788 |
| 752 // Commit this transaction and send the updated JumpList to Windows. | 789 // Commit this transaction and send the updated JumpList to Windows. |
| 753 if (jumplist_updater.CommitUpdate()) | 790 bool commit_success = jumplist_updater.CommitUpdate(); |
| 754 update_results->update_success = true; | |
| 755 | 791 |
| 792 // If CommitUpdate call takes longer than the maximum allowed time, skip the | |
| 793 // next |kUpdatesToSkipUnderHeavyLoad| updates. | |
| 756 if (commit_update_timer.Elapsed() >= kTimeOutForJumplistCommitUpdate) | 794 if (commit_update_timer.Elapsed() >= kTimeOutForJumplistCommitUpdate) |
| 757 update_results->update_timeout = true; | 795 update_transaction->update_timeout = true; |
| 796 | |
| 797 if (commit_success) { | |
| 798 update_transaction->update_success = true; | |
| 799 | |
| 800 // The move assignments below ensure update_transaction always has the icons | |
| 801 // to keep. | |
| 802 if (most_visited_should_update) { | |
| 803 update_transaction->most_visited_icons = | |
| 804 std::move(most_visited_icons_next); | |
| 805 } | |
| 806 if (recently_closed_should_update) { | |
| 807 update_transaction->recently_closed_icons = | |
| 808 std::move(recently_closed_icons_next); | |
| 809 } | |
| 810 } | |
| 758 } | 811 } |
| 759 | 812 |
| 760 // static | 813 // static |
| 761 int JumpList::UpdateIconFiles(const base::FilePath& icon_dir, | 814 int JumpList::UpdateIconFiles(const base::FilePath& icon_dir, |
| 762 const ShellLinkItemList& page_list, | 815 const ShellLinkItemList& item_list, |
| 763 size_t slot_limit, | 816 size_t max_items, |
| 764 URLIconCache* icon_cache) { | 817 URLIconCache* icon_cur, |
| 818 URLIconCache* icon_next) { | |
| 819 DCHECK(icon_cur); | |
| 820 DCHECK(icon_next); | |
| 821 | |
| 822 // Clear the JumpList icon folder at |icon_dir| and the caches when | |
| 823 // 1) |icon_cur| is empty. This happens when "Most visited" or "Recently | |
| 824 // closed" category updates for the 1st time after Chrome is launched. | |
| 825 // 2) The number of icons in |icon_dir| has exceeded the limit. | |
| 826 if (icon_cur->empty() || FilesExceedLimitInDir(icon_dir, max_items * 2)) { | |
| 827 DeleteDirectoryContentAndLogRuntime(icon_dir, kFileDeleteLimit); | |
| 828 icon_cur->clear(); | |
| 829 icon_next->clear(); | |
| 830 // Create new icons only when the directory exists and is empty. | |
| 831 if (!base::CreateDirectory(icon_dir) || !base::IsDirectoryEmpty(icon_dir)) | |
| 832 return 0; | |
| 833 } else if (!base::CreateDirectory(icon_dir)) { | |
| 834 return 0; | |
| 835 } | |
| 836 | |
| 837 return CreateIconFiles(icon_dir, item_list, max_items, *icon_cur, icon_next); | |
| 838 } | |
| 839 | |
| 840 // static | |
| 841 int JumpList::CreateIconFiles(const base::FilePath& icon_dir, | |
| 842 const ShellLinkItemList& item_list, | |
| 843 size_t max_items, | |
| 844 const URLIconCache& icon_cur, | |
| 845 URLIconCache* icon_next) { | |
| 846 DCHECK(icon_next); | |
| 847 | |
| 848 // TODO(chengx): Remove the UMA histogram after fixing http://crbug.com/40407. | |
| 849 SCOPED_UMA_HISTOGRAM_TIMER("WinJumplist.CreateIconFilesDuration"); | |
| 850 | |
| 765 int icons_created = 0; | 851 int icons_created = 0; |
| 766 | 852 |
| 767 // Clear the JumpList icon folder at |icon_dir| and the cache when | 853 // Reuse icons for urls that already present in the current JumpList. |
| 768 // 1) |icon_cache| is empty. This happens when "Most visited" or "Recently | 854 for (ShellLinkItemList::const_iterator iter = item_list.begin(); |
| 769 // closed" category updates for the 1st time after Chrome is launched. | 855 iter != item_list.end() && max_items > 0; ++iter, --max_items) { |
| 770 // 2) The number of icons in |icon_dir| has exceeded the limit. | 856 ShellLinkItem* item = iter->get(); |
| 771 if (icon_cache->empty() || FilesExceedLimitInDir(icon_dir, slot_limit * 2)) { | 857 auto cache_iter = icon_cur.find(item->url()); |
| 772 DeleteDirectoryContentAndLogRuntime(icon_dir, kFileDeleteLimit); | 858 if (cache_iter != icon_cur.end()) { |
| 773 icon_cache->clear(); | 859 item->set_icon(cache_iter->second.value(), 0); |
| 774 // Create new icons only when the directory exists and is empty. | 860 (*icon_next)[item->url()] = cache_iter->second; |
| 775 if (base::CreateDirectory(icon_dir) && base::IsDirectoryEmpty(icon_dir)) | 861 } else { |
| 776 icons_created += | 862 base::FilePath icon_path; |
| 777 CreateIconFiles(icon_dir, page_list, slot_limit, icon_cache); | 863 if (CreateIconFile(item->icon_image(), icon_dir, &icon_path)) { |
| 778 } else if (base::CreateDirectory(icon_dir)) { | 864 ++icons_created; |
| 779 icons_created += | 865 item->set_icon(icon_path.value(), 0); |
| 780 CreateIconFiles(icon_dir, page_list, slot_limit, icon_cache); | 866 (*icon_next)[item->url()] = icon_path; |
| 781 DeleteIconFiles(icon_dir, icon_cache); | 867 } |
| 868 } | |
| 782 } | 869 } |
| 783 | 870 |
| 784 return icons_created; | 871 return icons_created; |
| 785 } | 872 } |
| 786 | 873 |
| 787 // static | 874 // static |
| 788 int JumpList::CreateIconFiles(const base::FilePath& icon_dir, | |
| 789 const ShellLinkItemList& item_list, | |
| 790 size_t max_items, | |
| 791 URLIconCache* icon_cache) { | |
| 792 // TODO(chengx): Remove the UMA histogram after fixing http://crbug.com/40407. | |
| 793 SCOPED_UMA_HISTOGRAM_TIMER("WinJumplist.CreateIconFilesDuration"); | |
| 794 | |
| 795 int icons_created = 0; | |
| 796 | |
| 797 // Reuse icons for urls that already present in the current JumpList. | |
| 798 URLIconCache updated_map; | |
| 799 for (ShellLinkItemList::const_iterator iter = item_list.begin(); | |
| 800 iter != item_list.end() && max_items > 0; ++iter, --max_items) { | |
| 801 ShellLinkItem* item = iter->get(); | |
| 802 auto cache_iter = icon_cache->find(item->url()); | |
| 803 if (cache_iter != icon_cache->end()) { | |
| 804 item->set_icon(cache_iter->second.value(), 0); | |
| 805 updated_map[item->url()] = cache_iter->second; | |
| 806 } else { | |
| 807 base::FilePath icon_path; | |
| 808 if (CreateIconFile(item->icon_image(), icon_dir, &icon_path)) { | |
| 809 ++icons_created; | |
| 810 item->set_icon(icon_path.value(), 0); | |
| 811 updated_map[item->url()] = icon_path; | |
| 812 } | |
| 813 } | |
| 814 } | |
| 815 icon_cache->swap(updated_map); | |
| 816 | |
| 817 return icons_created; | |
| 818 } | |
| 819 | |
| 820 // static | |
| 821 void JumpList::DeleteIconFiles(const base::FilePath& icon_dir, | 875 void JumpList::DeleteIconFiles(const base::FilePath& icon_dir, |
| 822 URLIconCache* icon_cache) { | 876 const URLIconCache& icons_cache) { |
| 823 // Put all cached icon file paths into a set. | 877 // Put all cached icon file paths into a set. |
| 824 base::flat_set<base::FilePath> cached_files; | 878 base::flat_set<base::FilePath> cached_files; |
| 825 cached_files.reserve(icon_cache->size()); | 879 cached_files.reserve(icons_cache.size()); |
| 826 | 880 |
| 827 for (const auto& url_path_pair : *icon_cache) | 881 for (const auto& url_path_pair : icons_cache) |
| 828 cached_files.insert(url_path_pair.second); | 882 cached_files.insert(url_path_pair.second); |
| 829 | 883 |
| 830 DeleteNonCachedFiles(icon_dir, cached_files); | 884 DeleteNonCachedFiles(icon_dir, cached_files); |
| 831 } | 885 } |
| OLD | NEW |