| 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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 | 52 |
| 53 ~Core() final { | 53 ~Core() final { |
| 54 for (const auto& pair : downloads_) | 54 for (const auto& pair : downloads_) |
| 55 pair.first->RemoveObserver(this); | 55 pair.first->RemoveObserver(this); |
| 56 } | 56 } |
| 57 | 57 |
| 58 // Returns a weak pointer that can be used to talk to |this|. | 58 // Returns a weak pointer that can be used to talk to |this|. |
| 59 base::WeakPtr<Core> GetWeakPtr() { return weak_ptr_factory_.GetWeakPtr(); } | 59 base::WeakPtr<Core> GetWeakPtr() { return weak_ptr_factory_.GetWeakPtr(); } |
| 60 | 60 |
| 61 // Starts fetching the |request| with the download manager. | 61 // Starts fetching the |request| with the download manager. |
| 62 void StartRequest(scoped_refptr<BackgroundFetchRequestInfo> request) { | 62 void StartRequest( |
| 63 scoped_refptr<BackgroundFetchRequestInfo> request, |
| 64 const net::NetworkTrafficAnnotationTag& traffic_annotation) { |
| 63 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 65 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 64 DCHECK(request_context_); | 66 DCHECK(request_context_); |
| 65 DCHECK(request); | 67 DCHECK(request); |
| 66 | 68 |
| 67 DownloadManager* download_manager = | 69 DownloadManager* download_manager = |
| 68 BrowserContext::GetDownloadManager(browser_context_); | 70 BrowserContext::GetDownloadManager(browser_context_); |
| 69 DCHECK(download_manager); | 71 DCHECK(download_manager); |
| 70 | 72 |
| 71 const ServiceWorkerFetchRequest& fetch_request = request->fetch_request(); | 73 const ServiceWorkerFetchRequest& fetch_request = request->fetch_request(); |
| 72 | 74 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 101 if (base::android::GetDownloadInternalDirectory(&download_directory)) { | 103 if (base::android::GetDownloadInternalDirectory(&download_directory)) { |
| 102 download_parameters->set_file_path(download_directory.Append( | 104 download_parameters->set_file_path(download_directory.Append( |
| 103 std::string(kBackgroundFetchFilePrefix) + base::GenerateGUID())); | 105 std::string(kBackgroundFetchFilePrefix) + base::GenerateGUID())); |
| 104 } | 106 } |
| 105 #endif // defined(OS_ANDROID) | 107 #endif // defined(OS_ANDROID) |
| 106 | 108 |
| 107 download_parameters->set_callback(base::Bind(&Core::DidStartRequest, | 109 download_parameters->set_callback(base::Bind(&Core::DidStartRequest, |
| 108 weak_ptr_factory_.GetWeakPtr(), | 110 weak_ptr_factory_.GetWeakPtr(), |
| 109 std::move(request))); | 111 std::move(request))); |
| 110 | 112 |
| 111 download_manager->DownloadUrl(std::move(download_parameters)); | 113 download_manager->DownloadUrl(std::move(download_parameters), |
| 114 traffic_annotation); |
| 112 } | 115 } |
| 113 | 116 |
| 114 // DownloadItem::Observer overrides: | 117 // DownloadItem::Observer overrides: |
| 115 void OnDownloadUpdated(DownloadItem* download_item) override { | 118 void OnDownloadUpdated(DownloadItem* download_item) override { |
| 116 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 119 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 117 | 120 |
| 118 auto iter = downloads_.find(download_item); | 121 auto iter = downloads_.find(download_item); |
| 119 DCHECK(iter != downloads_.end()); | 122 DCHECK(iter != downloads_.end()); |
| 120 | 123 |
| 121 scoped_refptr<BackgroundFetchRequestInfo> request = iter->second; | 124 scoped_refptr<BackgroundFetchRequestInfo> request = iter->second; |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 ui_core_.reset(new Core(weak_ptr_factory_.GetWeakPtr(), registration_id, | 232 ui_core_.reset(new Core(weak_ptr_factory_.GetWeakPtr(), registration_id, |
| 230 browser_context, std::move(request_context))); | 233 browser_context, std::move(request_context))); |
| 231 | 234 |
| 232 // Get a WeakPtr over which we can talk to the |ui_core_|. | 235 // Get a WeakPtr over which we can talk to the |ui_core_|. |
| 233 ui_core_ptr_ = ui_core_->GetWeakPtr(); | 236 ui_core_ptr_ = ui_core_->GetWeakPtr(); |
| 234 } | 237 } |
| 235 | 238 |
| 236 BackgroundFetchJobController::~BackgroundFetchJobController() = default; | 239 BackgroundFetchJobController::~BackgroundFetchJobController() = default; |
| 237 | 240 |
| 238 void BackgroundFetchJobController::Start( | 241 void BackgroundFetchJobController::Start( |
| 239 std::vector<scoped_refptr<BackgroundFetchRequestInfo>> initial_requests) { | 242 std::vector<scoped_refptr<BackgroundFetchRequestInfo>> initial_requests, |
| 243 const net::NetworkTrafficAnnotationTag& traffic_annotation) { |
| 240 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 244 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 241 DCHECK_LE(initial_requests.size(), kMaximumBackgroundFetchParallelRequests); | 245 DCHECK_LE(initial_requests.size(), kMaximumBackgroundFetchParallelRequests); |
| 242 DCHECK_EQ(state_, State::INITIALIZED); | 246 DCHECK_EQ(state_, State::INITIALIZED); |
| 243 | 247 |
| 244 state_ = State::FETCHING; | 248 state_ = State::FETCHING; |
| 245 | 249 |
| 246 for (const auto& request : initial_requests) | 250 for (const auto& request : initial_requests) |
| 247 StartRequest(request); | 251 StartRequest(request, traffic_annotation); |
| 248 } | 252 } |
| 249 | 253 |
| 250 void BackgroundFetchJobController::StartRequest( | 254 void BackgroundFetchJobController::StartRequest( |
| 251 scoped_refptr<BackgroundFetchRequestInfo> request) { | 255 scoped_refptr<BackgroundFetchRequestInfo> request, |
| 256 const net::NetworkTrafficAnnotationTag& traffic_annotation) { |
| 252 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 257 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 253 DCHECK_EQ(state_, State::FETCHING); | 258 DCHECK_EQ(state_, State::FETCHING); |
| 254 BrowserThread::PostTask( | 259 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 255 BrowserThread::UI, FROM_HERE, | 260 base::Bind(&Core::StartRequest, ui_core_ptr_, |
| 256 base::Bind(&Core::StartRequest, ui_core_ptr_, std::move(request))); | 261 std::move(request), traffic_annotation)); |
| 257 } | 262 } |
| 258 | 263 |
| 259 void BackgroundFetchJobController::DidStartRequest( | 264 void BackgroundFetchJobController::DidStartRequest( |
| 260 scoped_refptr<BackgroundFetchRequestInfo> request, | 265 scoped_refptr<BackgroundFetchRequestInfo> request, |
| 261 const std::string& download_guid) { | 266 const std::string& download_guid) { |
| 262 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 267 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 263 data_manager_->MarkRequestAsStarted(registration_id_, request.get(), | 268 data_manager_->MarkRequestAsStarted(registration_id_, request.get(), |
| 264 download_guid); | 269 download_guid); |
| 265 } | 270 } |
| 266 | 271 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 278 weak_ptr_factory_.GetWeakPtr())); | 283 weak_ptr_factory_.GetWeakPtr())); |
| 279 } | 284 } |
| 280 | 285 |
| 281 void BackgroundFetchJobController::DidGetNextRequest( | 286 void BackgroundFetchJobController::DidGetNextRequest( |
| 282 scoped_refptr<BackgroundFetchRequestInfo> request) { | 287 scoped_refptr<BackgroundFetchRequestInfo> request) { |
| 283 DCHECK_LE(pending_completed_file_acknowledgements_, 1); | 288 DCHECK_LE(pending_completed_file_acknowledgements_, 1); |
| 284 pending_completed_file_acknowledgements_--; | 289 pending_completed_file_acknowledgements_--; |
| 285 | 290 |
| 286 // If a |request| has been given, start downloading the file and bail. | 291 // If a |request| has been given, start downloading the file and bail. |
| 287 if (request) { | 292 if (request) { |
| 288 StartRequest(std::move(request)); | 293 StartRequest(std::move(request), NO_TRAFFIC_ANNOTATION_YET); |
| 289 return; | 294 return; |
| 290 } | 295 } |
| 291 | 296 |
| 292 // If there are outstanding completed file acknowlegements, bail as well. | 297 // If there are outstanding completed file acknowlegements, bail as well. |
| 293 if (pending_completed_file_acknowledgements_ > 0) | 298 if (pending_completed_file_acknowledgements_ > 0) |
| 294 return; | 299 return; |
| 295 | 300 |
| 296 state_ = State::COMPLETED; | 301 state_ = State::COMPLETED; |
| 297 | 302 |
| 298 // Otherwise the job this controller is responsible for has completed. | 303 // Otherwise the job this controller is responsible for has completed. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 310 | 315 |
| 311 // TODO(harkness): Abort all in-progress downloads. | 316 // TODO(harkness): Abort all in-progress downloads. |
| 312 | 317 |
| 313 state_ = State::ABORTED; | 318 state_ = State::ABORTED; |
| 314 | 319 |
| 315 // Inform the owner of the controller about the job having completed. | 320 // Inform the owner of the controller about the job having completed. |
| 316 std::move(completed_callback_).Run(this); | 321 std::move(completed_callback_).Run(this); |
| 317 } | 322 } |
| 318 | 323 |
| 319 } // namespace content | 324 } // namespace content |
| OLD | NEW |