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

Unified Diff: chrome/browser/android/offline_pages/background_loader_offliner.cc

Issue 2714733003: [Offline Pages] Add a timer to BackgroundLoaderOffliner to delay SavePage by 2 seconds. (Closed)
Patch Set: Created 3 years, 10 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/android/offline_pages/background_loader_offliner.cc
diff --git a/chrome/browser/android/offline_pages/background_loader_offliner.cc b/chrome/browser/android/offline_pages/background_loader_offliner.cc
index 2bf511a66d992509ba0bf92c682e0e22dc883d88..ea41256f640a6f6ad7fec0e214f0e36d5c6f64e1 100644
--- a/chrome/browser/android/offline_pages/background_loader_offliner.cc
+++ b/chrome/browser/android/offline_pages/background_loader_offliner.cc
@@ -18,6 +18,10 @@
namespace offline_pages {
+namespace {
+long kOfflinePageDelayMs = 2000;
Pete Williamson 2017/02/24 17:31:45 As a follow up change, let's put this into Offline
chili 2017/02/24 21:43:47 Acknowledged.
+} // namespace
+
BackgroundLoaderOffliner::BackgroundLoaderOffliner(
content::BrowserContext* browser_context,
const OfflinerPolicy* policy,
@@ -94,6 +98,9 @@ bool BackgroundLoaderOffliner::LoadAndSave(const SavePageRequest& request,
if (!loader_)
ResetState();
+ // Invalidate ptrs for all delayed/saving tasks.
+ weak_ptr_factory_.InvalidateWeakPtrs();
+
// Track copy of pending request.
pending_request_.reset(new SavePageRequest(request));
completion_callback_ = callback;
@@ -129,40 +136,15 @@ void BackgroundLoaderOffliner::DidStopLoading() {
return;
}
- SavePageRequest request(*pending_request_.get());
Pete Williamson 2017/02/24 17:31:45 How about leaving this code where it is instead of
chili 2017/02/24 21:43:47 I think we still need this check in SavePage becau
- // If there was an error navigating to page, return loading failed.
- if (page_load_state_ != SUCCESS) {
- Offliner::RequestStatus status =
- (page_load_state_ == RETRIABLE)
- ? Offliner::RequestStatus::LOADING_FAILED
- : Offliner::RequestStatus::LOADING_FAILED_NO_RETRY;
- completion_callback_.Run(request, status);
- ResetState();
- return;
- }
-
- save_state_ = SAVING;
- content::WebContents* web_contents(
- content::WebContentsObserver::web_contents());
-
- std::unique_ptr<OfflinePageArchiver> archiver(
- new OfflinePageMHTMLArchiver(web_contents));
-
- OfflinePageModel::SavePageParams params;
- params.url = web_contents->GetLastCommittedURL();
- params.client_id = request.client_id();
- params.proposed_offline_id = request.request_id();
- params.is_background = true;
+ // Invalidate ptrs for any ongoing save operation.
+ weak_ptr_factory_.InvalidateWeakPtrs();
- // Pass in the original URL if it's different from last committed
- // when redirects occur.
- if (params.url != request.url())
- params.original_url = request.url();
-
- offline_page_model_->SavePage(
- params, std::move(archiver),
- base::Bind(&BackgroundLoaderOffliner::OnPageSaved,
- weak_ptr_factory_.GetWeakPtr()));
+ // Post SavePage task with 2 second delay.
+ base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
+ FROM_HERE,
+ base::Bind(&BackgroundLoaderOffliner::SavePage,
+ weak_ptr_factory_.GetWeakPtr()),
+ base::TimeDelta::FromMilliseconds(kOfflinePageDelayMs));
}
void BackgroundLoaderOffliner::RenderProcessGone(
@@ -232,6 +214,48 @@ void BackgroundLoaderOffliner::DidFinishNavigation(
}
}
+void BackgroundLoaderOffliner::SavePage() {
+ if (!pending_request_.get()) {
+ DVLOG(1) << "Pending request was cleared during delay.";
+ return;
+ }
+
+ SavePageRequest request(*pending_request_.get());
+ // If there was an error navigating to page, return loading failed.
+ if (page_load_state_ != SUCCESS) {
+ Offliner::RequestStatus status =
+ (page_load_state_ == RETRIABLE)
+ ? Offliner::RequestStatus::LOADING_FAILED
+ : Offliner::RequestStatus::LOADING_FAILED_NO_RETRY;
+ completion_callback_.Run(request, status);
+ ResetState();
+ return;
+ }
+
+ save_state_ = SAVING;
+ content::WebContents* web_contents(
+ content::WebContentsObserver::web_contents());
+
+ std::unique_ptr<OfflinePageArchiver> archiver(
+ new OfflinePageMHTMLArchiver(web_contents));
+
+ OfflinePageModel::SavePageParams params;
+ params.url = web_contents->GetLastCommittedURL();
+ params.client_id = request.client_id();
+ params.proposed_offline_id = request.request_id();
+ params.is_background = true;
+
+ // Pass in the original URL if it's different from last committed
+ // when redirects occur.
+ if (params.url != request.url())
+ params.original_url = request.url();
+
+ offline_page_model_->SavePage(
+ params, std::move(archiver),
+ base::Bind(&BackgroundLoaderOffliner::OnPageSaved,
+ weak_ptr_factory_.GetWeakPtr()));
+}
+
void BackgroundLoaderOffliner::OnPageSaved(SavePageResult save_result,
int64_t offline_id) {
if (!pending_request_)

Powered by Google App Engine
This is Rietveld 408576698