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#pragma once
27
28#if ENABLE(SERVICE_WORKER)
29
30#include "ServiceWorkerClientIdentifier.h"
31#include "ServiceWorkerContextData.h"
32#include "ServiceWorkerRegistration.h"
33#include "WorkerGlobalScope.h"
34
35namespace WebCore {
36
37class DeferredPromise;
38class ExtendableEvent;
39struct ServiceWorkerClientData;
40class ServiceWorkerClient;
41class ServiceWorkerClients;
42class ServiceWorkerThread;
43
44class ServiceWorkerGlobalScope final : public WorkerGlobalScope {
45 WTF_MAKE_ISO_ALLOCATED(ServiceWorkerGlobalScope);
46public:
47 static Ref<ServiceWorkerGlobalScope> create(const ServiceWorkerContextData&, const URL&, Ref<SecurityOrigin>&&, const String& identifier, const String& userAgent, bool isOnline, ServiceWorkerThread&, const ContentSecurityPolicyResponseHeaders&, bool shouldBypassMainWorldContentSecurityPolicy, Ref<SecurityOrigin>&& topOrigin, MonotonicTime timeOrigin, IDBClient::IDBConnectionProxy*, SocketProvider*, PAL::SessionID);
48
49 ~ServiceWorkerGlobalScope();
50
51 bool isServiceWorkerGlobalScope() const final { return true; }
52
53 ServiceWorkerClients& clients() { return m_clients.get(); }
54 ServiceWorkerRegistration& registration() { return m_registration.get(); }
55
56 void skipWaiting(Ref<DeferredPromise>&&);
57
58 EventTargetInterface eventTargetInterface() const final;
59
60 ServiceWorkerThread& thread();
61
62 ServiceWorkerClient* serviceWorkerClient(ServiceWorkerClientIdentifier);
63 void addServiceWorkerClient(ServiceWorkerClient&);
64 void removeServiceWorkerClient(ServiceWorkerClient&);
65
66 void updateExtendedEventsSet(ExtendableEvent* newEvent = nullptr);
67
68 const ServiceWorkerContextData::ImportedScript* scriptResource(const URL&) const;
69 void setScriptResource(const URL&, ServiceWorkerContextData::ImportedScript&&);
70
71private:
72 ServiceWorkerGlobalScope(const ServiceWorkerContextData&, const URL&, Ref<SecurityOrigin>&&, const String& identifier, const String& userAgent, bool isOnline, ServiceWorkerThread&, bool shouldBypassMainWorldContentSecurityPolicy, Ref<SecurityOrigin>&& topOrigin, MonotonicTime timeOrigin, IDBClient::IDBConnectionProxy*, SocketProvider*, PAL::SessionID);
73
74 bool hasPendingEvents() const { return !m_extendedEvents.isEmpty(); }
75
76 ServiceWorkerContextData m_contextData;
77 Ref<ServiceWorkerRegistration> m_registration;
78 Ref<ServiceWorkerClients> m_clients;
79 HashMap<ServiceWorkerClientIdentifier, ServiceWorkerClient*> m_clientMap;
80 Vector<Ref<ExtendableEvent>> m_extendedEvents;
81
82 uint64_t m_lastRequestIdentifier { 0 };
83 HashMap<uint64_t, RefPtr<DeferredPromise>> m_pendingSkipWaitingPromises;
84};
85
86} // namespace WebCore
87
88SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::ServiceWorkerGlobalScope)
89 static bool isType(const WebCore::ScriptExecutionContext& context) { return is<WebCore::WorkerGlobalScope>(context) && downcast<WebCore::WorkerGlobalScope>(context).isServiceWorkerGlobalScope(); }
90 static bool isType(const WebCore::WorkerGlobalScope& context) { return context.isServiceWorkerGlobalScope(); }
91SPECIALIZE_TYPE_TRAITS_END()
92
93#endif // ENABLE(SERVICE_WORKER)
94