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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
// Copyright 2014 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/serviceworkers/ServiceWorkerClients.h"
#include "bindings/core/v8/CallbackPromiseAdapter.h"
#include "bindings/core/v8/ScriptPromiseResolver.h"
#include "core/dom/DOMException.h"
#include "core/dom/ExceptionCode.h"
#include "core/workers/WorkerGlobalScope.h"
#include "core/workers/WorkerLocation.h"
#include "modules/serviceworkers/ServiceWorkerError.h"
#include "modules/serviceworkers/ServiceWorkerGlobalScopeClient.h"
#include "modules/serviceworkers/ServiceWorkerWindowClient.h"
#include "modules/serviceworkers/ServiceWorkerWindowClientCallback.h"
#include "public/platform/modules/serviceworker/WebServiceWorkerClientQueryOptions.h"
#include "public/platform/modules/serviceworker/WebServiceWorkerClientsInfo.h"
#include "wtf/PtrUtil.h"
#include "wtf/RefPtr.h"
#include "wtf/Vector.h"
#include <memory>
#include <utility>
namespace blink {
namespace {
class ClientArray {
public:
using WebType = const WebServiceWorkerClientsInfo&;
static HeapVector<Member<ServiceWorkerClient>> take(
ScriptPromiseResolver*,
const WebServiceWorkerClientsInfo& webClients) {
HeapVector<Member<ServiceWorkerClient>> clients;
for (size_t i = 0; i < webClients.clients.size(); ++i) {
const WebServiceWorkerClientInfo& client = webClients.clients[i];
if (client.clientType == WebServiceWorkerClientTypeWindow)
clients.push_back(ServiceWorkerWindowClient::create(client));
else
clients.push_back(ServiceWorkerClient::create(client));
}
return clients;
}
private:
WTF_MAKE_NONCOPYABLE(ClientArray);
ClientArray() = delete;
};
WebServiceWorkerClientType getClientType(const String& type) {
if (type == "window")
return WebServiceWorkerClientTypeWindow;
if (type == "worker")
return WebServiceWorkerClientTypeWorker;
if (type == "sharedworker")
return WebServiceWorkerClientTypeSharedWorker;
if (type == "all")
return WebServiceWorkerClientTypeAll;
ASSERT_NOT_REACHED();
return WebServiceWorkerClientTypeWindow;
}
class GetCallback : public WebServiceWorkerClientCallbacks {
public:
explicit GetCallback(ScriptPromiseResolver* resolver)
: m_resolver(resolver) {}
~GetCallback() override {}
void onSuccess(
std::unique_ptr<WebServiceWorkerClientInfo> webClient) override {
std::unique_ptr<WebServiceWorkerClientInfo> client =
WTF::wrapUnique(webClient.release());
if (!m_resolver->getExecutionContext() ||
m_resolver->getExecutionContext()->isContextDestroyed())
return;
if (!client) {
// Resolve the promise with undefined.
m_resolver->resolve();
return;
}
m_resolver->resolve(
ServiceWorkerClient::take(m_resolver, std::move(client)));
}
void onError(const WebServiceWorkerError& error) override {
if (!m_resolver->getExecutionContext() ||
m_resolver->getExecutionContext()->isContextDestroyed())
return;
m_resolver->reject(ServiceWorkerError::take(m_resolver.get(), error));
}
private:
Persistent<ScriptPromiseResolver> m_resolver;
WTF_MAKE_NONCOPYABLE(GetCallback);
};
} // namespace
ServiceWorkerClients* ServiceWorkerClients::create() {
return new ServiceWorkerClients();
}
ServiceWorkerClients::ServiceWorkerClients() {}
ScriptPromise ServiceWorkerClients::get(ScriptState* scriptState,
const String& id) {
ExecutionContext* executionContext = scriptState->getExecutionContext();
// TODO(jungkees): May be null due to worker termination:
// http://crbug.com/413518.
if (!executionContext)
return ScriptPromise();
ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
ScriptPromise promise = resolver->promise();
ServiceWorkerGlobalScopeClient::from(executionContext)
->getClient(id, WTF::makeUnique<GetCallback>(resolver));
return promise;
}
ScriptPromise ServiceWorkerClients::matchAll(
ScriptState* scriptState,
const ClientQueryOptions& options) {
ExecutionContext* executionContext = scriptState->getExecutionContext();
// FIXME: May be null due to worker termination: http://crbug.com/413518.
if (!executionContext)
return ScriptPromise();
ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
ScriptPromise promise = resolver->promise();
WebServiceWorkerClientQueryOptions webOptions;
webOptions.clientType = getClientType(options.type());
webOptions.includeUncontrolled = options.includeUncontrolled();
ServiceWorkerGlobalScopeClient::from(executionContext)
->getClients(webOptions,
WTF::makeUnique<
CallbackPromiseAdapter<ClientArray, ServiceWorkerError>>(
resolver));
return promise;
}
ScriptPromise ServiceWorkerClients::claim(ScriptState* scriptState) {
ExecutionContext* executionContext = scriptState->getExecutionContext();
// FIXME: May be null due to worker termination: http://crbug.com/413518.
if (!executionContext)
return ScriptPromise();
ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
ScriptPromise promise = resolver->promise();
auto callbacks =
WTF::makeUnique<CallbackPromiseAdapter<void, ServiceWorkerError>>(
resolver);
ServiceWorkerGlobalScopeClient::from(executionContext)
->claim(std::move(callbacks));
return promise;
}
ScriptPromise ServiceWorkerClients::openWindow(ScriptState* scriptState,
const String& url) {
ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
ScriptPromise promise = resolver->promise();
ExecutionContext* context = scriptState->getExecutionContext();
KURL parsedUrl = KURL(toWorkerGlobalScope(context)->location()->url(), url);
if (!parsedUrl.isValid()) {
resolver->reject(V8ThrowException::createTypeError(
scriptState->isolate(), "'" + url + "' is not a valid URL."));
return promise;
}
if (!context->getSecurityOrigin()->canDisplay(parsedUrl)) {
resolver->reject(V8ThrowException::createTypeError(
scriptState->isolate(),
"'" + parsedUrl.elidedString() + "' cannot be opened."));
return promise;
}
if (!context->isWindowInteractionAllowed()) {
resolver->reject(DOMException::create(InvalidAccessError,
"Not allowed to open a window."));
return promise;
}
context->consumeWindowInteraction();
ServiceWorkerGlobalScopeClient::from(context)->openWindow(
parsedUrl, WTF::makeUnique<NavigateClientCallback>(resolver));
return promise;
}
} // namespace blink
|