Chromium Code Reviews| 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_job_data.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 std::unique_ptr<BackgroundFetchJobData> job_data) |
| 25 : browser_context_(browser_context), | 25 : browser_context_(browser_context), |
| 26 storage_partition_(storage_partition), | 26 storage_partition_(storage_partition), |
| 27 job_data_(std::move(job_data)) {} | 27 job_data_(std::move(job_data)), |
| 28 weak_ptr_factory_(this) {} | |
| 28 | 29 |
| 29 BackgroundFetchJobController::~BackgroundFetchJobController() = default; | 30 BackgroundFetchJobController::~BackgroundFetchJobController() {} |
|
Peter Beverloo
2017/03/10 13:40:52
nit: revert
harkness
2017/03/10 14:21:00
oops, done.
| |
| 30 | 31 |
| 31 void BackgroundFetchJobController::Shutdown() { | 32 void BackgroundFetchJobController::Shutdown() { |
| 32 // TODO(harkness): Remove any observers. | |
| 33 // TODO(harkness): Write final status to the DataManager. After this call, the | 33 // TODO(harkness): Write final status to the DataManager. After this call, the |
| 34 // |job_data_| is no longer valid. | 34 // |job_data_| is no longer valid. |
| 35 job_data_.reset(nullptr); | 35 StopObservations(); |
|
Peter Beverloo
2017/03/10 13:40:52
nit: consider inlining
harkness
2017/03/10 14:21:00
Done.
| |
| 36 job_data_.reset(); | |
| 36 } | 37 } |
| 37 | 38 |
| 38 void BackgroundFetchJobController::StartProcessing() { | 39 void BackgroundFetchJobController::StartProcessing() { |
| 39 DCHECK(job_data_); | 40 DCHECK(job_data_); |
| 40 | 41 |
| 41 const BackgroundFetchRequestInfo& fetch_request = | 42 const BackgroundFetchRequestInfo& fetch_request = |
| 42 job_data_->GetNextBackgroundFetchRequestInfo(); | 43 job_data_->GetNextBackgroundFetchRequestInfo(); |
| 43 ProcessRequest(fetch_request); | 44 ProcessRequest(fetch_request); |
| 44 | 45 |
| 45 // Currently this only supports jobs of size 1. | 46 // Currently, this processes a single request at a time. |
| 46 // TODO(crbug.com/692544) | 47 } |
| 47 DCHECK(!job_data_->HasRequestsRemaining()); | 48 |
| 49 void BackgroundFetchJobController::DownloadStarted( | |
| 50 const std::string& request_guid, | |
| 51 DownloadItem* item, | |
| 52 DownloadInterruptReason reason) { | |
| 53 // Start observing the DownloadItem. No need to store the pointer because it | |
| 54 // can be retrieved from the DownloadManager. | |
| 55 item->AddObserver(this); | |
| 56 download_guid_map_[item->GetGuid()] = request_guid; | |
| 57 job_data_->SetRequestDownloadGuid(request_guid, item->GetGuid()); | |
| 58 } | |
| 59 | |
| 60 void BackgroundFetchJobController::OnDownloadUpdated(DownloadItem* item) { | |
| 61 DCHECK(download_guid_map_.find(item->GetGuid()) != download_guid_map_.end()); | |
| 62 | |
| 63 bool requests_remaining = false; | |
| 64 switch (item->GetState()) { | |
| 65 case DownloadItem::DownloadState::CANCELLED: | |
| 66 // TODO(harkness): Expand the "complete" flag on the RequestInfo to | |
| 67 // include error states. | |
| 68 case DownloadItem::DownloadState::COMPLETE: | |
| 69 requests_remaining = job_data_->BackgroundFetchRequestInfoComplete( | |
| 70 download_guid_map_[item->GetGuid()]); | |
| 71 // TODO(harkness): Tell the notification service to update the download | |
| 72 // notification. | |
| 73 | |
| 74 if (requests_remaining) { | |
| 75 ProcessRequest(job_data_->GetNextBackgroundFetchRequestInfo()); | |
| 76 } else if (job_data_->IsComplete()) { | |
| 77 // TODO(harkness): Return the data to the context. | |
| 78 } | |
| 79 break; | |
| 80 case DownloadItem::DownloadState::INTERRUPTED: | |
| 81 // TODO(harkness): Just update the notification that it is paused. | |
| 82 break; | |
| 83 case DownloadItem::DownloadState::IN_PROGRESS: | |
| 84 // TODO(harkness): If the download was previously paused, this should now | |
| 85 // unpause the notification. | |
| 86 break; | |
| 87 case DownloadItem::DownloadState::MAX_DOWNLOAD_STATE: | |
| 88 // TODO(harkness): Consider how we want to handle errors here. | |
| 89 break; | |
| 90 } | |
| 91 } | |
| 92 | |
| 93 void BackgroundFetchJobController::OnDownloadDestroyed(DownloadItem* item) { | |
| 94 // This can be called as part of Shutdown. Make sure the observers are | |
| 95 // removed. | |
| 96 item->RemoveObserver(this); | |
| 97 } | |
| 98 | |
| 99 void BackgroundFetchJobController::StopObservations() { | |
| 100 DownloadManager* manager = | |
| 101 BrowserContext::GetDownloadManager(browser_context_); | |
| 102 // For any downloads in progress, remove the observer. | |
| 103 for (auto& guid_map_entry : download_guid_map_) { | |
| 104 DownloadItem* item = manager->GetDownloadByGuid(guid_map_entry.first); | |
| 105 if (item) | |
|
Peter Beverloo
2017/03/10 13:40:52
When would this be NULL?
harkness
2017/03/10 14:21:00
It really shouldn't be unless there's a coding mis
Peter Beverloo
2017/03/10 14:25:11
If it only happens when *we* make a coding mistake
harkness
2017/03/11 20:38:49
Technically, the download_manager.h interface does
| |
| 106 item->RemoveObserver(this); | |
| 107 } | |
| 108 download_guid_map_.clear(); | |
| 48 } | 109 } |
| 49 | 110 |
| 50 void BackgroundFetchJobController::ProcessRequest( | 111 void BackgroundFetchJobController::ProcessRequest( |
| 51 const BackgroundFetchRequestInfo& fetch_request) { | 112 const BackgroundFetchRequestInfo& fetch_request) { |
| 52 // TODO(harkness): Start the observer watching for this download. | |
| 53 // TODO(harkness): Check if the download is already in progress or completed. | 113 // TODO(harkness): Check if the download is already in progress or completed. |
| 54 | 114 |
| 55 // Send the fetch request to the DownloadManager. | 115 // Set up the download parameters and the OnStartedCallback. |
| 56 std::unique_ptr<DownloadUrlParameters> params( | 116 std::unique_ptr<DownloadUrlParameters> params( |
| 57 base::MakeUnique<DownloadUrlParameters>( | 117 base::MakeUnique<DownloadUrlParameters>( |
| 58 fetch_request.url(), storage_partition_->GetURLRequestContext())); | 118 fetch_request.url(), storage_partition_->GetURLRequestContext())); |
| 59 // TODO(harkness): Currently this is the only place the browser_context is | 119 params->set_callback( |
| 60 // used. Evaluate whether we can just pass in the download manager explicitly. | 120 base::Bind(&BackgroundFetchJobController::DownloadStarted, |
| 121 weak_ptr_factory_.GetWeakPtr(), fetch_request.guid())); | |
| 122 | |
| 61 BrowserContext::GetDownloadManager(browser_context_) | 123 BrowserContext::GetDownloadManager(browser_context_) |
| 62 ->DownloadUrl(std::move(params)); | 124 ->DownloadUrl(std::move(params)); |
| 63 } | 125 } |
| 64 | 126 |
| 65 } // namespace content | 127 } // namespace content |
| OLD | NEW |