| 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_data_manager.h" | 5 #include "content/browser/background_fetch/background_fetch_data_manager.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <queue> | 8 #include <queue> |
| 9 | 9 |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 | 204 |
| 205 for (const auto& request : requests) { | 205 for (const auto& request : requests) { |
| 206 BackgroundFetchSettledFetch settled_fetch; | 206 BackgroundFetchSettledFetch settled_fetch; |
| 207 settled_fetch.request = request->fetch_request(); | 207 settled_fetch.request = request->fetch_request(); |
| 208 | 208 |
| 209 // TODO(peter): Find the appropriate way to generalize CORS security checks. | 209 // TODO(peter): Find the appropriate way to generalize CORS security checks. |
| 210 const bool opaque = !registration_id.origin().IsSameOriginWith( | 210 const bool opaque = !registration_id.origin().IsSameOriginWith( |
| 211 url::Origin(request->GetURLChain().back().GetOrigin())); | 211 url::Origin(request->GetURLChain().back().GetOrigin())); |
| 212 | 212 |
| 213 settled_fetch.response.url_list = request->GetURLChain(); | 213 settled_fetch.response.url_list = request->GetURLChain(); |
| 214 | |
| 215 // TODO(peter): The |status_code| should match what the download manager ran | |
| 216 // in to in the BackgroundFetchResponseInfo. | |
| 217 settled_fetch.response.status_code = 200; | |
| 218 // TODO: settled_fetch.response.status_text | |
| 219 | |
| 220 settled_fetch.response.response_type = | 214 settled_fetch.response.response_type = |
| 221 blink::kWebServiceWorkerResponseTypeDefault; | 215 blink::kWebServiceWorkerResponseTypeDefault; |
| 222 | 216 |
| 223 // TODO(peter): The |headers| should be set to the real response headers, | 217 if (!opaque) { |
| 224 // but the download manager does not relay those to us yet. | 218 settled_fetch.response.status_code = request->GetResponseCode(); |
| 225 if (!request->GetResponseType().empty() && !opaque) { | 219 settled_fetch.response.status_text = request->GetResponseText(); |
| 226 settled_fetch.response.headers["Content-Type"] = | 220 settled_fetch.response.headers.insert( |
| 227 request->GetResponseType(); | 221 request->GetResponseHeaders().begin(), |
| 228 } | 222 request->GetResponseHeaders().end()); |
| 229 | 223 |
| 230 if (request->GetFileSize() > 0 && !opaque) { | 224 // Include the response data as a blob backed by the downloaded file. |
| 231 DCHECK(!request->GetFilePath().empty()); | 225 if (request->GetFileSize() > 0) { |
| 226 DCHECK(!request->GetFilePath().empty()); |
| 227 std::unique_ptr<BlobHandle> blob_handle = |
| 228 blob_storage_context_->CreateFileBackedBlob( |
| 229 request->GetFilePath(), 0 /* offset */, request->GetFileSize(), |
| 230 base::Time() /* expected_modification_time */); |
| 232 | 231 |
| 233 std::unique_ptr<BlobHandle> blob_handle = | 232 // TODO(peter): Appropriately handle !blob_handle |
| 234 blob_storage_context_->CreateFileBackedBlob( | 233 if (blob_handle) { |
| 235 request->GetFilePath(), 0 /* offset */, request->GetFileSize(), | 234 settled_fetch.response.blob_uuid = blob_handle->GetUUID(); |
| 236 base::Time() /* expected_modification_time */); | 235 settled_fetch.response.blob_size = request->GetFileSize(); |
| 237 | 236 |
| 238 // TODO(peter): Appropriately handle !blob_handle | 237 blob_handles.push_back(std::move(blob_handle)); |
| 239 if (blob_handle) { | 238 } |
| 240 settled_fetch.response.blob_uuid = blob_handle->GetUUID(); | |
| 241 settled_fetch.response.blob_size = request->GetFileSize(); | |
| 242 | |
| 243 // TODO(peter): Remove when we relay the real response headers. | |
| 244 settled_fetch.response.headers["Content-Length"] = | |
| 245 std::to_string(request->GetFileSize()); | |
| 246 | |
| 247 blob_handles.push_back(std::move(blob_handle)); | |
| 248 } | 239 } |
| 249 } | 240 } |
| 250 | 241 |
| 251 // TODO: settled_fetch.response.error | 242 // TODO: settled_fetch.response.error |
| 252 settled_fetch.response.response_time = request->GetResponseTime(); | 243 settled_fetch.response.response_time = request->GetResponseTime(); |
| 253 // TODO: settled_fetch.response.cors_exposed_header_names | 244 // TODO: settled_fetch.response.cors_exposed_header_names |
| 254 | 245 |
| 255 settled_fetches.push_back(settled_fetch); | 246 settled_fetches.push_back(settled_fetch); |
| 256 } | 247 } |
| 257 | 248 |
| 258 std::move(callback).Run(blink::mojom::BackgroundFetchError::NONE, | 249 std::move(callback).Run(blink::mojom::BackgroundFetchError::NONE, |
| 259 std::move(settled_fetches), std::move(blob_handles)); | 250 std::move(settled_fetches), std::move(blob_handles)); |
| 260 } | 251 } |
| 261 | 252 |
| 262 void BackgroundFetchDataManager::DeleteRegistration( | 253 void BackgroundFetchDataManager::DeleteRegistration( |
| 263 const BackgroundFetchRegistrationId& registration_id, | 254 const BackgroundFetchRegistrationId& registration_id, |
| 264 DeleteRegistrationCallback callback) { | 255 DeleteRegistrationCallback callback) { |
| 265 auto iter = registrations_.find(registration_id); | 256 auto iter = registrations_.find(registration_id); |
| 266 if (iter == registrations_.end()) { | 257 if (iter == registrations_.end()) { |
| 267 std::move(callback).Run(blink::mojom::BackgroundFetchError::INVALID_TAG); | 258 std::move(callback).Run(blink::mojom::BackgroundFetchError::INVALID_TAG); |
| 268 return; | 259 return; |
| 269 } | 260 } |
| 270 | 261 |
| 271 registrations_.erase(iter); | 262 registrations_.erase(iter); |
| 272 | 263 |
| 273 std::move(callback).Run(blink::mojom::BackgroundFetchError::NONE); | 264 std::move(callback).Run(blink::mojom::BackgroundFetchError::NONE); |
| 274 } | 265 } |
| 275 | 266 |
| 276 } // namespace content | 267 } // namespace content |
| OLD | NEW |