| 1 | /* |
| 2 | * Copyright (C) 2017 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "ServiceWorkerGlobalScope.h" |
| 28 | |
| 29 | #if ENABLE(SERVICE_WORKER) |
| 30 | |
| 31 | #include "ExtendableEvent.h" |
| 32 | #include "SWContextManager.h" |
| 33 | #include "ServiceWorkerClient.h" |
| 34 | #include "ServiceWorkerClients.h" |
| 35 | #include "ServiceWorkerContainer.h" |
| 36 | #include "ServiceWorkerThread.h" |
| 37 | #include "ServiceWorkerWindowClient.h" |
| 38 | #include "WorkerNavigator.h" |
| 39 | #include <wtf/IsoMallocInlines.h> |
| 40 | |
| 41 | namespace WebCore { |
| 42 | |
| 43 | WTF_MAKE_ISO_ALLOCATED_IMPL(ServiceWorkerGlobalScope); |
| 44 | |
| 45 | Ref<ServiceWorkerGlobalScope> ServiceWorkerGlobalScope::create(const ServiceWorkerContextData& data, const URL& url, Ref<SecurityOrigin>&& origin, const String& identifier, const String& userAgent, bool isOnline, ServiceWorkerThread& thread, const ContentSecurityPolicyResponseHeaders& contentSecurityPolicy, bool shouldBypassMainWorldContentSecurityPolicy, Ref<SecurityOrigin>&& topOrigin, MonotonicTime timeOrigin, IDBClient::IDBConnectionProxy* connectionProxy, SocketProvider* socketProvider, PAL::SessionID sessionID) |
| 46 | { |
| 47 | auto scope = adoptRef(*new ServiceWorkerGlobalScope { data, url, WTFMove(origin), identifier, userAgent, isOnline, thread, shouldBypassMainWorldContentSecurityPolicy, WTFMove(topOrigin), timeOrigin, connectionProxy, socketProvider, sessionID }); |
| 48 | scope->applyContentSecurityPolicyResponseHeaders(contentSecurityPolicy); |
| 49 | return scope; |
| 50 | } |
| 51 | |
| 52 | ServiceWorkerGlobalScope::ServiceWorkerGlobalScope(const ServiceWorkerContextData& data, const URL& url, Ref<SecurityOrigin>&& origin, const String& identifier, const String& userAgent, bool isOnline, ServiceWorkerThread& thread, bool shouldBypassMainWorldContentSecurityPolicy, Ref<SecurityOrigin>&& topOrigin, MonotonicTime timeOrigin, IDBClient::IDBConnectionProxy* connectionProxy, SocketProvider* socketProvider, PAL::SessionID sessionID) |
| 53 | : WorkerGlobalScope(url, WTFMove(origin), identifier, userAgent, isOnline, thread, shouldBypassMainWorldContentSecurityPolicy, WTFMove(topOrigin), timeOrigin, connectionProxy, socketProvider, sessionID) |
| 54 | , m_contextData(crossThreadCopy(data)) |
| 55 | , m_registration(ServiceWorkerRegistration::getOrCreate(*this, navigator().serviceWorker(), WTFMove(m_contextData.registration))) |
| 56 | , m_clients(ServiceWorkerClients::create()) |
| 57 | { |
| 58 | } |
| 59 | |
| 60 | ServiceWorkerGlobalScope::~ServiceWorkerGlobalScope() = default; |
| 61 | |
| 62 | void ServiceWorkerGlobalScope::skipWaiting(Ref<DeferredPromise>&& promise) |
| 63 | { |
| 64 | uint64_t requestIdentifier = ++m_lastRequestIdentifier; |
| 65 | m_pendingSkipWaitingPromises.add(requestIdentifier, WTFMove(promise)); |
| 66 | |
| 67 | callOnMainThread([workerThread = makeRef(thread()), requestIdentifier]() mutable { |
| 68 | if (auto* connection = SWContextManager::singleton().connection()) { |
| 69 | auto identifier = workerThread->identifier(); |
| 70 | connection->skipWaiting(identifier, [workerThread = WTFMove(workerThread), requestIdentifier] { |
| 71 | workerThread->runLoop().postTask([requestIdentifier](auto& context) { |
| 72 | auto& scope = downcast<ServiceWorkerGlobalScope>(context); |
| 73 | if (auto promise = scope.m_pendingSkipWaitingPromises.take(requestIdentifier)) |
| 74 | promise->resolve(); |
| 75 | }); |
| 76 | }); |
| 77 | } |
| 78 | }); |
| 79 | } |
| 80 | |
| 81 | EventTargetInterface ServiceWorkerGlobalScope::eventTargetInterface() const |
| 82 | { |
| 83 | return ServiceWorkerGlobalScopeEventTargetInterfaceType; |
| 84 | } |
| 85 | |
| 86 | ServiceWorkerThread& ServiceWorkerGlobalScope::thread() |
| 87 | { |
| 88 | return static_cast<ServiceWorkerThread&>(WorkerGlobalScope::thread()); |
| 89 | } |
| 90 | |
| 91 | ServiceWorkerClient* ServiceWorkerGlobalScope::serviceWorkerClient(ServiceWorkerClientIdentifier identifier) |
| 92 | { |
| 93 | return m_clientMap.get(identifier); |
| 94 | } |
| 95 | |
| 96 | void ServiceWorkerGlobalScope::addServiceWorkerClient(ServiceWorkerClient& client) |
| 97 | { |
| 98 | auto result = m_clientMap.add(client.identifier(), &client); |
| 99 | ASSERT_UNUSED(result, result.isNewEntry); |
| 100 | } |
| 101 | |
| 102 | void ServiceWorkerGlobalScope::removeServiceWorkerClient(ServiceWorkerClient& client) |
| 103 | { |
| 104 | auto isRemoved = m_clientMap.remove(client.identifier()); |
| 105 | ASSERT_UNUSED(isRemoved, isRemoved); |
| 106 | } |
| 107 | |
| 108 | // https://w3c.github.io/ServiceWorker/#update-service-worker-extended-events-set-algorithm |
| 109 | void ServiceWorkerGlobalScope::updateExtendedEventsSet(ExtendableEvent* newEvent) |
| 110 | { |
| 111 | ASSERT(!isMainThread()); |
| 112 | ASSERT(!newEvent || !newEvent->isBeingDispatched()); |
| 113 | bool hadPendingEvents = hasPendingEvents(); |
| 114 | m_extendedEvents.removeAllMatching([](auto& event) { |
| 115 | return !event->pendingPromiseCount(); |
| 116 | }); |
| 117 | |
| 118 | if (newEvent && newEvent->pendingPromiseCount()) { |
| 119 | m_extendedEvents.append(*newEvent); |
| 120 | newEvent->whenAllExtendLifetimePromisesAreSettled([this](auto&&) { |
| 121 | this->updateExtendedEventsSet(); |
| 122 | }); |
| 123 | // Clear out the event's target as it is the WorkerGlobalScope and we do not want to keep it |
| 124 | // alive unnecessarily. |
| 125 | newEvent->setTarget(nullptr); |
| 126 | } |
| 127 | |
| 128 | bool hasPendingEvents = this->hasPendingEvents(); |
| 129 | if (hasPendingEvents == hadPendingEvents) |
| 130 | return; |
| 131 | |
| 132 | callOnMainThread([threadIdentifier = thread().identifier(), hasPendingEvents] { |
| 133 | if (auto* connection = SWContextManager::singleton().connection()) |
| 134 | connection->setServiceWorkerHasPendingEvents(threadIdentifier, hasPendingEvents); |
| 135 | }); |
| 136 | } |
| 137 | |
| 138 | const ServiceWorkerContextData::ImportedScript* ServiceWorkerGlobalScope::scriptResource(const URL& url) const |
| 139 | { |
| 140 | auto iterator = m_contextData.scriptResourceMap.find(url); |
| 141 | return iterator == m_contextData.scriptResourceMap.end() ? nullptr : &iterator->value; |
| 142 | } |
| 143 | |
| 144 | void ServiceWorkerGlobalScope::setScriptResource(const URL& url, ServiceWorkerContextData::ImportedScript&& script) |
| 145 | { |
| 146 | callOnMainThread([threadIdentifier = thread().identifier(), url = url.isolatedCopy(), script = script.isolatedCopy()] { |
| 147 | if (auto* connection = SWContextManager::singleton().connection()) |
| 148 | connection->setScriptResource(threadIdentifier, url, script); |
| 149 | }); |
| 150 | |
| 151 | m_contextData.scriptResourceMap.set(url, WTFMove(script)); |
| 152 | } |
| 153 | |
| 154 | } // namespace WebCore |
| 155 | |
| 156 | #endif // ENABLE(SERVICE_WORKER) |
| 157 | |