| 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" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "components/offline_pages/background/offliner_factory.h" | 12 #include "components/offline_pages/background/offliner_factory.h" |
| 13 #include "components/offline_pages/background/offliner_policy.h" | 13 #include "components/offline_pages/background/offliner_policy.h" |
| 14 #include "components/offline_pages/background/request_picker.h" | 14 #include "components/offline_pages/background/request_picker.h" |
| 15 #include "components/offline_pages/background/save_page_request.h" | 15 #include "components/offline_pages/background/save_page_request.h" |
| 16 #include "components/offline_pages/background/scheduler.h" | |
| 17 #include "components/offline_pages/offline_page_item.h" | 16 #include "components/offline_pages/offline_page_item.h" |
| 18 | 17 |
| 19 namespace offline_pages { | 18 namespace offline_pages { |
| 20 | 19 |
| 20 namespace { |
| 21 // TODO(dougarnett/petewil): Move to Policy object. Also consider lower minimum |
| 22 // battery percentage once there is some processing time limits in place. |
| 23 const Scheduler::TriggerConditions kUserRequestTriggerConditions( |
| 24 false /* require_power_connected */, |
| 25 50 /* minimum_battery_percentage */, |
| 26 false /* require_unmetered_network */); |
| 27 } // namespace |
| 28 |
| 21 RequestCoordinator::RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy, | 29 RequestCoordinator::RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy, |
| 22 std::unique_ptr<OfflinerFactory> factory, | 30 std::unique_ptr<OfflinerFactory> factory, |
| 23 std::unique_ptr<RequestQueue> queue, | 31 std::unique_ptr<RequestQueue> queue, |
| 24 std::unique_ptr<Scheduler> scheduler) | 32 std::unique_ptr<Scheduler> scheduler) |
| 25 : policy_(std::move(policy)), | 33 : policy_(std::move(policy)), |
| 26 factory_(std::move(factory)), | 34 factory_(std::move(factory)), |
| 27 queue_(std::move(queue)), | 35 queue_(std::move(queue)), |
| 28 scheduler_(std::move(scheduler)), | 36 scheduler_(std::move(scheduler)), |
| 29 last_offlining_status_(Offliner::RequestStatus::UNKNOWN), | 37 last_offlining_status_(Offliner::RequestStatus::UNKNOWN), |
| 30 weak_ptr_factory_(this) { | 38 weak_ptr_factory_(this) { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 56 // immediately for testing. It should call SendRequestToOffliner() | 64 // immediately for testing. It should call SendRequestToOffliner() |
| 57 | 65 |
| 58 return true; | 66 return true; |
| 59 } | 67 } |
| 60 | 68 |
| 61 void RequestCoordinator::AddRequestResultCallback( | 69 void RequestCoordinator::AddRequestResultCallback( |
| 62 RequestQueue::AddRequestResult result, | 70 RequestQueue::AddRequestResult result, |
| 63 const SavePageRequest& request) { | 71 const SavePageRequest& request) { |
| 64 | 72 |
| 65 // Inform the scheduler that we have an outstanding task. | 73 // Inform the scheduler that we have an outstanding task. |
| 66 // TODO(petewil): Define proper TriggerConditions and set them. | 74 // TODO(petewil): Determine trigger conditions from policy. |
| 67 Scheduler::TriggerCondition conditions; | 75 scheduler_->Schedule(GetTriggerConditionsForUserRequest()); |
| 68 scheduler_->Schedule(conditions); | |
| 69 } | 76 } |
| 70 | 77 |
| 71 // Called in response to updating a request in the request queue. | 78 // Called in response to updating a request in the request queue. |
| 72 void RequestCoordinator::UpdateRequestCallback( | 79 void RequestCoordinator::UpdateRequestCallback( |
| 73 RequestQueue::UpdateRequestResult result) {} | 80 RequestQueue::UpdateRequestResult result) {} |
| 74 | 81 |
| 75 // Called by the request picker when a request has been picked. | 82 // Called by the request picker when a request has been picked. |
| 76 void RequestCoordinator::RequestPicked(const SavePageRequest& request) { | 83 void RequestCoordinator::RequestPicked(const SavePageRequest& request) { |
| 77 // Send the request on to the offliner. | 84 // Send the request on to the offliner. |
| 78 SendRequestToOffliner(request); | 85 SendRequestToOffliner(request); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 107 picker_->ChooseNextRequest( | 114 picker_->ChooseNextRequest( |
| 108 base::Bind(&RequestCoordinator::RequestPicked, | 115 base::Bind(&RequestCoordinator::RequestPicked, |
| 109 weak_ptr_factory_.GetWeakPtr()), | 116 weak_ptr_factory_.GetWeakPtr()), |
| 110 base::Bind(&RequestCoordinator::RequestQueueEmpty, | 117 base::Bind(&RequestCoordinator::RequestQueueEmpty, |
| 111 weak_ptr_factory_.GetWeakPtr())); | 118 weak_ptr_factory_.GetWeakPtr())); |
| 112 } | 119 } |
| 113 | 120 |
| 114 void RequestCoordinator::StopProcessing() { | 121 void RequestCoordinator::StopProcessing() { |
| 115 } | 122 } |
| 116 | 123 |
| 124 Scheduler::TriggerConditions const& |
| 125 RequestCoordinator::GetTriggerConditionsForUserRequest() { |
| 126 return kUserRequestTriggerConditions; |
| 127 } |
| 128 |
| 117 void RequestCoordinator::SendRequestToOffliner(const SavePageRequest& request) { | 129 void RequestCoordinator::SendRequestToOffliner(const SavePageRequest& request) { |
| 118 // TODO(petewil): Ensure only one offliner at a time is used. | 130 // TODO(petewil): Ensure only one offliner at a time is used. |
| 119 // TODO(petewil): When we have multiple offliners, we need to pick one. | 131 // TODO(petewil): When we have multiple offliners, we need to pick one. |
| 120 Offliner* offliner = factory_->GetOffliner(policy_.get()); | 132 Offliner* offliner = factory_->GetOffliner(policy_.get()); |
| 121 if (!offliner) { | 133 if (!offliner) { |
| 122 DVLOG(0) << "Unable to create Offliner. " | 134 DVLOG(0) << "Unable to create Offliner. " |
| 123 << "Cannot background offline page."; | 135 << "Cannot background offline page."; |
| 124 return; | 136 return; |
| 125 } | 137 } |
| 126 | 138 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 145 weak_ptr_factory_.GetWeakPtr())); | 157 weak_ptr_factory_.GetWeakPtr())); |
| 146 | 158 |
| 147 // TODO(petewil): Check time budget. Return to the scheduler if we are out. | 159 // TODO(petewil): Check time budget. Return to the scheduler if we are out. |
| 148 | 160 |
| 149 // Start another request if we have time. | 161 // Start another request if we have time. |
| 150 TryNextRequest(); | 162 TryNextRequest(); |
| 151 } | 163 } |
| 152 } | 164 } |
| 153 | 165 |
| 154 } // namespace offline_pages | 166 } // namespace offline_pages |
| OLD | NEW |