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