| // Copyright 2018 The Chromium Authors. All rights reserved. |
| // 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/storage/get_settled_fetches_task.h" |
| |
| #include "base/barrier_closure.h" |
| #include "content/browser/background_fetch/background_fetch_data_manager.h" |
| #include "content/browser/background_fetch/storage/database_helpers.h" |
| #include "content/browser/cache_storage/cache_storage_manager.h" |
| #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| |
| namespace mojo { |
| |
| // TODO(leonhsl): Deprecate this converter once we replace usages of |
| // content::ServiceWorkerResponse with blink::mojom::FetchAPIResponse in |
| // background_fetch codes. |
| template <> |
| struct TypeConverter<content::ServiceWorkerResponse, |
| blink::mojom::FetchAPIResponse> { |
| static content::ServiceWorkerResponse Convert( |
| const blink::mojom::FetchAPIResponse& input) { |
| content::ServiceWorkerResponse output; |
| output.url_list = std::move(input.url_list); |
| output.status_code = input.status_code; |
| output.status_text = std::move(input.status_text); |
| output.response_type = input.response_type; |
| output.headers.insert(input.headers.begin(), input.headers.end()); |
| if (input.blob) { |
| output.blob_uuid = input.blob->uuid; |
| output.blob_size = input.blob->size; |
| output.blob = base::MakeRefCounted<storage::BlobHandle>( |
| blink::mojom::BlobPtr(std::move(input.blob->blob))); |
| } |
| output.error = input.error; |
| output.response_time = input.response_time; |
| output.is_in_cache_storage = input.is_in_cache_storage; |
| if (input.cache_storage_cache_name) { |
| output.cache_storage_cache_name = |
| std::move(*(input.cache_storage_cache_name)); |
| } |
| output.cors_exposed_header_names = |
| std::move(input.cors_exposed_header_names); |
| if (input.side_data_blob) { |
| output.side_data_blob_uuid = input.side_data_blob->uuid; |
| output.side_data_blob_size = input.side_data_blob->size; |
| output.side_data_blob = base::MakeRefCounted<storage::BlobHandle>( |
| blink::mojom::BlobPtr(std::move(input.side_data_blob->blob))); |
| } |
| return output; |
| } |
| }; |
| |
| } // namespace mojo |
| |
| namespace content { |
| |
| namespace background_fetch { |
| |
| GetSettledFetchesTask::GetSettledFetchesTask( |
| DatabaseTaskHost* host, |
| BackgroundFetchRegistrationId registration_id, |
| std::unique_ptr<BackgroundFetchRequestMatchParams> match_params, |
| SettledFetchesCallback callback) |
| : DatabaseTask(host), |
| registration_id_(registration_id), |
| match_params_(std::move(match_params)), |
| settled_fetches_callback_(std::move(callback)), |
| weak_factory_(this) {} |
| |
| GetSettledFetchesTask::~GetSettledFetchesTask() = default; |
| |
| void GetSettledFetchesTask::Start() { |
| base::RepeatingClosure barrier_closure = base::BarrierClosure( |
| 2u, base::BindOnce(&GetSettledFetchesTask::GetResponses, |
| weak_factory_.GetWeakPtr())); |
| |
| cache_manager()->OpenCache( |
| registration_id_.origin(), CacheStorageOwner::kBackgroundFetch, |
| registration_id_.unique_id() /* cache_name */, |
| base::BindOnce(&GetSettledFetchesTask::DidOpenCache, |
| weak_factory_.GetWeakPtr(), barrier_closure)); |
| |
| service_worker_context()->GetRegistrationUserDataByKeyPrefix( |
| registration_id_.service_worker_registration_id(), |
| {CompletedRequestKeyPrefix(registration_id_.unique_id())}, |
| base::BindOnce(&GetSettledFetchesTask::DidGetCompletedRequests, |
| weak_factory_.GetWeakPtr(), barrier_closure)); |
| } |
| |
| void GetSettledFetchesTask::DidOpenCache( |
| base::OnceClosure done_closure, |
| CacheStorageCacheHandle handle, |
| blink::mojom::CacheStorageError error) { |
| if (error != blink::mojom::CacheStorageError::kSuccess) { |
| // TODO(crbug.com/780025): Log failures to UMA. |
| error_ = blink::mojom::BackgroundFetchError::STORAGE_ERROR; |
| } else { |
| DCHECK(handle.value()); |
| handle_ = std::move(handle); |
| } |
| std::move(done_closure).Run(); |
| } |
| |
| void GetSettledFetchesTask::DidGetCompletedRequests( |
| base::OnceClosure done_closure, |
| const std::vector<std::string>& data, |
| blink::ServiceWorkerStatusCode status) { |
| switch (ToDatabaseStatus(status)) { |
| case DatabaseStatus::kOk: |
| break; |
| // TODO(crbug.com/780025): Log failures to UMA. |
| case DatabaseStatus::kFailed: |
| error_ = blink::mojom::BackgroundFetchError::STORAGE_ERROR; |
| break; |
| case DatabaseStatus::kNotFound: |
| background_fetch_succeeded_ = false; |
| error_ = blink::mojom::BackgroundFetchError::INVALID_ID; |
| break; |
| } |
| |
| completed_requests_.reserve(data.size()); |
| for (const std::string& serialized_completed_request : data) { |
| completed_requests_.emplace_back(); |
| if (!completed_requests_.back().ParseFromString( |
| serialized_completed_request)) { |
| // Service worker database has been corrupted. Abandon fetches. |
| error_ = blink::mojom::BackgroundFetchError::STORAGE_ERROR; |
| background_fetch_succeeded_ = false; |
| AbandonFetches(registration_id_.service_worker_registration_id()); |
| break; |
| } |
| if (!completed_requests_.back().succeeded()) |
| background_fetch_succeeded_ = false; |
| } |
| std::move(done_closure).Run(); |
| } |
| |
| void GetSettledFetchesTask::GetResponses() { |
| if (error_ != blink::mojom::BackgroundFetchError::NONE) { |
| FinishWithError(error_); |
| return; |
| } |
| if (completed_requests_.empty()) { |
| FinishWithError(blink::mojom::BackgroundFetchError::NONE); |
| return; |
| } |
| |
| if (match_params_->FilterByRequest()) { |
| // Get a response only for the relevant fetch. |
| settled_fetches_.emplace_back(); |
| settled_fetches_.back().request = match_params_->request_to_match(); |
| for (const auto& completed_request : completed_requests_) { |
| if (completed_request.serialized_request() != |
| match_params_->request_to_match().Serialize()) { |
| continue; |
| } |
| // A matching request! |
| FillResponse(&settled_fetches_.back(), |
| base::BindOnce(&GetSettledFetchesTask::FinishWithError, |
| weak_factory_.GetWeakPtr(), |
| blink::mojom::BackgroundFetchError::NONE)); |
| // TODO(crbug.com/863852): Add support for matchAll(); |
| return; |
| } |
| |
| // No matching request found. |
| FillUncachedResponse( |
| &settled_fetches_.back(), |
| base::BindOnce(&GetSettledFetchesTask::FinishWithError, |
| weak_factory_.GetWeakPtr(), |
| blink::mojom::BackgroundFetchError::NONE)); |
| return; |
| } |
| |
| // Process all completed requests. |
| base::RepeatingClosure barrier_closure = base::BarrierClosure( |
| completed_requests_.size(), |
| base::BindOnce(&GetSettledFetchesTask::FinishWithError, |
| weak_factory_.GetWeakPtr(), |
| blink::mojom::BackgroundFetchError::NONE)); |
| settled_fetches_.reserve(completed_requests_.size()); |
| for (const auto& completed_request : completed_requests_) { |
| settled_fetches_.emplace_back(); |
| settled_fetches_.back().request = |
| std::move(ServiceWorkerFetchRequest::ParseFromString( |
| completed_request.serialized_request())); |
| FillResponse(&settled_fetches_.back(), barrier_closure); |
| } |
| } |
| |
| void GetSettledFetchesTask::FillResponse( |
| BackgroundFetchSettledFetch* settled_fetch, |
| base::OnceClosure callback) { |
| DCHECK(settled_fetch); |
| DCHECK(handle_.value()); |
| |
| auto request = |
| std::make_unique<ServiceWorkerFetchRequest>(settled_fetch->request); |
| handle_.value()->Match(std::move(request), |
| match_params_->cloned_cache_query_params(), |
| base::BindOnce(&GetSettledFetchesTask::DidMatchRequest, |
| weak_factory_.GetWeakPtr(), |
| settled_fetch, std::move(callback))); |
| } |
| |
| void GetSettledFetchesTask::DidMatchRequest( |
| BackgroundFetchSettledFetch* settled_fetch, |
| base::OnceClosure callback, |
| blink::mojom::CacheStorageError error, |
| blink::mojom::FetchAPIResponsePtr cache_response) { |
| if (error != blink::mojom::CacheStorageError::kSuccess) { |
| DCHECK(settled_fetch); |
| FillUncachedResponse(settled_fetch, std::move(callback)); |
| return; |
| } |
| settled_fetch->response = cache_response->To<ServiceWorkerResponse>(); |
| std::move(callback).Run(); |
| } |
| |
| void GetSettledFetchesTask::FillUncachedResponse( |
| BackgroundFetchSettledFetch* settled_fetch, |
| base::OnceClosure callback) { |
| background_fetch_succeeded_ = false; |
| |
| // TODO(rayankans): Fill unmatched response with error reports. |
| settled_fetch->response.response_type = |
| network::mojom::FetchResponseType::kError; |
| settled_fetch->response.url_list.push_back(settled_fetch->request.url); |
| |
| std::move(callback).Run(); |
| } |
| |
| void GetSettledFetchesTask::FinishWithError( |
| blink::mojom::BackgroundFetchError error) { |
| std::move(settled_fetches_callback_) |
| .Run(error, background_fetch_succeeded_, std::move(settled_fetches_), |
| {} /* blob_data_handles */); |
| Finished(); // Destroys |this|. |
| } |
| |
| } // namespace background_fetch |
| |
| } // namespace content |