Commit 22db776a authored by Rayan Kanso's avatar Rayan Kanso Committed by Commit Bot

[Background Fetch] Add BackgroundFetchSettledEvent idl files and basic implementation.

Specs are defined in https://wicg.github.io/background-fetch/#backgroundfetchsettledevent.

This is the first step in replacing the BackgroundFetched* API.

Bug: 822765
Change-Id: Ib725b0c389edc32563f45bd26d72621b7031ad31
Reviewed-on: https://chromium-review.googlesource.com/975549
Commit-Queue: Rayan Kanso <rayankans@chromium.org>
Reviewed-by: default avatarMike West <mkwst@chromium.org>
Reviewed-by: default avatarPeter Beverloo <peter@chromium.org>
Cr-Commit-Position: refs/heads/master@{#549226}
parent 17c889fe
...@@ -79,23 +79,23 @@ PASS BackgroundFetchEvent interface: existence and properties of interface proto ...@@ -79,23 +79,23 @@ PASS BackgroundFetchEvent interface: existence and properties of interface proto
PASS BackgroundFetchEvent interface: existence and properties of interface prototype object's @@unscopables property PASS BackgroundFetchEvent interface: existence and properties of interface prototype object's @@unscopables property
PASS BackgroundFetchEvent interface: attribute id PASS BackgroundFetchEvent interface: attribute id
PASS Unscopable handled correctly for id property on BackgroundFetchEvent PASS Unscopable handled correctly for id property on BackgroundFetchEvent
FAIL BackgroundFetchSettledEvent interface: existence and properties of interface object assert_own_property: self does not have own property "BackgroundFetchSettledEvent" expected property "BackgroundFetchSettledEvent" missing PASS BackgroundFetchSettledEvent interface: existence and properties of interface object
FAIL BackgroundFetchSettledEvent interface object length assert_own_property: self does not have own property "BackgroundFetchSettledEvent" expected property "BackgroundFetchSettledEvent" missing PASS BackgroundFetchSettledEvent interface object length
FAIL BackgroundFetchSettledEvent interface object name assert_own_property: self does not have own property "BackgroundFetchSettledEvent" expected property "BackgroundFetchSettledEvent" missing PASS BackgroundFetchSettledEvent interface object name
FAIL BackgroundFetchSettledEvent interface: existence and properties of interface prototype object assert_own_property: self does not have own property "BackgroundFetchSettledEvent" expected property "BackgroundFetchSettledEvent" missing PASS BackgroundFetchSettledEvent interface: existence and properties of interface prototype object
FAIL BackgroundFetchSettledEvent interface: existence and properties of interface prototype object's "constructor" property assert_own_property: self does not have own property "BackgroundFetchSettledEvent" expected property "BackgroundFetchSettledEvent" missing PASS BackgroundFetchSettledEvent interface: existence and properties of interface prototype object's "constructor" property
FAIL BackgroundFetchSettledEvent interface: existence and properties of interface prototype object's @@unscopables property assert_own_property: self does not have own property "BackgroundFetchSettledEvent" expected property "BackgroundFetchSettledEvent" missing PASS BackgroundFetchSettledEvent interface: existence and properties of interface prototype object's @@unscopables property
FAIL BackgroundFetchSettledEvent interface: attribute fetches assert_own_property: self does not have own property "BackgroundFetchSettledEvent" expected property "BackgroundFetchSettledEvent" missing PASS BackgroundFetchSettledEvent interface: attribute fetches
PASS Unscopable handled correctly for fetches property on BackgroundFetchSettledEvent PASS Unscopable handled correctly for fetches property on BackgroundFetchSettledEvent
FAIL BackgroundFetchSettledFetches interface: existence and properties of interface object assert_own_property: self does not have own property "BackgroundFetchSettledFetches" expected property "BackgroundFetchSettledFetches" missing PASS BackgroundFetchSettledFetches interface: existence and properties of interface object
FAIL BackgroundFetchSettledFetches interface object length assert_own_property: self does not have own property "BackgroundFetchSettledFetches" expected property "BackgroundFetchSettledFetches" missing PASS BackgroundFetchSettledFetches interface object length
FAIL BackgroundFetchSettledFetches interface object name assert_own_property: self does not have own property "BackgroundFetchSettledFetches" expected property "BackgroundFetchSettledFetches" missing PASS BackgroundFetchSettledFetches interface object name
FAIL BackgroundFetchSettledFetches interface: existence and properties of interface prototype object assert_own_property: self does not have own property "BackgroundFetchSettledFetches" expected property "BackgroundFetchSettledFetches" missing PASS BackgroundFetchSettledFetches interface: existence and properties of interface prototype object
FAIL BackgroundFetchSettledFetches interface: existence and properties of interface prototype object's "constructor" property assert_own_property: self does not have own property "BackgroundFetchSettledFetches" expected property "BackgroundFetchSettledFetches" missing PASS BackgroundFetchSettledFetches interface: existence and properties of interface prototype object's "constructor" property
FAIL BackgroundFetchSettledFetches interface: existence and properties of interface prototype object's @@unscopables property assert_own_property: self does not have own property "BackgroundFetchSettledFetches" expected property "BackgroundFetchSettledFetches" missing PASS BackgroundFetchSettledFetches interface: existence and properties of interface prototype object's @@unscopables property
FAIL BackgroundFetchSettledFetches interface: operation match(RequestInfo) assert_own_property: self does not have own property "BackgroundFetchSettledFetches" expected property "BackgroundFetchSettledFetches" missing PASS BackgroundFetchSettledFetches interface: operation match(RequestInfo)
PASS Unscopable handled correctly for match(RequestInfo) on BackgroundFetchSettledFetches PASS Unscopable handled correctly for match(RequestInfo) on BackgroundFetchSettledFetches
FAIL BackgroundFetchSettledFetches interface: operation values() assert_own_property: self does not have own property "BackgroundFetchSettledFetches" expected property "BackgroundFetchSettledFetches" missing PASS BackgroundFetchSettledFetches interface: operation values()
PASS Unscopable handled correctly for values() on BackgroundFetchSettledFetches PASS Unscopable handled correctly for values() on BackgroundFetchSettledFetches
PASS BackgroundFetchSettledFetch interface: existence and properties of interface object PASS BackgroundFetchSettledFetch interface: existence and properties of interface object
FAIL BackgroundFetchSettledFetch interface object length assert_equals: wrong value for BackgroundFetchSettledFetch.length expected 0 but got 2 FAIL BackgroundFetchSettledFetch interface object length assert_equals: wrong value for BackgroundFetchSettledFetch.length expected 0 but got 2
......
...@@ -47,10 +47,19 @@ interface BackgroundFetchRegistration : EventTarget ...@@ -47,10 +47,19 @@ interface BackgroundFetchRegistration : EventTarget
method abort method abort
method constructor method constructor
setter onprogress setter onprogress
interface BackgroundFetchSettledEvent : BackgroundFetchEvent
attribute @@toStringTag
getter fetches
method constructor
interface BackgroundFetchSettledFetch : BackgroundFetchFetch interface BackgroundFetchSettledFetch : BackgroundFetchFetch
attribute @@toStringTag attribute @@toStringTag
getter response getter response
method constructor method constructor
interface BackgroundFetchSettledFetches
attribute @@toStringTag
method constructor
method match
method values
interface BackgroundFetchedEvent : BackgroundFetchEvent interface BackgroundFetchedEvent : BackgroundFetchEvent
attribute @@toStringTag attribute @@toStringTag
getter fetches getter fetches
......
...@@ -237,6 +237,7 @@ jumbo_source_set("unit_tests") { ...@@ -237,6 +237,7 @@ jumbo_source_set("unit_tests") {
"animationworklet/worklet_animation_test.cc", "animationworklet/worklet_animation_test.cc",
"background_fetch/background_fetch_icon_loader_test.cc", "background_fetch/background_fetch_icon_loader_test.cc",
"background_fetch/background_fetch_manager_test.cc", "background_fetch/background_fetch_manager_test.cc",
"background_fetch/background_fetch_settled_fetches_test.cc",
"cachestorage/cache_test.cc", "cachestorage/cache_test.cc",
"canvas/canvas2d/canvas_rendering_context_2d_api_test.cc", "canvas/canvas2d/canvas_rendering_context_2d_api_test.cc",
"canvas/canvas2d/canvas_rendering_context_2d_test.cc", "canvas/canvas2d/canvas_rendering_context_2d_test.cc",
......
...@@ -22,8 +22,12 @@ blink_modules_sources("background_fetch") { ...@@ -22,8 +22,12 @@ blink_modules_sources("background_fetch") {
"background_fetch_manager.h", "background_fetch_manager.h",
"background_fetch_registration.cc", "background_fetch_registration.cc",
"background_fetch_registration.h", "background_fetch_registration.h",
"background_fetch_settled_event.cc",
"background_fetch_settled_event.h",
"background_fetch_settled_fetch.cc", "background_fetch_settled_fetch.cc",
"background_fetch_settled_fetch.h", "background_fetch_settled_fetch.h",
"background_fetch_settled_fetches.cc",
"background_fetch_settled_fetches.h",
"background_fetch_type_converters.cc", "background_fetch_type_converters.cc",
"background_fetch_type_converters.h", "background_fetch_type_converters.h",
"background_fetched_event.cc", "background_fetched_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_event.h"
namespace blink {
BackgroundFetchSettledEvent::BackgroundFetchSettledEvent(
const AtomicString& type,
const BackgroundFetchSettledEventInit& initializer)
: BackgroundFetchEvent(type, initializer, nullptr /* observer */),
fetches_(*initializer.fetches()) {}
BackgroundFetchSettledEvent::~BackgroundFetchSettledEvent() = default;
BackgroundFetchSettledFetches* BackgroundFetchSettledEvent::fetches() const {
return fetches_;
}
void BackgroundFetchSettledEvent::Trace(blink::Visitor* visitor) {
visitor->Trace(fetches_);
BackgroundFetchEvent::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_EVENT_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_BACKGROUND_FETCH_BACKGROUND_FETCH_SETTLED_EVENT_H_
#include "third_party/blink/renderer/modules/background_fetch/background_fetch_event.h"
#include "third_party/blink/renderer/modules/background_fetch/background_fetch_settled_event_init.h"
#include "third_party/blink/renderer/modules/background_fetch/background_fetch_settled_fetches.h"
#include "third_party/blink/renderer/modules/modules_export.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/heap/member.h"
#include "third_party/blink/renderer/platform/wtf/text/atomic_string.h"
namespace blink {
// Event for interacting with fetch requests that have completed.
class MODULES_EXPORT BackgroundFetchSettledEvent final
: public BackgroundFetchEvent {
DEFINE_WRAPPERTYPEINFO();
public:
static BackgroundFetchSettledEvent* Create(
const AtomicString& type,
const BackgroundFetchSettledEventInit& initializer) {
return new BackgroundFetchSettledEvent(type, initializer);
}
~BackgroundFetchSettledEvent() override;
// Web Exposed attribute defined in the IDL file.
BackgroundFetchSettledFetches* fetches() const;
void Trace(blink::Visitor*) override;
private:
BackgroundFetchSettledEvent(
const AtomicString& type,
const BackgroundFetchSettledEventInit& initializer);
Member<BackgroundFetchSettledFetches> fetches_;
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_BACKGROUND_FETCH_BACKGROUND_FETCH_SETTLED_EVENT_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
[
Constructor(DOMString type, BackgroundFetchSettledEventInit init),
Exposed=ServiceWorker,
RuntimeEnabled=BackgroundFetch
] interface BackgroundFetchSettledEvent : BackgroundFetchEvent {
readonly attribute BackgroundFetchSettledFetches fetches;
};
// 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
dictionary BackgroundFetchSettledEventInit : BackgroundFetchEventInit {
required BackgroundFetchSettledFetches fetches;
};
\ No newline at end of file
// 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:
BackgroundFetchSettledFetches(
ScriptState*,
const WebVector<WebBackgroundFetchSettledFetch>& fetches);
~BackgroundFetchSettledFetches() override = default;
// Web Exposed functions defined in the IDL file.
ScriptPromise match(ScriptState*, const RequestOrUSVString&);
ScriptPromise values(ScriptState*);
void Trace(blink::Visitor*);
private:
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/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"});
BackgroundFetchSettledFetches bgf_settled_fetches(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/"});
BackgroundFetchSettledFetches bgf_settled_fetches(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/"});
BackgroundFetchSettledFetches bgf_settled_fetches(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
...@@ -69,7 +69,9 @@ modules_idl_files = ...@@ -69,7 +69,9 @@ modules_idl_files =
"background_fetch/background_fetch_fetch.idl", "background_fetch/background_fetch_fetch.idl",
"background_fetch/background_fetch_manager.idl", "background_fetch/background_fetch_manager.idl",
"background_fetch/background_fetch_registration.idl", "background_fetch/background_fetch_registration.idl",
"background_fetch/background_fetch_settled_event.idl",
"background_fetch/background_fetch_settled_fetch.idl", "background_fetch/background_fetch_settled_fetch.idl",
"background_fetch/background_fetch_settled_fetches.idl",
"background_fetch/background_fetched_event.idl", "background_fetch/background_fetched_event.idl",
"background_sync/sync_event.idl", "background_sync/sync_event.idl",
"background_sync/sync_manager.idl", "background_sync/sync_manager.idl",
...@@ -454,6 +456,7 @@ modules_dictionary_idl_files = ...@@ -454,6 +456,7 @@ modules_dictionary_idl_files =
"background_fetch/background_fetch_event_init.idl", "background_fetch/background_fetch_event_init.idl",
"background_fetch/background_fetch_fail_event_init.idl", "background_fetch/background_fetch_fail_event_init.idl",
"background_fetch/background_fetch_options.idl", "background_fetch/background_fetch_options.idl",
"background_fetch/background_fetch_settled_event_init.idl",
"background_fetch/background_fetched_event_init.idl", "background_fetch/background_fetched_event_init.idl",
"background_fetch/icon_definition.idl", "background_fetch/icon_definition.idl",
"background_sync/sync_event_init.idl", "background_sync/sync_event_init.idl",
......
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