| 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" |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 for (int count = 0; count < count_to_purge; ++count) { | 141 for (int count = 0; count < count_to_purge; ++count) { |
| 142 pages_to_purge.push_back(pages_queue.top()->offline_id); | 142 pages_to_purge.push_back(pages_queue.top()->offline_id); |
| 143 pages_queue.pop(); | 143 pages_queue.pop(); |
| 144 } | 144 } |
| 145 | 145 |
| 146 page_model_->DeletePagesByOfflineId( | 146 page_model_->DeletePagesByOfflineId( |
| 147 pages_to_purge, base::Bind(&RecentTabHelper::ContinueSnapshotAfterPurge, | 147 pages_to_purge, base::Bind(&RecentTabHelper::ContinueSnapshotAfterPurge, |
| 148 weak_ptr_factory_.GetWeakPtr())); | 148 weak_ptr_factory_.GetWeakPtr())); |
| 149 } | 149 } |
| 150 | 150 |
| 151 void RecentTabHelper::ContinueSnapshotAfterPurge( | 151 void RecentTabHelper::ContinueSnapshotAfterPurge(DeletePageResult result) { |
| 152 OfflinePageModel::DeletePageResult result) { | |
| 153 // NOT_FOUND is because it's what we get when passing empty vector of ids. | 152 // NOT_FOUND is because it's what we get when passing empty vector of ids. |
| 154 // TODO(dimich): remove NOT_FOUND when bug 608057 is fixed. | 153 // TODO(dimich): remove NOT_FOUND when bug 608057 is fixed. |
| 155 if (result != OfflinePageModel::DeletePageResult::SUCCESS && | 154 if (result != DeletePageResult::SUCCESS && |
| 156 result != OfflinePageModel::DeletePageResult::NOT_FOUND) { | 155 result != DeletePageResult::NOT_FOUND) { |
| 157 // If previous pages can't be deleted, don't add new ones. | 156 // If previous pages can't be deleted, don't add new ones. |
| 158 ReportSnapshotCompleted(); | 157 ReportSnapshotCompleted(); |
| 159 return; | 158 return; |
| 160 } | 159 } |
| 161 | 160 |
| 162 if (!IsSamePage()) { | 161 if (!IsSamePage()) { |
| 163 ReportSnapshotCompleted(); | 162 ReportSnapshotCompleted(); |
| 164 return; | 163 return; |
| 165 } | 164 } |
| 166 | 165 |
| 167 // Create either test Archiver or a regular one. | 166 // Create either test Archiver or a regular one. |
| 168 std::unique_ptr<OfflinePageArchiver> archiver; | 167 std::unique_ptr<OfflinePageArchiver> archiver; |
| 169 if (test_archive_factory_.get()) { | 168 if (test_archive_factory_.get()) { |
| 170 archiver = test_archive_factory_->CreateArchiver(web_contents()); | 169 archiver = test_archive_factory_->CreateArchiver(web_contents()); |
| 171 } else { | 170 } else { |
| 172 archiver.reset(new OfflinePageMHTMLArchiver(web_contents())); | 171 archiver.reset(new OfflinePageMHTMLArchiver(web_contents())); |
| 173 } | 172 } |
| 174 | 173 |
| 175 page_model_->SavePage( | 174 page_model_->SavePage( |
| 176 snapshot_url_, client_id(), std::move(archiver), | 175 snapshot_url_, client_id(), std::move(archiver), |
| 177 base::Bind(&RecentTabHelper::SavePageCallback, | 176 base::Bind(&RecentTabHelper::SavePageCallback, |
| 178 weak_ptr_factory_.GetWeakPtr())); | 177 weak_ptr_factory_.GetWeakPtr())); |
| 179 } | 178 } |
| 180 | 179 |
| 181 void RecentTabHelper::SavePageCallback(OfflinePageModel::SavePageResult result, | 180 void RecentTabHelper::SavePageCallback(SavePageResult result, |
| 182 int64_t offline_id) { | 181 int64_t offline_id) { |
| 183 // TODO(dimich): add UMA, including result. Bug 608112. | 182 // TODO(dimich): add UMA, including result. Bug 608112. |
| 184 ReportSnapshotCompleted(); | 183 ReportSnapshotCompleted(); |
| 185 } | 184 } |
| 186 | 185 |
| 187 void RecentTabHelper::ReportSnapshotCompleted() { | 186 void RecentTabHelper::ReportSnapshotCompleted() { |
| 188 snapshot_controller_->PendingSnapshotCompleted(); | 187 snapshot_controller_->PendingSnapshotCompleted(); |
| 189 } | 188 } |
| 190 | 189 |
| 191 bool RecentTabHelper::IsSamePage() const { | 190 bool RecentTabHelper::IsSamePage() const { |
| 192 return web_contents() && | 191 return web_contents() && |
| 193 (web_contents()->GetLastCommittedURL() == snapshot_url_); | 192 (web_contents()->GetLastCommittedURL() == snapshot_url_); |
| 194 } | 193 } |
| 195 | 194 |
| 196 ClientId RecentTabHelper::client_id() const { | 195 ClientId RecentTabHelper::client_id() const { |
| 197 return ClientId(kClientNamespace, ""); | 196 return ClientId(kClientNamespace, ""); |
| 198 } | 197 } |
| 199 | 198 |
| 200 void RecentTabHelper::SetArchiveFactoryForTest( | 199 void RecentTabHelper::SetArchiveFactoryForTest( |
| 201 std::unique_ptr<TestArchiveFactory> test_archive_factory) { | 200 std::unique_ptr<TestArchiveFactory> test_archive_factory) { |
| 202 test_archive_factory_ = std::move(test_archive_factory); | 201 test_archive_factory_ = std::move(test_archive_factory); |
| 203 } | 202 } |
| 204 | 203 |
| 205 void RecentTabHelper::SetTaskRunnerForTest( | 204 void RecentTabHelper::SetTaskRunnerForTest( |
| 206 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) { | 205 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) { |
| 207 snapshot_controller_.reset(new SnapshotController(task_runner, this)); | 206 snapshot_controller_.reset(new SnapshotController(task_runner, this)); |
| 208 } | 207 } |
| 209 | 208 |
| 210 } // namespace offline_pages | 209 } // namespace offline_pages |
| OLD | NEW |