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 "components/offline_pages/background/request_coordinator.h" | 5 #include "components/offline_pages/background/request_coordinator.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 72 // is nothing for it to do. | 72 // is nothing for it to do. |
| 73 void EmptySchedulerCallback(bool started) {} | 73 void EmptySchedulerCallback(bool started) {} |
| 74 | 74 |
| 75 } // namespace | 75 } // namespace |
| 76 | 76 |
| 77 RequestCoordinator::RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy, | 77 RequestCoordinator::RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy, |
| 78 std::unique_ptr<OfflinerFactory> factory, | 78 std::unique_ptr<OfflinerFactory> factory, |
| 79 std::unique_ptr<RequestQueue> queue, | 79 std::unique_ptr<RequestQueue> queue, |
| 80 std::unique_ptr<Scheduler> scheduler) | 80 std::unique_ptr<Scheduler> scheduler) |
| 81 : is_busy_(false), | 81 : is_busy_(false), |
| 82 is_starting_(false), | |
| 82 is_stopped_(false), | 83 is_stopped_(false), |
| 83 use_test_connection_type_(false), | 84 use_test_connection_type_(false), |
| 84 test_connection_type_(), | 85 test_connection_type_(), |
| 85 offliner_(nullptr), | 86 offliner_(nullptr), |
| 86 policy_(std::move(policy)), | 87 policy_(std::move(policy)), |
| 87 factory_(std::move(factory)), | 88 factory_(std::move(factory)), |
| 88 queue_(std::move(queue)), | 89 queue_(std::move(queue)), |
| 89 scheduler_(std::move(scheduler)), | 90 scheduler_(std::move(scheduler)), |
| 90 active_request_(nullptr), | 91 active_request_(nullptr), |
| 91 last_offlining_status_(Offliner::RequestStatus::UNKNOWN), | 92 last_offlining_status_(Offliner::RequestStatus::UNKNOWN), |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 338 // Let the scheduler know we are done processing. | 339 // Let the scheduler know we are done processing. |
| 339 scheduler_callback_.Run(true); | 340 scheduler_callback_.Run(true); |
| 340 } | 341 } |
| 341 | 342 |
| 342 // Returns true if the caller should expect a callback, false otherwise. For | 343 // Returns true if the caller should expect a callback, false otherwise. For |
| 343 // instance, this would return false if a request is already in progress. | 344 // instance, this would return false if a request is already in progress. |
| 344 bool RequestCoordinator::StartProcessing( | 345 bool RequestCoordinator::StartProcessing( |
| 345 const DeviceConditions& device_conditions, | 346 const DeviceConditions& device_conditions, |
| 346 const base::Callback<void(bool)>& callback) { | 347 const base::Callback<void(bool)>& callback) { |
| 347 current_conditions_.reset(new DeviceConditions(device_conditions)); | 348 current_conditions_.reset(new DeviceConditions(device_conditions)); |
| 348 if (is_busy_) return false; | 349 if (is_starting_ || is_busy_) |
|
Pete Williamson
2016/09/06 22:56:44
Please add a bug to refactor to a single state var
dougarnett
2016/09/06 23:39:10
Done. https://bugs.chromium.org/p/chromium/issues/
| |
| 350 return false; | |
|
Pete Williamson
2016/09/06 22:56:44
(as we discussed in person) BackgroundOfflinerTask
dougarnett
2016/09/06 23:39:10
Done.
| |
| 351 is_starting_ = true; | |
| 349 | 352 |
| 350 // Mark the time at which we started processing so we can check our time | 353 // Mark the time at which we started processing so we can check our time |
| 351 // budget. | 354 // budget. |
| 352 operation_start_time_ = base::Time::Now(); | 355 operation_start_time_ = base::Time::Now(); |
| 353 | 356 |
| 354 is_stopped_ = false; | 357 is_stopped_ = false; |
| 355 scheduler_callback_ = callback; | 358 scheduler_callback_ = callback; |
| 356 | 359 |
| 357 TryNextRequest(); | 360 TryNextRequest(); |
| 358 | 361 |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 378 void RequestCoordinator::TryNextRequest() { | 381 void RequestCoordinator::TryNextRequest() { |
| 379 // If there is no time left in the budget, return to the scheduler. | 382 // If there is no time left in the budget, return to the scheduler. |
| 380 // We do not remove the pending task that was set up earlier in case | 383 // We do not remove the pending task that was set up earlier in case |
| 381 // we run out of time, so the background scheduler will return to us | 384 // we run out of time, so the background scheduler will return to us |
| 382 // at the next opportunity to run background tasks. | 385 // at the next opportunity to run background tasks. |
| 383 if (base::Time::Now() - operation_start_time_ > | 386 if (base::Time::Now() - operation_start_time_ > |
| 384 base::TimeDelta::FromSeconds( | 387 base::TimeDelta::FromSeconds( |
| 385 policy_->GetBackgroundProcessingTimeBudgetSeconds())) { | 388 policy_->GetBackgroundProcessingTimeBudgetSeconds())) { |
| 386 // Let the scheduler know we are done processing. | 389 // Let the scheduler know we are done processing. |
| 387 scheduler_callback_.Run(true); | 390 scheduler_callback_.Run(true); |
| 388 | 391 |
|
Pete Williamson
2016/09/06 22:56:44
is_starting_ = false
dougarnett
2016/09/06 23:39:10
Done. Thanks!
| |
| 389 return; | 392 return; |
| 390 } | 393 } |
| 391 | 394 |
| 392 // Choose a request to process that meets the available conditions. | 395 // Choose a request to process that meets the available conditions. |
| 393 // This is an async call, and returns right away. | 396 // This is an async call, and returns right away. |
| 394 picker_->ChooseNextRequest(base::Bind(&RequestCoordinator::RequestPicked, | 397 picker_->ChooseNextRequest(base::Bind(&RequestCoordinator::RequestPicked, |
| 395 weak_ptr_factory_.GetWeakPtr()), | 398 weak_ptr_factory_.GetWeakPtr()), |
| 396 base::Bind(&RequestCoordinator::RequestNotPicked, | 399 base::Bind(&RequestCoordinator::RequestNotPicked, |
| 397 weak_ptr_factory_.GetWeakPtr()), | 400 weak_ptr_factory_.GetWeakPtr()), |
| 398 current_conditions_.get()); | 401 current_conditions_.get()); |
| 399 } | 402 } |
| 400 | 403 |
| 401 // Called by the request picker when a request has been picked. | 404 // Called by the request picker when a request has been picked. |
| 402 void RequestCoordinator::RequestPicked(const SavePageRequest& request) { | 405 void RequestCoordinator::RequestPicked(const SavePageRequest& request) { |
| 403 // Send the request on to the offliner. | 406 is_starting_ = false; |
| 404 SendRequestToOffliner(request); | 407 |
| 408 // Make sure we were not stopped while picking. | |
| 409 if (!is_stopped_) { | |
| 410 // Send the request on to the offliner. | |
| 411 SendRequestToOffliner(request); | |
| 412 } | |
| 405 } | 413 } |
| 406 | 414 |
| 407 void RequestCoordinator::RequestNotPicked( | 415 void RequestCoordinator::RequestNotPicked( |
| 408 bool non_user_requested_tasks_remaining) { | 416 bool non_user_requested_tasks_remaining) { |
| 417 is_starting_ = false; | |
| 418 | |
| 409 // Clear the outstanding "safety" task in the scheduler. | 419 // Clear the outstanding "safety" task in the scheduler. |
| 410 scheduler_->Unschedule(); | 420 scheduler_->Unschedule(); |
| 411 | 421 |
| 412 if (non_user_requested_tasks_remaining) | 422 if (non_user_requested_tasks_remaining) |
| 413 scheduler_->Schedule(GetTriggerConditions(!kUserRequest)); | 423 scheduler_->Schedule(GetTriggerConditions(!kUserRequest)); |
| 414 // Let the scheduler know we are done processing. | 424 // Let the scheduler know we are done processing. |
| 415 scheduler_callback_.Run(true); | 425 scheduler_callback_.Run(true); |
| 416 } | 426 } |
| 417 | 427 |
| 418 void RequestCoordinator::SendRequestToOffliner(const SavePageRequest& request) { | 428 void RequestCoordinator::SendRequestToOffliner(const SavePageRequest& request) { |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 560 FOR_EACH_OBSERVER(Observer, observers_, OnChanged(request)); | 570 FOR_EACH_OBSERVER(Observer, observers_, OnChanged(request)); |
| 561 } | 571 } |
| 562 | 572 |
| 563 void RequestCoordinator::GetOffliner() { | 573 void RequestCoordinator::GetOffliner() { |
| 564 if (!offliner_) { | 574 if (!offliner_) { |
| 565 offliner_ = factory_->GetOffliner(policy_.get()); | 575 offliner_ = factory_->GetOffliner(policy_.get()); |
| 566 } | 576 } |
| 567 } | 577 } |
| 568 | 578 |
| 569 } // namespace offline_pages | 579 } // namespace offline_pages |
| OLD | NEW |