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#ifndef DatabaseProcess_h
27#define DatabaseProcess_h
28
29#if ENABLE(DATABASE_PROCESS)
30
31#include "ChildProcess.h"
32#include "UniqueIDBDatabaseIdentifier.h"
33#include <wtf/NeverDestroyed.h>
34
35class WorkQueue;
36
37namespace WebKit {
38
39class AsyncTask;
40class DatabaseToWebProcessConnection;
41class UniqueIDBDatabase;
42class WebOriginDataManager;
43
44struct DatabaseProcessCreationParameters;
45
46class DatabaseProcess : public ChildProcess  {
47    WTF_MAKE_NONCOPYABLE(DatabaseProcess);
48    friend class NeverDestroyed<DatabaseProcess>;
49public:
50    static DatabaseProcess& shared();
51    ~DatabaseProcess();
52
53    const String& indexedDatabaseDirectory() const { return m_indexedDatabaseDirectory; }
54
55    PassRefPtr<UniqueIDBDatabase> getOrCreateUniqueIDBDatabase(const UniqueIDBDatabaseIdentifier&);
56    void removeUniqueIDBDatabase(const UniqueIDBDatabase&);
57
58    void ensureIndexedDatabaseRelativePathExists(const String&);
59    String absoluteIndexedDatabasePathFromDatabaseRelativePath(const String&);
60
61    WorkQueue& queue() { return m_queue.get(); }
62
63    void getIndexedDatabaseOrigins(uint64_t callbackID);
64    void deleteIndexedDatabaseEntriesForOrigin(const SecurityOriginData&, uint64_t callbackID);
65    void deleteIndexedDatabaseEntriesModifiedBetweenDates(double startDate, double endDate, uint64_t callbackID);
66    void deleteAllIndexedDatabaseEntries(uint64_t callbackID);
67
68private:
69    DatabaseProcess();
70
71    // ChildProcess
72    virtual void initializeProcess(const ChildProcessInitializationParameters&) override;
73    virtual void initializeProcessName(const ChildProcessInitializationParameters&) override;
74    virtual void initializeSandbox(const ChildProcessInitializationParameters&, SandboxInitializationParameters&) override;
75    virtual void initializeConnection(IPC::Connection*) override;
76    virtual bool shouldTerminate() override;
77
78    // IPC::Connection::Client
79    virtual void didReceiveMessage(IPC::Connection*, IPC::MessageDecoder&) override;
80    virtual void didClose(IPC::Connection*) override;
81    virtual void didReceiveInvalidMessage(IPC::Connection*, IPC::StringReference messageReceiverName, IPC::StringReference messageName) override;
82    void didReceiveDatabaseProcessMessage(IPC::Connection*, IPC::MessageDecoder&);
83
84    // Message Handlers
85    void initializeDatabaseProcess(const DatabaseProcessCreationParameters&);
86    void createDatabaseToWebProcessConnection();
87
88    void postDatabaseTask(std::unique_ptr<AsyncTask>);
89
90    // For execution on work queue thread only
91    void performNextDatabaseTask();
92    void ensurePathExists(const String&);
93    void doGetIndexedDatabaseOrigins(uint64_t callbackID);
94    void doDeleteIndexedDatabaseEntriesForOrigin(const SecurityOriginData&, uint64_t callbackID);
95    void doDeleteIndexedDatabaseEntriesModifiedBetweenDates(double startDate, double endDate, uint64_t callbackID);
96    void doDeleteAllIndexedDatabaseEntries(uint64_t callbackID);
97
98    Vector<RefPtr<DatabaseToWebProcessConnection>> m_databaseToWebProcessConnections;
99
100    Ref<WorkQueue> m_queue;
101
102    String m_indexedDatabaseDirectory;
103
104    HashMap<UniqueIDBDatabaseIdentifier, RefPtr<UniqueIDBDatabase>> m_idbDatabases;
105
106    Deque<std::unique_ptr<AsyncTask>> m_databaseTasks;
107    Mutex m_databaseTaskMutex;
108
109    std::unique_ptr<WebOriginDataManager> m_webOriginDataManager;
110};
111
112} // namespace WebKit
113
114#endif // ENABLE(DATABASE_PROCESS)
115
116#endif // DatabaseProcess_h
117