1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
content / browser / background_fetch / background_fetch_request_info.cc [blame]
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/background_fetch/background_fetch_request_info.h"
#include <utility>
#include "base/strings/string_util.h"
#include "base/task/sequenced_task_runner.h"
#include "base/uuid.h"
#include "components/download/public/common/download_item.h"
#include "content/browser/blob_storage/chrome_blob_storage_context.h"
#include "content/public/browser/background_fetch_response.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/service_worker_context.h"
#include "net/http/http_response_headers.h"
#include "storage/browser/blob/blob_data_builder.h"
#include "storage/browser/blob/blob_storage_context.h"
namespace content {
// Holds state on the IO thread. Needed because blobs are bound to the IO
// thread.
class BackgroundFetchRequestInfo::BlobDataOnIO {
public:
BlobDataOnIO() = default;
~BlobDataOnIO() = default;
BlobDataOnIO(const BlobDataOnIO&) = delete;
BlobDataOnIO& operator=(const BlobDataOnIO&) = delete;
void CreateBlobDataHandle(
scoped_refptr<ChromeBlobStorageContext> blob_storage_context,
std::unique_ptr<storage::BlobDataHandle> blob_handle,
const base::FilePath& file_path,
uint64_t file_size,
uint64_t expected_response_size) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
DCHECK(!blob_data_handle_);
// In Incognito mode, |blob_handle| will be populated.
if (blob_handle) {
blob_data_handle_ = std::move(blob_handle);
return;
}
// In a normal profile, |file_path| and |file_size| will
// be populated.
auto blob_builder = std::make_unique<storage::BlobDataBuilder>(
base::Uuid::GenerateRandomV4().AsLowercaseString());
blob_builder->AppendFile(file_path, /* offset= */ 0, file_size,
/* expected_modification_time= */ base::Time());
blob_data_handle_ = GetBlobStorageContext(blob_storage_context.get())
->AddFinishedBlob(std::move(blob_builder));
DCHECK_EQ(expected_response_size,
blob_data_handle_ ? blob_data_handle_->size() : 0);
}
std::unique_ptr<storage::BlobDataHandle> TakeBlobDataHandle() {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
return std::move(blob_data_handle_);
}
private:
std::unique_ptr<storage::BlobDataHandle> blob_data_handle_;
};
BackgroundFetchRequestInfo::BackgroundFetchRequestInfo(
int request_index,
blink::mojom::FetchAPIRequestPtr fetch_request,
uint64_t request_body_size)
: RefCountedDeleteOnSequence<BackgroundFetchRequestInfo>(
base::SequencedTaskRunner::GetCurrentDefault()),
request_index_(request_index),
fetch_request_(std::move(fetch_request)),
request_body_size_(request_body_size) {}
BackgroundFetchRequestInfo::~BackgroundFetchRequestInfo() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}
void BackgroundFetchRequestInfo::InitializeDownloadGuid() {
DCHECK(download_guid_.empty());
download_guid_ = base::Uuid::GenerateRandomV4().AsLowercaseString();
}
void BackgroundFetchRequestInfo::SetDownloadGuid(
const std::string& download_guid) {
DCHECK(!download_guid.empty());
DCHECK(download_guid_.empty());
download_guid_ = download_guid;
}
void BackgroundFetchRequestInfo::SetResult(
std::unique_ptr<BackgroundFetchResult> result) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(result);
result_ = std::move(result);
// The BackgroundFetchResponse was extracted when the download started.
// This is sent over again when the download was complete in case the
// browser was restarted.
if (response_headers_.empty())
PopulateWithResponse(std::move(result_->response));
else
result_->response.reset();
// Get the response size.
if (result_->blob_handle)
response_size_ = result_->blob_handle->size();
else
response_size_ = result_->file_size;
}
void BackgroundFetchRequestInfo::SetEmptyResultWithFailureReason(
BackgroundFetchResult::FailureReason failure_reason) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
result_ = std::make_unique<BackgroundFetchResult>(
/* response= */ nullptr, base::Time::Now(), failure_reason);
}
void BackgroundFetchRequestInfo::PopulateWithResponse(
std::unique_ptr<BackgroundFetchResponse> response) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(response);
url_chain_ = response->url_chain;
// |headers| can be null when the request fails.
if (!response->headers)
return;
// The response code, text and headers all are stored in the
// net::HttpResponseHeaders object, shared by the |download_item|.
response_code_ = response->headers->response_code();
response_text_ = response->headers->GetStatusText();
size_t iter = 0;
std::string name, value;
while (response->headers->EnumerateHeaderLines(&iter, &name, &value))
response_headers_[base::ToLowerASCII(name)] = value;
}
int BackgroundFetchRequestInfo::GetResponseCode() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return response_code_;
}
const std::string& BackgroundFetchRequestInfo::GetResponseText() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return response_text_;
}
const std::map<std::string, std::string>&
BackgroundFetchRequestInfo::GetResponseHeaders() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return response_headers_;
}
const std::vector<GURL>& BackgroundFetchRequestInfo::GetURLChain() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return url_chain_;
}
void BackgroundFetchRequestInfo::CreateResponseBlobDataHandle(
scoped_refptr<ChromeBlobStorageContext> blob_storage_context) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(result_);
DCHECK(!io_blob_data_);
if (!result_->blob_handle && result_->file_path.empty())
return;
io_blob_data_.reset(new BlobDataOnIO());
std::unique_ptr<storage::BlobDataHandle> handle =
result_->blob_handle ? std::make_unique<storage::BlobDataHandle>(
result_->blob_handle.value())
: nullptr;
result_->blob_handle.reset();
// base::Unretained is safe because |io_blob_data_| is deleted on the IO
// thread in a task that must run after this task.
GetIOThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(&BlobDataOnIO::CreateBlobDataHandle,
base::Unretained(io_blob_data_.get()),
std::move(blob_storage_context), std::move(handle),
result_->file_path, result_->file_size, response_size_));
}
std::unique_ptr<storage::BlobDataHandle>
BackgroundFetchRequestInfo::TakeResponseBlobDataHandleOnIO() {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
if (!io_blob_data_)
return nullptr;
return io_blob_data_->TakeBlobDataHandle();
}
uint64_t BackgroundFetchRequestInfo::GetResponseSize() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(result_);
return response_size_;
}
const base::Time& BackgroundFetchRequestInfo::GetResponseTime() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(result_);
return result_->response_time;
}
bool BackgroundFetchRequestInfo::IsResultSuccess() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(result_);
return result_->failure_reason == BackgroundFetchResult::FailureReason::NONE;
}
} // namespace content