| 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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 is_canceled_(false), | 53 is_canceled_(false), |
| 54 offliner_timeout_(base::TimeDelta::FromSeconds(OFFLINER_TIMEOUT_SECONDS)), | 54 offliner_timeout_(base::TimeDelta::FromSeconds(OFFLINER_TIMEOUT_SECONDS)), |
| 55 offliner_(nullptr), | 55 offliner_(nullptr), |
| 56 policy_(std::move(policy)), | 56 policy_(std::move(policy)), |
| 57 factory_(std::move(factory)), | 57 factory_(std::move(factory)), |
| 58 queue_(std::move(queue)), | 58 queue_(std::move(queue)), |
| 59 scheduler_(std::move(scheduler)), | 59 scheduler_(std::move(scheduler)), |
| 60 last_offlining_status_(Offliner::RequestStatus::UNKNOWN), | 60 last_offlining_status_(Offliner::RequestStatus::UNKNOWN), |
| 61 weak_ptr_factory_(this) { | 61 weak_ptr_factory_(this) { |
| 62 DCHECK(policy_ != nullptr); | 62 DCHECK(policy_ != nullptr); |
| 63 picker_.reset(new RequestPicker(queue_.get())); | 63 picker_.reset(new RequestPicker(queue_.get(), policy.get())); |
| 64 } | 64 } |
| 65 | 65 |
| 66 RequestCoordinator::~RequestCoordinator() {} | 66 RequestCoordinator::~RequestCoordinator() {} |
| 67 | 67 |
| 68 bool RequestCoordinator::SavePageLater( | 68 bool RequestCoordinator::SavePageLater( |
| 69 const GURL& url, const ClientId& client_id) { | 69 const GURL& url, const ClientId& client_id, bool was_user_requested) { |
| 70 DVLOG(2) << "URL is " << url << " " << __FUNCTION__; | 70 DVLOG(2) << "URL is " << url << " " << __FUNCTION__; |
| 71 | 71 |
| 72 // TODO(petewil): We need a robust scheme for allocating new IDs. | 72 // TODO(petewil): We need a robust scheme for allocating new IDs. |
| 73 static int64_t id = 0; | 73 static int64_t id = 0; |
| 74 | 74 |
| 75 // Build a SavePageRequest. | 75 // Build a SavePageRequest. |
| 76 // TODO(petewil): Use something like base::Clock to help in testing. | 76 // TODO(petewil): Use something like base::Clock to help in testing. |
| 77 offline_pages::SavePageRequest request( | 77 offline_pages::SavePageRequest request( |
| 78 id++, url, client_id, base::Time::Now()); | 78 id++, url, client_id, base::Time::Now(), was_user_requested); |
| 79 | 79 |
| 80 // Put the request on the request queue. | 80 // Put the request on the request queue. |
| 81 queue_->AddRequest(request, | 81 queue_->AddRequest(request, |
| 82 base::Bind(&RequestCoordinator::AddRequestResultCallback, | 82 base::Bind(&RequestCoordinator::AddRequestResultCallback, |
| 83 weak_ptr_factory_.GetWeakPtr())); | 83 weak_ptr_factory_.GetWeakPtr())); |
| 84 return true; | 84 return true; |
| 85 } | 85 } |
| 86 | 86 |
| 87 void RequestCoordinator::AddRequestResultCallback( | 87 void RequestCoordinator::AddRequestResultCallback( |
| 88 RequestQueue::AddRequestResult result, | 88 RequestQueue::AddRequestResult result, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 109 | 109 |
| 110 // Let the scheduler know we are done processing. | 110 // Let the scheduler know we are done processing. |
| 111 scheduler_callback_.Run(true); | 111 scheduler_callback_.Run(true); |
| 112 } | 112 } |
| 113 | 113 |
| 114 // Returns true if the caller should expect a callback, false otherwise. For | 114 // Returns true if the caller should expect a callback, false otherwise. For |
| 115 // instance, this would return false if a request is already in progress. | 115 // instance, this would return false if a request is already in progress. |
| 116 bool RequestCoordinator::StartProcessing( | 116 bool RequestCoordinator::StartProcessing( |
| 117 const DeviceConditions& device_conditions, | 117 const DeviceConditions& device_conditions, |
| 118 const base::Callback<void(bool)>& callback) { | 118 const base::Callback<void(bool)>& callback) { |
| 119 current_conditions_.reset(new DeviceConditions(device_conditions)); |
| 119 if (is_busy_) return false; | 120 if (is_busy_) return false; |
| 120 | 121 |
| 121 is_canceled_ = false; | 122 is_canceled_ = false; |
| 122 scheduler_callback_ = callback; | 123 scheduler_callback_ = callback; |
| 123 // TODO(petewil): Check existing conditions (should be passed down from | 124 // TODO(petewil): Check existing conditions (should be passed down from |
| 124 // BackgroundTask) | 125 // BackgroundTask) |
| 125 | 126 |
| 126 TryNextRequest(); | 127 TryNextRequest(); |
| 127 | 128 |
| 128 return true; | 129 return true; |
| 129 } | 130 } |
| 130 | 131 |
| 131 void RequestCoordinator::TryNextRequest() { | 132 void RequestCoordinator::TryNextRequest() { |
| 132 // Choose a request to process that meets the available conditions. | 133 // Choose a request to process that meets the available conditions. |
| 133 // This is an async call, and returns right away. | 134 // This is an async call, and returns right away. |
| 134 picker_->ChooseNextRequest( | 135 picker_->ChooseNextRequest( |
| 135 base::Bind(&RequestCoordinator::RequestPicked, | 136 base::Bind(&RequestCoordinator::RequestPicked, |
| 136 weak_ptr_factory_.GetWeakPtr()), | 137 weak_ptr_factory_.GetWeakPtr()), |
| 137 base::Bind(&RequestCoordinator::RequestQueueEmpty, | 138 base::Bind(&RequestCoordinator::RequestQueueEmpty, |
| 138 weak_ptr_factory_.GetWeakPtr())); | 139 weak_ptr_factory_.GetWeakPtr()), |
| 140 current_conditions_.get()); |
| 139 } | 141 } |
| 140 | 142 |
| 141 // Called by the request picker when a request has been picked. | 143 // Called by the request picker when a request has been picked. |
| 142 void RequestCoordinator::RequestPicked(const SavePageRequest& request) { | 144 void RequestCoordinator::RequestPicked(const SavePageRequest& request) { |
| 143 // Send the request on to the offliner. | 145 // Send the request on to the offliner. |
| 144 SendRequestToOffliner(request); | 146 SendRequestToOffliner(request); |
| 145 } | 147 } |
| 146 | 148 |
| 147 void RequestCoordinator::RequestQueueEmpty() { | 149 void RequestCoordinator::RequestQueueEmpty() { |
| 148 // Clear the outstanding "safety" task in the scheduler. | 150 // Clear the outstanding "safety" task in the scheduler. |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 return kUserRequestTriggerConditions; | 215 return kUserRequestTriggerConditions; |
| 214 } | 216 } |
| 215 | 217 |
| 216 void RequestCoordinator::GetOffliner() { | 218 void RequestCoordinator::GetOffliner() { |
| 217 if (!offliner_) { | 219 if (!offliner_) { |
| 218 offliner_ = factory_->GetOffliner(policy_.get()); | 220 offliner_ = factory_->GetOffliner(policy_.get()); |
| 219 } | 221 } |
| 220 } | 222 } |
| 221 | 223 |
| 222 } // namespace offline_pages | 224 } // namespace offline_pages |
| OLD | NEW |