| 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" | 16 #include "components/offline_pages/background/scheduler.h" |
| 17 #include "components/offline_pages/offline_page_item.h" | 17 #include "components/offline_pages/offline_page_item.h" |
| 18 | 18 |
| 19 namespace offline_pages { | 19 namespace offline_pages { |
| 20 | 20 |
| 21 RequestCoordinator::RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy, | 21 RequestCoordinator::RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy, |
| 22 std::unique_ptr<OfflinerFactory> factory, | 22 std::unique_ptr<OfflinerFactory> factory, |
| 23 std::unique_ptr<RequestQueue> queue, | 23 std::unique_ptr<RequestQueue> queue, |
| 24 std::unique_ptr<Scheduler> scheduler) | 24 std::unique_ptr<Scheduler> scheduler) |
| 25 : policy_(std::move(policy)), | 25 : is_busy_(false), |
| 26 policy_(std::move(policy)), |
| 26 factory_(std::move(factory)), | 27 factory_(std::move(factory)), |
| 27 queue_(std::move(queue)), | 28 queue_(std::move(queue)), |
| 28 scheduler_(std::move(scheduler)), | 29 scheduler_(std::move(scheduler)), |
| 29 last_offlining_status_(Offliner::RequestStatus::UNKNOWN), | 30 last_offlining_status_(Offliner::RequestStatus::UNKNOWN), |
| 30 weak_ptr_factory_(this) { | 31 weak_ptr_factory_(this) { |
| 31 DCHECK(policy_ != nullptr); | 32 DCHECK(policy_ != nullptr); |
| 32 picker_.reset(new RequestPicker(queue_.get())); | 33 picker_.reset(new RequestPicker(queue_.get())); |
| 33 } | 34 } |
| 34 | 35 |
| 35 RequestCoordinator::~RequestCoordinator() {} | 36 RequestCoordinator::~RequestCoordinator() {} |
| 36 | 37 |
| 37 bool RequestCoordinator::SavePageLater( | 38 bool RequestCoordinator::SavePageLater( |
| 38 const GURL& url, const ClientId& client_id) { | 39 const GURL& url, const ClientId& client_id) { |
| 39 DVLOG(2) << "URL is " << url << " " << __FUNCTION__; | 40 DVLOG(2) << "URL is " << url << " " << __FUNCTION__; |
| 40 | 41 |
| 41 // TODO(petewil): We need a robust scheme for allocating new IDs. | 42 // TODO(petewil): We need a robust scheme for allocating new IDs. |
| 42 static int64_t id = 0; | 43 static int64_t id = 0; |
| 43 | 44 |
| 44 // Build a SavePageRequest. | 45 // Build a SavePageRequest. |
| 45 // TODO(petewil): Use something like base::Clock to help in testing. | 46 // TODO(petewil): Use something like base::Clock to help in testing. |
| 46 offline_pages::SavePageRequest request( | 47 offline_pages::SavePageRequest request( |
| 47 id++, url, client_id, base::Time::Now()); | 48 id++, url, client_id, base::Time::Now()); |
| 48 | 49 |
| 49 // Put the request on the request queue. | 50 // Put the request on the request queue. |
| 50 queue_->AddRequest(request, | 51 queue_->AddRequest(request, |
| 51 base::Bind(&RequestCoordinator::AddRequestResultCallback, | 52 base::Bind(&RequestCoordinator::AddRequestResultCallback, |
| 52 weak_ptr_factory_.GetWeakPtr())); | 53 weak_ptr_factory_.GetWeakPtr())); |
| 53 // TODO(petewil): Do I need to persist the request in case the add fails? | |
| 54 | |
| 55 // TODO(petewil): Make a new chromium command line switch to send the request | |
| 56 // immediately for testing. It should call SendRequestToOffliner() | |
| 57 | |
| 58 return true; | 54 return true; |
| 59 } | 55 } |
| 60 | 56 |
| 61 void RequestCoordinator::AddRequestResultCallback( | 57 void RequestCoordinator::AddRequestResultCallback( |
| 62 RequestQueue::AddRequestResult result, | 58 RequestQueue::AddRequestResult result, |
| 63 const SavePageRequest& request) { | 59 const SavePageRequest& request) { |
| 64 | 60 |
| 65 // Inform the scheduler that we have an outstanding task. | 61 // Inform the scheduler that we have an outstanding task. |
| 66 // TODO(petewil): Define proper TriggerConditions and set them. | 62 // TODO(petewil): Define proper TriggerConditions and set them. |
| 67 Scheduler::TriggerCondition conditions; | 63 Scheduler::TriggerCondition conditions; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 78 SendRequestToOffliner(request); | 74 SendRequestToOffliner(request); |
| 79 } | 75 } |
| 80 | 76 |
| 81 void RequestCoordinator::RequestQueueEmpty() { | 77 void RequestCoordinator::RequestQueueEmpty() { |
| 82 // Clear the outstanding "safety" task in the scheduler. | 78 // Clear the outstanding "safety" task in the scheduler. |
| 83 scheduler_->Unschedule(); | 79 scheduler_->Unschedule(); |
| 84 // Return control to the scheduler when there is no more to do. | 80 // Return control to the scheduler when there is no more to do. |
| 85 scheduler_callback_.Run(true); | 81 scheduler_callback_.Run(true); |
| 86 } | 82 } |
| 87 | 83 |
| 84 // Returns true if the caller should expect a callback, false otherwise. For |
| 85 // instance, this would return false if a request is already in progress. |
| 88 bool RequestCoordinator::StartProcessing( | 86 bool RequestCoordinator::StartProcessing( |
| 89 const DeviceConditions& device_conditions, | 87 const DeviceConditions& device_conditions, |
| 90 const base::Callback<void(bool)>& callback) { | 88 const base::Callback<void(bool)>& callback) { |
| 89 if (is_busy_) return false; |
| 90 |
| 91 scheduler_callback_ = callback; | 91 scheduler_callback_ = callback; |
| 92 // TODO(petewil): Check existing conditions (should be passed down from | 92 // TODO(petewil): Check existing conditions (should be passed down from |
| 93 // BackgroundTask) | 93 // BackgroundTask) |
| 94 | 94 |
| 95 TryNextRequest(); | 95 TryNextRequest(); |
| 96 | 96 |
| 97 // TODO(petewil): Should return true if the caller should expect a | |
| 98 // callback. Return false if there is already a request running. | |
| 99 // Probably best to do this when I prevent multiple instances from | |
| 100 // running at the same time. | |
| 101 return true; | 97 return true; |
| 102 } | 98 } |
| 103 | 99 |
| 104 void RequestCoordinator::TryNextRequest() { | 100 void RequestCoordinator::TryNextRequest() { |
| 105 // Choose a request to process that meets the available conditions. | 101 // Choose a request to process that meets the available conditions. |
| 106 // This is an async call, and returns right away. | 102 // This is an async call, and returns right away. |
| 107 picker_->ChooseNextRequest( | 103 picker_->ChooseNextRequest( |
| 108 base::Bind(&RequestCoordinator::RequestPicked, | 104 base::Bind(&RequestCoordinator::RequestPicked, |
| 109 weak_ptr_factory_.GetWeakPtr()), | 105 weak_ptr_factory_.GetWeakPtr()), |
| 110 base::Bind(&RequestCoordinator::RequestQueueEmpty, | 106 base::Bind(&RequestCoordinator::RequestQueueEmpty, |
| 111 weak_ptr_factory_.GetWeakPtr())); | 107 weak_ptr_factory_.GetWeakPtr())); |
| 112 } | 108 } |
| 113 | 109 |
| 114 void RequestCoordinator::StopProcessing() { | 110 void RequestCoordinator::StopProcessing() { |
| 115 } | 111 } |
| 116 | 112 |
| 117 void RequestCoordinator::SendRequestToOffliner(const SavePageRequest& request) { | 113 void RequestCoordinator::SendRequestToOffliner(const SavePageRequest& request) { |
| 118 // TODO(petewil): Ensure only one offliner at a time is used. | |
| 119 // TODO(petewil): When we have multiple offliners, we need to pick one. | 114 // TODO(petewil): When we have multiple offliners, we need to pick one. |
| 120 Offliner* offliner = factory_->GetOffliner(policy_.get()); | 115 Offliner* offliner = factory_->GetOffliner(policy_.get()); |
| 121 if (!offliner) { | 116 if (!offliner) { |
| 122 DVLOG(0) << "Unable to create Offliner. " | 117 DVLOG(0) << "Unable to create Offliner. " |
| 123 << "Cannot background offline page."; | 118 << "Cannot background offline page."; |
| 124 return; | 119 return; |
| 125 } | 120 } |
| 126 | 121 |
| 122 DCHECK(!is_busy_); |
| 123 is_busy_ = true; |
| 124 |
| 127 // Start the load and save process in the offliner (Async). | 125 // Start the load and save process in the offliner (Async). |
| 128 offliner->LoadAndSave(request, | 126 offliner->LoadAndSave(request, |
| 129 base::Bind(&RequestCoordinator::OfflinerDoneCallback, | 127 base::Bind(&RequestCoordinator::OfflinerDoneCallback, |
| 130 weak_ptr_factory_.GetWeakPtr())); | 128 weak_ptr_factory_.GetWeakPtr())); |
| 131 } | 129 } |
| 132 | 130 |
| 133 void RequestCoordinator::OfflinerDoneCallback(const SavePageRequest& request, | 131 void RequestCoordinator::OfflinerDoneCallback(const SavePageRequest& request, |
| 134 Offliner::RequestStatus status) { | 132 Offliner::RequestStatus status) { |
| 135 DVLOG(2) << "offliner finished, saved: " | 133 DVLOG(2) << "offliner finished, saved: " |
| 136 << (status == Offliner::RequestStatus::SAVED) << ", " | 134 << (status == Offliner::RequestStatus::SAVED) << ", " |
| 137 << __FUNCTION__; | 135 << __FUNCTION__; |
| 138 last_offlining_status_ = status; | 136 last_offlining_status_ = status; |
| 139 | 137 |
| 138 is_busy_ = false; |
| 139 |
| 140 // If the request succeeded, remove it from the Queue and maybe schedule | 140 // If the request succeeded, remove it from the Queue and maybe schedule |
| 141 // another one. | 141 // another one. |
| 142 if (status == Offliner::RequestStatus::SAVED) { | 142 if (status == Offliner::RequestStatus::SAVED) { |
| 143 queue_->RemoveRequest(request.request_id(), | 143 queue_->RemoveRequest(request.request_id(), |
| 144 base::Bind(&RequestCoordinator::UpdateRequestCallback, | 144 base::Bind(&RequestCoordinator::UpdateRequestCallback, |
| 145 weak_ptr_factory_.GetWeakPtr())); | 145 weak_ptr_factory_.GetWeakPtr())); |
| 146 | 146 |
| 147 // TODO(petewil): Check time budget. Return to the scheduler if we are out. | 147 // TODO(petewil): Check time budget. Return to the scheduler if we are out. |
| 148 | 148 |
| 149 // Start another request if we have time. | 149 // Start another request if we have time. |
| 150 TryNextRequest(); | 150 TryNextRequest(); |
| 151 } | 151 } |
| 152 } | 152 } |
| 153 | 153 |
| 154 } // namespace offline_pages | 154 } // namespace offline_pages |
| OLD | NEW |