Commit 3f5b892e authored by Peter Beverloo's avatar Peter Beverloo Committed by Commit Bot

Populate ServiceWorkerResponse.blob for Background Fetch

Using Mojo for blob handles has recently been enabled, but the
Background Fetch code hadn't been updated yet. In effect, responses
originating from Background Fetches started hitting a DCHECK.

BUG=

Change-Id: I0e4d765eb8ae42f02144261ba517b134be432ece
Reviewed-on: https://chromium-review.googlesource.com/746921
Commit-Queue: Peter Beverloo <peter@chromium.org>
Reviewed-by: default avatarDan Elphick <delphick@chromium.org>
Cr-Commit-Position: refs/heads/master@{#513113}
parent 6037cf9e
...@@ -13,9 +13,9 @@ ...@@ -13,9 +13,9 @@
#include "content/browser/background_fetch/background_fetch_registration_notifier.h" #include "content/browser/background_fetch/background_fetch_registration_notifier.h"
#include "content/browser/service_worker/service_worker_context_wrapper.h" #include "content/browser/service_worker/service_worker_context_wrapper.h"
#include "content/public/browser/background_fetch_delegate.h" #include "content/public/browser/background_fetch_delegate.h"
#include "content/public/browser/blob_handle.h"
#include "content/public/browser/browser_context.h" #include "content/public/browser/browser_context.h"
#include "net/url_request/url_request_context_getter.h" #include "net/url_request/url_request_context_getter.h"
#include "storage/browser/blob/blob_data_handle.h"
namespace content { namespace content {
...@@ -172,7 +172,7 @@ void BackgroundFetchContext::DidGetSettledFetches( ...@@ -172,7 +172,7 @@ void BackgroundFetchContext::DidGetSettledFetches(
blink::mojom::BackgroundFetchError error, blink::mojom::BackgroundFetchError error,
bool background_fetch_succeeded, bool background_fetch_succeeded,
std::vector<BackgroundFetchSettledFetch> settled_fetches, std::vector<BackgroundFetchSettledFetch> settled_fetches,
std::vector<std::unique_ptr<BlobHandle>> blob_handles) { std::vector<std::unique_ptr<storage::BlobDataHandle>> blob_data_handles) {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
if (error != blink::mojom::BackgroundFetchError::NONE) { if (error != blink::mojom::BackgroundFetchError::NONE) {
...@@ -189,24 +189,24 @@ void BackgroundFetchContext::DidGetSettledFetches( ...@@ -189,24 +189,24 @@ void BackgroundFetchContext::DidGetSettledFetches(
base::Bind(&BackgroundFetchContext::CleanupRegistration, base::Bind(&BackgroundFetchContext::CleanupRegistration,
weak_factory_.GetWeakPtr(), registration_id, weak_factory_.GetWeakPtr(), registration_id,
// The blob uuid is sent as part of |settled_fetches|. Bind // The blob uuid is sent as part of |settled_fetches|. Bind
// |blob_handles| to the callback to keep them alive until // |blob_data_handles| to the callback to keep them alive
// the waitUntil event is resolved. // until the waitUntil event is resolved.
std::move(blob_handles))); std::move(blob_data_handles)));
} else { } else {
event_dispatcher_.DispatchBackgroundFetchFailEvent( event_dispatcher_.DispatchBackgroundFetchFailEvent(
registration_id, std::move(settled_fetches), registration_id, std::move(settled_fetches),
base::Bind(&BackgroundFetchContext::CleanupRegistration, base::Bind(&BackgroundFetchContext::CleanupRegistration,
weak_factory_.GetWeakPtr(), registration_id, weak_factory_.GetWeakPtr(), registration_id,
// The blob uuid is sent as part of |settled_fetches|. Bind // The blob uuid is sent as part of |settled_fetches|. Bind
// |blob_handles| to the callback to keep them alive until // |blob_data_handles| to the callback to keep them alive
// the waitUntil event is resolved. // until the waitUntil event is resolved.
std::move(blob_handles))); std::move(blob_data_handles)));
} }
} }
void BackgroundFetchContext::CleanupRegistration( void BackgroundFetchContext::CleanupRegistration(
const BackgroundFetchRegistrationId& registration_id, const BackgroundFetchRegistrationId& registration_id,
const std::vector<std::unique_ptr<BlobHandle>>& blob_handles) { const std::vector<std::unique_ptr<storage::BlobDataHandle>>& blob_handles) {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
// If we had an active JobController, it is no longer necessary, as the // If we had an active JobController, it is no longer necessary, as the
......
...@@ -20,13 +20,16 @@ ...@@ -20,13 +20,16 @@
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "third_party/WebKit/public/platform/modules/background_fetch/background_fetch.mojom.h" #include "third_party/WebKit/public/platform/modules/background_fetch/background_fetch.mojom.h"
namespace storage {
class BlobDataHandle;
}
namespace content { namespace content {
class BackgroundFetchJobController; class BackgroundFetchJobController;
struct BackgroundFetchOptions; struct BackgroundFetchOptions;
class BackgroundFetchRegistrationId; class BackgroundFetchRegistrationId;
class BackgroundFetchRegistrationNotifier; class BackgroundFetchRegistrationNotifier;
class BlobHandle;
class BrowserContext; class BrowserContext;
class ServiceWorkerContextWrapper; class ServiceWorkerContextWrapper;
struct ServiceWorkerFetchRequest; struct ServiceWorkerFetchRequest;
...@@ -111,14 +114,15 @@ class CONTENT_EXPORT BackgroundFetchContext ...@@ -111,14 +114,15 @@ class CONTENT_EXPORT BackgroundFetchContext
blink::mojom::BackgroundFetchError error, blink::mojom::BackgroundFetchError error,
bool background_fetch_succeeded, bool background_fetch_succeeded,
std::vector<BackgroundFetchSettledFetch> settled_fetches, std::vector<BackgroundFetchSettledFetch> settled_fetches,
std::vector<std::unique_ptr<BlobHandle>> blob_handles); std::vector<std::unique_ptr<storage::BlobDataHandle>> blob_data_handles);
// Called when all processing for the |registration_id| has been finished and // Called when all processing for the |registration_id| has been finished and
// the job is ready to be deleted. |blob_handles| are unused, but some callers // the job is ready to be deleted. |blob_handles| are unused, but some callers
// use it to keep blobs alive for the right duration. // use it to keep blobs alive for the right duration.
void CleanupRegistration( void CleanupRegistration(
const BackgroundFetchRegistrationId& registration_id, const BackgroundFetchRegistrationId& registration_id,
const std::vector<std::unique_ptr<BlobHandle>>& blob_handles); const std::vector<std::unique_ptr<storage::BlobDataHandle>>&
blob_data_handles);
// Called when the last JavaScript BackgroundFetchRegistration object has been // Called when the last JavaScript BackgroundFetchRegistration object has been
// garbage collected for a registration marked for deletion, and so it is now // garbage collected for a registration marked for deletion, and so it is now
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "base/command_line.h" #include "base/command_line.h"
#include "base/containers/flat_set.h" #include "base/containers/flat_set.h"
#include "base/containers/queue.h" #include "base/containers/queue.h"
#include "base/guid.h"
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/numerics/checked_math.h" #include "base/numerics/checked_math.h"
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
...@@ -21,13 +22,17 @@ ...@@ -21,13 +22,17 @@
#include "content/browser/background_fetch/background_fetch_request_info.h" #include "content/browser/background_fetch/background_fetch_request_info.h"
#include "content/browser/blob_storage/chrome_blob_storage_context.h" #include "content/browser/blob_storage/chrome_blob_storage_context.h"
#include "content/browser/service_worker/service_worker_context_wrapper.h" #include "content/browser/service_worker/service_worker_context_wrapper.h"
#include "content/public/browser/blob_handle.h"
#include "content/public/browser/browser_context.h" #include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "content/public/browser/download_interrupt_reasons.h" #include "content/public/browser/download_interrupt_reasons.h"
#include "content/public/browser/download_item.h" #include "content/public/browser/download_item.h"
#include "content/public/common/content_features.h"
#include "content/public/common/content_switches.h" #include "content/public/common/content_switches.h"
#include "services/network/public/interfaces/fetch_api.mojom.h" #include "services/network/public/interfaces/fetch_api.mojom.h"
#include "storage/browser/blob/blob_data_builder.h"
#include "storage/browser/blob/blob_impl.h"
#include "storage/browser/blob/blob_storage_context.h"
#include "third_party/WebKit/common/blob/blob.mojom.h"
// Service Worker DB UserData schema // Service Worker DB UserData schema
// ================================= // =================================
...@@ -1039,7 +1044,7 @@ void BackgroundFetchDataManager::GetSettledFetchesForRegistration( ...@@ -1039,7 +1044,7 @@ void BackgroundFetchDataManager::GetSettledFetchesForRegistration(
std::vector<BackgroundFetchSettledFetch> settled_fetches; std::vector<BackgroundFetchSettledFetch> settled_fetches;
settled_fetches.reserve(requests.size()); settled_fetches.reserve(requests.size());
std::vector<std::unique_ptr<BlobHandle>> blob_handles; std::vector<std::unique_ptr<storage::BlobDataHandle>> blob_data_handles;
for (const auto& request : requests) { for (const auto& request : requests) {
BackgroundFetchSettledFetch settled_fetch; BackgroundFetchSettledFetch settled_fetch;
...@@ -1063,17 +1068,32 @@ void BackgroundFetchDataManager::GetSettledFetchesForRegistration( ...@@ -1063,17 +1068,32 @@ void BackgroundFetchDataManager::GetSettledFetchesForRegistration(
if (request->GetFileSize() > 0) { if (request->GetFileSize() > 0) {
DCHECK(!request->GetFilePath().empty()); DCHECK(!request->GetFilePath().empty());
std::unique_ptr<BlobHandle> blob_handle = DCHECK(blob_storage_context_);
blob_storage_context_->CreateFileBackedBlob(
request->GetFilePath(), 0 /* offset */, request->GetFileSize(), storage::BlobDataBuilder blob_builder(base::GenerateGUID());
blob_builder.AppendFile(request->GetFilePath(), 0 /* offset */,
request->GetFileSize(),
base::Time() /* expected_modification_time */); base::Time() /* expected_modification_time */);
// TODO(peter): Appropriately handle !blob_handle auto blob_data_handle =
if (blob_handle) { GetBlobStorageContext(blob_storage_context_.get())
settled_fetch.response.blob_uuid = blob_handle->GetUUID(); ->AddFinishedBlob(&blob_builder);
settled_fetch.response.blob_size = request->GetFileSize();
blob_handles.push_back(std::move(blob_handle)); // TODO(peter): Appropriately handle !blob_data_handle
if (blob_data_handle) {
settled_fetch.response.blob_uuid = blob_data_handle->uuid();
settled_fetch.response.blob_size = blob_data_handle->size();
if (features::IsMojoBlobsEnabled()) {
blink::mojom::BlobPtr blob_ptr;
storage::BlobImpl::Create(
std::make_unique<storage::BlobDataHandle>(*blob_data_handle),
MakeRequest(&blob_ptr));
settled_fetch.response.blob =
base::MakeRefCounted<storage::BlobHandle>(std::move(blob_ptr));
}
blob_data_handles.push_back(std::move(blob_data_handle));
} }
} }
} else { } else {
...@@ -1091,9 +1111,9 @@ void BackgroundFetchDataManager::GetSettledFetchesForRegistration( ...@@ -1091,9 +1111,9 @@ void BackgroundFetchDataManager::GetSettledFetchesForRegistration(
settled_fetches.push_back(settled_fetch); settled_fetches.push_back(settled_fetch);
} }
std::move(callback).Run(blink::mojom::BackgroundFetchError::NONE, std::move(callback).Run(
background_fetch_succeeded, blink::mojom::BackgroundFetchError::NONE, background_fetch_succeeded,
std::move(settled_fetches), std::move(blob_handles)); std::move(settled_fetches), std::move(blob_data_handles));
} }
void BackgroundFetchDataManager::MarkRegistrationForDeletion( void BackgroundFetchDataManager::MarkRegistrationForDeletion(
......
...@@ -22,11 +22,14 @@ ...@@ -22,11 +22,14 @@
#include "third_party/WebKit/public/platform/modules/background_fetch/background_fetch.mojom.h" #include "third_party/WebKit/public/platform/modules/background_fetch/background_fetch.mojom.h"
#include "url/origin.h" #include "url/origin.h"
namespace storage {
class BlobDataHandle;
}
namespace content { namespace content {
class BackgroundFetchRequestInfo; class BackgroundFetchRequestInfo;
struct BackgroundFetchSettledFetch; struct BackgroundFetchSettledFetch;
class BlobHandle;
class BrowserContext; class BrowserContext;
class ChromeBlobStorageContext; class ChromeBlobStorageContext;
class ServiceWorkerContextWrapper; class ServiceWorkerContextWrapper;
...@@ -48,11 +51,11 @@ class CONTENT_EXPORT BackgroundFetchDataManager { ...@@ -48,11 +51,11 @@ class CONTENT_EXPORT BackgroundFetchDataManager {
base::OnceCallback<void(scoped_refptr<BackgroundFetchRequestInfo>)>; base::OnceCallback<void(scoped_refptr<BackgroundFetchRequestInfo>)>;
using MarkedCompleteCallback = using MarkedCompleteCallback =
base::OnceCallback<void(bool /* has_pending_or_active_requests */)>; base::OnceCallback<void(bool /* has_pending_or_active_requests */)>;
using SettledFetchesCallback = using SettledFetchesCallback = base::OnceCallback<void(
base::OnceCallback<void(blink::mojom::BackgroundFetchError, blink::mojom::BackgroundFetchError,
bool /* background_fetch_succeeded */, bool /* background_fetch_succeeded */,
std::vector<BackgroundFetchSettledFetch>, std::vector<BackgroundFetchSettledFetch>,
std::vector<std::unique_ptr<BlobHandle>>)>; std::vector<std::unique_ptr<storage::BlobDataHandle>>)>;
// Note that this also handles non-error cases where the NONE is NONE. // Note that this also handles non-error cases where the NONE is NONE.
using HandleBackgroundFetchErrorCallback = using HandleBackgroundFetchErrorCallback =
base::OnceCallback<void(blink::mojom::BackgroundFetchError)>; base::OnceCallback<void(blink::mojom::BackgroundFetchError)>;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment