| 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 adding most visited pages custom category via |
| 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(320); |
| 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 }; |
| 91 const base::CommandLine& command_line = | 91 const base::CommandLine& command_line = |
| 92 *base::CommandLine::ForCurrentProcess(); | 92 *base::CommandLine::ForCurrentProcess(); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 113 if (!base::CreateTemporaryFileInDir(icon_dir, &path)) | 113 if (!base::CreateTemporaryFileInDir(icon_dir, &path)) |
| 114 return false; | 114 return false; |
| 115 | 115 |
| 116 // Create an icon file from the favicon attached to the given |page|, and | 116 // Create an icon file from the favicon attached to the given |page|, and |
| 117 // save it as the temporary file. | 117 // save it as the temporary file. |
| 118 gfx::ImageFamily image_family; | 118 gfx::ImageFamily image_family; |
| 119 if (!image_skia.isNull()) { | 119 if (!image_skia.isNull()) { |
| 120 std::vector<float> supported_scales = image_skia.GetSupportedScales(); | 120 std::vector<float> supported_scales = image_skia.GetSupportedScales(); |
| 121 for (auto& scale : supported_scales) { | 121 for (auto& scale : supported_scales) { |
| 122 gfx::ImageSkiaRep image_skia_rep = image_skia.GetRepresentation(scale); | 122 gfx::ImageSkiaRep image_skia_rep = image_skia.GetRepresentation(scale); |
| 123 if (!image_skia_rep.is_null()) | 123 if (!image_skia_rep.is_null()) { |
| 124 image_family.Add( | 124 image_family.Add( |
| 125 gfx::Image::CreateFrom1xBitmap(image_skia_rep.sk_bitmap())); | 125 gfx::Image::CreateFrom1xBitmap(image_skia_rep.sk_bitmap())); |
| 126 } |
| 126 } | 127 } |
| 127 } | 128 } |
| 128 | 129 |
| 129 if (!IconUtil::CreateIconFileFromImageFamily(image_family, path, | 130 if (!IconUtil::CreateIconFileFromImageFamily(image_family, path, |
| 130 IconUtil::NORMAL_WRITE)) { | 131 IconUtil::NORMAL_WRITE)) { |
| 131 // Delete the file created by CreateTemporaryFileInDir as it won't be used. | 132 // Delete the file created by CreateTemporaryFileInDir as it won't be used. |
| 132 base::DeleteFile(path, false); | 133 base::DeleteFile(path, false); |
| 133 return false; | 134 return false; |
| 134 } | 135 } |
| 135 | 136 |
| (...skipping 575 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 711 bool most_visited_should_update, | 712 bool most_visited_should_update, |
| 712 bool recently_closed_should_update, | 713 bool recently_closed_should_update, |
| 713 IncognitoModePrefs::Availability incognito_availability, | 714 IncognitoModePrefs::Availability incognito_availability, |
| 714 UpdateTransaction* update_transaction) { | 715 UpdateTransaction* update_transaction) { |
| 715 DCHECK(update_transaction); | 716 DCHECK(update_transaction); |
| 716 | 717 |
| 717 JumpListUpdater jumplist_updater(app_id); | 718 JumpListUpdater jumplist_updater(app_id); |
| 718 | 719 |
| 719 base::ElapsedTimer begin_update_timer; | 720 base::ElapsedTimer begin_update_timer; |
| 720 | 721 |
| 721 if (!jumplist_updater.BeginUpdate()) | 722 bool begin_success = jumplist_updater.BeginUpdate(); |
| 722 return; | |
| 723 | 723 |
| 724 // Discard this JumpList update if JumpListUpdater::BeginUpdate takes longer | 724 // If JumpListUpdater::BeginUpdate takes longer than the maximum allowed time, |
| 725 // than the maximum allowed time, as it's very likely the following update | 725 // abort the current update as it's very likely the following steps will also |
| 726 // steps will also take a long time. | 726 // take a long time, and skip the next |kUpdatesToSkipUnderHeavyLoad| updates. |
| 727 if (begin_update_timer.Elapsed() >= kTimeOutForJumplistBeginUpdate) { | 727 if (begin_update_timer.Elapsed() >= kTimeOutForJumplistBeginUpdate) { |
| 728 update_transaction->update_timeout = true; | 728 update_transaction->update_timeout = true; |
| 729 return; | 729 return; |
| 730 } | 730 } |
| 731 | 731 |
| 732 if (!begin_success) |
| 733 return; |
| 734 |
| 732 // Record the desired number of icons created in this JumpList update. | 735 // Record the desired number of icons created in this JumpList update. |
| 733 int icons_created = 0; | 736 int icons_created = 0; |
| 734 | 737 |
| 735 URLIconCache most_visited_icons_next; | 738 URLIconCache most_visited_icons_next; |
| 736 URLIconCache recently_closed_icons_next; | 739 URLIconCache recently_closed_icons_next; |
| 737 | 740 |
| 738 // Update the icons for "Most Visisted" category of the JumpList if needed. | 741 // Update the icons for "Most Visisted" category of the JumpList if needed. |
| 739 if (most_visited_should_update) { | 742 if (most_visited_should_update) { |
| 740 icons_created += UpdateIconFiles( | 743 icons_created += UpdateIconFiles( |
| 741 most_visited_icon_dir, most_visited_pages, kMostVisitedItems, | 744 most_visited_icon_dir, most_visited_pages, kMostVisitedItems, |
| 742 &update_transaction->most_visited_icons, &most_visited_icons_next); | 745 &update_transaction->most_visited_icons, &most_visited_icons_next); |
| 743 } | 746 } |
| 744 | 747 |
| 745 // Update the icons for "Recently Closed" category of the JumpList if needed. | 748 // Update the icons for "Recently Closed" category of the JumpList if needed. |
| 746 if (recently_closed_should_update) { | 749 if (recently_closed_should_update) { |
| 747 icons_created += UpdateIconFiles( | 750 icons_created += UpdateIconFiles( |
| 748 recently_closed_icon_dir, recently_closed_pages, kRecentlyClosedItems, | 751 recently_closed_icon_dir, recently_closed_pages, kRecentlyClosedItems, |
| 749 &update_transaction->recently_closed_icons, | 752 &update_transaction->recently_closed_icons, |
| 750 &recently_closed_icons_next); | 753 &recently_closed_icons_next); |
| 751 } | 754 } |
| 752 | 755 |
| 753 base::ElapsedTimer add_custom_category_timer; | 756 base::ElapsedTimer add_custom_category_timer; |
| 754 | 757 |
| 755 // Update the "Most Visited" category of the JumpList if it exists. | 758 // Update the "Most Visited" category of the JumpList if it exists. |
| 756 // This update request is applied into the JumpList when we commit this | 759 // This update request is applied into the JumpList when we commit this |
| 757 // transaction. | 760 // transaction. |
| 758 if (!jumplist_updater.AddCustomCategory( | 761 bool add_category_success = jumplist_updater.AddCustomCategory( |
| 759 l10n_util::GetStringUTF16(IDS_NEW_TAB_MOST_VISITED), | 762 l10n_util::GetStringUTF16(IDS_NEW_TAB_MOST_VISITED), most_visited_pages, |
| 760 most_visited_pages, kMostVisitedItems)) { | 763 kMostVisitedItems); |
| 764 |
| 765 // If AddCustomCategory takes longer than the maximum allowed time, abort the |
| 766 // current update and skip the next |kUpdatesToSkipUnderHeavyLoad| updates. |
| 767 // |
| 768 // We only time adding custom category for most visited pages because |
| 769 // 1. If processing the first category times out or fails, there is no need to |
| 770 // process the second category. In this case, we are not able to time both |
| 771 // categories. Then we need to select one category from the two. |
| 772 // 2. Most visited category is selected because it always has 5 items except |
| 773 // for a new Chrome user who has not closed 5 distinct websites yet. In |
| 774 // comparison, the number of items in recently closed category is much less |
| 775 // stable. It has 3 items only after an user closes 3 websites in one |
| 776 // session. This means the runtime of AddCustomCategory API should be fixed |
| 777 // for most visited category, but not for recently closed category. So a |
| 778 // fixed timeout threshold is only valid for most visited category. |
| 779 if (add_custom_category_timer.Elapsed() >= kTimeOutForAddCustomCategory) { |
| 780 update_transaction->update_timeout = true; |
| 761 return; | 781 return; |
| 762 } | 782 } |
| 763 | 783 |
| 784 if (!add_category_success) |
| 785 return; |
| 786 |
| 764 base::TimeDelta most_visited_category_time = | 787 base::TimeDelta most_visited_category_time = |
| 765 add_custom_category_timer.Elapsed(); | 788 add_custom_category_timer.Elapsed(); |
| 766 | 789 |
| 767 // Update the "Recently Closed" category of the JumpList. | 790 // Update the "Recently Closed" category of the JumpList. |
| 768 if (!jumplist_updater.AddCustomCategory( | 791 if (!jumplist_updater.AddCustomCategory( |
| 769 l10n_util::GetStringUTF16(IDS_RECENTLY_CLOSED), recently_closed_pages, | 792 l10n_util::GetStringUTF16(IDS_RECENTLY_CLOSED), recently_closed_pages, |
| 770 kRecentlyClosedItems)) { | 793 kRecentlyClosedItems)) { |
| 771 return; | 794 return; |
| 772 } | 795 } |
| 773 | 796 |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 887 const URLIconCache& icons_cache) { | 910 const URLIconCache& icons_cache) { |
| 888 // Put all cached icon file paths into a set. | 911 // Put all cached icon file paths into a set. |
| 889 base::flat_set<base::FilePath> cached_files; | 912 base::flat_set<base::FilePath> cached_files; |
| 890 cached_files.reserve(icons_cache.size()); | 913 cached_files.reserve(icons_cache.size()); |
| 891 | 914 |
| 892 for (const auto& url_path_pair : icons_cache) | 915 for (const auto& url_path_pair : icons_cache) |
| 893 cached_files.insert(url_path_pair.second); | 916 cached_files.insert(url_path_pair.second); |
| 894 | 917 |
| 895 DeleteNonCachedFiles(icon_dir, cached_files); | 918 DeleteNonCachedFiles(icon_dir, cached_files); |
| 896 } | 919 } |
| OLD | NEW |