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 "EventTarget.h"
32#include "JSDOMPromiseDeferred.h"
33#include "SWClientConnection.h"
34#include "ServiceWorkerRegistrationData.h"
35#include "Timer.h"
36
37namespace WebCore {
38
39class ScriptExecutionContext;
40class ServiceWorker;
41class ServiceWorkerContainer;
42
43class ServiceWorkerRegistration final : public RefCounted<ServiceWorkerRegistration>, public EventTargetWithInlineData, public ActiveDOMObject {
44 WTF_MAKE_ISO_ALLOCATED(ServiceWorkerRegistration);
45public:
46 static Ref<ServiceWorkerRegistration> getOrCreate(ScriptExecutionContext&, Ref<ServiceWorkerContainer>&&, ServiceWorkerRegistrationData&&);
47
48 ~ServiceWorkerRegistration();
49
50 ServiceWorkerRegistrationIdentifier identifier() const { return m_registrationData.identifier; }
51
52 ServiceWorker* installing();
53 ServiceWorker* waiting();
54 ServiceWorker* active();
55
56 ServiceWorker* getNewestWorker();
57
58 const String& scope() const;
59
60 ServiceWorkerUpdateViaCache updateViaCache() const;
61 void setUpdateViaCache(ServiceWorkerUpdateViaCache);
62
63 WallTime lastUpdateTime() const;
64 void setLastUpdateTime(WallTime);
65
66 bool needsUpdate() const { return lastUpdateTime() && (WallTime::now() - lastUpdateTime()) > 86400_s; }
67
68 void update(Ref<DeferredPromise>&&);
69 void unregister(Ref<DeferredPromise>&&);
70
71 void scheduleSoftUpdate();
72
73 using RefCounted::ref;
74 using RefCounted::deref;
75
76 const ServiceWorkerRegistrationData& data() const { return m_registrationData; }
77
78 void updateStateFromServer(ServiceWorkerRegistrationState, RefPtr<ServiceWorker>&&);
79 void fireUpdateFoundEvent();
80
81private:
82 ServiceWorkerRegistration(ScriptExecutionContext&, Ref<ServiceWorkerContainer>&&, ServiceWorkerRegistrationData&&);
83 void updatePendingActivityForEventDispatch();
84
85 EventTargetInterface eventTargetInterface() const final;
86 ScriptExecutionContext* scriptExecutionContext() const final;
87 void refEventTarget() final { ref(); }
88 void derefEventTarget() final { deref(); }
89
90 void softUpdate();
91
92 // ActiveDOMObject.
93 const char* activeDOMObjectName() const final;
94 bool canSuspendForDocumentSuspension() const final;
95 void stop() final;
96
97 ServiceWorkerRegistrationData m_registrationData;
98 Ref<ServiceWorkerContainer> m_container;
99
100 RefPtr<ServiceWorker> m_installingWorker;
101 RefPtr<ServiceWorker> m_waitingWorker;
102 RefPtr<ServiceWorker> m_activeWorker;
103
104 bool m_isStopped { false };
105 RefPtr<PendingActivity<ServiceWorkerRegistration>> m_pendingActivityForEventDispatch;
106 Timer m_softUpdateTimer;
107};
108
109} // namespace WebCore
110
111#endif // ENABLE(SERVICE_WORKER)
112