| 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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 }; | 52 }; |
| 53 } // namespace | 53 } // namespace |
| 54 | 54 |
| 55 namespace offline_pages { | 55 namespace offline_pages { |
| 56 | 56 |
| 57 RecentTabHelper::RecentTabHelper(content::WebContents* web_contents) | 57 RecentTabHelper::RecentTabHelper(content::WebContents* web_contents) |
| 58 : content::WebContentsObserver(web_contents), | 58 : content::WebContentsObserver(web_contents), |
| 59 page_model_(nullptr), | 59 page_model_(nullptr), |
| 60 snapshots_enabled_(false), | 60 snapshots_enabled_(false), |
| 61 is_page_ready_for_snapshot_(false), | 61 is_page_ready_for_snapshot_(false), |
| 62 previous_request_id_for_load_(OfflinePageModel::kInvalidOfflineId), | |
| 63 delegate_(new DefaultDelegate()), | 62 delegate_(new DefaultDelegate()), |
| 64 weak_ptr_factory_(this) { | 63 weak_ptr_factory_(this) { |
| 65 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 64 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 66 } | 65 } |
| 67 | 66 |
| 68 RecentTabHelper::~RecentTabHelper() { | 67 RecentTabHelper::~RecentTabHelper() { |
| 69 } | 68 } |
| 70 | 69 |
| 71 void RecentTabHelper::SetDelegate( | 70 void RecentTabHelper::SetDelegate( |
| 72 std::unique_ptr<RecentTabHelper::Delegate> delegate) { | 71 std::unique_ptr<RecentTabHelper::Delegate> delegate) { |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 } | 152 } |
| 154 | 153 |
| 155 if (offline_pages::IsOffliningRecentPagesEnabled()) { | 154 if (offline_pages::IsOffliningRecentPagesEnabled()) { |
| 156 download_info_ = base::MakeUnique<DownloadPageInfo>( | 155 download_info_ = base::MakeUnique<DownloadPageInfo>( |
| 157 GetRecentPagesClientId(), OfflinePageModel::kInvalidOfflineId); | 156 GetRecentPagesClientId(), OfflinePageModel::kInvalidOfflineId); |
| 158 } else { | 157 } else { |
| 159 download_info_.reset(); | 158 download_info_.reset(); |
| 160 } | 159 } |
| 161 | 160 |
| 162 is_page_ready_for_snapshot_ = false; | 161 is_page_ready_for_snapshot_ = false; |
| 163 previous_request_id_for_load_ = OfflinePageModel::kInvalidOfflineId; | |
| 164 | 162 |
| 165 // New navigation, new snapshot session. | 163 // New navigation, new snapshot session. |
| 166 snapshot_url_ = web_contents()->GetLastCommittedURL(); | 164 snapshot_url_ = web_contents()->GetLastCommittedURL(); |
| 167 | 165 |
| 168 // Check for conditions that would cause us not to snapshot. | 166 // Check for conditions that would cause us not to snapshot. |
| 169 bool can_save = !navigation_handle->IsErrorPage() && | 167 bool can_save = !navigation_handle->IsErrorPage() && |
| 170 OfflinePageModel::CanSaveURL(snapshot_url_) && | 168 OfflinePageModel::CanSaveURL(snapshot_url_) && |
| 171 OfflinePageUtils::GetOfflinePageFromWebContents( | 169 OfflinePageUtils::GetOfflinePageFromWebContents( |
| 172 web_contents()) == nullptr; | 170 web_contents()) == nullptr; |
| 173 | 171 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 GetRecentPagesClientId(), | 217 GetRecentPagesClientId(), |
| 220 base::Bind(&RecentTabHelper::ContinueSnapshotWithIdsToPurge, | 218 base::Bind(&RecentTabHelper::ContinueSnapshotWithIdsToPurge, |
| 221 weak_ptr_factory_.GetWeakPtr())); | 219 weak_ptr_factory_.GetWeakPtr())); |
| 222 } | 220 } |
| 223 | 221 |
| 224 void RecentTabHelper::ContinueSnapshotWithIdsToPurge( | 222 void RecentTabHelper::ContinueSnapshotWithIdsToPurge( |
| 225 const std::vector<int64_t>& page_ids) { | 223 const std::vector<int64_t>& page_ids) { |
| 226 if (!download_info_) | 224 if (!download_info_) |
| 227 return; | 225 return; |
| 228 | 226 |
| 227 // Also remove the download page if this is not a first snapshot. |
| 229 std::vector<int64_t> ids(page_ids); | 228 std::vector<int64_t> ids(page_ids); |
| 230 // Also remove the download page if this is not a first snapshot. | |
| 231 ids.push_back(download_info_->request_id_); | 229 ids.push_back(download_info_->request_id_); |
| 232 // Do not delete a previous save from the same page load right now as it's | |
| 233 // unknown at this point in time if this snapshot will succeed. | |
| 234 for (auto it = ids.begin(); it != ids.end(); ++it) { | |
| 235 if (*it == previous_request_id_for_load_) { | |
| 236 ids.erase(it); | |
| 237 break; | |
| 238 } | |
| 239 } | |
| 240 | 230 |
| 241 page_model_->DeletePagesByOfflineId( | 231 page_model_->DeletePagesByOfflineId( |
| 242 ids, base::Bind(&RecentTabHelper::ContinueSnapshotAfterPurge, | 232 ids, base::Bind(&RecentTabHelper::ContinueSnapshotAfterPurge, |
| 243 weak_ptr_factory_.GetWeakPtr())); | 233 weak_ptr_factory_.GetWeakPtr())); |
| 244 } | 234 } |
| 245 | 235 |
| 246 void RecentTabHelper::ContinueSnapshotAfterPurge( | 236 void RecentTabHelper::ContinueSnapshotAfterPurge( |
| 247 OfflinePageModel::DeletePageResult result) { | 237 OfflinePageModel::DeletePageResult result) { |
| 248 if (!download_info_ || | 238 if (!download_info_ || |
| 249 result != OfflinePageModel::DeletePageResult::SUCCESS || | 239 result != OfflinePageModel::DeletePageResult::SUCCESS || |
| (...skipping 11 matching lines...) Expand all Loading... |
| 261 base::Bind(&RecentTabHelper::SavePageCallback, | 251 base::Bind(&RecentTabHelper::SavePageCallback, |
| 262 weak_ptr_factory_.GetWeakPtr())); | 252 weak_ptr_factory_.GetWeakPtr())); |
| 263 } | 253 } |
| 264 | 254 |
| 265 void RecentTabHelper::SavePageCallback(OfflinePageModel::SavePageResult result, | 255 void RecentTabHelper::SavePageCallback(OfflinePageModel::SavePageResult result, |
| 266 int64_t offline_id) { | 256 int64_t offline_id) { |
| 267 if (!download_info_) | 257 if (!download_info_) |
| 268 return; | 258 return; |
| 269 download_info_->page_snapshot_completed_ = | 259 download_info_->page_snapshot_completed_ = |
| 270 (result == SavePageResult::SUCCESS); | 260 (result == SavePageResult::SUCCESS); |
| 271 | |
| 272 if (download_info_->page_snapshot_completed_) { | |
| 273 int64_t previous_request_id = previous_request_id_for_load_; | |
| 274 previous_request_id_for_load_ = offline_id; | |
| 275 | |
| 276 // If there is a previous snapshot saved during the same page load it must | |
| 277 // be deleted before finishing the snapshot process. | |
| 278 if (previous_request_id != OfflinePageModel::kInvalidOfflineId) { | |
| 279 std::vector<int64_t> page_id = {previous_request_id}; | |
| 280 page_model_->DeletePagesByOfflineId( | |
| 281 page_id, base::Bind(&RecentTabHelper::PreviousSaveWasPurged, | |
| 282 weak_ptr_factory_.GetWeakPtr())); | |
| 283 return; | |
| 284 } | |
| 285 } | |
| 286 | |
| 287 ReportSnapshotCompleted(); | 261 ReportSnapshotCompleted(); |
| 288 } | 262 } |
| 289 | 263 |
| 290 void RecentTabHelper::PreviousSaveWasPurged( | |
| 291 OfflinePageModel::DeletePageResult result) { | |
| 292 if (!download_info_) | |
| 293 return; | |
| 294 | |
| 295 ReportSnapshotCompleted(); | |
| 296 } | |
| 297 | |
| 298 void RecentTabHelper::ReportSnapshotCompleted() { | 264 void RecentTabHelper::ReportSnapshotCompleted() { |
| 299 snapshot_controller_->PendingSnapshotCompleted(); | 265 snapshot_controller_->PendingSnapshotCompleted(); |
| 300 // Tell RequestCoordinator how the request should be processed further. | 266 // Tell RequestCoordinator how the request should be processed further. |
| 301 ReportDownloadStatusToRequestCoordinator(); | 267 ReportDownloadStatusToRequestCoordinator(); |
| 302 } | 268 } |
| 303 | 269 |
| 304 void RecentTabHelper::ReportDownloadStatusToRequestCoordinator() { | 270 void RecentTabHelper::ReportDownloadStatusToRequestCoordinator() { |
| 305 if (!download_info_) | 271 if (!download_info_) |
| 306 return; | 272 return; |
| 307 | 273 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 327 bool RecentTabHelper::IsSamePage() const { | 293 bool RecentTabHelper::IsSamePage() const { |
| 328 return web_contents() && | 294 return web_contents() && |
| 329 (web_contents()->GetLastCommittedURL() == snapshot_url_); | 295 (web_contents()->GetLastCommittedURL() == snapshot_url_); |
| 330 } | 296 } |
| 331 | 297 |
| 332 ClientId RecentTabHelper::GetRecentPagesClientId() const { | 298 ClientId RecentTabHelper::GetRecentPagesClientId() const { |
| 333 return ClientId(kLastNNamespace, tab_id_); | 299 return ClientId(kLastNNamespace, tab_id_); |
| 334 } | 300 } |
| 335 | 301 |
| 336 } // namespace offline_pages | 302 } // namespace offline_pages |
| OLD | NEW |