3b27a2b5630d843cd8de95dda3cb8ec75313747f
[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 COMPUTER, 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 }
39
40 namespace WebKit {
41
42 class AsyncRequest;
43
44 class WebIDBServerConnection final : public WebCore::IDBServerConnection, public IPC::MessageSender {
45 public:
46     static PassRefPtr<WebIDBServerConnection> create(const String& databaseName, const WebCore::SecurityOrigin& openingOrigin, const WebCore::SecurityOrigin& mainFrameOrigin);
47
48     virtual ~WebIDBServerConnection();
49
50     virtual bool isClosed() override;
51
52     typedef std::function<void (bool success)> BoolCallbackFunction;
53
54     // Factory-level operations
55     virtual void deleteDatabase(const String& name, BoolCallbackFunction successCallback) override;
56
57     // Database-level operations
58     virtual void getOrEstablishIDBDatabaseMetadata(GetIDBDatabaseMetadataFunction) override;
59     virtual void close() override;
60
61     // Transaction-level operations
62     virtual void openTransaction(int64_t transactionID, const HashSet<int64_t>& objectStoreIds, WebCore::IndexedDB::TransactionMode, BoolCallbackFunction successCallback) override;
63     virtual void beginTransaction(int64_t transactionID, std::function<void()> completionCallback) override;
64     virtual void commitTransaction(int64_t transactionID, BoolCallbackFunction successCallback) override;
65     virtual void resetTransaction(int64_t transactionID, std::function<void()> completionCallback) override;
66     virtual void rollbackTransaction(int64_t transactionID, std::function<void()> completionCallback) override;
67
68     virtual void setIndexKeys(int64_t transactionID, int64_t databaseID, int64_t objectStoreID, const WebCore::IDBObjectStoreMetadata&, WebCore::IDBKey& primaryKey, const Vector<int64_t>& indexIDs, const Vector<Vector<RefPtr<WebCore::IDBKey>>>& indexKeys, std::function<void(PassRefPtr<WebCore::IDBDatabaseError>)> completionCallback) override;
69
70     virtual void createObjectStore(WebCore::IDBTransactionBackend&, const WebCore::CreateObjectStoreOperation&, std::function<void(PassRefPtr<WebCore::IDBDatabaseError>)> completionCallback) override;
71     virtual void createIndex(WebCore::IDBTransactionBackend&, const WebCore::CreateIndexOperation&, std::function<void(PassRefPtr<WebCore::IDBDatabaseError>)> completionCallback) override;
72     virtual void deleteIndex(WebCore::IDBTransactionBackend&, const WebCore::DeleteIndexOperation&, std::function<void(PassRefPtr<WebCore::IDBDatabaseError>)> completionCallback) override;
73     virtual void get(WebCore::IDBTransactionBackend&, const WebCore::GetOperation&, std::function<void(const WebCore::IDBGetResult&, PassRefPtr<WebCore::IDBDatabaseError>)> completionCallback) override;
74     virtual void put(WebCore::IDBTransactionBackend&, const WebCore::PutOperation&, std::function<void(PassRefPtr<WebCore::IDBKey>, PassRefPtr<WebCore::IDBDatabaseError>)> completionCallback) override;
75     virtual void openCursor(WebCore::IDBTransactionBackend&, const WebCore::OpenCursorOperation&, std::function<void(int64_t, PassRefPtr<WebCore::IDBDatabaseError>)> completionCallback) override;
76     virtual void count(WebCore::IDBTransactionBackend&, const WebCore::CountOperation&, std::function<void(int64_t, PassRefPtr<WebCore::IDBDatabaseError>)> completionCallback) override;
77     virtual void deleteRange(WebCore::IDBTransactionBackend&, const WebCore::DeleteRangeOperation&, std::function<void(PassRefPtr<WebCore::IDBDatabaseError>)> completionCallback) override;
78     virtual void clearObjectStore(WebCore::IDBTransactionBackend&, const WebCore::ClearObjectStoreOperation&, std::function<void(PassRefPtr<WebCore::IDBDatabaseError>)> completionCallback) override;
79     virtual void deleteObjectStore(WebCore::IDBTransactionBackend&, const WebCore::DeleteObjectStoreOperation&, std::function<void(PassRefPtr<WebCore::IDBDatabaseError>)> completionCallback) override;
80     virtual void changeDatabaseVersion(WebCore::IDBTransactionBackend&, const WebCore::IDBDatabaseBackend::VersionChangeOperation&, std::function<void(PassRefPtr<WebCore::IDBDatabaseError>)> completionCallback) override;
81
82     // Cursor-level operations
83     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;
84     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;
85
86     // Message handlers.
87     void didReceiveWebIDBServerConnectionMessage(IPC::Connection*, IPC::MessageDecoder&);
88
89     // IPC::MessageSender
90     virtual uint64_t messageSenderDestinationID() override { return m_serverConnectionIdentifier; }
91
92 private:
93     WebIDBServerConnection(const String& databaseName, const WebCore::SecurityOrigin& openingOrigin, const WebCore::SecurityOrigin& mainFrameOrigin);
94
95     // IPC::MessageSender
96     virtual IPC::Connection* messageSenderConnection() override;
97
98     void didGetOrEstablishIDBDatabaseMetadata(uint64_t requestID, bool success, const WebCore::IDBDatabaseMetadata&);
99     void didOpenTransaction(uint64_t requestID, bool success);
100     void didBeginTransaction(uint64_t requestID, bool success);
101     void didCommitTransaction(uint64_t requestID, bool success);
102     void didResetTransaction(uint64_t requestID, bool success);
103     void didRollbackTransaction(uint64_t requestID, bool success);
104     void didChangeDatabaseVersion(uint64_t requestID, bool success);
105     void didCreateObjectStore(uint64_t requestID, bool success);
106     void didDeleteObjectStore(uint64_t requestID, bool success);
107     void didClearObjectStore(uint64_t requestID, bool success);
108     void didCreateIndex(uint64_t requestID, bool success);
109     void didDeleteIndex(uint64_t requestID, bool success);
110     void didPutRecord(uint64_t requestID, const WebCore::IDBKeyData&, uint32_t errorCode, const String& errorMessage);
111     void didGetRecord(uint64_t requestID, const WebCore::IDBGetResult&, uint32_t errorCode, const String& errorMessage);
112     void didOpenCursor(uint64_t requestID, int64_t cursorID, uint32_t errorCode, const String& errorMessage);
113     void didAdvanceCursor(uint64_t requestID, WebCore::IDBKeyData&, WebCore::IDBKeyData&, const IPC::DataReference&, uint32_t errorCode, const String& errorMessage);
114     void didIterateCursor(uint64_t requestID, WebCore::IDBKeyData&, WebCore::IDBKeyData&, const IPC::DataReference&, uint32_t errorCode, const String& errorMessage);
115
116     uint64_t m_serverConnectionIdentifier;
117
118     String m_databaseName;
119     Ref<WebCore::SecurityOrigin> m_openingOrigin;
120     Ref<WebCore::SecurityOrigin> m_mainFrameOrigin;
121
122     HashMap<uint64_t, RefPtr<AsyncRequest>> m_serverRequests;
123 };
124
125 } // namespace WebKit
126
127 #endif // ENABLE(INDEXED_DATABASE) && ENABLE(DATABASE_PROCESS)
128 #endif // WebIDBServerConnection_h