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

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

Issue 2789503002: Revert of [Offline pages] Integrate snapshot controller fully into background loader. (Closed)
Patch Set: 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
« no previous file with comments | « chrome/browser/android/offline_pages/background_loader_offliner.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 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/background_loader_offliner.h" 5 #include "chrome/browser/android/offline_pages/background_loader_offliner.h"
6 6
7 #include "base/metrics/histogram_macros.h" 7 #include "base/metrics/histogram_macros.h"
8 #include "base/sys_info.h" 8 #include "base/sys_info.h"
9 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" 9 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h"
10 #include "chrome/browser/android/offline_pages/offliner_helper.h" 10 #include "chrome/browser/android/offline_pages/offliner_helper.h"
11 #include "chrome/browser/profiles/profile.h" 11 #include "chrome/browser/profiles/profile.h"
12 #include "components/offline_pages/core/background/save_page_request.h" 12 #include "components/offline_pages/core/background/save_page_request.h"
13 #include "components/offline_pages/core/client_namespace_constants.h" 13 #include "components/offline_pages/core/client_namespace_constants.h"
14 #include "components/offline_pages/core/offline_page_model.h" 14 #include "components/offline_pages/core/offline_page_model.h"
15 #include "content/public/browser/browser_context.h" 15 #include "content/public/browser/browser_context.h"
16 #include "content/public/browser/navigation_handle.h" 16 #include "content/public/browser/navigation_handle.h"
17 #include "content/public/browser/render_frame_host.h"
18 #include "content/public/browser/web_contents.h" 17 #include "content/public/browser/web_contents.h"
19 #include "content/public/browser/web_contents_user_data.h" 18 #include "content/public/browser/web_contents_user_data.h"
20 19
21 namespace offline_pages { 20 namespace offline_pages {
22 21
23 namespace { 22 namespace {
24 const long kOfflinePageDelayMs = 2000; 23 const long kOfflinePageDelayMs = 2000;
25 const long kOfflineDomContentLoadedMs = 25000;
26 24
27 class OfflinerData : public content::WebContentsUserData<OfflinerData> { 25 class OfflinerData : public content::WebContentsUserData<OfflinerData> {
28 public: 26 public:
29 static void AddToWebContents(content::WebContents* webcontents, 27 static void AddToWebContents(content::WebContents* webcontents,
30 BackgroundLoaderOffliner* offliner) { 28 BackgroundLoaderOffliner* offliner) {
31 DCHECK(offliner); 29 DCHECK(offliner);
32 webcontents->SetUserData(UserDataKey(), std::unique_ptr<OfflinerData>( 30 webcontents->SetUserData(UserDataKey(), std::unique_ptr<OfflinerData>(
33 new OfflinerData(offliner))); 31 new OfflinerData(offliner)));
34 } 32 }
35 33
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 } 146 }
149 147
150 if (!OfflinePageModel::CanSaveURL(request.url())) { 148 if (!OfflinePageModel::CanSaveURL(request.url())) {
151 DVLOG(1) << "Not able to save page for requested url: " << request.url(); 149 DVLOG(1) << "Not able to save page for requested url: " << request.url();
152 return false; 150 return false;
153 } 151 }
154 152
155 if (!loader_) 153 if (!loader_)
156 ResetState(); 154 ResetState();
157 155
156 // Invalidate ptrs for all delayed/saving tasks.
157 weak_ptr_factory_.InvalidateWeakPtrs();
158
158 // Track copy of pending request. 159 // Track copy of pending request.
159 pending_request_.reset(new SavePageRequest(request)); 160 pending_request_.reset(new SavePageRequest(request));
160 completion_callback_ = completion_callback; 161 completion_callback_ = completion_callback;
161 progress_callback_ = progress_callback; 162 progress_callback_ = progress_callback;
162 163
163 // Listen for app foreground/background change. 164 // Listen for app foreground/background change.
164 app_listener_.reset(new base::android::ApplicationStatusListener( 165 app_listener_.reset(new base::android::ApplicationStatusListener(
165 base::Bind(&BackgroundLoaderOffliner::OnApplicationStateChange, 166 base::Bind(&BackgroundLoaderOffliner::OnApplicationStateChange,
166 weak_ptr_factory_.GetWeakPtr()))); 167 weak_ptr_factory_.GetWeakPtr())));
167 168
168 // Load page attempt. 169 // Load page attempt.
169 loader_.get()->LoadPage(request.url()); 170 loader_.get()->LoadPage(request.url());
170 171
171 snapshot_controller_.reset(
172 new SnapshotController(base::ThreadTaskRunnerHandle::Get(), this,
173 kOfflineDomContentLoadedMs, page_delay_ms_));
174
175 return true; 172 return true;
176 } 173 }
177 174
178 void BackgroundLoaderOffliner::Cancel(const CancelCallback& callback) { 175 void BackgroundLoaderOffliner::Cancel(const CancelCallback& callback) {
179 // TODO(chili): We are not able to cancel a pending 176 // TODO(chili): We are not able to cancel a pending
180 // OfflinePageModel::SaveSnapshot() operation. We will notify caller that 177 // OfflinePageModel::SavePage() operation. We will notify caller that
181 // cancel completed once the SavePage operation returns. 178 // cancel completed once the SavePage operation returns.
182 if (!pending_request_) { 179 if (!pending_request_) {
183 callback.Run(0LL); 180 callback.Run(0LL);
184 return; 181 return;
185 } 182 }
186 183
187 if (save_state_ != NONE) { 184 if (save_state_ != NONE) {
188 save_state_ = DELETE_AFTER_SAVE; 185 save_state_ = DELETE_AFTER_SAVE;
189 cancel_callback_ = callback; 186 cancel_callback_ = callback;
190 return; 187 return;
191 } 188 }
192 189
193 int64_t request_id = pending_request_->request_id(); 190 int64_t request_id = pending_request_->request_id();
194 ResetState(); 191 ResetState();
195 callback.Run(request_id); 192 callback.Run(request_id);
196 } 193 }
197 194
198 bool BackgroundLoaderOffliner::HandleTimeout(const SavePageRequest& request) { 195 bool BackgroundLoaderOffliner::HandleTimeout(const SavePageRequest& request) {
199 // TODO(romax) Decide if we want to also take a snapshot on the last timeout 196 // TODO(romax) Decide if we want to also take a snapshot on the last timeout
200 // for the background loader offliner. 197 // for the background loader offliner.
201 return false; 198 return false;
202 } 199 }
203 200
204 void BackgroundLoaderOffliner::DocumentLoadedInFrame(
205 content::RenderFrameHost* render_host) {
206 // Inform snapshot controller if in main frame.
207 if (!render_host->GetParent())
208 snapshot_controller_->DocumentAvailableInMainFrame();
209 }
210
211 void BackgroundLoaderOffliner::DidStopLoading() { 201 void BackgroundLoaderOffliner::DidStopLoading() {
212 if (!pending_request_.get()) { 202 if (!pending_request_.get()) {
213 DVLOG(1) << "DidStopLoading called even though no pending request."; 203 DVLOG(1) << "DidStopLoading called even though no pending request.";
214 return; 204 return;
215 } 205 }
216 206
217 snapshot_controller_->DocumentOnLoadCompletedInMainFrame(); 207 // Invalidate ptrs for any ongoing save operation.
208 weak_ptr_factory_.InvalidateWeakPtrs();
209
210 // Post SavePage task with 2 second delay.
211 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
212 FROM_HERE,
213 base::Bind(&BackgroundLoaderOffliner::SavePage,
214 weak_ptr_factory_.GetWeakPtr()),
215 base::TimeDelta::FromMilliseconds(page_delay_ms_));
218 } 216 }
219 217
220 void BackgroundLoaderOffliner::RenderProcessGone( 218 void BackgroundLoaderOffliner::RenderProcessGone(
221 base::TerminationStatus status) { 219 base::TerminationStatus status) {
222 if (pending_request_) { 220 if (pending_request_) {
223 SavePageRequest request(*pending_request_.get()); 221 SavePageRequest request(*pending_request_.get());
224 switch (status) { 222 switch (status) {
225 case base::TERMINATION_STATUS_OOM: 223 case base::TERMINATION_STATUS_OOM:
226 case base::TERMINATION_STATUS_PROCESS_CRASHED: 224 case base::TERMINATION_STATUS_PROCESS_CRASHED:
227 case base::TERMINATION_STATUS_STILL_RUNNING: 225 case base::TERMINATION_STATUS_STILL_RUNNING:
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
281 case net::ERR_UNKNOWN_URL_SCHEME: 279 case net::ERR_UNKNOWN_URL_SCHEME:
282 page_load_state_ = NONRETRIABLE; 280 page_load_state_ = NONRETRIABLE;
283 break; 281 break;
284 case net::ERR_INTERNET_DISCONNECTED: 282 case net::ERR_INTERNET_DISCONNECTED:
285 page_load_state_ = DELAY_RETRY; 283 page_load_state_ = DELAY_RETRY;
286 break; 284 break;
287 default: 285 default:
288 page_load_state_ = RETRIABLE; 286 page_load_state_ = RETRIABLE;
289 } 287 }
290 } 288 }
289
290 // If the document is not the same invalidate any pending save tasks.
291 //
292 // Downloads or 204/205 response codes do not commit (no new navigation)
293 // Same-Document (committed) navigations are:
294 // - reference fragment navigations
295 // - pushState/replaceState
296 // - same document history navigation
297 if (navigation_handle->HasCommitted() &&
298 !navigation_handle->IsSameDocument()) {
299 weak_ptr_factory_.InvalidateWeakPtrs();
300 }
291 } 301 }
292 302
293 void BackgroundLoaderOffliner::SetPageDelayForTest(long delay_ms) { 303 void BackgroundLoaderOffliner::SetPageDelayForTest(long delay_ms) {
294 page_delay_ms_ = delay_ms; 304 page_delay_ms_ = delay_ms;
295 } 305 }
296 306
297 void BackgroundLoaderOffliner::OnNetworkBytesChanged(int64_t bytes) { 307 void BackgroundLoaderOffliner::OnNetworkBytesChanged(int64_t bytes) {
298 if (pending_request_ && save_state_ != SAVING) { 308 if (pending_request_ && save_state_ != SAVING) {
299 network_bytes_ += bytes; 309 network_bytes_ += bytes;
300 progress_callback_.Run(*pending_request_, network_bytes_); 310 progress_callback_.Run(*pending_request_, network_bytes_);
301 } 311 }
302 } 312 }
303 313
304 void BackgroundLoaderOffliner::StartSnapshot() { 314 void BackgroundLoaderOffliner::SavePage() {
305 if (!pending_request_.get()) { 315 if (!pending_request_.get()) {
306 DVLOG(1) << "Pending request was cleared during delay."; 316 DVLOG(1) << "Pending request was cleared during delay.";
307 return; 317 return;
308 } 318 }
309 319
310 SavePageRequest request(*pending_request_.get()); 320 SavePageRequest request(*pending_request_.get());
311 // If there was an error navigating to page, return loading failed. 321 // If there was an error navigating to page, return loading failed.
312 if (page_load_state_ != SUCCESS) { 322 if (page_load_state_ != SUCCESS) {
313 Offliner::RequestStatus status; 323 Offliner::RequestStatus status;
314 switch (page_load_state_) { 324 switch (page_load_state_) {
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
365 SavePageRequest request(*pending_request_.get()); 375 SavePageRequest request(*pending_request_.get());
366 ResetState(); 376 ResetState();
367 377
368 if (save_state_ == DELETE_AFTER_SAVE) { 378 if (save_state_ == DELETE_AFTER_SAVE) {
369 save_state_ = NONE; 379 save_state_ = NONE;
370 cancel_callback_.Run(request.request_id()); 380 cancel_callback_.Run(request.request_id());
371 return; 381 return;
372 } 382 }
373 383
374 save_state_ = NONE; 384 save_state_ = NONE;
375 // Prevent snapshot controller from making any more snapshots.
376 snapshot_controller_->Stop();
377 385
378 Offliner::RequestStatus save_status; 386 Offliner::RequestStatus save_status;
379 if (save_result == SavePageResult::SUCCESS) 387 if (save_result == SavePageResult::SUCCESS)
380 save_status = RequestStatus::SAVED; 388 save_status = RequestStatus::SAVED;
381 else 389 else
382 save_status = RequestStatus::SAVE_FAILED; 390 save_status = RequestStatus::SAVE_FAILED;
383 391
384 completion_callback_.Run(request, save_status); 392 completion_callback_.Run(request, save_status);
385 } 393 }
386 394
387 void BackgroundLoaderOffliner::ResetState() { 395 void BackgroundLoaderOffliner::ResetState() {
388 pending_request_.reset(); 396 pending_request_.reset();
389 snapshot_controller_.reset();
390 page_load_state_ = SUCCESS; 397 page_load_state_ = SUCCESS;
391 network_bytes_ = 0LL; 398 network_bytes_ = 0LL;
392 // TODO(chili): Remove after RequestCoordinator can handle multiple offliners. 399 // TODO(chili): Remove after RequestCoordinator can handle multiple offliners.
393 // We reset the loader and observer after completion so loaders 400 // We reset the loader and observer after completion so loaders
394 // will not be re-used across different requests/tries. This is a temporary 401 // will not be re-used across different requests/tries. This is a temporary
395 // solution while there exists assumptions about the number of offliners 402 // solution while there exists assumptions about the number of offliners
396 // there are. 403 // there are.
397 loader_.reset( 404 loader_.reset(
398 new background_loader::BackgroundLoaderContents(browser_context_)); 405 new background_loader::BackgroundLoaderContents(browser_context_));
399 content::WebContents* contents = loader_->web_contents(); 406 content::WebContents* contents = loader_->web_contents();
(...skipping 21 matching lines...) Expand all
421 int64_t offline_id) { 428 int64_t offline_id) {
422 // If for some reason the request was reset during while waiting for callback 429 // If for some reason the request was reset during while waiting for callback
423 // ignore the completion callback. 430 // ignore the completion callback.
424 if (pending_request_ && pending_request_->request_id() != offline_id) 431 if (pending_request_ && pending_request_->request_id() != offline_id)
425 return; 432 return;
426 completion_callback_.Run(request, RequestStatus::FOREGROUND_CANCELED); 433 completion_callback_.Run(request, RequestStatus::FOREGROUND_CANCELED);
427 } 434 }
428 } // namespace offline_pages 435 } // namespace offline_pages
429 436
430 DEFINE_WEB_CONTENTS_USER_DATA_KEY(offline_pages::OfflinerData); 437 DEFINE_WEB_CONTENTS_USER_DATA_KEY(offline_pages::OfflinerData);
OLDNEW
« no previous file with comments | « chrome/browser/android/offline_pages/background_loader_offliner.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698