| 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 "components/offline_pages/background/request_coordinator.h" | 5 #include "components/offline_pages/background/request_coordinator.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 529 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 540 GetOffliner(); | 540 GetOffliner(); |
| 541 if (!offliner_) { | 541 if (!offliner_) { |
| 542 DVLOG(0) << "Unable to create Offliner. " | 542 DVLOG(0) << "Unable to create Offliner. " |
| 543 << "Cannot background offline page."; | 543 << "Cannot background offline page."; |
| 544 return; | 544 return; |
| 545 } | 545 } |
| 546 | 546 |
| 547 DCHECK(!is_busy_); | 547 DCHECK(!is_busy_); |
| 548 is_busy_ = true; | 548 is_busy_ = true; |
| 549 | 549 |
| 550 // Update the request for this attempt to start it. | 550 // Mark attempt started in the database and start offliner when completed. |
| 551 SavePageRequest updated_request(request); | 551 queue_->MarkAttemptStarted( |
| 552 updated_request.MarkAttemptStarted(base::Time::Now()); | 552 request.request_id(), |
| 553 queue_->UpdateRequest( | 553 base::Bind(&RequestCoordinator::StartOffliner, |
| 554 updated_request, | 554 weak_ptr_factory_.GetWeakPtr(), request.request_id(), |
| 555 base::Bind(&RequestCoordinator::UpdateRequestCallback, | 555 request.client_id().name_space)); |
| 556 weak_ptr_factory_.GetWeakPtr(), updated_request.client_id())); | 556 } |
| 557 active_request_.reset(new SavePageRequest(updated_request)); | 557 |
| 558 void RequestCoordinator::StartOffliner( |
| 559 int64_t request_id, |
| 560 const std::string& client_namespace, |
| 561 std::unique_ptr<UpdateRequestsResult> update_result) { |
| 562 if (update_result->store_state != StoreState::LOADED || |
| 563 update_result->item_statuses.size() != 1 || |
| 564 update_result->item_statuses.at(0).first != request_id || |
| 565 update_result->item_statuses.at(0).second != ItemActionStatus::SUCCESS) { |
| 566 is_busy_ = false; |
| 567 // TODO(fgorski): what is the best result? Do we create a new status? |
| 568 StopProcessing(Offliner::PRERENDERING_NOT_STARTED); |
| 569 DVLOG(1) << "Failed to mark attempt started: " << request_id; |
| 570 event_logger_.RecordUpdateRequestFailed( |
| 571 client_namespace, |
| 572 update_result->store_state != StoreState::LOADED |
| 573 ? RequestQueue::UpdateRequestResult::STORE_FAILURE |
| 574 : RequestQueue::UpdateRequestResult::REQUEST_DOES_NOT_EXIST); |
| 575 return; |
| 576 } |
| 577 |
| 578 // TODO(fgorski): Switch to request_id only, so that this value is not written |
| 579 // back to the store. |
| 580 active_request_.reset( |
| 581 new SavePageRequest(update_result->updated_items.at(0))); |
| 558 | 582 |
| 559 // Start the load and save process in the offliner (Async). | 583 // Start the load and save process in the offliner (Async). |
| 560 if (offliner_->LoadAndSave( | 584 if (offliner_->LoadAndSave( |
| 561 updated_request, base::Bind(&RequestCoordinator::OfflinerDoneCallback, | 585 update_result->updated_items.at(0), |
| 562 weak_ptr_factory_.GetWeakPtr()))) { | 586 base::Bind(&RequestCoordinator::OfflinerDoneCallback, |
| 587 weak_ptr_factory_.GetWeakPtr()))) { |
| 563 base::TimeDelta timeout; | 588 base::TimeDelta timeout; |
| 564 if (processing_state_ == ProcessingWindowState::SCHEDULED_WINDOW) { | 589 if (processing_state_ == ProcessingWindowState::SCHEDULED_WINDOW) { |
| 565 timeout = base::TimeDelta::FromSeconds( | 590 timeout = base::TimeDelta::FromSeconds( |
| 566 policy_->GetSinglePageTimeLimitWhenBackgroundScheduledInSeconds()); | 591 policy_->GetSinglePageTimeLimitWhenBackgroundScheduledInSeconds()); |
| 567 } else { | 592 } else { |
| 568 DCHECK(processing_state_ == ProcessingWindowState::IMMEDIATE_WINDOW); | 593 DCHECK(processing_state_ == ProcessingWindowState::IMMEDIATE_WINDOW); |
| 569 timeout = base::TimeDelta::FromSeconds( | 594 timeout = base::TimeDelta::FromSeconds( |
| 570 policy_->GetSinglePageTimeLimitForImmediateLoadInSeconds()); | 595 policy_->GetSinglePageTimeLimitForImmediateLoadInSeconds()); |
| 571 } | 596 } |
| 597 |
| 598 // Start a watchdog timer to catch pre-renders running too long |
| 572 watchdog_timer_.Start(FROM_HERE, timeout, this, | 599 watchdog_timer_.Start(FROM_HERE, timeout, this, |
| 573 &RequestCoordinator::HandleWatchdogTimeout); | 600 &RequestCoordinator::HandleWatchdogTimeout); |
| 574 } else { | 601 } else { |
| 575 is_busy_ = false; | 602 is_busy_ = false; |
| 576 DVLOG(0) << "Unable to start LoadAndSave"; | 603 DVLOG(0) << "Unable to start LoadAndSave"; |
| 577 StopProcessing(Offliner::PRERENDERING_NOT_STARTED); | 604 StopProcessing(Offliner::PRERENDERING_NOT_STARTED); |
| 578 } | 605 } |
| 579 } | 606 } |
| 580 | 607 |
| 581 void RequestCoordinator::OfflinerDoneCallback(const SavePageRequest& request, | 608 void RequestCoordinator::OfflinerDoneCallback(const SavePageRequest& request, |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 723 | 750 |
| 724 ClientPolicyController* RequestCoordinator::GetPolicyController() { | 751 ClientPolicyController* RequestCoordinator::GetPolicyController() { |
| 725 return policy_controller_.get(); | 752 return policy_controller_.get(); |
| 726 } | 753 } |
| 727 | 754 |
| 728 void RequestCoordinator::Shutdown() { | 755 void RequestCoordinator::Shutdown() { |
| 729 network_quality_estimator_ = nullptr; | 756 network_quality_estimator_ = nullptr; |
| 730 } | 757 } |
| 731 | 758 |
| 732 } // namespace offline_pages | 759 } // namespace offline_pages |
| OLD | NEW |