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/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 9 #include "base/metrics/histogram_macros.h" | 9 #include "base/metrics/histogram_macros.h" |
| 10 #include "base/sys_info.h" | 10 #include "base/sys_info.h" |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 74 "OfflinePages.Background.BackgroundLoadingFailedCode"), | 74 "OfflinePages.Background.BackgroundLoadingFailedCode"), |
| 75 std::abs(error_code)); | 75 std::abs(error_code)); |
| 76 } | 76 } |
| 77 | 77 |
| 78 void HandleApplicationStateChangeCancel( | 78 void HandleApplicationStateChangeCancel( |
| 79 const Offliner::CompletionCallback& completion_callback, | 79 const Offliner::CompletionCallback& completion_callback, |
| 80 const SavePageRequest& canceled_request) { | 80 const SavePageRequest& canceled_request) { |
| 81 completion_callback.Run(canceled_request, | 81 completion_callback.Run(canceled_request, |
| 82 Offliner::RequestStatus::FOREGROUND_CANCELED); | 82 Offliner::RequestStatus::FOREGROUND_CANCELED); |
| 83 } | 83 } |
| 84 | |
| 84 } // namespace | 85 } // namespace |
| 85 | 86 |
| 86 BackgroundLoaderOffliner::BackgroundLoaderOffliner( | 87 BackgroundLoaderOffliner::BackgroundLoaderOffliner( |
| 87 content::BrowserContext* browser_context, | 88 content::BrowserContext* browser_context, |
| 88 const OfflinerPolicy* policy, | 89 const OfflinerPolicy* policy, |
| 89 OfflinePageModel* offline_page_model) | 90 OfflinePageModel* offline_page_model) |
| 90 : browser_context_(browser_context), | 91 : browser_context_(browser_context), |
| 91 offline_page_model_(offline_page_model), | 92 offline_page_model_(offline_page_model), |
| 92 policy_(policy), | 93 policy_(policy), |
| 93 is_low_end_device_(base::SysInfo::IsLowEndDevice()), | 94 is_low_end_device_(base::SysInfo::IsLowEndDevice()), |
| (...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 407 void BackgroundLoaderOffliner::OnPageSaved(SavePageResult save_result, | 408 void BackgroundLoaderOffliner::OnPageSaved(SavePageResult save_result, |
| 408 int64_t offline_id) { | 409 int64_t offline_id) { |
| 409 if (!pending_request_) | 410 if (!pending_request_) |
| 410 return; | 411 return; |
| 411 | 412 |
| 412 SavePageRequest request(*pending_request_.get()); | 413 SavePageRequest request(*pending_request_.get()); |
| 413 bool did_snapshot_on_last_retry = did_snapshot_on_last_retry_; | 414 bool did_snapshot_on_last_retry = did_snapshot_on_last_retry_; |
| 414 ResetState(); | 415 ResetState(); |
| 415 | 416 |
| 416 if (save_state_ == DELETE_AFTER_SAVE) { | 417 if (save_state_ == DELETE_AFTER_SAVE) { |
| 418 // Delete the saved page off disk and from the OPM. | |
| 419 std::vector<int64_t> offline_ids; | |
| 420 offline_ids.push_back(offline_id); | |
| 421 offline_page_model_->DeletePagesByOfflineId( | |
| 422 offline_ids, | |
| 423 base::Bind(&BackgroundLoaderOffliner::DeleteOfflinePageCallback, | |
| 424 weak_ptr_factory_.GetWeakPtr(), request)); | |
| 417 save_state_ = NONE; | 425 save_state_ = NONE; |
| 418 cancel_callback_.Run(request); | 426 // We don't return to the caller until the page has been removed from the |
|
Dmitry Titov
2017/05/01 23:00:38
I think this comment is not needed, we do this kin
| |
| 427 // OfflinePageModel to prevent retrying a page while we are deleting it. | |
| 419 return; | 428 return; |
| 420 } | 429 } |
| 421 | 430 |
| 422 save_state_ = NONE; | 431 save_state_ = NONE; |
| 423 | 432 |
| 424 Offliner::RequestStatus save_status; | 433 Offliner::RequestStatus save_status; |
| 425 if (save_result == SavePageResult::ALREADY_EXISTS) { | 434 if (save_result == SavePageResult::ALREADY_EXISTS) { |
| 426 save_status = RequestStatus::SAVED; | 435 save_status = RequestStatus::SAVED; |
| 427 } else if (save_result == SavePageResult::SUCCESS) { | 436 } else if (save_result == SavePageResult::SUCCESS) { |
| 428 if (did_snapshot_on_last_retry) | 437 if (did_snapshot_on_last_retry) |
| 429 save_status = RequestStatus::SAVED_ON_LAST_RETRY; | 438 save_status = RequestStatus::SAVED_ON_LAST_RETRY; |
| 430 else | 439 else |
| 431 save_status = RequestStatus::SAVED; | 440 save_status = RequestStatus::SAVED; |
| 432 } else { | 441 } else { |
| 433 save_status = RequestStatus::SAVE_FAILED; | 442 save_status = RequestStatus::SAVE_FAILED; |
| 434 } | 443 } |
| 435 | 444 |
| 436 completion_callback_.Run(request, save_status); | 445 completion_callback_.Run(request, save_status); |
| 437 } | 446 } |
| 438 | 447 |
| 448 void BackgroundLoaderOffliner::DeleteOfflinePageCallback( | |
| 449 const SavePageRequest& request, | |
| 450 DeletePageResult result) { | |
| 451 cancel_callback_.Run(request); | |
| 452 } | |
| 453 | |
| 439 void BackgroundLoaderOffliner::ResetState() { | 454 void BackgroundLoaderOffliner::ResetState() { |
| 440 pending_request_.reset(); | 455 pending_request_.reset(); |
| 441 snapshot_controller_.reset(); | 456 snapshot_controller_.reset(); |
| 442 page_load_state_ = SUCCESS; | 457 page_load_state_ = SUCCESS; |
| 443 network_bytes_ = 0LL; | 458 network_bytes_ = 0LL; |
| 444 is_low_bar_met_ = false; | 459 is_low_bar_met_ = false; |
| 445 did_snapshot_on_last_retry_ = false; | 460 did_snapshot_on_last_retry_ = false; |
| 446 // TODO(chili): Remove after RequestCoordinator can handle multiple offliners. | 461 // TODO(chili): Remove after RequestCoordinator can handle multiple offliners. |
| 447 // We reset the loader and observer after completion so loaders | 462 // We reset the loader and observer after completion so loaders |
| 448 // will not be re-used across different requests/tries. This is a temporary | 463 // will not be re-used across different requests/tries. This is a temporary |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 476 // Given the choice between int and double, we choose to implicitly convert to | 491 // Given the choice between int and double, we choose to implicitly convert to |
| 477 // a double since it maintains more precision (we can get a longer time in | 492 // a double since it maintains more precision (we can get a longer time in |
| 478 // milliseconds than we can with a 2 bit int, 53 bits vs 32). | 493 // milliseconds than we can with a 2 bit int, 53 bits vs 32). |
| 479 double delay = delay_so_far.InMilliseconds(); | 494 double delay = delay_so_far.InMilliseconds(); |
| 480 signal_data_.SetDouble(signal_name, delay); | 495 signal_data_.SetDouble(signal_name, delay); |
| 481 } | 496 } |
| 482 | 497 |
| 483 } // namespace offline_pages | 498 } // namespace offline_pages |
| 484 | 499 |
| 485 DEFINE_WEB_CONTENTS_USER_DATA_KEY(offline_pages::OfflinerData); | 500 DEFINE_WEB_CONTENTS_USER_DATA_KEY(offline_pages::OfflinerData); |
| OLD | NEW |