0aeecdc9e36b6be6b9eb993e7460fcd88b735c54
[WebKit-https.git] / Source / WebKit2 / WebProcess / Databases / IndexedDB / WebIDBServerConnection.h
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. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  */
26
27 #ifndef WebIDBServerConnection_h
28 #define WebIDBServerConnection_h
29
30 #if ENABLE(INDEXED_DATABASE) && ENABLE(DATABASE_PROCESS)
31
32 #include "MessageSender.h"
33 #include <WebCore/IDBDatabaseMetadata.h>
34 #include <WebCore/IDBServerConnection.h>
35
36 namespace WebCore {
37 struct IDBKeyData;
38 class SecurityOrigin;
39 }
40
41 namespace WebKit {
42
43 class AsyncRequest;
44
45 class WebIDBServerConnection final : public WebCore::IDBServerConnection, public IPC::MessageSender {
46 public:
47     static PassRefPtr<WebIDBServerConnection> create(const String& databaseName, const WebCore::SecurityOrigin& openingOrigin, const WebCore::SecurityOrigin& mainFrameOrigin);
48
49     virtual ~WebIDBServerConnection();
50
51     virtual bool isClosed() override;
52
53     typedef std::function<void (bool success)> BoolCallbackFunction;
54
55     // Factory-level operations
56     virtual void deleteDatabase(const String& name, BoolCallbackFunction successCallback) override;
57
58     // Database-level operations
59     virtual void getOrEstablishIDBDatabaseMetadata(GetIDBDatabaseMetadataFunction) override;
60     virtual void close() override;
61
62     // Transaction-level operations
63     virtual void openTransaction(int64_t transactionID, const HashSet<int64_t>& objectStoreIds, WebCore::IndexedDB::TransactionMode, BoolCallbackFunction successCallback) override;
64     virtual void beginTransaction(int64_t transactionID, std::function<void ()> completionCallback) override;
65     virtual void commitTransaction(int64_t transactionID, BoolCallbackFunction successCallback) override;
66     virtual void resetTransaction(int64_t transactionID, std::function<void ()> completionCallback) override;
67     virtual bool resetTransactionSync(int64_t transactionID) override;
68     virtual void rollbackTransaction(int64_t transactionID, std::function<void ()> completionCallback) override;
69     virtual bool rollbackTransactionSync(int64_t transactionID) override;
70
71     virtual void setIndexKeys(int64_t transactionID, int64_t databaseID, int64_t objectStoreID, const WebCore::IDBObjectStoreMetadata&, WebCore::IDBKey& primaryKey, const Vector<int64_t, 1>& indexIDs, const Vector<Vector<RefPtr<WebCore::IDBKey>>, 1>& indexKeys, std::function<void (PassRefPtr<WebCore::IDBDatabaseError>)> completionCallback) override;
72
73     virtual void createObjectStore(WebCore::IDBTransactionBackend&, const WebCore::CreateObjectStoreOperation&, std::function<void (PassRefPtr<WebCore::IDBDatabaseError>)> completionCallback) override;
74     virtual void createIndex(WebCore::IDBTransactionBackend&, const WebCore::CreateIndexOperation&, std::function<void (PassRefPtr<WebCore::IDBDatabaseError>)> completionCallback) override;
75     virtual void deleteIndex(WebCore::IDBTransactionBackend&, const WebCore::DeleteIndexOperation&, std::function<void (PassRefPtr<WebCore::IDBDatabaseError>)> completionCallback) override;
76     virtual void get(WebCore::IDBTransactionBackend&, const WebCore::GetOperation&, std::function<void (const WebCore::IDBGetResult&, PassRefPtr<WebCore::IDBDatabaseError>)> completionCallback) override;
77     virtual void put(WebCore::IDBTransactionBackend&, const WebCore::PutOperation&, std::function<void (PassRefPtr<WebCore::IDBKey>, PassRefPtr<WebCore::IDBDatabaseError>)> completionCallback) override;
78     virtual void openCursor(WebCore::IDBTransactionBackend&, const WebCore::OpenCursorOperation&, std::function<void (int64_t, PassRefPtr<WebCore::IDBKey>, PassRefPtr<WebCore::IDBKey>, PassRefPtr<WebCore::SharedBuffer>, PassRefPtr<WebCore::IDBDatabaseError>)> completionCallback) override;
79     virtual void count(WebCore::IDBTransactionBackend&, const WebCore::CountOperation&, std::function<void (int64_t, PassRefPtr<WebCore::IDBDatabaseError>)> completionCallback) override;
80     virtual void deleteRange(WebCore::IDBTransactionBackend&, const WebCore::DeleteRangeOperation&, std::function<void (PassRefPtr<WebCore::IDBDatabaseError>)> completionCallback) override;
81     virtual void clearObjectStore(WebCore::IDBTransactionBackend&, const WebCore::ClearObjectStoreOperation&, std::function<void (PassRefPtr<WebCore::IDBDatabaseError>)> completionCallback) override;
82     virtual void deleteObjectStore(WebCore::IDBTransactionBackend&, const WebCore::DeleteObjectStoreOperation&, std::function<void (PassRefPtr<WebCore::IDBDatabaseError>)> completionCallback) override;
83     virtual void changeDatabaseVersion(WebCore::IDBTransactionBackend&, const WebCore::IDBDatabaseBackend::VersionChangeOperation&, std::function<void (PassRefPtr<WebCore::IDBDatabaseError>)> completionCallback) override;
84
85     // Cursor-level operations
86     virtual void cursorAdvance(WebCore::IDBCursorBackend&, const WebCore::CursorAdvanceOperation&, std::function<void (PassRefPtr<WebCore::IDBKey>, PassRefPtr<WebCore::IDBKey>, PassRefPtr<WebCore::SharedBuffer>, PassRefPtr<WebCore::IDBDatabaseError>)> completionCallback) override;
87     virtual void cursorIterate(WebCore::IDBCursorBackend&, const WebCore::CursorIterationOperation&, std::function<void (PassRefPtr<WebCore::IDBKey>, PassRefPtr<WebCore::IDBKey>, PassRefPtr<WebCore::SharedBuffer>, PassRefPtr<WebCore::IDBDatabaseError>)> completionCallback) override;
88
89     // Message handlers.
90     void didReceiveWebIDBServerConnectionMessage(IPC::Connection&, IPC::MessageDecoder&);
91
92     // IPC::MessageSender
93     virtual uint64_t messageSenderDestinationID() override { return m_serverConnectionIdentifier; }
94
95 private:
96     WebIDBServerConnection(const String& databaseName, const WebCore::SecurityOrigin& openingOrigin, const WebCore::SecurityOrigin& mainFrameOrigin);
97
98     // IPC::MessageSender
99     virtual IPC::Connection* messageSenderConnection() override;
100
101     void didGetOrEstablishIDBDatabaseMetadata(uint64_t requestID, bool success, const WebCore::IDBDatabaseMetadata&);
102     void didDeleteDatabase(uint64_t requestID, bool success);
103     void didOpenTransaction(uint64_t requestID, bool success);
104     void didBeginTransaction(uint64_t requestID, bool success);
105     void didCommitTransaction(uint64_t requestID, bool success);
106     void didResetTransaction(uint64_t requestID, bool success);
107     void didRollbackTransaction(uint64_t requestID, bool success);
108     void didChangeDatabaseVersion(uint64_t requestID, bool success);
109     void didCreateObjectStore(uint64_t requestID, bool success);
110     void didDeleteObjectStore(uint64_t requestID, bool success);
111     void didClearObjectStore(uint64_t requestID, bool success);
112     void didCreateIndex(uint64_t requestID, bool success);
113     void didDeleteIndex(uint64_t requestID, bool success);
114     void didPutRecord(uint64_t requestID, const WebCore::IDBKeyData&, uint32_t errorCode, const String& errorMessage);
115     void didGetRecord(uint64_t requestID, const WebCore::IDBGetResult&, uint32_t errorCode, const String& errorMessage);
116     void didOpenCursor(uint64_t requestID, int64_t cursorID, const WebCore::IDBKeyData&, const WebCore::IDBKeyData&, const IPC::DataReference&, uint32_t errorCode, const String& errorMessage);
117     void didAdvanceCursor(uint64_t requestID, const WebCore::IDBKeyData&, const WebCore::IDBKeyData&, const IPC::DataReference&, uint32_t errorCode, const String& errorMessage);
118     void didIterateCursor(uint64_t requestID, const WebCore::IDBKeyData&, const WebCore::IDBKeyData&, const IPC::DataReference&, uint32_t errorCode, const String& errorMessage);
119     void didCount(uint64_t requestID, int64_t count, uint32_t errorCode, const String& errorMessage);
120     void didDeleteRange(uint64_t requestID, uint32_t errorCode, const String& errorMessage);
121
122     uint64_t m_serverConnectionIdentifier;
123
124     String m_databaseName;
125     Ref<WebCore::SecurityOrigin> m_openingOrigin;
126     Ref<WebCore::SecurityOrigin> m_mainFrameOrigin;
127
128     HashMap<uint64_t, RefPtr<AsyncRequest>> m_serverRequests;
129 };
130
131 } // namespace WebKit
132
133 #endif // ENABLE(INDEXED_DATABASE) && ENABLE(DATABASE_PROCESS)
134 #endif // WebIDBServerConnection_h