1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
// Copyright 2015 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 "modules/background_sync/SyncManager.h"
#include "bindings/core/v8/CallbackPromiseAdapter.h"
#include "bindings/core/v8/ScriptPromise.h"
#include "bindings/core/v8/ScriptPromiseResolver.h"
#include "bindings/core/v8/ScriptState.h"
#include "core/dom/DOMException.h"
#include "core/dom/ExceptionCode.h"
#include "core/dom/ExecutionContext.h"
#include "modules/serviceworkers/ServiceWorkerRegistration.h"
#include "platform/heap/Persistent.h"
#include "public/platform/InterfaceProvider.h"
#include "public/platform/Platform.h"
#include "wtf/Functional.h"
#include "wtf/PtrUtil.h"
namespace blink {
SyncManager::SyncManager(ServiceWorkerRegistration* registration)
: m_registration(registration) {
DCHECK(registration);
}
ScriptPromise SyncManager::registerFunction(ScriptState* scriptState,
const String& tag) {
// TODO(jkarlin): Wait for the registration to become active instead of
// rejecting. See crbug.com/542437.
if (!m_registration->active())
return ScriptPromise::rejectWithDOMException(
scriptState,
DOMException::create(AbortError,
"Registration failed - no active Service Worker"));
ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
ScriptPromise promise = resolver->promise();
mojom::blink::SyncRegistrationPtr syncRegistration =
mojom::blink::SyncRegistration::New();
syncRegistration->id = SyncManager::kUnregisteredSyncID;
syncRegistration->tag = tag;
syncRegistration->network_state =
blink::mojom::BackgroundSyncNetworkState::ONLINE;
getBackgroundSyncServicePtr()->Register(
std::move(syncRegistration),
m_registration->webRegistration()->registrationId(),
convertToBaseCallback(
WTF::bind(SyncManager::registerCallback, wrapPersistent(resolver))));
return promise;
}
ScriptPromise SyncManager::getTags(ScriptState* scriptState) {
ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
ScriptPromise promise = resolver->promise();
getBackgroundSyncServicePtr()->GetRegistrations(
m_registration->webRegistration()->registrationId(),
convertToBaseCallback(WTF::bind(&SyncManager::getRegistrationsCallback,
wrapPersistent(resolver))));
return promise;
}
const mojom::blink::BackgroundSyncServicePtr&
SyncManager::getBackgroundSyncServicePtr() {
if (!m_backgroundSyncService.get()) {
Platform::current()->interfaceProvider()->getInterface(
mojo::MakeRequest(&m_backgroundSyncService));
}
return m_backgroundSyncService;
}
// static
void SyncManager::registerCallback(ScriptPromiseResolver* resolver,
mojom::blink::BackgroundSyncError error,
mojom::blink::SyncRegistrationPtr options) {
// TODO(iclelland): Determine the correct error message to return in each case
switch (error) {
case mojom::blink::BackgroundSyncError::NONE:
if (!options) {
resolver->resolve(v8::Null(resolver->getScriptState()->isolate()));
return;
}
resolver->resolve();
break;
case mojom::blink::BackgroundSyncError::NOT_FOUND:
NOTREACHED();
break;
case mojom::blink::BackgroundSyncError::STORAGE:
resolver->reject(
DOMException::create(UnknownError, "Background Sync is disabled."));
break;
case mojom::blink::BackgroundSyncError::NOT_ALLOWED:
resolver->reject(
DOMException::create(InvalidAccessError,
"Attempted to register a sync event without a "
"window or registration tag too long."));
break;
case mojom::blink::BackgroundSyncError::PERMISSION_DENIED:
resolver->reject(
DOMException::create(PermissionDeniedError, "Permission denied."));
break;
case mojom::blink::BackgroundSyncError::NO_SERVICE_WORKER:
resolver->reject(
DOMException::create(UnknownError, "No service worker is active."));
break;
}
}
// static
void SyncManager::getRegistrationsCallback(
ScriptPromiseResolver* resolver,
mojom::blink::BackgroundSyncError error,
WTF::Vector<mojom::blink::SyncRegistrationPtr> registrations) {
// TODO(iclelland): Determine the correct error message to return in each case
switch (error) {
case mojom::blink::BackgroundSyncError::NONE: {
Vector<String> tags;
for (const auto& r : registrations) {
tags.push_back(r->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::GetRegistrations
NOTREACHED();
break;
case mojom::blink::BackgroundSyncError::STORAGE:
resolver->reject(
DOMException::create(UnknownError, "Background Sync is disabled."));
break;
case mojom::blink::BackgroundSyncError::NO_SERVICE_WORKER:
resolver->reject(
DOMException::create(UnknownError, "No service worker is active."));
break;
}
}
DEFINE_TRACE(SyncManager) {
visitor->trace(m_registration);
}
} // namespace blink
|