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 base::OnceClosure completed_closure) |
25 : browser_context_(browser_context), | 26 : browser_context_(browser_context), |
26 storage_partition_(storage_partition), | 27 storage_partition_(storage_partition), |
27 job_data_(std::move(job_data)), | 28 job_data_(std::move(job_data)), |
| 29 completed_closure_(std::move(completed_closure)), |
28 weak_ptr_factory_(this) {} | 30 weak_ptr_factory_(this) {} |
29 | 31 |
30 BackgroundFetchJobController::~BackgroundFetchJobController() = default; | 32 BackgroundFetchJobController::~BackgroundFetchJobController() = default; |
31 | 33 |
32 void BackgroundFetchJobController::Shutdown() { | 34 void BackgroundFetchJobController::Shutdown() { |
33 DownloadManager* manager = | 35 DownloadManager* manager = |
34 BrowserContext::GetDownloadManager(browser_context_); | 36 BrowserContext::GetDownloadManager(browser_context_); |
35 // For any downloads in progress, remove the observer. | 37 // For any downloads in progress, remove the observer. |
36 for (auto& guid_map_entry : download_guid_map_) { | 38 for (auto& guid_map_entry : download_guid_map_) { |
37 DownloadItem* item = manager->GetDownloadByGuid(guid_map_entry.first); | 39 DownloadItem* item = manager->GetDownloadByGuid(guid_map_entry.first); |
(...skipping 21 matching lines...) Expand all Loading... |
59 | 61 |
60 void BackgroundFetchJobController::DownloadStarted( | 62 void BackgroundFetchJobController::DownloadStarted( |
61 const std::string& request_guid, | 63 const std::string& request_guid, |
62 DownloadItem* item, | 64 DownloadItem* item, |
63 DownloadInterruptReason reason) { | 65 DownloadInterruptReason reason) { |
64 // Start observing the DownloadItem. No need to store the pointer because it | 66 // Start observing the DownloadItem. No need to store the pointer because it |
65 // can be retrieved from the DownloadManager. | 67 // can be retrieved from the DownloadManager. |
66 item->AddObserver(this); | 68 item->AddObserver(this); |
67 download_guid_map_[item->GetGuid()] = request_guid; | 69 download_guid_map_[item->GetGuid()] = request_guid; |
68 job_data_->SetRequestDownloadGuid(request_guid, item->GetGuid()); | 70 job_data_->SetRequestDownloadGuid(request_guid, item->GetGuid()); |
| 71 |
| 72 // TODO(harkness): If DownloadStarted is called with a real interrupt reason, |
| 73 // record that and don't mark it as in-progress. |
69 } | 74 } |
70 | 75 |
71 void BackgroundFetchJobController::OnDownloadUpdated(DownloadItem* item) { | 76 void BackgroundFetchJobController::OnDownloadUpdated(DownloadItem* item) { |
72 DCHECK(download_guid_map_.find(item->GetGuid()) != download_guid_map_.end()); | 77 DCHECK(download_guid_map_.find(item->GetGuid()) != download_guid_map_.end()); |
73 | 78 |
74 bool requests_remaining = false; | 79 // Update the state of the request on the JobData. If the state is a final |
| 80 // state, this will also update the complete status of the JobData. |
| 81 bool requests_remaining = job_data_->UpdateBackgroundFetchRequestState( |
| 82 download_guid_map_[item->GetGuid()], item->GetState(), |
| 83 item->GetLastReason()); |
| 84 |
75 switch (item->GetState()) { | 85 switch (item->GetState()) { |
76 case DownloadItem::DownloadState::CANCELLED: | 86 case DownloadItem::DownloadState::CANCELLED: |
77 // TODO(harkness): Expand the "complete" flag on the RequestInfo to | |
78 // include error states. | |
79 case DownloadItem::DownloadState::COMPLETE: | 87 case DownloadItem::DownloadState::COMPLETE: |
80 requests_remaining = job_data_->BackgroundFetchRequestInfoComplete( | |
81 download_guid_map_[item->GetGuid()]); | |
82 // TODO(harkness): Tell the notification service to update the download | 88 // TODO(harkness): Tell the notification service to update the download |
83 // notification. | 89 // notification. |
84 | 90 |
85 if (requests_remaining) { | 91 if (requests_remaining) { |
86 ProcessRequest(job_data_->GetNextBackgroundFetchRequestInfo()); | 92 ProcessRequest(job_data_->GetNextBackgroundFetchRequestInfo()); |
87 } else if (job_data_->IsComplete()) { | 93 } else if (job_data_->IsComplete()) { |
88 // TODO(harkness): Return the data to the context. | 94 std::move(completed_closure_).Run(); |
89 } | 95 } |
90 break; | 96 break; |
91 case DownloadItem::DownloadState::INTERRUPTED: | 97 case DownloadItem::DownloadState::INTERRUPTED: |
92 // TODO(harkness): Just update the notification that it is paused. | 98 // TODO(harkness): Just update the notification that it is paused. |
93 break; | 99 break; |
94 case DownloadItem::DownloadState::IN_PROGRESS: | 100 case DownloadItem::DownloadState::IN_PROGRESS: |
95 // TODO(harkness): If the download was previously paused, this should now | 101 // TODO(harkness): If the download was previously paused, this should now |
96 // unpause the notification. | 102 // unpause the notification. |
97 break; | 103 break; |
98 case DownloadItem::DownloadState::MAX_DOWNLOAD_STATE: | 104 case DownloadItem::DownloadState::MAX_DOWNLOAD_STATE: |
(...skipping 18 matching lines...) Expand all Loading... |
117 fetch_request.url(), storage_partition_->GetURLRequestContext())); | 123 fetch_request.url(), storage_partition_->GetURLRequestContext())); |
118 params->set_callback( | 124 params->set_callback( |
119 base::Bind(&BackgroundFetchJobController::DownloadStarted, | 125 base::Bind(&BackgroundFetchJobController::DownloadStarted, |
120 weak_ptr_factory_.GetWeakPtr(), fetch_request.guid())); | 126 weak_ptr_factory_.GetWeakPtr(), fetch_request.guid())); |
121 | 127 |
122 BrowserContext::GetDownloadManager(browser_context_) | 128 BrowserContext::GetDownloadManager(browser_context_) |
123 ->DownloadUrl(std::move(params)); | 129 ->DownloadUrl(std::move(params)); |
124 } | 130 } |
125 | 131 |
126 } // namespace content | 132 } // namespace content |
OLD | NEW |