02e698791caece37512d4b000533f86bcf29528c
[WebKit-https.git] / Source / WebCore / Modules / indexeddb / server / SQLiteIDBBackingStore.h
1 /*
2  * Copyright (C) 2016 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 #pragma once
27
28 #if ENABLE(INDEXED_DATABASE)
29
30 #include "IDBBackingStore.h"
31 #include "IDBDatabaseIdentifier.h"
32 #include "IDBDatabaseInfo.h"
33 #include "IDBResourceIdentifier.h"
34 #include "SQLiteIDBTransaction.h"
35 #include <JavaScriptCore/Strong.h>
36 #include <wtf/HashMap.h>
37
38 namespace WebCore {
39
40 class IndexKey;
41 class SQLiteDatabase;
42 class SQLiteStatement;
43
44 namespace IDBServer {
45
46 class SQLiteIDBCursor;
47
48 class SQLiteIDBBackingStore : public IDBBackingStore {
49 public:
50     SQLiteIDBBackingStore(const IDBDatabaseIdentifier&, const String& databaseRootDirectory, IDBBackingStoreTemporaryFileHandler&);
51     
52     ~SQLiteIDBBackingStore() final;
53
54     IDBError getOrEstablishDatabaseInfo(IDBDatabaseInfo&) final;
55
56     IDBError beginTransaction(const IDBTransactionInfo&) final;
57     IDBError abortTransaction(const IDBResourceIdentifier& transactionIdentifier) final;
58     IDBError commitTransaction(const IDBResourceIdentifier& transactionIdentifier) final;
59     IDBError createObjectStore(const IDBResourceIdentifier& transactionIdentifier, const IDBObjectStoreInfo&) final;
60     IDBError deleteObjectStore(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier) final;
61     IDBError renameObjectStore(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, const String& newName) final;
62     IDBError clearObjectStore(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier) final;
63     IDBError createIndex(const IDBResourceIdentifier& transactionIdentifier, const IDBIndexInfo&) final;
64     IDBError deleteIndex(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t indexIdentifier) final;
65     IDBError renameIndex(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t indexIdentifier, const String& newName) final;
66     IDBError keyExistsInObjectStore(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, const IDBKeyData&, bool& keyExists) final;
67     IDBError deleteRange(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, const IDBKeyRangeData&) final;
68     IDBError addRecord(const IDBResourceIdentifier& transactionIdentifier, const IDBObjectStoreInfo&, const IDBKeyData&, const IDBValue&) final;
69     IDBError getRecord(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, const IDBKeyRangeData&, IDBGetRecordDataType, IDBGetResult& outValue) final;
70     IDBError getAllRecords(const IDBResourceIdentifier& transactionIdentifier, const IDBGetAllRecordsData&, IDBGetAllResult& outValue) final;
71     IDBError getIndexRecord(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t indexIdentifier, IndexedDB::IndexRecordType, const IDBKeyRangeData&, IDBGetResult& outValue) final;
72     IDBError getCount(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t indexIdentifier, const IDBKeyRangeData&, uint64_t& outCount) final;
73     IDBError generateKeyNumber(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t& keyNumber) final;
74     IDBError revertGeneratedKeyNumber(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t keyNumber) final;
75     IDBError maybeUpdateKeyGeneratorNumber(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, double newKeyNumber) final;
76     IDBError openCursor(const IDBResourceIdentifier& transactionIdentifier, const IDBCursorInfo&, IDBGetResult& outResult) final;
77     IDBError iterateCursor(const IDBResourceIdentifier& transactionIdentifier, const IDBResourceIdentifier& cursorIdentifier, const IDBIterateCursorData&, IDBGetResult& outResult) final;
78
79     IDBObjectStoreInfo* infoForObjectStore(uint64_t objectStoreIdentifier) final;
80     void deleteBackingStore() final;
81
82     bool supportsSimultaneousTransactions() final { return false; }
83     bool isEphemeral() final { return false; }
84
85     void unregisterCursor(SQLiteIDBCursor&);
86
87     String fullDatabaseDirectory() const;
88
89     IDBBackingStoreTemporaryFileHandler& temporaryFileHandler() const { return m_temporaryFileHandler; }
90
91     IDBError getBlobRecordsForObjectStoreRecord(int64_t objectStoreRecord, Vector<String>& blobURLs, Vector<String>& blobFilePaths);
92
93     static String databaseNameFromEncodedFilename(const String&);
94
95 private:
96     String filenameForDatabaseName() const;
97     String fullDatabasePath() const;
98
99     bool ensureValidRecordsTable();
100     bool ensureValidIndexRecordsTable();
101     bool ensureValidIndexRecordsIndex();
102     bool ensureValidBlobTables();
103     std::unique_ptr<IDBDatabaseInfo> createAndPopulateInitialDatabaseInfo();
104     std::unique_ptr<IDBDatabaseInfo> extractExistingDatabaseInfo();
105
106     IDBError deleteRecord(SQLiteIDBTransaction&, int64_t objectStoreID, const IDBKeyData&);
107     IDBError uncheckedGetKeyGeneratorValue(int64_t objectStoreID, uint64_t& outValue);
108     IDBError uncheckedSetKeyGeneratorValue(int64_t objectStoreID, uint64_t value);
109
110     IDBError updateAllIndexesForAddRecord(const IDBObjectStoreInfo&, const IDBKeyData&, const ThreadSafeDataBuffer& value, int64_t recordID);
111     IDBError updateOneIndexForAddRecord(const IDBIndexInfo&, const IDBKeyData&, const ThreadSafeDataBuffer& value, int64_t recordID);
112     IDBError uncheckedPutIndexKey(const IDBIndexInfo&, const IDBKeyData& keyValue, const IndexKey&, int64_t recordID);
113     IDBError uncheckedPutIndexRecord(int64_t objectStoreID, int64_t indexID, const IDBKeyData& keyValue, const IDBKeyData& indexKey, int64_t recordID);
114     IDBError uncheckedHasIndexRecord(const IDBIndexInfo&, const IDBKeyData&, bool& hasRecord);
115     IDBError uncheckedGetIndexRecordForOneKey(int64_t indexeID, int64_t objectStoreID, IndexedDB::IndexRecordType, const IDBKeyData&, IDBGetResult&);
116
117     IDBError deleteUnusedBlobFileRecords(SQLiteIDBTransaction&);
118
119     IDBError getAllObjectStoreRecords(const IDBResourceIdentifier& transactionIdentifier, const IDBGetAllRecordsData&, IDBGetAllResult& outValue);
120     IDBError getAllIndexRecords(const IDBResourceIdentifier& transactionIdentifier, const IDBGetAllRecordsData&, IDBGetAllResult& outValue);
121
122     void closeSQLiteDB();
123
124     enum class SQL : size_t {
125         CreateObjectStoreInfo,
126         CreateObjectStoreKeyGenerator,
127         DeleteObjectStoreInfo,
128         DeleteObjectStoreKeyGenerator,
129         DeleteObjectStoreRecords,
130         DeleteObjectStoreIndexInfo,
131         DeleteObjectStoreIndexRecords,
132         DeleteObjectStoreBlobRecords,
133         RenameObjectStore,
134         ClearObjectStoreRecords,
135         ClearObjectStoreIndexRecords,
136         CreateIndexInfo,
137         DeleteIndexInfo,
138         HasIndexRecord,
139         PutIndexRecord,
140         GetIndexRecordForOneKey,
141         DeleteIndexRecords,
142         RenameIndex,
143         KeyExistsInObjectStore,
144         GetUnusedBlobFilenames,
145         DeleteUnusedBlobs,
146         GetObjectStoreRecordID,
147         DeleteBlobRecord,
148         DeleteObjectStoreRecord,
149         DeleteObjectStoreIndexRecord,
150         AddObjectStoreRecord,
151         AddBlobRecord,
152         BlobFilenameForBlobURL,
153         AddBlobFilename,
154         GetBlobURL,
155         GetKeyGeneratorValue,
156         SetKeyGeneratorValue,
157         GetAllKeyRecordsLowerOpenUpperOpen,
158         GetAllKeyRecordsLowerOpenUpperClosed,
159         GetAllKeyRecordsLowerClosedUpperOpen,
160         GetAllKeyRecordsLowerClosedUpperClosed,
161         GetValueRecordsLowerOpenUpperOpen,
162         GetValueRecordsLowerOpenUpperClosed,
163         GetValueRecordsLowerClosedUpperOpen,
164         GetValueRecordsLowerClosedUpperClosed,
165         GetKeyRecordsLowerOpenUpperOpen,
166         GetKeyRecordsLowerOpenUpperClosed,
167         GetKeyRecordsLowerClosedUpperOpen,
168         GetKeyRecordsLowerClosedUpperClosed,
169         Count
170     };
171
172     SQLiteStatement* cachedStatement(SQL, const char*);
173     SQLiteStatement* cachedStatementForGetAllObjectStoreRecords(const IDBGetAllRecordsData&);
174
175     std::unique_ptr<SQLiteStatement> m_cachedStatements[static_cast<int>(SQL::Count)];
176
177     JSC::VM& vm();
178     JSC::JSGlobalObject& globalObject();
179     void initializeVM();
180
181     IDBDatabaseIdentifier m_identifier;
182     std::unique_ptr<IDBDatabaseInfo> m_databaseInfo;
183     std::unique_ptr<IDBDatabaseInfo> m_originalDatabaseInfoBeforeVersionChange;
184
185     std::unique_ptr<SQLiteDatabase> m_sqliteDB;
186
187     HashMap<IDBResourceIdentifier, std::unique_ptr<SQLiteIDBTransaction>> m_transactions;
188     HashMap<IDBResourceIdentifier, SQLiteIDBCursor*> m_cursors;
189
190     String m_absoluteDatabaseDirectory;
191
192     RefPtr<JSC::VM> m_vm;
193     JSC::Strong<JSC::JSGlobalObject> m_globalObject;
194
195     IDBBackingStoreTemporaryFileHandler& m_temporaryFileHandler;
196 };
197
198 } // namespace IDBServer
199 } // namespace WebCore
200
201 #endif // ENABLE(INDEXED_DATABASE)