| 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 <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 541 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 552 // Make sure we are not on svelte device to start immediately. | 552 // Make sure we are not on svelte device to start immediately. |
| 553 if (is_low_end_device_ && | 553 if (is_low_end_device_ && |
| 554 !offline_pages::IsOfflinePagesSvelteConcurrentLoadingEnabled()) { | 554 !offline_pages::IsOfflinePagesSvelteConcurrentLoadingEnabled()) { |
| 555 DVLOG(2) << "low end device, returning"; | 555 DVLOG(2) << "low end device, returning"; |
| 556 // Let the scheduler know we are done processing and failed due to svelte. | 556 // Let the scheduler know we are done processing and failed due to svelte. |
| 557 immediate_schedule_callback_.Run(false); | 557 immediate_schedule_callback_.Run(false); |
| 558 return OfflinerImmediateStartStatus::NOT_STARTED_ON_SVELTE; | 558 return OfflinerImmediateStartStatus::NOT_STARTED_ON_SVELTE; |
| 559 } | 559 } |
| 560 | 560 |
| 561 if (GetConnectionType() == | 561 if (GetConnectionType() == |
| 562 net::NetworkChangeNotifier::ConnectionType::CONNECTION_NONE) | 562 net::NetworkChangeNotifier::ConnectionType::CONNECTION_NONE) { |
| 563 RequestConnectedEventForStarting(); |
| 563 return OfflinerImmediateStartStatus::NO_CONNECTION; | 564 return OfflinerImmediateStartStatus::NO_CONNECTION; |
| 565 } else { |
| 566 // Clear any pending connected event request since we have connection |
| 567 // and will start processing. |
| 568 ClearConnectedEventRequest(); |
| 569 } |
| 564 | 570 |
| 565 // Start processing with manufactured conservative battery conditions | 571 // Start processing with manufactured conservative battery conditions |
| 566 // (i.e., assume no battery). | 572 // (i.e., assume no battery). |
| 567 // TODO(dougarnett): Obtain actual battery conditions (from Android/Java). | 573 // TODO(dougarnett): Obtain actual battery conditions (from Android/Java). |
| 568 | 574 |
| 569 DeviceConditions device_conditions(false, 0, GetConnectionType()); | 575 DeviceConditions device_conditions(false, 0, GetConnectionType()); |
| 570 if (StartProcessingInternal(ProcessingWindowState::IMMEDIATE_WINDOW, | 576 if (StartProcessingInternal(ProcessingWindowState::IMMEDIATE_WINDOW, |
| 571 device_conditions, immediate_schedule_callback_)) | 577 device_conditions, immediate_schedule_callback_)) |
| 572 return OfflinerImmediateStartStatus::STARTED; | 578 return OfflinerImmediateStartStatus::STARTED; |
| 573 else | 579 else |
| 574 return OfflinerImmediateStartStatus::NOT_ACCEPTED; | 580 return OfflinerImmediateStartStatus::NOT_ACCEPTED; |
| 575 } | 581 } |
| 576 | 582 |
| 583 void RequestCoordinator::RequestConnectedEventForStarting() { |
| 584 connection_notifier_.reset(new ConnectionNotifier( |
| 585 base::Bind(&RequestCoordinator::HandleConnectedEventForStarting, |
| 586 weak_ptr_factory_.GetWeakPtr()))); |
| 587 } |
| 588 |
| 589 void RequestCoordinator::ClearConnectedEventRequest() { |
| 590 connection_notifier_.reset(nullptr); |
| 591 } |
| 592 |
| 593 void RequestCoordinator::HandleConnectedEventForStarting() { |
| 594 ClearConnectedEventRequest(); |
| 595 StartImmediatelyIfConnected(); |
| 596 } |
| 597 |
| 577 void RequestCoordinator::TryNextRequest(bool is_start_of_processing) { | 598 void RequestCoordinator::TryNextRequest(bool is_start_of_processing) { |
| 578 is_starting_ = true; | 599 is_starting_ = true; |
| 579 base::TimeDelta processing_time_budget; | 600 base::TimeDelta processing_time_budget; |
| 580 if (processing_state_ == ProcessingWindowState::SCHEDULED_WINDOW) { | 601 if (processing_state_ == ProcessingWindowState::SCHEDULED_WINDOW) { |
| 581 processing_time_budget = base::TimeDelta::FromSeconds( | 602 processing_time_budget = base::TimeDelta::FromSeconds( |
| 582 policy_->GetProcessingTimeBudgetWhenBackgroundScheduledInSeconds()); | 603 policy_->GetProcessingTimeBudgetWhenBackgroundScheduledInSeconds()); |
| 583 } else { | 604 } else { |
| 584 DCHECK(processing_state_ == ProcessingWindowState::IMMEDIATE_WINDOW); | 605 DCHECK(processing_state_ == ProcessingWindowState::IMMEDIATE_WINDOW); |
| 585 processing_time_budget = base::TimeDelta::FromSeconds( | 606 processing_time_budget = base::TimeDelta::FromSeconds( |
| 586 policy_->GetProcessingTimeBudgetForImmediateLoadInSeconds()); | 607 policy_->GetProcessingTimeBudgetForImmediateLoadInSeconds()); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 597 | 618 |
| 598 // If there is no network or no time left in the budget, return to the | 619 // If there is no network or no time left in the budget, return to the |
| 599 // scheduler. We do not remove the pending scheduler task that was set | 620 // scheduler. We do not remove the pending scheduler task that was set |
| 600 // up earlier in case we run out of time, so the background scheduler | 621 // up earlier in case we run out of time, so the background scheduler |
| 601 // will return to us at the next opportunity to run background tasks. | 622 // will return to us at the next opportunity to run background tasks. |
| 602 if (connection_type == | 623 if (connection_type == |
| 603 net::NetworkChangeNotifier::ConnectionType::CONNECTION_NONE || | 624 net::NetworkChangeNotifier::ConnectionType::CONNECTION_NONE || |
| 604 (base::Time::Now() - operation_start_time_) > processing_time_budget) { | 625 (base::Time::Now() - operation_start_time_) > processing_time_budget) { |
| 605 is_starting_ = false; | 626 is_starting_ = false; |
| 606 | 627 |
| 628 // If we were doing immediate processing, try to start it again |
| 629 // when we get connected. |
| 630 if (processing_state_ == ProcessingWindowState::IMMEDIATE_WINDOW) |
| 631 RequestConnectedEventForStarting(); |
| 632 |
| 607 // Let the scheduler know we are done processing. | 633 // Let the scheduler know we are done processing. |
| 608 // TODO: Make sure the scheduler callback is valid before running it. | 634 // TODO: Make sure the scheduler callback is valid before running it. |
| 609 scheduler_callback_.Run(true); | 635 scheduler_callback_.Run(true); |
| 610 DVLOG(2) << " out of time, giving up. " << __func__; | 636 DVLOG(2) << " out of time, giving up. " << __func__; |
| 611 | 637 |
| 612 return; | 638 return; |
| 613 } | 639 } |
| 614 | 640 |
| 615 // Ask request queue to make a new PickRequestTask object, then put it on the | 641 // Ask request queue to make a new PickRequestTask object, then put it on the |
| 616 // task queue. | 642 // task queue. |
| (...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 956 | 982 |
| 957 ClientPolicyController* RequestCoordinator::GetPolicyController() { | 983 ClientPolicyController* RequestCoordinator::GetPolicyController() { |
| 958 return policy_controller_.get(); | 984 return policy_controller_.get(); |
| 959 } | 985 } |
| 960 | 986 |
| 961 void RequestCoordinator::Shutdown() { | 987 void RequestCoordinator::Shutdown() { |
| 962 network_quality_estimator_ = nullptr; | 988 network_quality_estimator_ = nullptr; |
| 963 } | 989 } |
| 964 | 990 |
| 965 } // namespace offline_pages | 991 } // namespace offline_pages |
| OLD | NEW |