Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 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/android/offline_pages/recent_tab_helper.h" | 5 #include "chrome/browser/android/offline_pages/recent_tab_helper.h" |
| 6 | 6 |
| 7 #include <queue> | 7 #include <queue> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/location.h" | 11 #include "base/location.h" |
| 12 #include "base/logging.h" | |
| 13 #include "base/macros.h" | 12 #include "base/macros.h" |
| 14 #include "base/metrics/histogram_macros.h" | 13 #include "base/metrics/histogram_macros.h" |
| 14 #include "base/strings/string_number_conversions.h" | |
| 15 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
| 16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 17 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" | 17 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" |
| 18 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" | 18 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" |
| 19 #include "chrome/browser/android/tab_android.h" | |
| 19 #include "components/offline_pages/client_namespace_constants.h" | 20 #include "components/offline_pages/client_namespace_constants.h" |
| 20 #include "components/offline_pages/offline_page_item.h" | 21 #include "components/offline_pages/offline_page_item.h" |
| 21 #include "components/offline_pages/offline_page_model.h" | 22 #include "components/offline_pages/offline_page_model.h" |
| 22 #include "content/public/browser/browser_context.h" | 23 #include "content/public/browser/browser_context.h" |
| 23 #include "content/public/browser/browser_thread.h" | 24 #include "content/public/browser/browser_thread.h" |
| 24 #include "content/public/browser/navigation_entry.h" | 25 #include "content/public/browser/navigation_entry.h" |
| 25 #include "content/public/browser/navigation_handle.h" | 26 #include "content/public/browser/navigation_handle.h" |
| 26 | 27 |
| 27 DEFINE_WEB_CONTENTS_USER_DATA_KEY(offline_pages::RecentTabHelper); | 28 DEFINE_WEB_CONTENTS_USER_DATA_KEY(offline_pages::RecentTabHelper); |
| 28 | 29 |
| 29 namespace { | |
| 30 | |
| 31 // Max number of pages to keep. The oldest pages that are over this count are | |
| 32 // deleted before the next one is saved. | |
| 33 const size_t kMaxPagesToKeep = 50; | |
| 34 | |
| 35 // Predicate for priority_queue used to compute the oldest pages. | |
| 36 struct ComparePagesForPurge { | |
| 37 bool operator()(const offline_pages::OfflinePageItem* left, | |
| 38 const offline_pages::OfflinePageItem* right) const { | |
| 39 return left->creation_time > right->creation_time; | |
| 40 } | |
| 41 }; | |
| 42 | |
| 43 } // namespace | |
| 44 | |
| 45 namespace offline_pages { | 30 namespace offline_pages { |
| 46 | 31 |
| 47 RecentTabHelper::RecentTabHelper(content::WebContents* web_contents) | 32 RecentTabHelper::RecentTabHelper(content::WebContents* web_contents) |
| 48 : content::WebContentsObserver(web_contents), | 33 : content::WebContentsObserver(web_contents), |
| 49 page_model_(nullptr), | 34 page_model_(nullptr), |
| 50 weak_ptr_factory_(this) { | 35 weak_ptr_factory_(this) { |
| 51 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 36 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 52 snapshot_controller_.reset(new SnapshotController( | 37 snapshot_controller_.reset(new SnapshotController( |
| 53 base::ThreadTaskRunnerHandle::Get(), this)); | 38 base::ThreadTaskRunnerHandle::Get(), this)); |
| 54 page_model_ = OfflinePageModelFactory::GetForBrowserContext( | 39 page_model_ = OfflinePageModelFactory::GetForBrowserContext( |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 99 snapshot_controller_->DocumentOnLoadCompletedInMainFrame(); | 84 snapshot_controller_->DocumentOnLoadCompletedInMainFrame(); |
| 100 } | 85 } |
| 101 | 86 |
| 102 // This starts a sequence of async operations chained through callbacks: | 87 // This starts a sequence of async operations chained through callbacks: |
| 103 // - compute the set of old 'last_n' pages that have to be purged | 88 // - compute the set of old 'last_n' pages that have to be purged |
| 104 // - delete the pages found in the previous step | 89 // - delete the pages found in the previous step |
| 105 // - snapshot the current web contents | 90 // - snapshot the current web contents |
| 106 // Along the chain, the original URL is passed and compared, to detect | 91 // Along the chain, the original URL is passed and compared, to detect |
| 107 // possible navigation and cancel snapshot in that case. | 92 // possible navigation and cancel snapshot in that case. |
| 108 void RecentTabHelper::StartSnapshot() { | 93 void RecentTabHelper::StartSnapshot() { |
| 109 // TODO(dimich): Implement automatic cleanup as per design doc, based on | 94 // Remove previously captured pages for this tab. |
| 110 // storage limits and page age. | |
| 111 // This algorithm (remove pages before making sure the save was successful) | |
| 112 // prefers keeping the storage use low and under control by potentially | |
| 113 // sacrificing the current snapshot. | |
| 114 page_model_->GetOfflineIdsForClientId( | 95 page_model_->GetOfflineIdsForClientId( |
| 115 client_id(), | 96 client_id(), |
| 116 base::Bind(&RecentTabHelper::ContinueSnapshotWithIdsToPurge, | 97 base::Bind(&RecentTabHelper::ContinueSnapshotWithIdsToPurge, |
| 117 weak_ptr_factory_.GetWeakPtr())); | 98 weak_ptr_factory_.GetWeakPtr())); |
| 118 } | 99 } |
| 119 | 100 |
| 120 // Collects following pages from lastN namespace: | |
| 121 // - the ones with the same online URL | |
| 122 // - the oldest pages, enough to keep kMaxPagesToKeep limit. | |
| 123 void RecentTabHelper::ContinueSnapshotWithIdsToPurge( | 101 void RecentTabHelper::ContinueSnapshotWithIdsToPurge( |
| 124 const std::vector<int64_t>& page_ids) { | 102 const std::vector<int64_t>& page_ids) { |
| 125 | 103 |
|
fgorski
2016/06/24 17:49:20
nit: remove the empty line, please.
| |
| 126 std::vector<int64_t> pages_to_purge; | |
| 127 // Use priority queue to figure out the set of oldest pages to purge. | |
| 128 std::priority_queue<const OfflinePageItem*, | |
| 129 std::vector<const OfflinePageItem*>, | |
| 130 ComparePagesForPurge> pages_queue; | |
| 131 | |
| 132 for (const auto& offline_id : page_ids) { | |
| 133 // TODO(dimich): get rid of 'Maybe'. Maybe make it return multiple pages. | |
| 134 const OfflinePageItem* page = | |
| 135 page_model_->MaybeGetPageByOfflineId(offline_id); | |
| 136 // If there is already a snapshot of this page, remove it so we don't | |
| 137 // have multiple snapshots of the same page. | |
| 138 if (page->url == snapshot_url_) { | |
| 139 pages_to_purge.push_back(offline_id); | |
| 140 } else { | |
| 141 pages_queue.push(page); | |
| 142 } | |
| 143 } | |
| 144 | |
| 145 // Negative counter means nothing else to purge. | |
| 146 int count_to_purge = | |
| 147 page_ids.size() - kMaxPagesToKeep - pages_to_purge.size(); | |
| 148 | |
| 149 for (int count = 0; count < count_to_purge; ++count) { | |
| 150 pages_to_purge.push_back(pages_queue.top()->offline_id); | |
| 151 pages_queue.pop(); | |
| 152 } | |
| 153 | |
| 154 page_model_->DeletePagesByOfflineId( | 104 page_model_->DeletePagesByOfflineId( |
| 155 pages_to_purge, base::Bind(&RecentTabHelper::ContinueSnapshotAfterPurge, | 105 page_ids, base::Bind(&RecentTabHelper::ContinueSnapshotAfterPurge, |
| 156 weak_ptr_factory_.GetWeakPtr())); | 106 weak_ptr_factory_.GetWeakPtr())); |
| 157 } | 107 } |
| 158 | 108 |
| 159 void RecentTabHelper::ContinueSnapshotAfterPurge( | 109 void RecentTabHelper::ContinueSnapshotAfterPurge( |
| 160 OfflinePageModel::DeletePageResult result) { | 110 OfflinePageModel::DeletePageResult result) { |
| 161 if (result != OfflinePageModel::DeletePageResult::SUCCESS) { | 111 if (result != OfflinePageModel::DeletePageResult::SUCCESS) { |
| 162 // If previous pages can't be deleted, don't add new ones. | 112 // If previous pages can't be deleted, don't add new ones. |
| 163 ReportSnapshotCompleted(); | 113 ReportSnapshotCompleted(); |
| 164 return; | 114 return; |
| 165 } | 115 } |
| 166 | 116 |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 190 | 140 |
| 191 void RecentTabHelper::ReportSnapshotCompleted() { | 141 void RecentTabHelper::ReportSnapshotCompleted() { |
| 192 snapshot_controller_->PendingSnapshotCompleted(); | 142 snapshot_controller_->PendingSnapshotCompleted(); |
| 193 } | 143 } |
| 194 | 144 |
| 195 bool RecentTabHelper::IsSamePage() const { | 145 bool RecentTabHelper::IsSamePage() const { |
| 196 return web_contents() && | 146 return web_contents() && |
| 197 (web_contents()->GetLastCommittedURL() == snapshot_url_); | 147 (web_contents()->GetLastCommittedURL() == snapshot_url_); |
| 198 } | 148 } |
| 199 | 149 |
| 200 ClientId RecentTabHelper::client_id() const { | 150 ClientId RecentTabHelper::client_id() const { |
|
fgorski
2016/06/24 17:49:20
could we please add client_id() verification in th
dewittj
2016/06/24 17:55:46
nit: This should be renamed GetClientId since it i
| |
| 201 return ClientId(kLastNNamespace, ""); | 151 TabAndroid* tab_android = TabAndroid::FromWebContents(web_contents()); |
| 152 if (!tab_android) { | |
| 153 return ClientId(kLastNNamespace, ""); | |
|
fgorski
2016/06/24 17:49:20
Please document what behavior you expect here. E.g
dewittj
2016/06/24 17:55:46
Is there a case where we can't get tab ID? If so
| |
| 154 } else { | |
| 155 std::string tab_id = base::IntToString(tab_android->GetAndroidId()); | |
| 156 return ClientId(kLastNNamespace, tab_id); | |
| 157 } | |
| 202 } | 158 } |
| 203 | 159 |
| 204 void RecentTabHelper::SetArchiveFactoryForTest( | 160 void RecentTabHelper::SetArchiveFactoryForTest( |
| 205 std::unique_ptr<TestArchiveFactory> test_archive_factory) { | 161 std::unique_ptr<TestArchiveFactory> test_archive_factory) { |
| 206 test_archive_factory_ = std::move(test_archive_factory); | 162 test_archive_factory_ = std::move(test_archive_factory); |
| 207 } | 163 } |
| 208 | 164 |
| 209 void RecentTabHelper::SetTaskRunnerForTest( | 165 void RecentTabHelper::SetTaskRunnerForTest( |
| 210 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) { | 166 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) { |
| 211 snapshot_controller_.reset(new SnapshotController(task_runner, this)); | 167 snapshot_controller_.reset(new SnapshotController(task_runner, this)); |
| 212 } | 168 } |
| 213 | 169 |
| 214 } // namespace offline_pages | 170 } // namespace offline_pages |
| OLD | NEW |