OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "content/browser/background_fetch/background_fetch_job_controller.h" | 5 #include "content/browser/background_fetch/background_fetch_job_controller.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
253 // be unsafe to obtain a weak pointer on the IO thread from a factory that | 253 // be unsafe to obtain a weak pointer on the IO thread from a factory that |
254 // lives on the UI thread, but it's ok in this constructor since the Core | 254 // lives on the UI thread, but it's ok in this constructor since the Core |
255 // can't be destroyed before this constructor finishes. | 255 // can't be destroyed before this constructor finishes. |
256 ui_core_ptr_ = ui_core_->GetWeakPtrOnUI(); | 256 ui_core_ptr_ = ui_core_->GetWeakPtrOnUI(); |
257 } | 257 } |
258 | 258 |
259 BackgroundFetchJobController::~BackgroundFetchJobController() { | 259 BackgroundFetchJobController::~BackgroundFetchJobController() { |
260 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 260 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
261 }; | 261 }; |
262 | 262 |
263 void BackgroundFetchJobController::Start( | 263 void BackgroundFetchJobController::Start() { |
264 std::vector<scoped_refptr<BackgroundFetchRequestInfo>> initial_requests) { | |
265 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 264 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
266 DCHECK_LE(initial_requests.size(), kMaximumBackgroundFetchParallelRequests); | |
267 DCHECK_EQ(state_, State::INITIALIZED); | 265 DCHECK_EQ(state_, State::INITIALIZED); |
268 | 266 |
269 state_ = State::FETCHING; | 267 state_ = State::FETCHING; |
270 | 268 |
271 for (const auto& request : initial_requests) | 269 // TODO(johnme): Enforce kMaximumBackgroundFetchParallelRequests globally |
272 StartRequest(request); | 270 // and/or per origin rather than per fetch. |
Peter Beverloo
2017/07/11 16:10:55
Please file a bug for this - scheduling will be a
johnme
2017/07/12 13:13:10
Done.
| |
271 for (size_t i = 0; i < kMaximumBackgroundFetchParallelRequests; i++) { | |
272 data_manager_->ActivateNextRequest( | |
273 registration_id_, | |
274 base::BindOnce(&BackgroundFetchJobController::StartRequest, | |
275 weak_ptr_factory_.GetWeakPtr())); | |
276 } | |
273 } | 277 } |
274 | 278 |
275 void BackgroundFetchJobController::StartRequest( | 279 void BackgroundFetchJobController::StartRequest( |
276 scoped_refptr<BackgroundFetchRequestInfo> request) { | 280 scoped_refptr<BackgroundFetchRequestInfo> request) { |
277 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 281 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
278 DCHECK_EQ(state_, State::FETCHING); | 282 DCHECK_EQ(state_, State::FETCHING); |
283 if (!request) | |
284 return; // No more pending requests to start. | |
Peter Beverloo
2017/07/11 16:10:55
Please document that this bail-out is only applica
johnme
2017/07/12 13:13:10
Done.
| |
279 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 285 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
280 base::Bind(&Core::StartRequest, ui_core_ptr_, | 286 base::Bind(&Core::StartRequest, ui_core_ptr_, |
281 std::move(request), traffic_annotation_)); | 287 std::move(request), traffic_annotation_)); |
282 } | 288 } |
283 | 289 |
284 void BackgroundFetchJobController::DidStartRequest( | 290 void BackgroundFetchJobController::DidStartRequest( |
285 scoped_refptr<BackgroundFetchRequestInfo> request, | 291 scoped_refptr<BackgroundFetchRequestInfo> request, |
286 const std::string& download_guid) { | 292 const std::string& download_guid) { |
287 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 293 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
288 data_manager_->MarkRequestAsStarted(registration_id_, request.get(), | 294 data_manager_->MarkRequestAsStarted(registration_id_, request.get(), |
289 download_guid); | 295 download_guid); |
290 } | 296 } |
291 | 297 |
292 void BackgroundFetchJobController::DidCompleteRequest( | 298 void BackgroundFetchJobController::DidCompleteRequest( |
293 scoped_refptr<BackgroundFetchRequestInfo> request) { | 299 scoped_refptr<BackgroundFetchRequestInfo> request) { |
294 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 300 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
295 | 301 |
296 // The DataManager must acknowledge that it stored the data and that there are | 302 // The DataManager must acknowledge that it stored the data and that there are |
297 // no more pending requests to avoid marking this job as completed too early. | 303 // no more pending requests to avoid marking this job as completed too early. |
298 pending_completed_file_acknowledgements_++; | 304 pending_completed_file_acknowledgements_++; |
299 | 305 |
300 data_manager_->MarkRequestAsCompleteAndGetNextRequest( | 306 data_manager_->MarkRequestAsCompleteAndActivateNextRequest( |
Peter Beverloo
2017/07/11 16:10:55
What about splitting up MarkRequestAsComplete so t
johnme
2017/07/12 13:13:10
Done (yeah, I'd been considering that; initially I
| |
301 registration_id_, request.get(), | 307 registration_id_, request.get(), |
302 base::BindOnce(&BackgroundFetchJobController::DidGetNextRequest, | 308 base::BindOnce(&BackgroundFetchJobController::DidGetNextRequest, |
303 weak_ptr_factory_.GetWeakPtr())); | 309 weak_ptr_factory_.GetWeakPtr())); |
304 } | 310 } |
305 | 311 |
306 void BackgroundFetchJobController::DidGetNextRequest( | 312 void BackgroundFetchJobController::DidGetNextRequest( |
307 scoped_refptr<BackgroundFetchRequestInfo> request) { | 313 scoped_refptr<BackgroundFetchRequestInfo> request) { |
308 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 314 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
309 DCHECK_LE(pending_completed_file_acknowledgements_, 1); | 315 DCHECK_LE(pending_completed_file_acknowledgements_, 1); |
310 pending_completed_file_acknowledgements_--; | 316 pending_completed_file_acknowledgements_--; |
(...skipping 25 matching lines...) Expand all Loading... | |
336 | 342 |
337 // TODO(harkness): Abort all in-progress downloads. | 343 // TODO(harkness): Abort all in-progress downloads. |
338 | 344 |
339 state_ = State::ABORTED; | 345 state_ = State::ABORTED; |
340 | 346 |
341 // Inform the owner of the controller about the job having completed. | 347 // Inform the owner of the controller about the job having completed. |
342 std::move(completed_callback_).Run(this); | 348 std::move(completed_callback_).Run(this); |
343 } | 349 } |
344 | 350 |
345 } // namespace content | 351 } // namespace content |
OLD | NEW |