| 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 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "content/browser/background_fetch/background_fetch_job_data.h" | 11 #include "content/browser/background_fetch/background_fetch_data_manager.h" |
| 12 #include "content/browser/background_fetch/background_fetch_request_info.h" | 12 #include "content/browser/background_fetch/background_fetch_request_info.h" |
| 13 #include "content/public/browser/browser_context.h" | 13 #include "content/public/browser/browser_context.h" |
| 14 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 15 #include "content/public/browser/download_manager.h" | 15 #include "content/public/browser/download_manager.h" |
| 16 #include "content/public/browser/storage_partition.h" | 16 #include "content/public/browser/storage_partition.h" |
| 17 | 17 |
| 18 namespace content { | 18 namespace content { |
| 19 | 19 |
| 20 BackgroundFetchJobController::BackgroundFetchJobController( | 20 BackgroundFetchJobController::BackgroundFetchJobController( |
| 21 const std::string& job_guid, | 21 const std::string& job_guid, |
| 22 BrowserContext* browser_context, | 22 BrowserContext* browser_context, |
| 23 StoragePartition* storage_partition, | 23 StoragePartition* storage_partition, |
| 24 std::unique_ptr<BackgroundFetchJobData> job_data, | 24 BackgroundFetchDataManager* data_manager, |
| 25 base::OnceClosure completed_closure) | 25 base::OnceClosure completed_closure) |
| 26 : browser_context_(browser_context), | 26 : job_guid_(job_guid), |
| 27 browser_context_(browser_context), |
| 27 storage_partition_(storage_partition), | 28 storage_partition_(storage_partition), |
| 28 job_data_(std::move(job_data)), | 29 data_manager_(data_manager), |
| 29 completed_closure_(std::move(completed_closure)), | 30 completed_closure_(std::move(completed_closure)), |
| 30 weak_ptr_factory_(this) {} | 31 weak_ptr_factory_(this) {} |
| 31 | 32 |
| 32 BackgroundFetchJobController::~BackgroundFetchJobController() = default; | 33 BackgroundFetchJobController::~BackgroundFetchJobController() = default; |
| 33 | 34 |
| 34 void BackgroundFetchJobController::Shutdown() { | 35 void BackgroundFetchJobController::Shutdown() { |
| 35 DownloadManager* manager = | 36 DownloadManager* manager = |
| 36 BrowserContext::GetDownloadManager(browser_context_); | 37 BrowserContext::GetDownloadManager(browser_context_); |
| 37 // For any downloads in progress, remove the observer. | 38 // For any downloads in progress, remove the observer. |
| 38 for (auto& guid_map_entry : download_guid_map_) { | 39 for (auto& guid_map_entry : download_guid_map_) { |
| 39 DownloadItem* item = manager->GetDownloadByGuid(guid_map_entry.first); | 40 DownloadItem* item = manager->GetDownloadByGuid(guid_map_entry.first); |
| 40 // TODO(harkness): Follow up with DownloadManager team about whether this | 41 // TODO(harkness): Follow up with DownloadManager team about whether this |
| 41 // should be a DCHECK. | 42 // should be a DCHECK. |
| 42 if (item) | 43 if (item) |
| 43 item->RemoveObserver(this); | 44 item->RemoveObserver(this); |
| 44 } | 45 } |
| 45 download_guid_map_.clear(); | 46 download_guid_map_.clear(); |
| 46 | 47 |
| 47 // TODO(harkness): Write final status to the DataManager. After this call, the | 48 // TODO(harkness): Write final status to the DataManager. After this call, the |
| 48 // |job_data_| is no longer valid. | 49 // |data_manager_| is no longer valid. |
| 49 job_data_.reset(); | 50 data_manager_ = nullptr; |
| 50 } | 51 } |
| 51 | 52 |
| 52 void BackgroundFetchJobController::StartProcessing() { | 53 void BackgroundFetchJobController::StartProcessing() { |
| 53 DCHECK(job_data_); | 54 DCHECK(data_manager_); |
| 54 | 55 |
| 55 const BackgroundFetchRequestInfo& fetch_request = | 56 const BackgroundFetchRequestInfo& fetch_request = |
| 56 job_data_->GetNextBackgroundFetchRequestInfo(); | 57 data_manager_->GetNextBackgroundFetchRequestInfo(job_guid_); |
| 57 ProcessRequest(fetch_request); | 58 ProcessRequest(fetch_request); |
| 58 | 59 |
| 59 // Currently, this processes a single request at a time. | 60 // Currently, this processes a single request at a time. |
| 60 } | 61 } |
| 61 | 62 |
| 62 void BackgroundFetchJobController::DownloadStarted( | 63 void BackgroundFetchJobController::DownloadStarted( |
| 63 const std::string& request_guid, | 64 const std::string& request_guid, |
| 64 DownloadItem* item, | 65 DownloadItem* item, |
| 65 DownloadInterruptReason reason) { | 66 DownloadInterruptReason reason) { |
| 66 // Start observing the DownloadItem. No need to store the pointer because it | 67 // Start observing the DownloadItem. No need to store the pointer because it |
| 67 // can be retrieved from the DownloadManager. | 68 // can be retrieved from the DownloadManager. |
| 68 item->AddObserver(this); | 69 item->AddObserver(this); |
| 69 download_guid_map_[item->GetGuid()] = request_guid; | 70 download_guid_map_[item->GetGuid()] = request_guid; |
| 70 job_data_->SetRequestDownloadGuid(request_guid, item->GetGuid()); | 71 data_manager_->UpdateRequestDownloadGuid(job_guid_, request_guid, |
| 72 item->GetGuid()); |
| 71 | 73 |
| 72 // TODO(harkness): If DownloadStarted is called with a real interrupt reason, | 74 // TODO(harkness): If DownloadStarted is called with a real interrupt reason, |
| 73 // record that and don't mark it as in-progress. | 75 // record that and don't mark it as in-progress. |
| 74 } | 76 } |
| 75 | 77 |
| 76 void BackgroundFetchJobController::OnDownloadUpdated(DownloadItem* item) { | 78 void BackgroundFetchJobController::OnDownloadUpdated(DownloadItem* item) { |
| 77 DCHECK(download_guid_map_.find(item->GetGuid()) != download_guid_map_.end()); | 79 DCHECK(download_guid_map_.find(item->GetGuid()) != download_guid_map_.end()); |
| 78 | 80 |
| 79 // Update the state of the request on the JobData. If the state is a final | 81 // Update the state of the request on the DataManager. If the state is a final |
| 80 // state, this will also update the complete status of the JobData. | 82 // state, this will also update the complete status of the JobInfo. |
| 81 bool requests_remaining = job_data_->UpdateBackgroundFetchRequestState( | 83 const std::string& request_guid = download_guid_map_[item->GetGuid()]; |
| 82 download_guid_map_[item->GetGuid()], item->GetState(), | 84 bool requests_remaining = data_manager_->UpdateRequestState( |
| 83 item->GetLastReason()); | 85 job_guid_, request_guid, item->GetState(), item->GetLastReason()); |
| 84 | 86 |
| 85 switch (item->GetState()) { | 87 switch (item->GetState()) { |
| 88 case DownloadItem::DownloadState::COMPLETE: |
| 89 // If the download completed, update the storage location and size. |
| 90 data_manager_->UpdateRequestStorageState(job_guid_, request_guid, |
| 91 item->GetTargetFilePath(), |
| 92 item->GetReceivedBytes()); |
| 93 // Fall through. |
| 86 case DownloadItem::DownloadState::CANCELLED: | 94 case DownloadItem::DownloadState::CANCELLED: |
| 87 case DownloadItem::DownloadState::COMPLETE: | |
| 88 // TODO(harkness): Tell the notification service to update the download | 95 // TODO(harkness): Tell the notification service to update the download |
| 89 // notification. | 96 // notification. |
| 90 | 97 |
| 91 if (requests_remaining) { | 98 if (requests_remaining) { |
| 92 ProcessRequest(job_data_->GetNextBackgroundFetchRequestInfo()); | 99 ProcessRequest( |
| 93 } else if (job_data_->IsComplete()) { | 100 data_manager_->GetNextBackgroundFetchRequestInfo(job_guid_)); |
| 101 } else if (data_manager_->IsComplete(job_guid_)) { |
| 94 std::move(completed_closure_).Run(); | 102 std::move(completed_closure_).Run(); |
| 95 } | 103 } |
| 96 break; | 104 break; |
| 97 case DownloadItem::DownloadState::INTERRUPTED: | 105 case DownloadItem::DownloadState::INTERRUPTED: |
| 98 // TODO(harkness): Just update the notification that it is paused. | 106 // TODO(harkness): Just update the notification that it is paused. |
| 99 break; | 107 break; |
| 100 case DownloadItem::DownloadState::IN_PROGRESS: | 108 case DownloadItem::DownloadState::IN_PROGRESS: |
| 101 // TODO(harkness): If the download was previously paused, this should now | 109 // TODO(harkness): If the download was previously paused, this should now |
| 102 // unpause the notification. | 110 // unpause the notification. |
| 103 break; | 111 break; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 123 fetch_request.url(), storage_partition_->GetURLRequestContext())); | 131 fetch_request.url(), storage_partition_->GetURLRequestContext())); |
| 124 params->set_callback( | 132 params->set_callback( |
| 125 base::Bind(&BackgroundFetchJobController::DownloadStarted, | 133 base::Bind(&BackgroundFetchJobController::DownloadStarted, |
| 126 weak_ptr_factory_.GetWeakPtr(), fetch_request.guid())); | 134 weak_ptr_factory_.GetWeakPtr(), fetch_request.guid())); |
| 127 | 135 |
| 128 BrowserContext::GetDownloadManager(browser_context_) | 136 BrowserContext::GetDownloadManager(browser_context_) |
| 129 ->DownloadUrl(std::move(params)); | 137 ->DownloadUrl(std::move(params)); |
| 130 } | 138 } |
| 131 | 139 |
| 132 } // namespace content | 140 } // namespace content |
| OLD | NEW |