| 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 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 << web_contents()->GetLastCommittedURL().spec(); | 205 << web_contents()->GetLastCommittedURL().spec(); |
| 206 | 206 |
| 207 // If there is an ongoing downloads request, lets make Background Offliner | 207 // If there is an ongoing downloads request, lets make Background Offliner |
| 208 // take over downloading that page. | 208 // take over downloading that page. |
| 209 if (downloads_ongoing_snapshot_info_) { | 209 if (downloads_ongoing_snapshot_info_) { |
| 210 DVLOG(1) << " - Passing ongoing downloads request to Background Offliner"; | 210 DVLOG(1) << " - Passing ongoing downloads request to Background Offliner"; |
| 211 ReportDownloadStatusToRequestCoordinator( | 211 ReportDownloadStatusToRequestCoordinator( |
| 212 downloads_ongoing_snapshot_info_.get(), false); | 212 downloads_ongoing_snapshot_info_.get(), false); |
| 213 } | 213 } |
| 214 | 214 |
| 215 // If the previous page was saved, delete it now. | |
| 216 if (last_n_latest_saved_snapshot_info_) { | |
| 217 std::vector<int64_t> id{last_n_latest_saved_snapshot_info_->request_id}; | |
| 218 page_model_->DeletePagesByOfflineId(id, DeletePageCallback()); | |
| 219 } | |
| 220 | |
| 221 // Cancel any and all in flight snapshot tasks from the previous page. | 215 // Cancel any and all in flight snapshot tasks from the previous page. |
| 216 DVLOG_IF(1, last_n_ongoing_snapshot_info_) |
| 217 << " - Canceling ongoing last_n snapshot"; |
| 222 CancelInFlightSnapshots(); | 218 CancelInFlightSnapshots(); |
| 223 downloads_snapshot_on_hold_ = false; | 219 downloads_snapshot_on_hold_ = false; |
| 224 | 220 |
| 225 // Always reset so that posted tasks get canceled. | 221 // Always reset so that posted tasks get canceled. |
| 226 snapshot_controller_->Reset(); | 222 snapshot_controller_->Reset(); |
| 227 | 223 |
| 228 // Check for conditions that should stop last_n from creating snapshots of | 224 // Check for conditions that would cause us not to snapshot. |
| 229 // this page: | |
| 230 // - It is an error page. | |
| 231 // - The navigation is a POST as offline pages are never loaded for them. | |
| 232 // - The navigated URL is not supported. | |
| 233 // - The page being loaded is already an offline page. | |
| 234 bool can_save = | 225 bool can_save = |
| 235 !navigation_handle->IsErrorPage() && !navigation_handle->IsPost() && | 226 !navigation_handle->IsErrorPage() && |
| 236 OfflinePageModel::CanSaveURL(web_contents()->GetLastCommittedURL()) && | 227 OfflinePageModel::CanSaveURL(web_contents()->GetLastCommittedURL()) && |
| 237 OfflinePageUtils::GetOfflinePageFromWebContents(web_contents()) == | 228 OfflinePageUtils::GetOfflinePageFromWebContents(web_contents()) == |
| 238 nullptr; | 229 nullptr; |
| 239 DVLOG_IF(1, !can_save) << " - Page can not be saved for offline usage"; | 230 DVLOG_IF(1, !can_save) << " - Page can not be saved for offline usage"; |
| 240 | 231 |
| 241 UMA_HISTOGRAM_BOOLEAN("OfflinePages.CanSaveRecentPage", can_save); | 232 UMA_HISTOGRAM_BOOLEAN("OfflinePages.CanSaveRecentPage", can_save); |
| 242 | 233 |
| 243 if (!can_save) | 234 if (!can_save) |
| 244 snapshot_controller_->Stop(); | 235 snapshot_controller_->Stop(); |
| 245 last_n_listen_to_tab_hidden_ = can_save && !delegate_->IsLowEndDevice() && | 236 last_n_listen_to_tab_hidden_ = can_save && !delegate_->IsLowEndDevice() && |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 421 OfflinePageUtils::GetOriginalURLFromWebContents(web_contents()); | 412 OfflinePageUtils::GetOriginalURLFromWebContents(web_contents()); |
| 422 page_model_->SavePage( | 413 page_model_->SavePage( |
| 423 save_page_params, delegate_->CreatePageArchiver(web_contents()), | 414 save_page_params, delegate_->CreatePageArchiver(web_contents()), |
| 424 base::Bind(&RecentTabHelper::SavePageCallback, | 415 base::Bind(&RecentTabHelper::SavePageCallback, |
| 425 weak_ptr_factory_.GetWeakPtr(), snapshot_info)); | 416 weak_ptr_factory_.GetWeakPtr(), snapshot_info)); |
| 426 } | 417 } |
| 427 | 418 |
| 428 void RecentTabHelper::SavePageCallback(SnapshotProgressInfo* snapshot_info, | 419 void RecentTabHelper::SavePageCallback(SnapshotProgressInfo* snapshot_info, |
| 429 OfflinePageModel::SavePageResult result, | 420 OfflinePageModel::SavePageResult result, |
| 430 int64_t offline_id) { | 421 int64_t offline_id) { |
| 431 DCHECK((snapshot_info->IsForLastN() && | 422 DCHECK(snapshot_info->IsForLastN() || |
| 432 snapshot_info->request_id == OfflinePageModel::kInvalidOfflineId) || | |
| 433 snapshot_info->request_id == offline_id); | 423 snapshot_info->request_id == offline_id); |
| 434 // Store the assigned offline_id (for downloads case it will already contain | |
| 435 // the same value). | |
| 436 snapshot_info->request_id = offline_id; | |
| 437 ReportSnapshotCompleted(snapshot_info, result == SavePageResult::SUCCESS); | 424 ReportSnapshotCompleted(snapshot_info, result == SavePageResult::SUCCESS); |
| 438 } | 425 } |
| 439 | 426 |
| 440 // Note: this is the final step in the chain of callbacks and it's where the | 427 // Note: this is the final step in the chain of callbacks and it's where the |
| 441 // behavior is different depending on this being a last_n or downloads snapshot. | 428 // behavior is different depending on this being a last_n or downloads snapshot. |
| 442 void RecentTabHelper::ReportSnapshotCompleted( | 429 void RecentTabHelper::ReportSnapshotCompleted( |
| 443 SnapshotProgressInfo* snapshot_info, | 430 SnapshotProgressInfo* snapshot_info, |
| 444 bool success) { | 431 bool success) { |
| 445 DVLOG(1) << (snapshot_info->IsForLastN() ? "Last_n" : "Downloads") | 432 DVLOG(1) << (snapshot_info->IsForLastN() ? "Last_n" : "Downloads") |
| 446 << " snapshot " << (success ? "succeeded" : "failed") | 433 << " snapshot " << (success ? "succeeded" : "failed") |
| 447 << " for: " << web_contents()->GetLastCommittedURL().spec(); | 434 << " for: " << web_contents()->GetLastCommittedURL().spec(); |
| 448 if (snapshot_info->IsForLastN()) { | 435 if (snapshot_info->IsForLastN()) { |
| 449 DCHECK_EQ(snapshot_info, last_n_ongoing_snapshot_info_.get()); | 436 DCHECK_EQ(snapshot_info, last_n_ongoing_snapshot_info_.get()); |
| 450 if (success) { | 437 last_n_ongoing_snapshot_info_.reset(); |
| 451 last_n_latest_saved_snapshot_info_ = | |
| 452 std::move(last_n_ongoing_snapshot_info_); | |
| 453 } else { | |
| 454 last_n_ongoing_snapshot_info_.reset(); | |
| 455 } | |
| 456 return; | 438 return; |
| 457 } | 439 } |
| 458 | 440 |
| 459 DCHECK_EQ(snapshot_info, downloads_ongoing_snapshot_info_.get()); | 441 DCHECK_EQ(snapshot_info, downloads_ongoing_snapshot_info_.get()); |
| 460 snapshot_controller_->PendingSnapshotCompleted(); | 442 snapshot_controller_->PendingSnapshotCompleted(); |
| 461 // Tell RequestCoordinator how the request should be processed further. | 443 // Tell RequestCoordinator how the request should be processed further. |
| 462 ReportDownloadStatusToRequestCoordinator(snapshot_info, success); | 444 ReportDownloadStatusToRequestCoordinator(snapshot_info, success); |
| 463 if (success) { | 445 if (success) { |
| 464 downloads_latest_saved_snapshot_info_ = | 446 downloads_latest_saved_snapshot_info_ = |
| 465 std::move(downloads_ongoing_snapshot_info_); | 447 std::move(downloads_ongoing_snapshot_info_); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 492 request_coordinator->EnableForOffliner(snapshot_info->request_id, | 474 request_coordinator->EnableForOffliner(snapshot_info->request_id, |
| 493 snapshot_info->client_id); | 475 snapshot_info->client_id); |
| 494 } | 476 } |
| 495 } | 477 } |
| 496 | 478 |
| 497 ClientId RecentTabHelper::GetRecentPagesClientId() const { | 479 ClientId RecentTabHelper::GetRecentPagesClientId() const { |
| 498 return ClientId(kLastNNamespace, tab_id_); | 480 return ClientId(kLastNNamespace, tab_id_); |
| 499 } | 481 } |
| 500 | 482 |
| 501 void RecentTabHelper::CancelInFlightSnapshots() { | 483 void RecentTabHelper::CancelInFlightSnapshots() { |
| 502 DVLOG_IF(1, last_n_ongoing_snapshot_info_) | |
| 503 << " - Canceling ongoing last_n snapshot"; | |
| 504 DVLOG_IF(1, downloads_ongoing_snapshot_info_) | |
| 505 << " - Canceling ongoing downloads snapshot"; | |
| 506 weak_ptr_factory_.InvalidateWeakPtrs(); | 484 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 507 downloads_ongoing_snapshot_info_.reset(); | 485 downloads_ongoing_snapshot_info_.reset(); |
| 508 downloads_latest_saved_snapshot_info_.reset(); | 486 downloads_latest_saved_snapshot_info_.reset(); |
| 509 last_n_ongoing_snapshot_info_.reset(); | 487 last_n_ongoing_snapshot_info_.reset(); |
| 510 last_n_latest_saved_snapshot_info_.reset(); | |
| 511 } | 488 } |
| 512 | 489 |
| 513 } // namespace offline_pages | 490 } // namespace offline_pages |
| OLD | NEW |