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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 70 | 70 |
| 71 // Inform the scheduler that we have an outstanding task. | 71 // Inform the scheduler that we have an outstanding task. |
| 72 // TODO(petewil): Determine trigger conditions from policy. | 72 // TODO(petewil): Determine trigger conditions from policy. |
| 73 scheduler_->Schedule(GetTriggerConditionsForUserRequest()); | 73 scheduler_->Schedule(GetTriggerConditionsForUserRequest()); |
| 74 } | 74 } |
| 75 | 75 |
| 76 // Called in response to updating a request in the request queue. | 76 // Called in response to updating a request in the request queue. |
| 77 void RequestCoordinator::UpdateRequestCallback( | 77 void RequestCoordinator::UpdateRequestCallback( |
| 78 RequestQueue::UpdateRequestResult result) {} | 78 RequestQueue::UpdateRequestResult result) {} |
| 79 | 79 |
| 80 // Called by the request picker when a request has been picked. | 80 void RequestCoordinator::StopProcessing() { |
| 81 void RequestCoordinator::RequestPicked(const SavePageRequest& request) { | 81 is_canceled_ = true; |
| 82 // Send the request on to the offliner. | 82 if (offliner_ && is_busy_) |
| 83 SendRequestToOffliner(request); | 83 offliner_->Cancel(); |
| 84 } | |
| 85 | |
| 86 void RequestCoordinator::RequestQueueEmpty() { | |
| 87 // Clear the outstanding "safety" task in the scheduler. | |
| 88 scheduler_->Unschedule(); | |
| 89 // Return control to the scheduler when there is no more to do. | |
| 90 scheduler_callback_.Run(true); | |
| 91 } | 84 } |
| 92 | 85 |
| 93 // Returns true if the caller should expect a callback, false otherwise. For | 86 // Returns true if the caller should expect a callback, false otherwise. For |
| 94 // instance, this would return false if a request is already in progress. | 87 // instance, this would return false if a request is already in progress. |
| 95 bool RequestCoordinator::StartProcessing( | 88 bool RequestCoordinator::StartProcessing( |
| 96 const DeviceConditions& device_conditions, | 89 const DeviceConditions& device_conditions, |
| 97 const base::Callback<void(bool)>& callback) { | 90 const base::Callback<void(bool)>& callback) { |
| 98 if (is_busy_) return false; | 91 if (is_busy_) return false; |
| 99 | 92 |
| 100 is_canceled_ = false; | 93 is_canceled_ = false; |
| 101 scheduler_callback_ = callback; | 94 scheduler_callback_ = callback; |
| 102 // TODO(petewil): Check existing conditions (should be passed down from | 95 // TODO(petewil): Check existing conditions (should be passed down from |
| 103 // BackgroundTask) | 96 // BackgroundTask) |
| 104 | 97 |
| 105 TryNextRequest(); | 98 TryNextRequest(); |
| 106 | 99 |
| 107 return true; | 100 return true; |
| 108 } | 101 } |
| 109 | 102 |
| 110 void RequestCoordinator::TryNextRequest() { | 103 void RequestCoordinator::TryNextRequest() { |
| 111 // Choose a request to process that meets the available conditions. | 104 // Choose a request to process that meets the available conditions. |
| 112 // This is an async call, and returns right away. | 105 // This is an async call, and returns right away. |
| 113 picker_->ChooseNextRequest( | 106 picker_->ChooseNextRequest( |
| 114 base::Bind(&RequestCoordinator::RequestPicked, | 107 base::Bind(&RequestCoordinator::RequestPicked, |
| 115 weak_ptr_factory_.GetWeakPtr()), | 108 weak_ptr_factory_.GetWeakPtr()), |
| 116 base::Bind(&RequestCoordinator::RequestQueueEmpty, | 109 base::Bind(&RequestCoordinator::RequestQueueEmpty, |
| 117 weak_ptr_factory_.GetWeakPtr())); | 110 weak_ptr_factory_.GetWeakPtr())); |
| 118 } | 111 } |
| 119 | 112 |
| 120 void RequestCoordinator::StopProcessing() { | 113 // Called by the request picker when a request has been picked. |
| 121 is_canceled_ = true; | 114 void RequestCoordinator::RequestPicked(const SavePageRequest& request) { |
| 122 if (offliner_ && is_busy_) | 115 // Send the request on to the offliner. |
| 123 offliner_->Cancel(); | 116 SendRequestToOffliner(request); |
| 124 } | 117 } |
| 125 | 118 |
| 126 Scheduler::TriggerConditions const& | 119 void RequestCoordinator::RequestQueueEmpty() { |
| 127 RequestCoordinator::GetTriggerConditionsForUserRequest() { | 120 // Clear the outstanding "safety" task in the scheduler. |
| 128 return kUserRequestTriggerConditions; | 121 scheduler_->Unschedule(); |
| 122 // Return control to the scheduler when there is no more to do. | |
| 123 scheduler_callback_.Run(true); | |
| 129 } | 124 } |
| 130 | 125 |
| 131 void RequestCoordinator::SendRequestToOffliner(const SavePageRequest& request) { | 126 void RequestCoordinator::SendRequestToOffliner(const SavePageRequest& request) { |
| 132 // Check that offlining didn't get cancelled while performing some async | 127 // Check that offlining didn't get cancelled while performing some async |
| 133 // steps. | 128 // steps. |
| 134 if (is_canceled_) | 129 if (is_canceled_) |
| 135 return; | 130 return; |
| 136 | 131 |
| 137 GetOffliner(); | 132 GetOffliner(); |
| 138 if (!offliner_) { | 133 if (!offliner_) { |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 166 base::Bind(&RequestCoordinator::UpdateRequestCallback, | 161 base::Bind(&RequestCoordinator::UpdateRequestCallback, |
| 167 weak_ptr_factory_.GetWeakPtr())); | 162 weak_ptr_factory_.GetWeakPtr())); |
| 168 | 163 |
| 169 // TODO(petewil): Check time budget. Return to the scheduler if we are out. | 164 // TODO(petewil): Check time budget. Return to the scheduler if we are out. |
| 170 | 165 |
| 171 // Start another request if we have time. | 166 // Start another request if we have time. |
| 172 TryNextRequest(); | 167 TryNextRequest(); |
| 173 } | 168 } |
| 174 } | 169 } |
| 175 | 170 |
| 171 Scheduler::TriggerConditions const& | |
|
fgorski
2016/06/24 18:39:17
Could you also update the order here to const type
| |
| 172 RequestCoordinator::GetTriggerConditionsForUserRequest() { | |
| 173 return kUserRequestTriggerConditions; | |
| 174 } | |
| 175 | |
| 176 void RequestCoordinator::GetOffliner() { | 176 void RequestCoordinator::GetOffliner() { |
| 177 if (!offliner_) { | 177 if (!offliner_) { |
| 178 offliner_ = factory_->GetOffliner(policy_.get()); | 178 offliner_ = factory_->GetOffliner(policy_.get()); |
| 179 } | 179 } |
| 180 } | 180 } |
| 181 | 181 |
| 182 } // namespace offline_pages | 182 } // namespace offline_pages |
| OLD | NEW |