Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(132)

Side by Side Diff: chrome/browser/android/offline_pages/recent_tab_helper.cc

Issue 2824623002: Last_n: Delete previously saved snapshot when navigating. (Closed)
Patch Set: Annotated method with @MainThread; comment updates Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
215 // Cancel any and all in flight snapshot tasks from the previous page. 221 // 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";
218 CancelInFlightSnapshots(); 222 CancelInFlightSnapshots();
219 downloads_snapshot_on_hold_ = false; 223 downloads_snapshot_on_hold_ = false;
220 224
221 // Always reset so that posted tasks get canceled. 225 // Always reset so that posted tasks get canceled.
222 snapshot_controller_->Reset(); 226 snapshot_controller_->Reset();
223 227
224 // Check for conditions that would cause us not to snapshot. 228 // Check for conditions that should stop last_n from creating snapshots of
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.
225 bool can_save = 234 bool can_save =
226 !navigation_handle->IsErrorPage() && 235 !navigation_handle->IsErrorPage() && !navigation_handle->IsPost() &&
227 OfflinePageModel::CanSaveURL(web_contents()->GetLastCommittedURL()) && 236 OfflinePageModel::CanSaveURL(web_contents()->GetLastCommittedURL()) &&
228 OfflinePageUtils::GetOfflinePageFromWebContents(web_contents()) == 237 OfflinePageUtils::GetOfflinePageFromWebContents(web_contents()) ==
229 nullptr; 238 nullptr;
230 DVLOG_IF(1, !can_save) << " - Page can not be saved for offline usage"; 239 DVLOG_IF(1, !can_save) << " - Page can not be saved for offline usage";
231 240
232 UMA_HISTOGRAM_BOOLEAN("OfflinePages.CanSaveRecentPage", can_save); 241 UMA_HISTOGRAM_BOOLEAN("OfflinePages.CanSaveRecentPage", can_save);
233 242
234 if (!can_save) 243 if (!can_save)
235 snapshot_controller_->Stop(); 244 snapshot_controller_->Stop();
236 last_n_listen_to_tab_hidden_ = can_save && !delegate_->IsLowEndDevice() && 245 last_n_listen_to_tab_hidden_ = can_save && !delegate_->IsLowEndDevice() &&
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
412 OfflinePageUtils::GetOriginalURLFromWebContents(web_contents()); 421 OfflinePageUtils::GetOriginalURLFromWebContents(web_contents());
413 page_model_->SavePage( 422 page_model_->SavePage(
414 save_page_params, delegate_->CreatePageArchiver(web_contents()), 423 save_page_params, delegate_->CreatePageArchiver(web_contents()),
415 base::Bind(&RecentTabHelper::SavePageCallback, 424 base::Bind(&RecentTabHelper::SavePageCallback,
416 weak_ptr_factory_.GetWeakPtr(), snapshot_info)); 425 weak_ptr_factory_.GetWeakPtr(), snapshot_info));
417 } 426 }
418 427
419 void RecentTabHelper::SavePageCallback(SnapshotProgressInfo* snapshot_info, 428 void RecentTabHelper::SavePageCallback(SnapshotProgressInfo* snapshot_info,
420 OfflinePageModel::SavePageResult result, 429 OfflinePageModel::SavePageResult result,
421 int64_t offline_id) { 430 int64_t offline_id) {
422 DCHECK(snapshot_info->IsForLastN() || 431 DCHECK((snapshot_info->IsForLastN() &&
432 snapshot_info->request_id == OfflinePageModel::kInvalidOfflineId) ||
423 snapshot_info->request_id == offline_id); 433 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;
424 ReportSnapshotCompleted(snapshot_info, result == SavePageResult::SUCCESS); 437 ReportSnapshotCompleted(snapshot_info, result == SavePageResult::SUCCESS);
425 } 438 }
426 439
427 // Note: this is the final step in the chain of callbacks and it's where the 440 // Note: this is the final step in the chain of callbacks and it's where the
428 // behavior is different depending on this being a last_n or downloads snapshot. 441 // behavior is different depending on this being a last_n or downloads snapshot.
429 void RecentTabHelper::ReportSnapshotCompleted( 442 void RecentTabHelper::ReportSnapshotCompleted(
430 SnapshotProgressInfo* snapshot_info, 443 SnapshotProgressInfo* snapshot_info,
431 bool success) { 444 bool success) {
432 DVLOG(1) << (snapshot_info->IsForLastN() ? "Last_n" : "Downloads") 445 DVLOG(1) << (snapshot_info->IsForLastN() ? "Last_n" : "Downloads")
433 << " snapshot " << (success ? "succeeded" : "failed") 446 << " snapshot " << (success ? "succeeded" : "failed")
434 << " for: " << web_contents()->GetLastCommittedURL().spec(); 447 << " for: " << web_contents()->GetLastCommittedURL().spec();
435 if (snapshot_info->IsForLastN()) { 448 if (snapshot_info->IsForLastN()) {
436 DCHECK_EQ(snapshot_info, last_n_ongoing_snapshot_info_.get()); 449 DCHECK_EQ(snapshot_info, last_n_ongoing_snapshot_info_.get());
437 last_n_ongoing_snapshot_info_.reset(); 450 if (success) {
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 }
438 return; 456 return;
439 } 457 }
440 458
441 DCHECK_EQ(snapshot_info, downloads_ongoing_snapshot_info_.get()); 459 DCHECK_EQ(snapshot_info, downloads_ongoing_snapshot_info_.get());
442 snapshot_controller_->PendingSnapshotCompleted(); 460 snapshot_controller_->PendingSnapshotCompleted();
443 // Tell RequestCoordinator how the request should be processed further. 461 // Tell RequestCoordinator how the request should be processed further.
444 ReportDownloadStatusToRequestCoordinator(snapshot_info, success); 462 ReportDownloadStatusToRequestCoordinator(snapshot_info, success);
445 if (success) { 463 if (success) {
446 downloads_latest_saved_snapshot_info_ = 464 downloads_latest_saved_snapshot_info_ =
447 std::move(downloads_ongoing_snapshot_info_); 465 std::move(downloads_ongoing_snapshot_info_);
(...skipping 26 matching lines...) Expand all
474 request_coordinator->EnableForOffliner(snapshot_info->request_id, 492 request_coordinator->EnableForOffliner(snapshot_info->request_id,
475 snapshot_info->client_id); 493 snapshot_info->client_id);
476 } 494 }
477 } 495 }
478 496
479 ClientId RecentTabHelper::GetRecentPagesClientId() const { 497 ClientId RecentTabHelper::GetRecentPagesClientId() const {
480 return ClientId(kLastNNamespace, tab_id_); 498 return ClientId(kLastNNamespace, tab_id_);
481 } 499 }
482 500
483 void RecentTabHelper::CancelInFlightSnapshots() { 501 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";
484 weak_ptr_factory_.InvalidateWeakPtrs(); 506 weak_ptr_factory_.InvalidateWeakPtrs();
485 downloads_ongoing_snapshot_info_.reset(); 507 downloads_ongoing_snapshot_info_.reset();
486 downloads_latest_saved_snapshot_info_.reset(); 508 downloads_latest_saved_snapshot_info_.reset();
487 last_n_ongoing_snapshot_info_.reset(); 509 last_n_ongoing_snapshot_info_.reset();
510 last_n_latest_saved_snapshot_info_.reset();
488 } 511 }
489 512
490 } // namespace offline_pages 513 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698