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 @@
#include "content/browser/background_fetch/background_fetch_registration_notifier.h"
#include "content/browser/service_worker/service_worker_context_wrapper.h"
#include "content/public/browser/background_fetch_delegate.h"
#include "content/public/browser/blob_handle.h"
#include "content/public/browser/browser_context.h"
#include "net/url_request/url_request_context_getter.h"
#include "storage/browser/blob/blob_data_handle.h"
namespace content {
......@@ -172,7 +172,7 @@ void BackgroundFetchContext::DidGetSettledFetches(
blink::mojom::BackgroundFetchError error,
bool background_fetch_succeeded,
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);
if (error != blink::mojom::BackgroundFetchError::NONE) {
......@@ -189,24 +189,24 @@ void BackgroundFetchContext::DidGetSettledFetches(
base::Bind(&BackgroundFetchContext::CleanupRegistration,
weak_factory_.GetWeakPtr(), registration_id,
// The blob uuid is sent as part of |settled_fetches|. Bind
// |blob_handles| to the callback to keep them alive until
// the waitUntil event is resolved.
std::move(blob_handles)));
// |blob_data_handles| to the callback to keep them alive
// until the waitUntil event is resolved.
std::move(blob_data_handles)));
} else {
event_dispatcher_.DispatchBackgroundFetchFailEvent(
registration_id, std::move(settled_fetches),
base::Bind(&BackgroundFetchContext::CleanupRegistration,
weak_factory_.GetWeakPtr(), registration_id,
// The blob uuid is sent as part of |settled_fetches|. Bind
// |blob_handles| to the callback to keep them alive until
// the waitUntil event is resolved.
std::move(blob_handles)));
// |blob_data_handles| to the callback to keep them alive
// until the waitUntil event is resolved.
std::move(blob_data_handles)));
}
}
void BackgroundFetchContext::CleanupRegistration(
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);
// If we had an active JobController, it is no longer necessary, as the
......
......@@ -20,13 +20,16 @@
#include "content/public/browser/browser_thread.h"
#include "third_party/WebKit/public/platform/modules/background_fetch/background_fetch.mojom.h"
namespace storage {
class BlobDataHandle;
}
namespace content {
class BackgroundFetchJobController;
struct BackgroundFetchOptions;
class BackgroundFetchRegistrationId;
class BackgroundFetchRegistrationNotifier;
class BlobHandle;
class BrowserContext;
class ServiceWorkerContextWrapper;
struct ServiceWorkerFetchRequest;
......@@ -111,14 +114,15 @@ class CONTENT_EXPORT BackgroundFetchContext
blink::mojom::BackgroundFetchError error,
bool background_fetch_succeeded,
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
// the job is ready to be deleted. |blob_handles| are unused, but some callers
// use it to keep blobs alive for the right duration.
void CleanupRegistration(
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
// garbage collected for a registration marked for deletion, and so it is now
......
......@@ -10,6 +10,7 @@
#include "base/command_line.h"
#include "base/containers/flat_set.h"
#include "base/containers/queue.h"
#include "base/guid.h"
#include "base/memory/ptr_util.h"
#include "base/numerics/checked_math.h"
#include "base/strings/string_number_conversions.h"
......@@ -21,13 +22,17 @@
#include "content/browser/background_fetch/background_fetch_request_info.h"
#include "content/browser/blob_storage/chrome_blob_storage_context.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_thread.h"
#include "content/public/browser/download_interrupt_reasons.h"
#include "content/public/browser/download_item.h"
#include "content/public/common/content_features.h"
#include "content/public/common/content_switches.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
// =================================
......@@ -1039,7 +1044,7 @@ void BackgroundFetchDataManager::GetSettledFetchesForRegistration(
std::vector<BackgroundFetchSettledFetch> settled_fetches;
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) {
BackgroundFetchSettledFetch settled_fetch;
......@@ -1063,17 +1068,32 @@ void BackgroundFetchDataManager::GetSettledFetchesForRegistration(
if (request->GetFileSize() > 0) {
DCHECK(!request->GetFilePath().empty());
std::unique_ptr<BlobHandle> blob_handle =
blob_storage_context_->CreateFileBackedBlob(
request->GetFilePath(), 0 /* offset */, request->GetFileSize(),
base::Time() /* expected_modification_time */);
// TODO(peter): Appropriately handle !blob_handle
if (blob_handle) {
settled_fetch.response.blob_uuid = blob_handle->GetUUID();
settled_fetch.response.blob_size = request->GetFileSize();
DCHECK(blob_storage_context_);
storage::BlobDataBuilder blob_builder(base::GenerateGUID());
blob_builder.AppendFile(request->GetFilePath(), 0 /* offset */,
request->GetFileSize(),
base::Time() /* expected_modification_time */);
auto blob_data_handle =
GetBlobStorageContext(blob_storage_context_.get())
->AddFinishedBlob(&blob_builder);
// 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_handles.push_back(std::move(blob_handle));
blob_data_handles.push_back(std::move(blob_data_handle));
}
}
} else {
......@@ -1091,9 +1111,9 @@ void BackgroundFetchDataManager::GetSettledFetchesForRegistration(
settled_fetches.push_back(settled_fetch);
}
std::move(callback).Run(blink::mojom::BackgroundFetchError::NONE,
background_fetch_succeeded,
std::move(settled_fetches), std::move(blob_handles));
std::move(callback).Run(
blink::mojom::BackgroundFetchError::NONE, background_fetch_succeeded,
std::move(settled_fetches), std::move(blob_data_handles));
}
void BackgroundFetchDataManager::MarkRegistrationForDeletion(
......
......@@ -22,11 +22,14 @@
#include "third_party/WebKit/public/platform/modules/background_fetch/background_fetch.mojom.h"
#include "url/origin.h"
namespace storage {
class BlobDataHandle;
}
namespace content {
class BackgroundFetchRequestInfo;
struct BackgroundFetchSettledFetch;
class BlobHandle;
class BrowserContext;
class ChromeBlobStorageContext;
class ServiceWorkerContextWrapper;
......@@ -48,11 +51,11 @@ class CONTENT_EXPORT BackgroundFetchDataManager {
base::OnceCallback<void(scoped_refptr<BackgroundFetchRequestInfo>)>;
using MarkedCompleteCallback =
base::OnceCallback<void(bool /* has_pending_or_active_requests */)>;
using SettledFetchesCallback =
base::OnceCallback<void(blink::mojom::BackgroundFetchError,
bool /* background_fetch_succeeded */,
std::vector<BackgroundFetchSettledFetch>,
std::vector<std::unique_ptr<BlobHandle>>)>;
using SettledFetchesCallback = base::OnceCallback<void(
blink::mojom::BackgroundFetchError,
bool /* background_fetch_succeeded */,
std::vector<BackgroundFetchSettledFetch>,
std::vector<std::unique_ptr<storage::BlobDataHandle>>)>;
// Note that this also handles non-error cases where the NONE is NONE.
using HandleBackgroundFetchErrorCallback =
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