Add support for unregistering a service worker
[WebKit-https.git] / Source / WebCore / workers / service / ServiceWorkerContainer.h
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 "ActiveDOMObject.h"
31 #include "DOMPromiseProxy.h"
32 #include "EventTarget.h"
33 #include "SWClientConnection.h"
34 #include "SWServer.h"
35 #include "ServiceWorkerJobClient.h"
36 #include "ServiceWorkerRegistration.h"
37 #include "ServiceWorkerRegistrationOptions.h"
38 #include <pal/SessionID.h>
39 #include <wtf/Threading.h>
40
41 namespace WebCore {
42
43 class DeferredPromise;
44 class NavigatorBase;
45 class ServiceWorker;
46
47 enum class ServiceWorkerUpdateViaCache;
48 enum class WorkerType;
49
50 class ServiceWorkerContainer final : public EventTargetWithInlineData, public ActiveDOMObject, public ServiceWorkerJobClient {
51 public:
52     ServiceWorkerContainer(ScriptExecutionContext&, NavigatorBase&);
53     ~ServiceWorkerContainer();
54
55     typedef WebCore::RegistrationOptions RegistrationOptions;
56
57     ServiceWorker* controller() const;
58
59     using ReadyPromise = DOMPromiseProxy<IDLInterface<ServiceWorkerRegistration>>;
60     ReadyPromise& ready() { return m_readyPromise; }
61
62     void addRegistration(const String& scriptURL, const RegistrationOptions&, Ref<DeferredPromise>&&);
63     void removeRegistration(const URL& scopeURL, Ref<DeferredPromise>&&);
64     void getRegistration(const String& url, Ref<DeferredPromise>&&);
65     void getRegistrations(Ref<DeferredPromise>&&);
66
67     void startMessages();
68
69     void ref() final { refEventTarget(); }
70     void deref() final { derefEventTarget(); }
71
72 private:
73     void scheduleJob(Ref<ServiceWorkerJob>&&);
74
75     void jobFailedWithException(ServiceWorkerJob&, const Exception&) final;
76     void jobResolvedWithRegistration(ServiceWorkerJob&, ServiceWorkerRegistrationData&&) final;
77     void jobResolvedWithUnregistrationResult(ServiceWorkerJob&, bool unregistrationResult) final;
78     void startScriptFetchForJob(ServiceWorkerJob&) final;
79     void jobFinishedLoadingScript(ServiceWorkerJob&, const String&) final;
80     void jobFailedLoadingScript(ServiceWorkerJob&, const ResourceError&) final;
81
82     void jobDidFinish(ServiceWorkerJob&);
83
84     uint64_t connectionIdentifier() final;
85
86     const char* activeDOMObjectName() const final;
87     bool canSuspendForDocumentSuspension() const final;
88     ScriptExecutionContext* scriptExecutionContext() const final { return ActiveDOMObject::scriptExecutionContext(); }
89     EventTargetInterface eventTargetInterface() const final { return ServiceWorkerContainerEventTargetInterfaceType; }
90     void refEventTarget() final;
91     void derefEventTarget() final;
92
93     ReadyPromise m_readyPromise;
94
95     NavigatorBase& m_navigator;
96
97     RefPtr<SWClientConnection> m_swConnection;
98     HashMap<uint64_t, RefPtr<ServiceWorkerJob>> m_jobMap;
99     mutable RefPtr<ServiceWorker> m_controller;
100
101 #ifndef NDEBUG
102     ThreadIdentifier m_creationThread { currentThread() };
103 #endif
104 };
105
106 } // namespace WebCore
107
108 #endif // ENABLE(SERVICE_WORKER)