Commit 2b9f6363 authored by Mugdha Lakhani's avatar Mugdha Lakhani Committed by Commit Bot

[BackgroundFetch] Part 1: Remove BackgroundFetchSettledFetches.

This removes BackgroundFetchSettledFetches from blink, idl files,
and the WPT tests.

In a follow up CL I'll remove it from all the event dispatcher code
on the browser side.

This fix also makes cache_query_params optional in MatchRequests() in mojo.
This is required because otherwise the mojo bindings logic crashes the renderer
when we try to pass in a nullptr for cache_query_params from blink.

Bug: 869918, 863016
Change-Id: Ifa933451f98f82902d8455ba5401c629c8c24811
Reviewed-on: https://chromium-review.googlesource.com/1186735
Commit-Queue: Mugdha Lakhani <nator@chromium.org>
Reviewed-by: default avatarPeter Beverloo <peter@chromium.org>
Reviewed-by: default avatarRayan Kanso <rayankans@chromium.org>
Reviewed-by: default avatarDmitry Gozman <dgozman@chromium.org>
Reviewed-by: default avatarTom Sepez <tsepez@chromium.org>
Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Cr-Commit-Position: refs/heads/master@{#585847}
parent fcc71691
importScripts('sw-helpers.js');
async function getFetchResult(settledFetch) {
if (!settledFetch.response)
async function getFetchResult(record) {
response = await record.responseReady;
if (!response)
return Promise.resolve(null);
return {
url: settledFetch.response.url,
status: settledFetch.response.status,
text: await settledFetch.response.text(),
url: response.url,
status: response.status,
text: await response.text(),
};
}
self.addEventListener('backgroundfetchsuccess', event => {
event.waitUntil(
event.fetches.values()
.then(fetches => Promise.all(fetches.map(fetch => getFetchResult(fetch))))
event.registration.matchAll()
.then(records => Promise.all(records.map(record => getFetchResult(record))))
.then(results => sendMessageToDocument({ type: event.type, results })));
});
......@@ -51,14 +51,8 @@ interface BackgroundFetchSettledFetch : BackgroundFetchFetch
attribute @@toStringTag
getter response
method constructor
interface BackgroundFetchSettledFetches
attribute @@toStringTag
method constructor
method match
method values
interface BackgroundFetchUpdateUIEvent : BackgroundFetchEvent
attribute @@toStringTag
getter fetches
method constructor
method updateUI
interface BarcodeDetector
......
......@@ -152,7 +152,7 @@ interface BackgroundFetchService {
string developer_id,
string unique_id,
FetchAPIRequest? request_to_match,
QueryParams cache_query_params,
QueryParams? cache_query_params,
bool match_all) => (array<BackgroundFetchSettledFetch> fetches);
// Registers the |observer| to receive events for the given registration
......
......@@ -242,7 +242,6 @@ jumbo_source_set("unit_tests") {
"animationworklet/worklet_animation_test.cc",
"background_fetch/background_fetch_icon_loader_test.cc",
"background_fetch/background_fetch_manager_test.cc",
"background_fetch/background_fetch_settled_fetches_test.cc",
"cache_storage/cache_test.cc",
"canvas/canvas2d/canvas_rendering_context_2d_api_test.cc",
"canvas/canvas2d/canvas_rendering_context_2d_test.cc",
......
......@@ -22,8 +22,6 @@ blink_modules_sources("background_fetch") {
"background_fetch_registration.h",
"background_fetch_settled_fetch.cc",
"background_fetch_settled_fetch.h",
"background_fetch_settled_fetches.cc",
"background_fetch_settled_fetches.h",
"background_fetch_type_converters.cc",
"background_fetch_type_converters.h",
"background_fetch_update_ui_event.cc",
......
// 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 "third_party/blink/renderer/modules/background_fetch/background_fetch_settled_fetches.h"
#include "third_party/blink/renderer/core/fetch/request.h"
#include "third_party/blink/renderer/core/fetch/response.h"
namespace blink {
BackgroundFetchSettledFetches::BackgroundFetchSettledFetches(
ScriptState* script_state,
const WebVector<WebBackgroundFetchSettledFetch>& fetches) {
fetches_.ReserveInitialCapacity(fetches.size());
for (const WebBackgroundFetchSettledFetch& fetch : fetches) {
auto* settled_fetch = BackgroundFetchSettledFetch::Create(
Request::Create(script_state, fetch.request),
Response::Create(script_state, fetch.response));
fetches_.push_back(settled_fetch);
}
}
ScriptPromise BackgroundFetchSettledFetches::match(
ScriptState* script_state,
const RequestOrUSVString& request) {
for (const auto& fetch : fetches_) {
if (request.IsNull())
continue;
String request_string = request.IsUSVString()
? request.GetAsUSVString()
: request.GetAsRequest()->url().GetString();
// TODO(crbug.com/824765): Update the resolve condition once behavior of
// match is defined.
if (request_string == fetch->request()->url())
return ScriptPromise::Cast(script_state, ToV8(fetch, script_state));
}
return ScriptPromise::Cast(script_state,
v8::Null(script_state->GetIsolate()));
}
ScriptPromise BackgroundFetchSettledFetches::values(ScriptState* script_state) {
return ScriptPromise::Cast(script_state, ToV8(fetches_, script_state));
}
void BackgroundFetchSettledFetches::Trace(blink::Visitor* visitor) {
visitor->Trace(fetches_);
ScriptWrappable::Trace(visitor);
}
} // namespace blink
// 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.
#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_BACKGROUND_FETCH_BACKGROUND_FETCH_SETTLED_FETCHES_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_BACKGROUND_FETCH_BACKGROUND_FETCH_SETTLED_FETCHES_H_
#include "third_party/blink/public/platform/modules/background_fetch/web_background_fetch_settled_fetch.h"
#include "third_party/blink/public/platform/web_vector.h"
#include "third_party/blink/renderer/bindings/core/v8/request_or_usv_string.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
#include "third_party/blink/renderer/modules/background_fetch/background_fetch_settled_fetch.h"
#include "third_party/blink/renderer/modules/modules_export.h"
#include "third_party/blink/renderer/platform/bindings/script_state.h"
#include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
namespace blink {
class MODULES_EXPORT BackgroundFetchSettledFetches final
: public ScriptWrappable {
DEFINE_WRAPPERTYPEINFO();
public:
static BackgroundFetchSettledFetches* Create(
ScriptState* script_state,
const WebVector<WebBackgroundFetchSettledFetch>& fetches) {
return new BackgroundFetchSettledFetches(script_state, fetches);
}
~BackgroundFetchSettledFetches() override = default;
// Web Exposed functions defined in the IDL file.
ScriptPromise match(ScriptState* script_state,
const RequestOrUSVString& request);
ScriptPromise values(ScriptState* script_state);
void Trace(blink::Visitor* visitor) override;
private:
BackgroundFetchSettledFetches(
ScriptState* script_state,
const WebVector<WebBackgroundFetchSettledFetch>& fetches);
HeapVector<Member<BackgroundFetchSettledFetch>> fetches_;
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_BACKGROUND_FETCH_BACKGROUND_FETCH_SETTLED_FETCHES_H_
// 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.
// https://wicg.github.io/background-fetch/#backgroundfetchsettledevent
[
Exposed=ServiceWorker,
RuntimeEnabled=BackgroundFetch
] interface BackgroundFetchSettledFetches {
[CallWith=ScriptState] Promise<BackgroundFetchSettledFetch> match(RequestInfo request);
[CallWith=ScriptState] Promise<FrozenArray<BackgroundFetchSettledFetch>> values();
};
// 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 "third_party/blink/renderer/modules/background_fetch/background_fetch_settled_fetches.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/bindings/core/v8/script_function.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_core.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_testing.h"
#include "third_party/blink/renderer/core/fetch/request.h"
namespace blink {
namespace {
WebVector<WebBackgroundFetchSettledFetch> CreateSettledFetches(
const std::vector<String>& request_urls) {
WebVector<WebBackgroundFetchSettledFetch> settled_fetches;
settled_fetches.reserve(request_urls.size());
for (const auto& request_url : request_urls) {
WebBackgroundFetchSettledFetch settled_fetch;
settled_fetch.request.SetURL(WebURL(KURL(request_url)));
settled_fetches.emplace_back(settled_fetch);
}
return settled_fetches;
}
class TestScriptFunctionHandler {
// Stats filled by the functions executed by test promises.
struct CallStats {
size_t num_calls = 0;
ScriptValue value;
};
// ScriptFunction run by test promises. Extracts the resolved value.
class TestScriptFunction : public ScriptFunction {
public:
static v8::Local<v8::Function> CreateFunction(ScriptState* script_state,
CallStats* stats) {
return (new TestScriptFunction(script_state, stats))->BindToV8Function();
}
ScriptValue Call(ScriptValue value) override {
stats_->value = value;
stats_->num_calls++;
return ScriptValue();
}
private:
TestScriptFunction(ScriptState* script_state, CallStats* stats)
: ScriptFunction(script_state), stats_(stats) {}
// Pointer to the private CallStats member variable in
// TestScriptFunctionHandler. Whenever the associated function is called,
// the CallStats variable is updated. Internal values can be accessed via
// the public getters.
CallStats* stats_;
};
public:
TestScriptFunctionHandler() = default;
v8::Local<v8::Function> GetFunction(ScriptState* script_state) {
return TestScriptFunction::CreateFunction(script_state, &stats_);
}
size_t NumCalls() const { return stats_.num_calls; }
ScriptValue Value() const { return stats_.value; }
private:
CallStats stats_;
};
ScriptValue ResolvePromise(ScriptState* script_state, ScriptPromise& promise) {
TestScriptFunctionHandler resolved;
TestScriptFunctionHandler rejected;
promise.Then(resolved.GetFunction(script_state),
rejected.GetFunction(script_state));
v8::MicrotasksScope::PerformCheckpoint(promise.GetIsolate());
EXPECT_EQ(1ul, resolved.NumCalls());
EXPECT_EQ(0ul, rejected.NumCalls());
return resolved.Value();
}
} // namespace
TEST(BackgroundFetchSettledFetchesTest, MatchNullValue) {
V8TestingScope scope;
RequestOrUSVString null_request;
auto settled_fetches = CreateSettledFetches({"foo.com"});
auto* bgf_settled_fetches = BackgroundFetchSettledFetches::Create(
scope.GetScriptState(), settled_fetches);
ScriptPromise promise =
bgf_settled_fetches->match(scope.GetScriptState(), null_request);
ScriptValue value = ResolvePromise(scope.GetScriptState(), promise);
EXPECT_TRUE(value.IsNull());
}
TEST(BackgroundFetchSettledFetchesTest, MatchUSVString) {
V8TestingScope scope;
auto matched_request = RequestOrUSVString::FromUSVString("http://foo.com/");
auto unmatched_request = RequestOrUSVString::FromUSVString("http://bar.com/");
auto settled_fetches = CreateSettledFetches(
{"http://t1.net/", "http://foo.com/", "http://t3.net/"});
auto* bgf_settled_fetches = BackgroundFetchSettledFetches::Create(
scope.GetScriptState(), settled_fetches);
ScriptPromise matched_promise =
bgf_settled_fetches->match(scope.GetScriptState(), matched_request);
ScriptPromise unmatched_promise =
bgf_settled_fetches->match(scope.GetScriptState(), unmatched_request);
ScriptValue matched_value =
ResolvePromise(scope.GetScriptState(), matched_promise);
EXPECT_TRUE(matched_value.IsObject());
ScriptValue unmatched_value =
ResolvePromise(scope.GetScriptState(), unmatched_promise);
EXPECT_TRUE(unmatched_value.IsNull());
}
TEST(BackgroundFetchSettledFetchesTest, MatchRequest) {
V8TestingScope scope;
auto matched_request = RequestOrUSVString::FromRequest(Request::Create(
scope.GetScriptState(), "http://foo.com/", scope.GetExceptionState()));
auto unmatched_request = RequestOrUSVString::FromRequest(Request::Create(
scope.GetScriptState(), "http://bar.com/", scope.GetExceptionState()));
auto settled_fetches = CreateSettledFetches(
{"http://t1.net/", "http://foo.com/", "http://t3.net/"});
auto* bgf_settled_fetches = BackgroundFetchSettledFetches::Create(
scope.GetScriptState(), settled_fetches);
ScriptPromise matched_promise =
bgf_settled_fetches->match(scope.GetScriptState(), matched_request);
ScriptPromise unmatched_promise =
bgf_settled_fetches->match(scope.GetScriptState(), unmatched_request);
ScriptValue matched_value =
ResolvePromise(scope.GetScriptState(), matched_promise);
EXPECT_TRUE(matched_value.IsObject());
ScriptValue unmatched_value =
ResolvePromise(scope.GetScriptState(), unmatched_promise);
EXPECT_TRUE(unmatched_value.IsNull());
}
} // namespace blink
......@@ -38,7 +38,6 @@ BackgroundFetchUpdateUIEvent::~BackgroundFetchUpdateUIEvent() = default;
void BackgroundFetchUpdateUIEvent::Trace(blink::Visitor* visitor) {
visitor->Trace(service_worker_registration_);
visitor->Trace(loader_);
visitor->Trace(fetches_);
BackgroundFetchEvent::Trace(visitor);
}
......
......@@ -8,7 +8,6 @@
#include "third_party/blink/public/platform/modules/background_fetch/background_fetch.mojom-blink.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
#include "third_party/blink/renderer/modules/background_fetch/background_fetch_event.h"
#include "third_party/blink/renderer/modules/background_fetch/background_fetch_settled_fetches.h"
#include "third_party/blink/renderer/modules/service_worker/service_worker_registration.h"
#include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
#include "third_party/blink/renderer/platform/wtf/text/atomic_string.h"
......@@ -45,11 +44,6 @@ class MODULES_EXPORT BackgroundFetchUpdateUIEvent final
~BackgroundFetchUpdateUIEvent() override;
// TODO(crbug.com/699957): Remove once matchAll() has been implemented on
// BackgroundFetchRegistration.
BackgroundFetchSettledFetches* fetches() const { return fetches_; }
void setFetches(BackgroundFetchSettledFetches* value) { fetches_ = value; }
// Web Exposed method defined in the IDL file.
ScriptPromise updateUI(ScriptState* script_state,
const BackgroundFetchUIOptions& ui_options);
......@@ -76,7 +70,6 @@ class MODULES_EXPORT BackgroundFetchUpdateUIEvent final
Member<ServiceWorkerRegistration> service_worker_registration_;
Member<BackgroundFetchIconLoader> loader_;
Member<BackgroundFetchSettledFetches> fetches_;
};
} // namespace blink
......
......@@ -9,8 +9,5 @@
Exposed=ServiceWorker,
RuntimeEnabled=BackgroundFetch
] interface BackgroundFetchUpdateUIEvent : BackgroundFetchEvent {
// TODO(crbug.com/863016): Remove fetches after implementing matchAll().
readonly attribute BackgroundFetchSettledFetches fetches;
[CallWith=ScriptState] Promise<void> updateUI(BackgroundFetchUIOptions options);
};
\ No newline at end of file
......@@ -70,7 +70,6 @@ modules_idl_files =
"background_fetch/background_fetch_record.idl",
"background_fetch/background_fetch_registration.idl",
"background_fetch/background_fetch_settled_fetch.idl",
"background_fetch/background_fetch_settled_fetches.idl",
"background_fetch/background_fetch_update_ui_event.idl",
"background_sync/sync_event.idl",
"background_sync/sync_manager.idl",
......
......@@ -54,7 +54,6 @@
#include "third_party/blink/renderer/modules/background_fetch/background_fetch_event.h"
#include "third_party/blink/renderer/modules/background_fetch/background_fetch_event_init.h"
#include "third_party/blink/renderer/modules/background_fetch/background_fetch_registration.h"
#include "third_party/blink/renderer/modules/background_fetch/background_fetch_settled_fetches.h"
#include "third_party/blink/renderer/modules/background_fetch/background_fetch_update_ui_event.h"
#include "third_party/blink/renderer/modules/background_sync/sync_event.h"
#include "third_party/blink/renderer/modules/cookie_store/cookie_change_event.h"
......@@ -126,7 +125,7 @@ void ServiceWorkerGlobalScopeProxy::DispatchBackgroundFetchAbortEvent(
WorkerGlobalScope()->ScriptController()->GetScriptState();
// Do not remove this, |scope| is needed by
// BackgroundFetchSettledFetches::Create which eventually calls ToV8.
// BackgroundFetchEvent::Create which eventually calls ToV8.
ScriptState::Scope scope(script_state);
BackgroundFetchEventInit init;
......@@ -170,7 +169,7 @@ void ServiceWorkerGlobalScopeProxy::DispatchBackgroundFetchFailEvent(
WorkerGlobalScope()->ScriptController()->GetScriptState();
// Do not remove this, |scope| is needed by
// BackgroundFetchSettledFetches::Create which eventually calls ToV8.
// BackgroundFetchSettledEvent::Create which eventually calls ToV8.
ScriptState::Scope scope(script_state);
BackgroundFetchEventInit init;
......@@ -181,8 +180,6 @@ void ServiceWorkerGlobalScopeProxy::DispatchBackgroundFetchFailEvent(
BackgroundFetchUpdateUIEvent* event = BackgroundFetchUpdateUIEvent::Create(
EventTypeNames::backgroundfetchfail, init, observer,
worker_global_scope_->registration());
event->setFetches(
BackgroundFetchSettledFetches::Create(script_state, fetches));
WorkerGlobalScope()->DispatchExtendableEvent(event, observer);
}
......@@ -200,7 +197,7 @@ void ServiceWorkerGlobalScopeProxy::DispatchBackgroundFetchSuccessEvent(
WorkerGlobalScope()->ScriptController()->GetScriptState();
// Do not remove this, |scope| is needed by
// BackgroundFetchSettledFetches::Create which eventually calls ToV8.
// BackgroundFetchSettledEvent::Create which eventually calls ToV8.
ScriptState::Scope scope(script_state);
BackgroundFetchEventInit init;
......@@ -211,8 +208,6 @@ void ServiceWorkerGlobalScopeProxy::DispatchBackgroundFetchSuccessEvent(
BackgroundFetchUpdateUIEvent* event = BackgroundFetchUpdateUIEvent::Create(
EventTypeNames::backgroundfetchsuccess, init, observer,
worker_global_scope_->registration());
event->setFetches(
BackgroundFetchSettledFetches::Create(script_state, fetches));
WorkerGlobalScope()->DispatchExtendableEvent(event, observer);
}
......
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