| 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 "ClientOrigin.h" |
| 31 | #include "ContentSecurityPolicyResponseHeaders.h" |
| 32 | #include "RegistrableDomain.h" |
| 33 | #include "ServiceWorkerClientData.h" |
| 34 | #include "ServiceWorkerContextData.h" |
| 35 | #include "ServiceWorkerData.h" |
| 36 | #include "ServiceWorkerIdentifier.h" |
| 37 | #include "ServiceWorkerRegistrationKey.h" |
| 38 | #include "ServiceWorkerTypes.h" |
| 39 | #include <wtf/RefCounted.h> |
| 40 | |
| 41 | namespace WebCore { |
| 42 | |
| 43 | class SWServer; |
| 44 | class SWServerRegistration; |
| 45 | class SWServerToContextConnection; |
| 46 | struct ServiceWorkerClientData; |
| 47 | struct ServiceWorkerClientIdentifier; |
| 48 | struct ServiceWorkerClientQueryOptions; |
| 49 | struct ServiceWorkerContextData; |
| 50 | struct ServiceWorkerJobDataIdentifier; |
| 51 | enum class WorkerType; |
| 52 | |
| 53 | class SWServerWorker : public RefCounted<SWServerWorker> { |
| 54 | public: |
| 55 | template <typename... Args> static Ref<SWServerWorker> create(Args&&... args) |
| 56 | { |
| 57 | return adoptRef(*new SWServerWorker(std::forward<Args>(args)...)); |
| 58 | } |
| 59 | |
| 60 | SWServerWorker(const SWServerWorker&) = delete; |
| 61 | WEBCORE_EXPORT ~SWServerWorker(); |
| 62 | |
| 63 | void terminate(); |
| 64 | |
| 65 | WEBCORE_EXPORT void whenActivated(WTF::Function<void(bool)>&&); |
| 66 | |
| 67 | enum class State { |
| 68 | Running, |
| 69 | Terminating, |
| 70 | NotRunning, |
| 71 | }; |
| 72 | bool isRunning() const { return m_state == State::Running; } |
| 73 | bool isTerminating() const { return m_state == State::Terminating; } |
| 74 | void setState(State); |
| 75 | |
| 76 | SWServer& server() { return m_server; } |
| 77 | const ServiceWorkerRegistrationKey& registrationKey() const { return m_registrationKey; } |
| 78 | const URL& scriptURL() const { return m_data.scriptURL; } |
| 79 | const String& script() const { return m_script; } |
| 80 | WorkerType type() const { return m_data.type; } |
| 81 | |
| 82 | ServiceWorkerIdentifier identifier() const { return m_data.identifier; } |
| 83 | |
| 84 | ServiceWorkerState state() const { return m_data.state; } |
| 85 | void setState(ServiceWorkerState); |
| 86 | |
| 87 | bool hasPendingEvents() const { return m_hasPendingEvents; } |
| 88 | void setHasPendingEvents(bool); |
| 89 | |
| 90 | void scriptContextFailedToStart(const Optional<ServiceWorkerJobDataIdentifier>&, const String& message); |
| 91 | void scriptContextStarted(const Optional<ServiceWorkerJobDataIdentifier>&); |
| 92 | void didFinishInstall(const Optional<ServiceWorkerJobDataIdentifier>&, bool wasSuccessful); |
| 93 | void didFinishActivation(); |
| 94 | void contextTerminated(); |
| 95 | WEBCORE_EXPORT Optional<ServiceWorkerClientData> findClientByIdentifier(const ServiceWorkerClientIdentifier&) const; |
| 96 | void matchAll(const ServiceWorkerClientQueryOptions&, ServiceWorkerClientsMatchAllCallback&&); |
| 97 | void claim(); |
| 98 | void setScriptResource(URL&&, ServiceWorkerContextData::ImportedScript&&); |
| 99 | |
| 100 | void skipWaiting(); |
| 101 | bool isSkipWaitingFlagSet() const { return m_isSkipWaitingFlagSet; } |
| 102 | |
| 103 | WEBCORE_EXPORT static SWServerWorker* existingWorkerForIdentifier(ServiceWorkerIdentifier); |
| 104 | static HashMap<ServiceWorkerIdentifier, SWServerWorker*>& allWorkers(); |
| 105 | |
| 106 | const ServiceWorkerData& data() const { return m_data; } |
| 107 | ServiceWorkerContextData contextData() const; |
| 108 | |
| 109 | const ClientOrigin& origin() const; |
| 110 | const RegistrableDomain& registrableDomain() const { return m_registrableDomain; } |
| 111 | |
| 112 | WEBCORE_EXPORT SWServerToContextConnection* contextConnection(); |
| 113 | String userAgent() const; |
| 114 | |
| 115 | private: |
| 116 | (SWServer&, SWServerRegistration&, const URL&, const String& script, const ContentSecurityPolicyResponseHeaders&, String&& referrerPolicy, WorkerType, ServiceWorkerIdentifier, HashMap<URL, ServiceWorkerContextData::ImportedScript>&&); |
| 117 | |
| 118 | void callWhenActivatedHandler(bool success); |
| 119 | |
| 120 | SWServer& m_server; |
| 121 | ServiceWorkerRegistrationKey m_registrationKey; |
| 122 | ServiceWorkerData m_data; |
| 123 | String m_script; |
| 124 | ContentSecurityPolicyResponseHeaders m_contentSecurityPolicy; |
| 125 | String m_referrerPolicy; |
| 126 | bool m_hasPendingEvents { false }; |
| 127 | State m_state { State::NotRunning }; |
| 128 | mutable Optional<ClientOrigin> m_origin; |
| 129 | RegistrableDomain m_registrableDomain; |
| 130 | bool m_isSkipWaitingFlagSet { false }; |
| 131 | Vector<Function<void(bool)>> m_whenActivatedHandlers; |
| 132 | HashMap<URL, ServiceWorkerContextData::ImportedScript> m_scriptResourceMap; |
| 133 | }; |
| 134 | |
| 135 | } // namespace WebCore |
| 136 | |
| 137 | #endif // ENABLE(SERVICE_WORKER) |
| 138 | |