| 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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 void RequestCoordinator::AddRequestResultCallback( | 61 void RequestCoordinator::AddRequestResultCallback( |
| 62 RequestQueue::AddRequestResult result, | 62 RequestQueue::AddRequestResult result, |
| 63 const SavePageRequest& request) { | 63 const SavePageRequest& request) { |
| 64 | 64 |
| 65 // Inform the scheduler that we have an outstanding task. | 65 // Inform the scheduler that we have an outstanding task. |
| 66 // TODO(petewil): Define proper TriggerConditions and set them. | 66 // TODO(petewil): Define proper TriggerConditions and set them. |
| 67 Scheduler::TriggerCondition conditions; | 67 Scheduler::TriggerCondition conditions; |
| 68 scheduler_->Schedule(conditions); | 68 scheduler_->Schedule(conditions); |
| 69 } | 69 } |
| 70 | 70 |
| 71 // Called in response to updating a request in the request queue. |
| 72 void RequestCoordinator::UpdateRequestCallback( |
| 73 RequestQueue::UpdateRequestResult result) {} |
| 74 |
| 75 // Called by the request picker when a request has been picked. |
| 71 void RequestCoordinator::RequestPicked(const SavePageRequest& request) { | 76 void RequestCoordinator::RequestPicked(const SavePageRequest& request) { |
| 72 Scheduler::TriggerCondition conditions; | 77 Scheduler::TriggerCondition conditions; |
| 73 | 78 |
| 74 // Ensure that the scheduler has actively scheduled a task. | 79 // Ensure that the scheduler has actively scheduled a task. |
| 75 scheduler_->Schedule(conditions); | 80 scheduler_->Schedule(conditions); |
| 76 | 81 |
| 77 // Send the request on to the offliner. | 82 // Send the request on to the offliner. |
| 78 SendRequestToOffliner(request); | 83 SendRequestToOffliner(request); |
| 79 } | 84 } |
| 80 | 85 |
| 81 void RequestCoordinator::RequestQueueEmpty() { | 86 void RequestCoordinator::RequestQueueEmpty() { |
| 82 // TODO(petewil): return to the BackgroundScheduler by calling | 87 // Clear the outstanding "safety" task in the scheduler. |
| 83 // ProcessingDoneCallback | 88 scheduler_->Unschedule(); |
| 89 // Return control to the scheduler when there is no more to do. |
| 90 scheduler_callback_.Run(true); |
| 84 } | 91 } |
| 85 | 92 |
| 86 bool RequestCoordinator::StartProcessing( | 93 bool RequestCoordinator::StartProcessing( |
| 87 const base::Callback<void(bool)>& callback) { | 94 const base::Callback<void(bool)>& callback) { |
| 95 scheduler_callback_ = callback; |
| 88 // TODO(petewil): Check existing conditions (should be passed down from | 96 // TODO(petewil): Check existing conditions (should be passed down from |
| 89 // BackgroundTask) | 97 // BackgroundTask) |
| 90 | 98 |
| 99 TryNextRequest(); |
| 100 |
| 101 // TODO(petewil): Should return true if the caller should expect a |
| 102 // callback. Return false if there is already a request running. |
| 103 // Probably best to do this when I prevent multiple instances from |
| 104 // running at the same time. |
| 105 return true; |
| 106 } |
| 107 |
| 108 void RequestCoordinator::TryNextRequest() { |
| 91 // Choose a request to process that meets the available conditions. | 109 // Choose a request to process that meets the available conditions. |
| 92 // This is an async call, and returns right away. | 110 // This is an async call, and returns right away. |
| 93 picker_->ChooseNextRequest( | 111 picker_->ChooseNextRequest( |
| 94 base::Bind(&RequestCoordinator::RequestPicked, | 112 base::Bind(&RequestCoordinator::RequestPicked, |
| 95 weak_ptr_factory_.GetWeakPtr()), | 113 weak_ptr_factory_.GetWeakPtr()), |
| 96 base::Bind(&RequestCoordinator::RequestQueueEmpty, | 114 base::Bind(&RequestCoordinator::RequestQueueEmpty, |
| 97 weak_ptr_factory_.GetWeakPtr())); | 115 weak_ptr_factory_.GetWeakPtr())); |
| 98 return false; | |
| 99 } | 116 } |
| 100 | 117 |
| 101 void RequestCoordinator::StopProcessing() { | 118 void RequestCoordinator::StopProcessing() { |
| 102 } | 119 } |
| 103 | 120 |
| 104 void RequestCoordinator::SendRequestToOffliner(const SavePageRequest& request) { | 121 void RequestCoordinator::SendRequestToOffliner(const SavePageRequest& request) { |
| 105 // TODO(petewil): Ensure only one offliner at a time is used. | 122 // TODO(petewil): Ensure only one offliner at a time is used. |
| 106 // TODO(petewil): When we have multiple offliners, we need to pick one. | 123 // TODO(petewil): When we have multiple offliners, we need to pick one. |
| 107 Offliner* offliner = factory_->GetOffliner(policy_.get()); | 124 Offliner* offliner = factory_->GetOffliner(policy_.get()); |
| 108 if (!offliner) { | 125 if (!offliner) { |
| 109 DVLOG(0) << "Unable to create Offliner. " | 126 DVLOG(0) << "Unable to create Offliner. " |
| 110 << "Cannot background offline page."; | 127 << "Cannot background offline page."; |
| 111 return; | 128 return; |
| 112 } | 129 } |
| 113 | 130 |
| 114 // Start the load and save process in the offliner (Async). | 131 // Start the load and save process in the offliner (Async). |
| 115 offliner->LoadAndSave(request, | 132 offliner->LoadAndSave(request, |
| 116 base::Bind(&RequestCoordinator::OfflinerDoneCallback, | 133 base::Bind(&RequestCoordinator::OfflinerDoneCallback, |
| 117 weak_ptr_factory_.GetWeakPtr())); | 134 weak_ptr_factory_.GetWeakPtr())); |
| 118 } | 135 } |
| 119 | 136 |
| 120 void RequestCoordinator::OfflinerDoneCallback(const SavePageRequest& request, | 137 void RequestCoordinator::OfflinerDoneCallback(const SavePageRequest& request, |
| 121 Offliner::RequestStatus status) { | 138 Offliner::RequestStatus status) { |
| 122 DVLOG(2) << "offliner finished, saved: " | 139 DVLOG(2) << "offliner finished, saved: " |
| 123 << (status == Offliner::RequestStatus::SAVED) << ", " | 140 << (status == Offliner::RequestStatus::SAVED) << ", " |
| 124 << __FUNCTION__; | 141 << __FUNCTION__; |
| 125 last_offlining_status_ = status; | 142 last_offlining_status_ = status; |
| 126 | 143 |
| 127 // TODO(petewil): Check time budget. Start a request if we have time, return | 144 // TODO(petewil): If the request succeeded, remove it from the Queue. |
| 128 // to the scheduler if we are out of time. | 145 if (status == Offliner::RequestStatus::SAVED) { |
| 146 queue_->RemoveRequest(request.request_id(), |
| 147 base::Bind(&RequestCoordinator::UpdateRequestCallback, |
| 148 weak_ptr_factory_.GetWeakPtr())); |
| 149 } |
| 129 | 150 |
| 130 // TODO(petewil): If the request succeeded, remove it from the Queue. | 151 // TODO(petewil): Check time budget. Return to the scheduler if we are out. |
| 152 |
| 153 |
| 154 // Start a request if we have time. |
| 155 TryNextRequest(); |
| 156 |
| 131 } | 157 } |
| 132 | 158 |
| 133 } // namespace offline_pages | 159 } // namespace offline_pages |
| OLD | NEW |