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 "SWServer.h"
31#include "ServiceWorkerClientIdentifier.h"
32#include "ServiceWorkerRegistrationData.h"
33#include "ServiceWorkerTypes.h"
34#include <wtf/HashCountedSet.h>
35#include <wtf/MonotonicTime.h>
36#include <wtf/WallTime.h>
37
38namespace WebCore {
39
40class SWServer;
41class SWServerWorker;
42enum class ServiceWorkerRegistrationState : uint8_t;
43enum class ServiceWorkerState : uint8_t;
44struct ExceptionData;
45struct ServiceWorkerContextData;
46struct ServiceWorkerFetchResult;
47
48class SWServerRegistration {
49 WTF_MAKE_FAST_ALLOCATED;
50public:
51 SWServerRegistration(SWServer&, const ServiceWorkerRegistrationKey&, ServiceWorkerUpdateViaCache, const URL& scopeURL, const URL& scriptURL);
52 ~SWServerRegistration();
53
54 const ServiceWorkerRegistrationKey& key() const { return m_registrationKey; }
55 ServiceWorkerRegistrationIdentifier identifier() const { return m_identifier; }
56
57 SWServerWorker* getNewestWorker();
58 WEBCORE_EXPORT ServiceWorkerRegistrationData data() const;
59
60 bool isUninstalling() const { return m_uninstalling; }
61 void setIsUninstalling(bool);
62
63 void setLastUpdateTime(WallTime);
64 WallTime lastUpdateTime() const { return m_lastUpdateTime; }
65
66 void setUpdateViaCache(ServiceWorkerUpdateViaCache);
67 ServiceWorkerUpdateViaCache updateViaCache() const { return m_updateViaCache; }
68
69 void updateRegistrationState(ServiceWorkerRegistrationState, SWServerWorker*);
70 void updateWorkerState(SWServerWorker&, ServiceWorkerState);
71 void fireUpdateFoundEvent();
72
73 void addClientServiceWorkerRegistration(SWServerConnectionIdentifier);
74 void removeClientServiceWorkerRegistration(SWServerConnectionIdentifier);
75
76 void setPreInstallationWorker(SWServerWorker*);
77 SWServerWorker* preInstallationWorker() const { return m_preInstallationWorker.get(); }
78 SWServerWorker* installingWorker() const { return m_installingWorker.get(); }
79 SWServerWorker* waitingWorker() const { return m_waitingWorker.get(); }
80 SWServerWorker* activeWorker() const { return m_activeWorker.get(); }
81
82 MonotonicTime creationTime() const { return m_creationTime; }
83
84 bool hasClientsUsingRegistration() const { return !m_clientsUsingRegistration.isEmpty(); }
85 void addClientUsingRegistration(const ServiceWorkerClientIdentifier&);
86 void removeClientUsingRegistration(const ServiceWorkerClientIdentifier&);
87 void unregisterServerConnection(SWServerConnectionIdentifier);
88
89 void notifyClientsOfControllerChange();
90 void controlClient(ServiceWorkerClientIdentifier);
91
92 void clear();
93 bool tryClear();
94 void tryActivate();
95 void didFinishActivation(ServiceWorkerIdentifier);
96
97 void forEachConnection(const WTF::Function<void(SWServer::Connection&)>&);
98
99private:
100 void activate();
101 void handleClientUnload();
102
103 ServiceWorkerRegistrationIdentifier m_identifier;
104 ServiceWorkerRegistrationKey m_registrationKey;
105 ServiceWorkerUpdateViaCache m_updateViaCache;
106 URL m_scopeURL;
107 URL m_scriptURL;
108
109 bool m_uninstalling { false };
110 RefPtr<SWServerWorker> m_preInstallationWorker; // Implementation detail, not part of the specification.
111 RefPtr<SWServerWorker> m_installingWorker;
112 RefPtr<SWServerWorker> m_waitingWorker;
113 RefPtr<SWServerWorker> m_activeWorker;
114
115 WallTime m_lastUpdateTime;
116
117 HashCountedSet<SWServerConnectionIdentifier> m_connectionsWithClientRegistrations;
118 SWServer& m_server;
119
120 MonotonicTime m_creationTime;
121 HashMap<SWServerConnectionIdentifier, HashSet<DocumentIdentifier>> m_clientsUsingRegistration;
122};
123
124} // namespace WebCore
125
126#endif // ENABLE(SERVICE_WORKER)
127