Chromium Code Reviews| 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() || | |
|
fgorski
2017/04/14 05:15:57
I am wondering whether we can make all these check
chili
2017/04/15 00:34:58
I agree with this, but can we make the change in a
fgorski
2017/04/17 17:48:59
Acknowledged.
| |
| 205 request.completed_attempt_count() + 1 >= | |
| 206 policy_->GetMaxCompletedTries())) { | |
| 207 did_snapshot_on_last_retry_ = true; | |
| 208 StartSnapshot(); | |
| 209 return true; | |
| 210 } | |
| 211 } | |
| 201 return false; | 212 return false; |
| 202 } | 213 } |
| 203 | 214 |
| 204 void BackgroundLoaderOffliner::DocumentAvailableInMainFrame() { | 215 void BackgroundLoaderOffliner::DocumentAvailableInMainFrame() { |
| 205 snapshot_controller_->DocumentAvailableInMainFrame(); | 216 snapshot_controller_->DocumentAvailableInMainFrame(); |
| 217 is_low_bar_met_ = true; | |
| 206 } | 218 } |
| 207 | 219 |
| 208 void BackgroundLoaderOffliner::DocumentOnLoadCompletedInMainFrame() { | 220 void BackgroundLoaderOffliner::DocumentOnLoadCompletedInMainFrame() { |
| 209 if (!pending_request_.get()) { | 221 if (!pending_request_.get()) { |
| 210 DVLOG(1) << "DidStopLoading called even though no pending request."; | 222 DVLOG(1) << "DidStopLoading called even though no pending request."; |
| 211 return; | 223 return; |
| 212 } | 224 } |
| 213 | 225 |
| 214 snapshot_controller_->DocumentOnLoadCompletedInMainFrame(); | 226 snapshot_controller_->DocumentOnLoadCompletedInMainFrame(); |
| 215 } | 227 } |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 327 base::Bind(&BackgroundLoaderOffliner::OnPageSaved, | 339 base::Bind(&BackgroundLoaderOffliner::OnPageSaved, |
| 328 weak_ptr_factory_.GetWeakPtr())); | 340 weak_ptr_factory_.GetWeakPtr())); |
| 329 } | 341 } |
| 330 | 342 |
| 331 void BackgroundLoaderOffliner::OnPageSaved(SavePageResult save_result, | 343 void BackgroundLoaderOffliner::OnPageSaved(SavePageResult save_result, |
| 332 int64_t offline_id) { | 344 int64_t offline_id) { |
| 333 if (!pending_request_) | 345 if (!pending_request_) |
| 334 return; | 346 return; |
| 335 | 347 |
| 336 SavePageRequest request(*pending_request_.get()); | 348 SavePageRequest request(*pending_request_.get()); |
| 349 bool did_snapshot_on_last_retry = did_snapshot_on_last_retry_; | |
| 337 ResetState(); | 350 ResetState(); |
| 338 | 351 |
| 339 if (save_state_ == DELETE_AFTER_SAVE) { | 352 if (save_state_ == DELETE_AFTER_SAVE) { |
| 340 save_state_ = NONE; | 353 save_state_ = NONE; |
| 341 cancel_callback_.Run(request.request_id()); | 354 cancel_callback_.Run(request.request_id()); |
| 342 return; | 355 return; |
| 343 } | 356 } |
| 344 | 357 |
| 345 save_state_ = NONE; | 358 save_state_ = NONE; |
| 346 | 359 |
| 347 Offliner::RequestStatus save_status; | 360 Offliner::RequestStatus save_status; |
| 348 if (save_result == SavePageResult::SUCCESS) | 361 if (save_result == SavePageResult::SUCCESS) { |
| 349 save_status = RequestStatus::SAVED; | 362 if (did_snapshot_on_last_retry) |
| 350 else | 363 save_status = RequestStatus::SAVED_ON_LAST_RETRY; |
| 364 else | |
| 365 save_status = RequestStatus::SAVED; | |
| 366 } else { | |
| 351 save_status = RequestStatus::SAVE_FAILED; | 367 save_status = RequestStatus::SAVE_FAILED; |
| 368 } | |
| 352 | 369 |
| 353 completion_callback_.Run(request, save_status); | 370 completion_callback_.Run(request, save_status); |
| 354 } | 371 } |
| 355 | 372 |
| 356 void BackgroundLoaderOffliner::ResetState() { | 373 void BackgroundLoaderOffliner::ResetState() { |
| 357 pending_request_.reset(); | 374 pending_request_.reset(); |
| 358 snapshot_controller_.reset(); | 375 snapshot_controller_.reset(); |
| 359 page_load_state_ = SUCCESS; | 376 page_load_state_ = SUCCESS; |
| 360 network_bytes_ = 0LL; | 377 network_bytes_ = 0LL; |
| 378 is_low_bar_met_ = false; | |
| 379 did_snapshot_on_last_retry_ = false; | |
| 361 // TODO(chili): Remove after RequestCoordinator can handle multiple offliners. | 380 // TODO(chili): Remove after RequestCoordinator can handle multiple offliners. |
| 362 // We reset the loader and observer after completion so loaders | 381 // We reset the loader and observer after completion so loaders |
| 363 // will not be re-used across different requests/tries. This is a temporary | 382 // will not be re-used across different requests/tries. This is a temporary |
| 364 // solution while there exists assumptions about the number of offliners | 383 // solution while there exists assumptions about the number of offliners |
| 365 // there are. | 384 // there are. |
| 366 loader_.reset( | 385 loader_.reset( |
| 367 new background_loader::BackgroundLoaderContents(browser_context_)); | 386 new background_loader::BackgroundLoaderContents(browser_context_)); |
| 368 content::WebContents* contents = loader_->web_contents(); | 387 content::WebContents* contents = loader_->web_contents(); |
| 369 content::WebContentsObserver::Observe(contents); | 388 content::WebContentsObserver::Observe(contents); |
| 370 OfflinerData::AddToWebContents(contents, this); | 389 OfflinerData::AddToWebContents(contents, this); |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 390 int64_t offline_id) { | 409 int64_t offline_id) { |
| 391 // If for some reason the request was reset during while waiting for callback | 410 // If for some reason the request was reset during while waiting for callback |
| 392 // ignore the completion callback. | 411 // ignore the completion callback. |
| 393 if (pending_request_ && pending_request_->request_id() != offline_id) | 412 if (pending_request_ && pending_request_->request_id() != offline_id) |
| 394 return; | 413 return; |
| 395 completion_callback_.Run(request, RequestStatus::FOREGROUND_CANCELED); | 414 completion_callback_.Run(request, RequestStatus::FOREGROUND_CANCELED); |
| 396 } | 415 } |
| 397 } // namespace offline_pages | 416 } // namespace offline_pages |
| 398 | 417 |
| 399 DEFINE_WEB_CONTENTS_USER_DATA_KEY(offline_pages::OfflinerData); | 418 DEFINE_WEB_CONTENTS_USER_DATA_KEY(offline_pages::OfflinerData); |
| OLD | NEW |