Close service worker database on network process suspension
[WebKit-https.git] / Source / WebCore / workers / service / server / RegistrationStore.cpp
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 #include "config.h"
27 #include "RegistrationStore.h"
28
29 #if ENABLE(SERVICE_WORKER)
30
31 #include "SWServerRegistration.h"
32
33 namespace WebCore {
34
35 RegistrationStore::RegistrationStore(SWServer& server, String&& databaseDirectory)
36     : m_server(server)
37     , m_database(RegistrationDatabase::create(*this, WTFMove(databaseDirectory)))
38     , m_databasePushTimer(*this, &RegistrationStore::pushChangesToDatabase)
39 {
40 }
41
42 RegistrationStore::~RegistrationStore()
43 {
44 }
45
46 void RegistrationStore::scheduleDatabasePushIfNecessary()
47 {
48     if (m_databasePushTimer.isActive())
49         return;
50     m_databasePushTimer.startOneShot(0_s);
51 }
52
53 void RegistrationStore::pushChangesToDatabase(WTF::CompletionHandler<void()>&& completionHandler)
54 {
55     if (m_isSuspended) {
56         m_needsPushingChanges = true;
57         return;
58     }
59
60     Vector<ServiceWorkerContextData> changesToPush;
61     changesToPush.reserveInitialCapacity(m_updatedRegistrations.size());
62     for (auto& value : m_updatedRegistrations.values())
63         changesToPush.uncheckedAppend(WTFMove(value));
64
65     m_updatedRegistrations.clear();
66     m_database->pushChanges(WTFMove(changesToPush), WTFMove(completionHandler));
67 }
68
69 void RegistrationStore::clearAll(WTF::CompletionHandler<void()>&& completionHandler)
70 {
71     m_needsPushingChanges = false;
72     m_updatedRegistrations.clear();
73     m_databasePushTimer.stop();
74     m_database->clearAll(WTFMove(completionHandler));
75 }
76
77 void RegistrationStore::flushChanges(WTF::CompletionHandler<void()>&& completionHandler)
78 {
79     if (m_databasePushTimer.isActive()) {
80         pushChangesToDatabase(WTFMove(completionHandler));
81         m_databasePushTimer.stop();
82         return;
83     }
84     completionHandler();
85 }
86
87 void RegistrationStore::startSuspension(WTF::CompletionHandler<void()>&& completionHandler)
88 {
89     m_isSuspended = true;
90     m_database->close(WTFMove(completionHandler));
91 }
92
93 void RegistrationStore::endSuspension()
94 {
95     m_isSuspended = false;
96     if (m_needsPushingChanges)
97         scheduleDatabasePushIfNecessary();
98 }
99
100 void RegistrationStore::updateRegistration(const ServiceWorkerContextData& data)
101 {
102     m_updatedRegistrations.set(data.registration.key, data);
103     scheduleDatabasePushIfNecessary();
104 }
105
106 void RegistrationStore::removeRegistration(SWServerRegistration& registration)
107 {
108     ServiceWorkerContextData contextData;
109     contextData.registration.key = registration.key();
110     m_updatedRegistrations.set(registration.key(), WTFMove(contextData));
111     scheduleDatabasePushIfNecessary();
112 }
113
114 void RegistrationStore::addRegistrationFromDatabase(ServiceWorkerContextData&& context)
115 {
116     m_server.addRegistrationFromStore(WTFMove(context));
117 }
118
119 void RegistrationStore::databaseFailedToOpen()
120 {
121     m_server.registrationStoreDatabaseFailedToOpen();
122 }
123
124 void RegistrationStore::databaseOpenedAndRecordsImported()
125 {
126     m_server.registrationStoreImportComplete();
127 }
128
129 } // namespace WebCore
130
131 #endif // ENABLE(SERVICE_WORKER)