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" |
11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
12 #include "content/browser/background_fetch/background_fetch_constants.h" | 12 #include "content/browser/background_fetch/background_fetch_constants.h" |
13 #include "content/browser/background_fetch/background_fetch_data_manager.h" | 13 #include "content/browser/background_fetch/background_fetch_data_manager.h" |
| 14 #include "content/common/service_worker/service_worker_types.h" |
14 #include "content/public/browser/browser_context.h" | 15 #include "content/public/browser/browser_context.h" |
15 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
16 #include "content/public/browser/download_interrupt_reasons.h" | 17 #include "content/public/browser/download_interrupt_reasons.h" |
17 #include "content/public/browser/download_manager.h" | 18 #include "content/public/browser/download_manager.h" |
18 #include "net/url_request/url_request_context_getter.h" | 19 #include "net/url_request/url_request_context_getter.h" |
19 | 20 |
20 namespace content { | 21 namespace content { |
21 | 22 |
22 // Internal functionality of the BackgroundFetchJobController that lives on the | 23 // Internal functionality of the BackgroundFetchJobController that lives on the |
23 // UI thread, where all interaction with the download manager must happen. | 24 // UI thread, where all interaction with the download manager must happen. |
(...skipping 18 matching lines...) Expand all Loading... |
42 // Starts fetching the |request| with the download manager. | 43 // Starts fetching the |request| with the download manager. |
43 void StartRequest(scoped_refptr<BackgroundFetchRequestInfo> request) { | 44 void StartRequest(scoped_refptr<BackgroundFetchRequestInfo> request) { |
44 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 45 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
45 DCHECK(request_context_); | 46 DCHECK(request_context_); |
46 DCHECK(request); | 47 DCHECK(request); |
47 | 48 |
48 DownloadManager* download_manager = | 49 DownloadManager* download_manager = |
49 BrowserContext::GetDownloadManager(browser_context_); | 50 BrowserContext::GetDownloadManager(browser_context_); |
50 DCHECK(download_manager); | 51 DCHECK(download_manager); |
51 | 52 |
| 53 const ServiceWorkerFetchRequest& fetch_request = request->fetch_request(); |
| 54 |
52 std::unique_ptr<DownloadUrlParameters> download_parameters( | 55 std::unique_ptr<DownloadUrlParameters> download_parameters( |
53 base::MakeUnique<DownloadUrlParameters>(request->GetURL(), | 56 base::MakeUnique<DownloadUrlParameters>(fetch_request.url, |
54 request_context_.get())); | 57 request_context_.get())); |
55 | 58 |
56 // TODO(peter): The |download_parameters| should be populated with all the | 59 // TODO(peter): The |download_parameters| should be populated with all the |
57 // properties set in the |request|'s ServiceWorkerFetchRequest member. | 60 // properties set in the |fetch_request| structure. |
58 | 61 |
59 download_parameters->set_callback(base::Bind(&Core::DidStartRequest, | 62 download_parameters->set_callback(base::Bind(&Core::DidStartRequest, |
60 weak_ptr_factory_.GetWeakPtr(), | 63 weak_ptr_factory_.GetWeakPtr(), |
61 std::move(request))); | 64 std::move(request))); |
62 | 65 |
63 download_manager->DownloadUrl(std::move(download_parameters)); | 66 download_manager->DownloadUrl(std::move(download_parameters)); |
64 } | 67 } |
65 | 68 |
66 // DownloadItem::Observer overrides: | 69 // DownloadItem::Observer overrides: |
67 void OnDownloadUpdated(DownloadItem* item) override { | 70 void OnDownloadUpdated(DownloadItem* download_item) override { |
68 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 71 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
69 | 72 |
70 auto iter = downloads_.find(item); | 73 auto iter = downloads_.find(download_item); |
71 DCHECK(iter != downloads_.end()); | 74 DCHECK(iter != downloads_.end()); |
72 | 75 |
73 const scoped_refptr<BackgroundFetchRequestInfo>& request = iter->second; | 76 scoped_refptr<BackgroundFetchRequestInfo> request = iter->second; |
74 | 77 |
75 switch (item->GetState()) { | 78 switch (download_item->GetState()) { |
76 case DownloadItem::DownloadState::COMPLETE: | 79 case DownloadItem::DownloadState::COMPLETE: |
77 // TODO(peter): Populate the responses' information in the |request|. | 80 request->PopulateResponseFromDownloadItem(download_item); |
78 | 81 download_item->RemoveObserver(this); |
79 item->RemoveObserver(this); | |
80 | 82 |
81 // Inform the host about |host| having completed. | 83 // Inform the host about |host| having completed. |
82 BrowserThread::PostTask( | 84 BrowserThread::PostTask( |
83 BrowserThread::IO, FROM_HERE, | 85 BrowserThread::IO, FROM_HERE, |
84 base::Bind(&BackgroundFetchJobController::DidCompleteRequest, | 86 base::Bind(&BackgroundFetchJobController::DidCompleteRequest, |
85 io_parent_, request)); | 87 io_parent_, std::move(request))); |
86 | 88 |
87 // Clear the local state for the |request|, it no longer is our concern. | 89 // Clear the local state for the |request|, it no longer is our concern. |
88 downloads_.erase(iter); | 90 downloads_.erase(iter); |
89 break; | 91 break; |
90 case DownloadItem::DownloadState::CANCELLED: | 92 case DownloadItem::DownloadState::CANCELLED: |
91 // TODO(harkness): Consider how we want to handle cancelled downloads. | 93 // TODO(harkness): Consider how we want to handle cancelled downloads. |
92 break; | 94 break; |
93 case DownloadItem::DownloadState::INTERRUPTED: | 95 case DownloadItem::DownloadState::INTERRUPTED: |
94 // TODO(harkness): Just update the notification that it is paused. | 96 // TODO(harkness): Just update the notification that it is paused. |
95 break; | 97 break; |
96 case DownloadItem::DownloadState::IN_PROGRESS: | 98 case DownloadItem::DownloadState::IN_PROGRESS: |
97 // TODO(harkness): If the download was previously paused, this should | 99 // TODO(harkness): If the download was previously paused, this should |
98 // now unpause the notification. | 100 // now unpause the notification. |
99 break; | 101 break; |
100 case DownloadItem::DownloadState::MAX_DOWNLOAD_STATE: | 102 case DownloadItem::DownloadState::MAX_DOWNLOAD_STATE: |
101 NOTREACHED(); | 103 NOTREACHED(); |
102 break; | 104 break; |
103 } | 105 } |
104 } | 106 } |
105 | 107 |
106 void OnDownloadDestroyed(DownloadItem* item) override { | 108 void OnDownloadDestroyed(DownloadItem* download_item) override { |
107 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 109 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
108 DCHECK_EQ(downloads_.count(item), 1u); | 110 DCHECK_EQ(downloads_.count(download_item), 1u); |
109 downloads_.erase(item); | 111 downloads_.erase(download_item); |
110 | 112 |
111 item->RemoveObserver(this); | 113 download_item->RemoveObserver(this); |
112 } | 114 } |
113 | 115 |
114 private: | 116 private: |
115 // Called when the download manager has started the given |request|. The | 117 // Called when the download manager has started the given |request|. The |
116 // |download_item| continues to be owned by the download system. The | 118 // |download_item| continues to be owned by the download system. The |
117 // |interrupt_reason| will indicate when a request could not be started. | 119 // |interrupt_reason| will indicate when a request could not be started. |
118 void DidStartRequest(scoped_refptr<BackgroundFetchRequestInfo> request, | 120 void DidStartRequest(scoped_refptr<BackgroundFetchRequestInfo> request, |
119 DownloadItem* download_item, | 121 DownloadItem* download_item, |
120 DownloadInterruptReason interrupt_reason) { | 122 DownloadInterruptReason interrupt_reason) { |
121 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 123 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
122 DCHECK_EQ(interrupt_reason, DOWNLOAD_INTERRUPT_REASON_NONE); | 124 DCHECK_EQ(interrupt_reason, DOWNLOAD_INTERRUPT_REASON_NONE); |
123 DCHECK(download_item); | 125 DCHECK(download_item); |
124 | 126 |
| 127 request->PopulateDownloadState(download_item, interrupt_reason); |
| 128 |
125 // TODO(peter): The above two DCHECKs are assumptions our implementation | 129 // TODO(peter): The above two DCHECKs are assumptions our implementation |
126 // currently makes, but are not fit for production. We need to handle such | 130 // currently makes, but are not fit for production. We need to handle such |
127 // failures gracefully. | 131 // failures gracefully. |
128 | 132 |
129 // Register for updates on the download's progress. | 133 // Register for updates on the download's progress. |
130 download_item->AddObserver(this); | 134 download_item->AddObserver(this); |
131 | 135 |
132 // Inform the host about the |request| having started. | 136 // Inform the host about the |request| having started. |
133 BrowserThread::PostTask( | 137 BrowserThread::PostTask( |
134 BrowserThread::IO, FROM_HERE, | 138 BrowserThread::IO, FROM_HERE, |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 | 262 |
259 // TODO(harkness): Abort all in-progress downloads. | 263 // TODO(harkness): Abort all in-progress downloads. |
260 | 264 |
261 state_ = State::ABORTED; | 265 state_ = State::ABORTED; |
262 | 266 |
263 // Inform the owner of the controller about the job having completed. | 267 // Inform the owner of the controller about the job having completed. |
264 std::move(completed_callback_).Run(this); | 268 std::move(completed_callback_).Run(this); |
265 } | 269 } |
266 | 270 |
267 } // namespace content | 271 } // namespace content |
OLD | NEW |