| OLD | NEW |
| 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/offliner_policy.h" |
| 12 #include "components/offline_pages/core/background/save_page_request.h" | 13 #include "components/offline_pages/core/background/save_page_request.h" |
| 13 #include "components/offline_pages/core/client_namespace_constants.h" | 14 #include "components/offline_pages/core/client_namespace_constants.h" |
| 14 #include "components/offline_pages/core/offline_page_model.h" | 15 #include "components/offline_pages/core/offline_page_model.h" |
| 15 #include "content/public/browser/browser_context.h" | 16 #include "content/public/browser/browser_context.h" |
| 16 #include "content/public/browser/navigation_handle.h" | 17 #include "content/public/browser/navigation_handle.h" |
| 17 #include "content/public/browser/render_frame_host.h" | 18 #include "content/public/browser/render_frame_host.h" |
| 18 #include "content/public/browser/web_contents.h" | 19 #include "content/public/browser/web_contents.h" |
| 19 #include "content/public/browser/web_contents_user_data.h" | 20 #include "content/public/browser/web_contents_user_data.h" |
| 20 | 21 |
| 21 namespace offline_pages { | 22 namespace offline_pages { |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 std::abs(error_code)); | 65 std::abs(error_code)); |
| 65 } | 66 } |
| 66 } // namespace | 67 } // namespace |
| 67 | 68 |
| 68 BackgroundLoaderOffliner::BackgroundLoaderOffliner( | 69 BackgroundLoaderOffliner::BackgroundLoaderOffliner( |
| 69 content::BrowserContext* browser_context, | 70 content::BrowserContext* browser_context, |
| 70 const OfflinerPolicy* policy, | 71 const OfflinerPolicy* policy, |
| 71 OfflinePageModel* offline_page_model) | 72 OfflinePageModel* offline_page_model) |
| 72 : browser_context_(browser_context), | 73 : browser_context_(browser_context), |
| 73 offline_page_model_(offline_page_model), | 74 offline_page_model_(offline_page_model), |
| 75 policy_(policy), |
| 74 is_low_end_device_(base::SysInfo::IsLowEndDevice()), | 76 is_low_end_device_(base::SysInfo::IsLowEndDevice()), |
| 75 save_state_(NONE), | 77 save_state_(NONE), |
| 76 page_load_state_(SUCCESS), | 78 page_load_state_(SUCCESS), |
| 77 page_delay_ms_(kOfflinePageDelayMs), | 79 page_delay_ms_(kOfflinePageDelayMs), |
| 78 network_bytes_(0LL), | 80 network_bytes_(0LL), |
| 79 weak_ptr_factory_(this) { | 81 weak_ptr_factory_(this) { |
| 80 DCHECK(offline_page_model_); | 82 DCHECK(offline_page_model_); |
| 81 DCHECK(browser_context_); | 83 DCHECK(browser_context_); |
| 82 } | 84 } |
| 83 | 85 |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 cancel_callback_ = callback; | 191 cancel_callback_ = callback; |
| 190 return; | 192 return; |
| 191 } | 193 } |
| 192 | 194 |
| 193 int64_t request_id = pending_request_->request_id(); | 195 int64_t request_id = pending_request_->request_id(); |
| 194 ResetState(); | 196 ResetState(); |
| 195 callback.Run(request_id); | 197 callback.Run(request_id); |
| 196 } | 198 } |
| 197 | 199 |
| 198 bool BackgroundLoaderOffliner::HandleTimeout(const SavePageRequest& request) { | 200 bool BackgroundLoaderOffliner::HandleTimeout(const SavePageRequest& request) { |
| 199 // TODO(romax): Decide if we want to also take a snapshot on the last timeout | 201 if (pending_request_) { |
| 200 // for the background loader offliner. crbug.com/705090 | 202 DCHECK(request.request_id() == pending_request_->request_id()); |
| 203 if (is_low_bar_met_ && |
| 204 (request.started_attempt_count() + 1 >= policy_->GetMaxStartedTries() || |
| 205 request.completed_attempt_count() + 1 >= |
| 206 policy_->GetMaxCompletedTries())) { |
| 207 // If we are already in the middle of a save operation, let it finish |
| 208 // but do not return SAVED_ON_LAST_RETRY |
| 209 if (save_state_ == NONE) { |
| 210 did_snapshot_on_last_retry_ = true; |
| 211 StartSnapshot(); |
| 212 } |
| 213 return true; |
| 214 } |
| 215 } |
| 201 return false; | 216 return false; |
| 202 } | 217 } |
| 203 | 218 |
| 204 void BackgroundLoaderOffliner::DocumentAvailableInMainFrame() { | 219 void BackgroundLoaderOffliner::DocumentAvailableInMainFrame() { |
| 205 snapshot_controller_->DocumentAvailableInMainFrame(); | 220 snapshot_controller_->DocumentAvailableInMainFrame(); |
| 221 is_low_bar_met_ = true; |
| 206 } | 222 } |
| 207 | 223 |
| 208 void BackgroundLoaderOffliner::DocumentOnLoadCompletedInMainFrame() { | 224 void BackgroundLoaderOffliner::DocumentOnLoadCompletedInMainFrame() { |
| 209 if (!pending_request_.get()) { | 225 if (!pending_request_.get()) { |
| 210 DVLOG(1) << "DidStopLoading called even though no pending request."; | 226 DVLOG(1) << "DidStopLoading called even though no pending request."; |
| 211 return; | 227 return; |
| 212 } | 228 } |
| 213 | 229 |
| 214 snapshot_controller_->DocumentOnLoadCompletedInMainFrame(); | 230 snapshot_controller_->DocumentOnLoadCompletedInMainFrame(); |
| 215 } | 231 } |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 327 base::Bind(&BackgroundLoaderOffliner::OnPageSaved, | 343 base::Bind(&BackgroundLoaderOffliner::OnPageSaved, |
| 328 weak_ptr_factory_.GetWeakPtr())); | 344 weak_ptr_factory_.GetWeakPtr())); |
| 329 } | 345 } |
| 330 | 346 |
| 331 void BackgroundLoaderOffliner::OnPageSaved(SavePageResult save_result, | 347 void BackgroundLoaderOffliner::OnPageSaved(SavePageResult save_result, |
| 332 int64_t offline_id) { | 348 int64_t offline_id) { |
| 333 if (!pending_request_) | 349 if (!pending_request_) |
| 334 return; | 350 return; |
| 335 | 351 |
| 336 SavePageRequest request(*pending_request_.get()); | 352 SavePageRequest request(*pending_request_.get()); |
| 353 bool did_snapshot_on_last_retry = did_snapshot_on_last_retry_; |
| 337 ResetState(); | 354 ResetState(); |
| 338 | 355 |
| 339 if (save_state_ == DELETE_AFTER_SAVE) { | 356 if (save_state_ == DELETE_AFTER_SAVE) { |
| 340 save_state_ = NONE; | 357 save_state_ = NONE; |
| 341 cancel_callback_.Run(request.request_id()); | 358 cancel_callback_.Run(request.request_id()); |
| 342 return; | 359 return; |
| 343 } | 360 } |
| 344 | 361 |
| 345 save_state_ = NONE; | 362 save_state_ = NONE; |
| 346 | 363 |
| 347 Offliner::RequestStatus save_status; | 364 Offliner::RequestStatus save_status; |
| 348 if (save_result == SavePageResult::SUCCESS) | 365 if (save_result == SavePageResult::SUCCESS) { |
| 349 save_status = RequestStatus::SAVED; | 366 if (did_snapshot_on_last_retry) |
| 350 else | 367 save_status = RequestStatus::SAVED_ON_LAST_RETRY; |
| 368 else |
| 369 save_status = RequestStatus::SAVED; |
| 370 } else { |
| 351 save_status = RequestStatus::SAVE_FAILED; | 371 save_status = RequestStatus::SAVE_FAILED; |
| 372 } |
| 352 | 373 |
| 353 completion_callback_.Run(request, save_status); | 374 completion_callback_.Run(request, save_status); |
| 354 } | 375 } |
| 355 | 376 |
| 356 void BackgroundLoaderOffliner::ResetState() { | 377 void BackgroundLoaderOffliner::ResetState() { |
| 357 pending_request_.reset(); | 378 pending_request_.reset(); |
| 358 snapshot_controller_.reset(); | 379 snapshot_controller_.reset(); |
| 359 page_load_state_ = SUCCESS; | 380 page_load_state_ = SUCCESS; |
| 360 network_bytes_ = 0LL; | 381 network_bytes_ = 0LL; |
| 382 is_low_bar_met_ = false; |
| 383 did_snapshot_on_last_retry_ = false; |
| 361 // TODO(chili): Remove after RequestCoordinator can handle multiple offliners. | 384 // TODO(chili): Remove after RequestCoordinator can handle multiple offliners. |
| 362 // We reset the loader and observer after completion so loaders | 385 // We reset the loader and observer after completion so loaders |
| 363 // will not be re-used across different requests/tries. This is a temporary | 386 // will not be re-used across different requests/tries. This is a temporary |
| 364 // solution while there exists assumptions about the number of offliners | 387 // solution while there exists assumptions about the number of offliners |
| 365 // there are. | 388 // there are. |
| 366 loader_.reset( | 389 loader_.reset( |
| 367 new background_loader::BackgroundLoaderContents(browser_context_)); | 390 new background_loader::BackgroundLoaderContents(browser_context_)); |
| 368 content::WebContents* contents = loader_->web_contents(); | 391 content::WebContents* contents = loader_->web_contents(); |
| 369 content::WebContentsObserver::Observe(contents); | 392 content::WebContentsObserver::Observe(contents); |
| 370 OfflinerData::AddToWebContents(contents, this); | 393 OfflinerData::AddToWebContents(contents, this); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 390 int64_t offline_id) { | 413 int64_t offline_id) { |
| 391 // If for some reason the request was reset during while waiting for callback | 414 // If for some reason the request was reset during while waiting for callback |
| 392 // ignore the completion callback. | 415 // ignore the completion callback. |
| 393 if (pending_request_ && pending_request_->request_id() != offline_id) | 416 if (pending_request_ && pending_request_->request_id() != offline_id) |
| 394 return; | 417 return; |
| 395 completion_callback_.Run(request, RequestStatus::FOREGROUND_CANCELED); | 418 completion_callback_.Run(request, RequestStatus::FOREGROUND_CANCELED); |
| 396 } | 419 } |
| 397 } // namespace offline_pages | 420 } // namespace offline_pages |
| 398 | 421 |
| 399 DEFINE_WEB_CONTENTS_USER_DATA_KEY(offline_pages::OfflinerData); | 422 DEFINE_WEB_CONTENTS_USER_DATA_KEY(offline_pages::OfflinerData); |
| OLD | NEW |