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 BackgroundFetchJobData* job_data) | 24 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_(base::WrapUnique(job_data)) {} | 27 job_data_(base::WrapUnique(job_data)), |
28 weak_ptr_factory_(this) {} | |
28 | 29 |
29 BackgroundFetchJobController::~BackgroundFetchJobController() = default; | 30 BackgroundFetchJobController::~BackgroundFetchJobController() { |
31 StopObservations(); | |
Peter Beverloo
2017/03/08 14:58:19
We should either call this from the destructor, or
harkness
2017/03/10 13:33:53
Done.
| |
32 } | |
30 | 33 |
31 void BackgroundFetchJobController::Shutdown() { | 34 void BackgroundFetchJobController::Shutdown() { |
32 // TODO(harkness): Remove any observers. | |
33 // TODO(harkness): Write final status to the DataManager. After this call, the | 35 // TODO(harkness): Write final status to the DataManager. After this call, the |
34 // |job_data_| is no longer valid. | 36 // |job_data_| is no longer valid. |
37 StopObservations(); | |
35 job_data_.reset(nullptr); | 38 job_data_.reset(nullptr); |
Peter Beverloo
2017/03/08 14:58:19
nit: drop `nullptr`
harkness
2017/03/10 13:33:53
Done.
| |
36 } | 39 } |
37 | 40 |
38 void BackgroundFetchJobController::StartProcessing() { | 41 void BackgroundFetchJobController::StartProcessing() { |
39 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 42 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
40 DCHECK(job_data_); | 43 DCHECK(job_data_); |
41 | 44 |
42 const BackgroundFetchRequestInfo& fetch_request = | 45 const BackgroundFetchRequestInfo& fetch_request = |
43 job_data_->GetNextBackgroundFetchRequestInfo(); | 46 job_data_->GetNextBackgroundFetchRequestInfo(); |
44 ProcessRequest(fetch_request); | 47 ProcessRequest(fetch_request); |
45 | 48 |
46 // Currently this only supports jobs of size 1. | 49 // Currently, this processes a single request at a time. |
47 // TODO(crbug.com/692544) | 50 } |
48 DCHECK(!job_data_->HasRequestsRemaining()); | 51 |
52 void BackgroundFetchJobController::DownloadStarted( | |
53 const std::string& request_guid, | |
54 DownloadItem* item, | |
55 DownloadInterruptReason reason) { | |
56 // Start observing the DownloadItem. No need to store the pointer because it | |
57 // can be retrieved from the DownloadManager. | |
58 item->AddObserver(this); | |
59 download_guid_map_[item->GetGuid()] = request_guid; | |
60 job_data_->SetRequestDownloadGuid(request_guid, item->GetGuid()); | |
61 } | |
62 | |
63 void BackgroundFetchJobController::OnDownloadUpdated(DownloadItem* item) { | |
64 DCHECK(download_guid_map_.find(item->GetGuid()) != download_guid_map_.end()); | |
65 | |
66 bool requests_remaining = false; | |
67 switch (item->GetState()) { | |
68 case DownloadItem::DownloadState::CANCELLED: | |
69 // TODO(harkness): Expand the "complete" flag on the RequestInfo to | |
70 // include error states. | |
71 case DownloadItem::DownloadState::COMPLETE: | |
72 requests_remaining = job_data_->BackgroundFetchRequestInfoComplete( | |
73 download_guid_map_[item->GetGuid()]); | |
74 // TODO(harkness): Tell the notification service to update the download | |
75 // notification. | |
76 | |
77 if (requests_remaining) { | |
78 ProcessRequest(job_data_->GetNextBackgroundFetchRequestInfo()); | |
79 } else if (job_data_->IsComplete()) { | |
80 // TODO(harkness): Return the data to the context. | |
81 } | |
82 return; | |
83 case DownloadItem::DownloadState::INTERRUPTED: | |
84 // TODO(harkness): Just update the notification that it is paused. | |
85 return; | |
86 default: | |
Peter Beverloo
2017/03/08 14:58:19
Let's drop `default`, and instead add empty cases
harkness
2017/03/10 13:33:53
Done.
| |
87 return; | |
88 } | |
89 } | |
90 | |
91 void BackgroundFetchJobController::OnDownloadDestroyed(DownloadItem* item) { | |
92 // This can be called as part of Shutdown. Make sure the observers are | |
93 // removed. | |
94 item->RemoveObserver(this); | |
95 } | |
96 | |
97 void BackgroundFetchJobController::StopObservations() { | |
98 DownloadManager* manager = | |
99 BrowserContext::GetDownloadManager(browser_context_); | |
100 // For any downloads in progress, remove the observer. | |
101 for (auto& guid_map_entry : download_guid_map_) { | |
102 DownloadItem* item = manager->GetDownloadByGuid(guid_map_entry.first); | |
103 if (item) | |
104 item->RemoveObserver(this); | |
105 } | |
106 download_guid_map_.clear(); | |
49 } | 107 } |
50 | 108 |
51 void BackgroundFetchJobController::ProcessRequest( | 109 void BackgroundFetchJobController::ProcessRequest( |
52 const BackgroundFetchRequestInfo& fetch_request) { | 110 const BackgroundFetchRequestInfo& fetch_request) { |
53 // TODO(harkness): Start the observer watching for this download. | |
54 // TODO(harkness): Check if the download is already in progress or completed. | 111 // TODO(harkness): Check if the download is already in progress or completed. |
55 | 112 |
56 // Send the fetch request to the DownloadManager. | 113 // Set up the download parameters and the OnStartedCallback. |
57 std::unique_ptr<DownloadUrlParameters> params( | 114 std::unique_ptr<DownloadUrlParameters> params( |
58 base::MakeUnique<DownloadUrlParameters>( | 115 base::MakeUnique<DownloadUrlParameters>( |
59 fetch_request.url(), storage_partition_->GetURLRequestContext())); | 116 fetch_request.url(), storage_partition_->GetURLRequestContext())); |
60 // TODO(harkness): Currently this is the only place the browser_context is | 117 DownloadUrlParameters::OnStartedCallback on_started_callback = |
Peter Beverloo
2017/03/08 14:58:19
nit: I'd use `auto` here. The variable name is cle
harkness
2017/03/10 13:33:53
Done.
| |
61 // used. Evaluate whether we can just pass in the download manager explicitly. | 118 base::Bind(&BackgroundFetchJobController::DownloadStarted, |
119 weak_ptr_factory_.GetWeakPtr(), fetch_request.guid()); | |
120 params->set_callback(on_started_callback); | |
121 | |
122 // Send the fetch request to the DownloadManager. | |
Peter Beverloo
2017/03/08 14:58:19
nit: I don't think this adds any value.
harkness
2017/03/10 13:33:53
Removed.
| |
62 BrowserContext::GetDownloadManager(browser_context_) | 123 BrowserContext::GetDownloadManager(browser_context_) |
63 ->DownloadUrl(std::move(params)); | 124 ->DownloadUrl(std::move(params)); |
64 } | 125 } |
65 | 126 |
66 } // namespace content | 127 } // namespace content |
OLD | NEW |