Commit 75ef794c authored by Mugdha Lakhani's avatar Mugdha Lakhani Committed by Commit Bot

[Background Sync] Add getTags()

Add getTags to the PeriodicSyncManager idl file and add a web test to
verify it works as expected.

Also update test expectations.

Bug: 925297
Change-Id: I7902474ad236803a40ac630100ecccebb0652a97
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1669306
Commit-Queue: Mugdha Lakhani <nator@chromium.org>
Reviewed-by: default avatarPeter Beverloo <peter@chromium.org>
Reviewed-by: default avatarRayan Kanso <rayankans@chromium.org>
Cr-Commit-Position: refs/heads/master@{#672177}
parent 566e529d
......@@ -2323,6 +2323,7 @@ enum WebFeature {
kLazyLoadImageLoadingAttributeEager = 2934,
kLazyLoadImageLoadingAttributeLazy = 2935,
kLazyLoadImageMissingDimensionsForLazy = 2936,
kPeriodicBackgroundSyncGetTags = 2937,
// Add new features immediately above this line. Don't change assigned
// numbers of any item, and don't reuse removed slots.
......
......@@ -47,6 +47,28 @@ ScriptPromise PeriodicSyncManager::registerPeriodicSync(
return promise;
}
ScriptPromise PeriodicSyncManager::getTags(ScriptState* script_state) {
auto* resolver = MakeGarbageCollected<ScriptPromiseResolver>(script_state);
ScriptPromise promise = resolver->Promise();
// Creating a Periodic Background Sync registration requires an activated
// service worker, so if |registration_| has not been activated yet, we can
// skip the Mojo roundtrip.
if (!registration_->active()) {
return ScriptPromise::Cast(script_state,
v8::Array::New(script_state->GetIsolate()));
}
// TODO(crbug.com/932591): Optimize this to only get the tags from the browser
// process instead of the registrations themselves.
GetBackgroundSyncServicePtr()->GetRegistrations(
registration_->RegistrationId(),
WTF::Bind(&PeriodicSyncManager::GetRegistrationsCallback,
WrapPersistent(this), WrapPersistent(resolver)));
return promise;
}
const mojom::blink::PeriodicBackgroundSyncServicePtr&
PeriodicSyncManager::GetBackgroundSyncServicePtr() {
if (!background_sync_service_.get()) {
......@@ -89,6 +111,37 @@ void PeriodicSyncManager::RegisterCallback(
}
}
void PeriodicSyncManager::GetRegistrationsCallback(
ScriptPromiseResolver* resolver,
mojom::blink::BackgroundSyncError error,
WTF::Vector<mojom::blink::SyncRegistrationOptionsPtr> registrations) {
switch (error) {
case mojom::blink::BackgroundSyncError::NONE: {
Vector<String> tags;
for (const auto& registration : registrations)
tags.push_back(registration->tag);
resolver->Resolve(tags);
break;
}
case mojom::blink::BackgroundSyncError::NOT_FOUND:
case mojom::blink::BackgroundSyncError::NOT_ALLOWED:
case mojom::blink::BackgroundSyncError::PERMISSION_DENIED:
// These errors should never be returned from
// BackgroundSyncManager::GetPeriodicSyncRegistrations
NOTREACHED();
break;
case mojom::blink::BackgroundSyncError::STORAGE:
resolver->Reject(MakeGarbageCollected<DOMException>(
DOMExceptionCode::kUnknownError,
"Periodic Background Sync is disabled."));
break;
case mojom::blink::BackgroundSyncError::NO_SERVICE_WORKER:
resolver->Reject(MakeGarbageCollected<DOMException>(
DOMExceptionCode::kUnknownError, "No service worker is active."));
break;
}
}
void PeriodicSyncManager::Trace(blink::Visitor* visitor) {
visitor->Trace(registration_);
ScriptWrappable::Trace(visitor);
......
......@@ -37,6 +37,7 @@ class PeriodicSyncManager final : public ScriptWrappable {
ScriptPromise registerPeriodicSync(ScriptState* script_state,
const String& tag,
const BackgroundSyncOptions* options);
ScriptPromise getTags(ScriptState* script_state);
void Trace(blink::Visitor* visitor) override;
......@@ -51,6 +52,10 @@ class PeriodicSyncManager final : public ScriptWrappable {
void RegisterCallback(ScriptPromiseResolver* resolver,
mojom::blink::BackgroundSyncError error,
mojom::blink::SyncRegistrationOptionsPtr options);
void GetRegistrationsCallback(
ScriptPromiseResolver* resolver,
mojom::blink::BackgroundSyncError error,
WTF::Vector<mojom::blink::SyncRegistrationOptionsPtr> registrations);
Member<ServiceWorkerRegistration> registration_;
scoped_refptr<base::SequencedTaskRunner> task_runner_;
......
......@@ -10,4 +10,5 @@
RuntimeEnabled=PeriodicBackgroundSync
] interface PeriodicSyncManager {
[MeasureAs=PeriodicBackgroundSyncRegister,CallWith=ScriptState,ImplementedAs=registerPeriodicSync] Promise<void> register(DOMString tag, optional BackgroundSyncOptions options);
[MeasureAs=PeriodicBackgroundSyncGetTags, CallWith=ScriptState] Promise<sequence<DOMString>> getTags();
};
......@@ -26,4 +26,11 @@
'register should have thrown an error');
}, 'register fails when invalid minInterval is provided');
periodicSyncTest(async (test, periodicSync) => {
await periodicSync.register('test');
const tags = await periodicSync.getTags();
assert_equals(tags.length, 1);
assert_equals(tags[0], 'test');
}, 'getTags works as expected');
</script>
\ No newline at end of file
......@@ -1093,6 +1093,7 @@ interface PeriodicSyncEvent : ExtendableEvent
interface PeriodicSyncManager
attribute @@toStringTag
method constructor
method getTags
method register
interface PermissionStatus : EventTarget
attribute @@toStringTag
......
......@@ -1027,6 +1027,7 @@ Starting worker: resources/global-interface-listing-worker.js
[Worker] interface PeriodicSyncManager
[Worker] attribute @@toStringTag
[Worker] method constructor
[Worker] method getTags
[Worker] method register
[Worker] interface PermissionStatus : EventTarget
[Worker] attribute @@toStringTag
......
......@@ -5530,6 +5530,7 @@ interface PerformanceTiming
interface PeriodicSyncManager
attribute @@toStringTag
method constructor
method getTags
method register
interface PeriodicWave
attribute @@toStringTag
......
......@@ -1009,6 +1009,7 @@ Starting worker: resources/global-interface-listing-worker.js
[Worker] interface PeriodicSyncManager
[Worker] attribute @@toStringTag
[Worker] method constructor
[Worker] method getTags
[Worker] method register
[Worker] interface PermissionStatus : EventTarget
[Worker] attribute @@toStringTag
......
......@@ -23771,6 +23771,7 @@ Called by update_net_error_codes.py.-->
<int value="2934" label="LazyLoadImageLoadingAttributeEager"/>
<int value="2935" label="LazyLoadImageLoadingAttributeLazy"/>
<int value="2936" label="LazyLoadImageMissingDimensionsForLazy"/>
<int value="2937" label="PeriodicBackgroundSyncGetTags"/>
</enum>
<enum name="FeaturePolicyAllowlistType">
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