| 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" |
| 11 #include "content/browser/background_fetch/background_fetch_constants.h" | 11 #include "content/browser/background_fetch/background_fetch_constants.h" |
| 12 #include "content/browser/background_fetch/background_fetch_context.h" | 12 #include "content/browser/background_fetch/background_fetch_context.h" |
| 13 #include "content/browser/background_fetch/background_fetch_cross_origin_filter.
h" | 13 #include "content/browser/background_fetch/background_fetch_cross_origin_filter.
h" |
| 14 #include "content/browser/background_fetch/background_fetch_request_info.h" | 14 #include "content/browser/background_fetch/background_fetch_request_info.h" |
| 15 #include "content/browser/blob_storage/chrome_blob_storage_context.h" | 15 #include "content/browser/blob_storage/chrome_blob_storage_context.h" |
| 16 #include "content/public/browser/blob_handle.h" | 16 #include "content/public/browser/blob_handle.h" |
| 17 #include "content/public/browser/browser_context.h" | 17 #include "content/public/browser/browser_context.h" |
| 18 #include "content/public/browser/download_interrupt_reasons.h" | 18 #include "content/public/browser/download_interrupt_reasons.h" |
| 19 #include "content/public/browser/download_item.h" | 19 #include "content/public/browser/download_item.h" |
| 20 #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWor
kerResponseType.h" | 20 #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWor
kerResponseType.h" |
| 21 | 21 |
| 22 namespace content { | 22 namespace content { |
| 23 | 23 |
| 24 // Returns whether the response contained in the Background Fetch |request| is |
| 25 // considered OK. See https://fetch.spec.whatwg.org/#ok-status aka a successful |
| 26 // 2xx status per https://tools.ietf.org/html/rfc7231#section-6.3. |
| 27 bool IsOK(const BackgroundFetchRequestInfo& request) { |
| 28 int status = request.GetResponseCode(); |
| 29 return status >= 200 && status < 300; |
| 30 } |
| 31 |
| 24 // The Registration Data class encapsulates the data stored for a particular | 32 // The Registration Data class encapsulates the data stored for a particular |
| 25 // Background Fetch registration. This roughly matches the on-disk format that | 33 // Background Fetch registration. This roughly matches the on-disk format that |
| 26 // will be adhered to in the future. | 34 // will be adhered to in the future. |
| 27 class BackgroundFetchDataManager::RegistrationData { | 35 class BackgroundFetchDataManager::RegistrationData { |
| 28 public: | 36 public: |
| 29 RegistrationData(const std::vector<ServiceWorkerFetchRequest>& requests, | 37 RegistrationData(const std::vector<ServiceWorkerFetchRequest>& requests, |
| 30 const BackgroundFetchOptions& options) | 38 const BackgroundFetchOptions& options) |
| 31 : options_(options) { | 39 : options_(options) { |
| 32 int request_index = 0; | 40 int request_index = 0; |
| 33 | 41 |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 SettledFetchesCallback callback) { | 199 SettledFetchesCallback callback) { |
| 192 auto iter = registrations_.find(registration_id); | 200 auto iter = registrations_.find(registration_id); |
| 193 DCHECK(iter != registrations_.end()); | 201 DCHECK(iter != registrations_.end()); |
| 194 | 202 |
| 195 RegistrationData* registration_data = iter->second.get(); | 203 RegistrationData* registration_data = iter->second.get(); |
| 196 DCHECK(!registration_data->HasPendingRequests()); | 204 DCHECK(!registration_data->HasPendingRequests()); |
| 197 | 205 |
| 198 const std::vector<scoped_refptr<BackgroundFetchRequestInfo>>& requests = | 206 const std::vector<scoped_refptr<BackgroundFetchRequestInfo>>& requests = |
| 199 registration_data->GetCompletedRequests(); | 207 registration_data->GetCompletedRequests(); |
| 200 | 208 |
| 209 bool background_fetch_succeeded = true; |
| 210 |
| 201 std::vector<BackgroundFetchSettledFetch> settled_fetches; | 211 std::vector<BackgroundFetchSettledFetch> settled_fetches; |
| 202 settled_fetches.reserve(requests.size()); | 212 settled_fetches.reserve(requests.size()); |
| 203 | 213 |
| 204 std::vector<std::unique_ptr<BlobHandle>> blob_handles; | 214 std::vector<std::unique_ptr<BlobHandle>> blob_handles; |
| 205 | 215 |
| 206 for (const auto& request : requests) { | 216 for (const auto& request : requests) { |
| 207 BackgroundFetchSettledFetch settled_fetch; | 217 BackgroundFetchSettledFetch settled_fetch; |
| 208 settled_fetch.request = request->fetch_request(); | 218 settled_fetch.request = request->fetch_request(); |
| 209 | 219 |
| 210 // The |filter| decides which values can be passed on to the Service Worker. | 220 // The |filter| decides which values can be passed on to the Service Worker. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 231 base::Time() /* expected_modification_time */); | 241 base::Time() /* expected_modification_time */); |
| 232 | 242 |
| 233 // TODO(peter): Appropriately handle !blob_handle | 243 // TODO(peter): Appropriately handle !blob_handle |
| 234 if (blob_handle) { | 244 if (blob_handle) { |
| 235 settled_fetch.response.blob_uuid = blob_handle->GetUUID(); | 245 settled_fetch.response.blob_uuid = blob_handle->GetUUID(); |
| 236 settled_fetch.response.blob_size = request->GetFileSize(); | 246 settled_fetch.response.blob_size = request->GetFileSize(); |
| 237 | 247 |
| 238 blob_handles.push_back(std::move(blob_handle)); | 248 blob_handles.push_back(std::move(blob_handle)); |
| 239 } | 249 } |
| 240 } | 250 } |
| 251 } else { |
| 252 // TODO(crbug.com/711354): Consider Background Fetches as failed when the |
| 253 // response cannot be relayed to the developer. |
| 254 background_fetch_succeeded = false; |
| 241 } | 255 } |
| 242 | 256 |
| 243 // TODO: settled_fetch.response.error | 257 // TODO: settled_fetch.response.error |
| 244 settled_fetch.response.response_time = request->GetResponseTime(); | 258 settled_fetch.response.response_time = request->GetResponseTime(); |
| 245 // TODO: settled_fetch.response.cors_exposed_header_names | 259 // TODO: settled_fetch.response.cors_exposed_header_names |
| 246 | 260 |
| 261 background_fetch_succeeded = background_fetch_succeeded && IsOK(*request); |
| 262 |
| 247 settled_fetches.push_back(settled_fetch); | 263 settled_fetches.push_back(settled_fetch); |
| 248 } | 264 } |
| 249 | 265 |
| 250 std::move(callback).Run(blink::mojom::BackgroundFetchError::NONE, | 266 std::move(callback).Run(blink::mojom::BackgroundFetchError::NONE, |
| 267 background_fetch_succeeded, |
| 251 std::move(settled_fetches), std::move(blob_handles)); | 268 std::move(settled_fetches), std::move(blob_handles)); |
| 252 } | 269 } |
| 253 | 270 |
| 254 void BackgroundFetchDataManager::DeleteRegistration( | 271 void BackgroundFetchDataManager::DeleteRegistration( |
| 255 const BackgroundFetchRegistrationId& registration_id, | 272 const BackgroundFetchRegistrationId& registration_id, |
| 256 DeleteRegistrationCallback callback) { | 273 DeleteRegistrationCallback callback) { |
| 257 auto iter = registrations_.find(registration_id); | 274 auto iter = registrations_.find(registration_id); |
| 258 if (iter == registrations_.end()) { | 275 if (iter == registrations_.end()) { |
| 259 std::move(callback).Run(blink::mojom::BackgroundFetchError::INVALID_TAG); | 276 std::move(callback).Run(blink::mojom::BackgroundFetchError::INVALID_TAG); |
| 260 return; | 277 return; |
| 261 } | 278 } |
| 262 | 279 |
| 263 registrations_.erase(iter); | 280 registrations_.erase(iter); |
| 264 | 281 |
| 265 std::move(callback).Run(blink::mojom::BackgroundFetchError::NONE); | 282 std::move(callback).Run(blink::mojom::BackgroundFetchError::NONE); |
| 266 } | 283 } |
| 267 | 284 |
| 268 } // namespace content | 285 } // namespace content |
| OLD | NEW |