Stop using isMainThread() in WebKit2
[WebKit-https.git] / Source / WebKit2 / DatabaseProcess / DatabaseProcess.cpp
1 /*
2  * Copyright (C) 2013 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 "DatabaseProcess.h"
28
29 #if ENABLE(DATABASE_PROCESS)
30
31 #include "AsyncTask.h"
32 #include "DatabaseProcessCreationParameters.h"
33 #include "DatabaseProcessProxyMessages.h"
34 #include "DatabaseToWebProcessConnection.h"
35 #include "UniqueIDBDatabase.h"
36 #include <WebCore/FileSystem.h>
37 #include <wtf/MainThread.h>
38
39 using namespace WebCore;
40
41 namespace WebKit {
42
43 DatabaseProcess& DatabaseProcess::shared()
44 {
45     static NeverDestroyed<DatabaseProcess> databaseProcess;
46     return databaseProcess;
47 }
48
49 DatabaseProcess::DatabaseProcess()
50     : m_queue(adoptRef(*WorkQueue::create("com.apple.WebKit.DatabaseProcess").leakRef()))
51 {
52 }
53
54 DatabaseProcess::~DatabaseProcess()
55 {
56 }
57
58 void DatabaseProcess::initializeConnection(IPC::Connection* connection)
59 {
60     ChildProcess::initializeConnection(connection);
61 }
62
63 bool DatabaseProcess::shouldTerminate()
64 {
65     return true;
66 }
67
68 void DatabaseProcess::didClose(IPC::Connection*)
69 {
70     RunLoop::current()->stop();
71 }
72
73 void DatabaseProcess::didReceiveInvalidMessage(IPC::Connection*, IPC::StringReference, IPC::StringReference)
74 {
75     RunLoop::current()->stop();
76 }
77
78 PassRefPtr<UniqueIDBDatabase> DatabaseProcess::getOrCreateUniqueIDBDatabase(const UniqueIDBDatabaseIdentifier& identifier)
79 {
80     auto addResult = m_idbDatabases.add(identifier, nullptr);
81
82     if (!addResult.isNewEntry)
83         return addResult.iterator->value;
84
85     RefPtr<UniqueIDBDatabase> database = UniqueIDBDatabase::create(identifier);
86     addResult.iterator->value = database.get();
87     return database.release();
88 }
89
90 void DatabaseProcess::removeUniqueIDBDatabase(const UniqueIDBDatabase& database)
91 {
92     const UniqueIDBDatabaseIdentifier& identifier = database.identifier();
93     ASSERT(m_idbDatabases.contains(identifier));
94
95     m_idbDatabases.remove(identifier);
96 }
97
98 void DatabaseProcess::initializeDatabaseProcess(const DatabaseProcessCreationParameters& parameters)
99 {
100     m_indexedDatabaseDirectory = parameters.indexedDatabaseDirectory;
101
102     ensureIndexedDatabaseRelativePathExists(StringImpl::empty());
103 }
104
105 void DatabaseProcess::ensureIndexedDatabaseRelativePathExists(const String& relativePath)
106 {
107     postDatabaseTask(createAsyncTask(*this, &DatabaseProcess::ensurePathExists, absoluteIndexedDatabasePathFromDatabaseRelativePath(relativePath)));
108 }
109
110 void DatabaseProcess::ensurePathExists(const String& path)
111 {
112     ASSERT(!RunLoop::isMain());
113
114     if (!makeAllDirectories(path))
115         LOG_ERROR("Failed to make all directories for path '%s'", path.utf8().data());
116 }
117
118 String DatabaseProcess::absoluteIndexedDatabasePathFromDatabaseRelativePath(const String& relativePath)
119 {
120     // FIXME: pathByAppendingComponent() was originally designed to append individual atomic components.
121     // We don't have a function designed to append a multi-component subpath, but we should.
122     return pathByAppendingComponent(m_indexedDatabaseDirectory, relativePath);
123 }
124
125 void DatabaseProcess::postDatabaseTask(std::unique_ptr<AsyncTask> task)
126 {
127     ASSERT(RunLoop::isMain());
128
129     MutexLocker locker(m_databaseTaskMutex);
130
131     m_databaseTasks.append(std::move(task));
132
133     m_queue->dispatch(bind(&DatabaseProcess::performNextDatabaseTask, this));
134 }
135
136 void DatabaseProcess::performNextDatabaseTask()
137 {
138     ASSERT(!RunLoop::isMain());
139
140     std::unique_ptr<AsyncTask> task;
141     {
142         MutexLocker locker(m_databaseTaskMutex);
143         ASSERT(!m_databaseTasks.isEmpty());
144         task = m_databaseTasks.takeFirst();
145     }
146
147     task->performTask();
148 }
149
150 void DatabaseProcess::createDatabaseToWebProcessConnection()
151 {
152 #if OS(DARWIN)
153     // Create the listening port.
154     mach_port_t listeningPort;
155     mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &listeningPort);
156
157     // Create a listening connection.
158     RefPtr<DatabaseToWebProcessConnection> connection = DatabaseToWebProcessConnection::create(IPC::Connection::Identifier(listeningPort));
159     m_databaseToWebProcessConnections.append(connection.release());
160
161     IPC::Attachment clientPort(listeningPort, MACH_MSG_TYPE_MAKE_SEND);
162     parentProcessConnection()->send(Messages::DatabaseProcessProxy::DidCreateDatabaseToWebProcessConnection(clientPort), 0);
163 #else
164     notImplemented();
165 #endif
166 }
167
168 #if !PLATFORM(COCOA)
169 void DatabaseProcess::initializeProcess(const ChildProcessInitializationParameters&)
170 {
171 }
172
173 void DatabaseProcess::initializeProcessName(const ChildProcessInitializationParameters&)
174 {
175 }
176
177 void DatabaseProcess::initializeSandbox(const ChildProcessInitializationParameters&, SandboxInitializationParameters&)
178 {
179 }
180 #endif
181
182 } // namespace WebKit
183
184 #endif // ENABLE(DATABASE_PROCESS)