ca6c336ac6303e62d177ba383b90a759de04c159
[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 <pal/SessionID.h>
37 #include <wtf/HashMap.h>
38
39 namespace WebCore {
40
41 class IndexKey;
42 class SQLiteDatabase;
43 class SQLiteStatement;
44
45 namespace IDBServer {
46
47 class IDBSerializationContext;
48 class SQLiteIDBCursor;
49
50 class SQLiteIDBBackingStore : public IDBBackingStore {
51     WTF_MAKE_FAST_ALLOCATED;
52 public:
53     SQLiteIDBBackingStore(PAL::SessionID, const IDBDatabaseIdentifier&, const String& databaseRootDirectory, IDBBackingStoreTemporaryFileHandler&);
54     
55     ~SQLiteIDBBackingStore() final;
56
57     IDBError getOrEstablishDatabaseInfo(IDBDatabaseInfo&) final;
58
59     IDBError beginTransaction(const IDBTransactionInfo&) final;
60     IDBError abortTransaction(const IDBResourceIdentifier& transactionIdentifier) final;
61     IDBError commitTransaction(const IDBResourceIdentifier& transactionIdentifier) final;
62     IDBError createObjectStore(const IDBResourceIdentifier& transactionIdentifier, const IDBObjectStoreInfo&) final;
63     IDBError deleteObjectStore(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier) final;
64     IDBError renameObjectStore(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, const String& newName) final;
65     IDBError clearObjectStore(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier) final;
66     IDBError createIndex(const IDBResourceIdentifier& transactionIdentifier, const IDBIndexInfo&) final;
67     IDBError deleteIndex(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t indexIdentifier) final;
68     IDBError renameIndex(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t indexIdentifier, const String& newName) final;
69     IDBError keyExistsInObjectStore(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, const IDBKeyData&, bool& keyExists) final;
70     IDBError deleteRange(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, const IDBKeyRangeData&) final;
71     IDBError addRecord(const IDBResourceIdentifier& transactionIdentifier, const IDBObjectStoreInfo&, const IDBKeyData&, const IDBValue&) final;
72     IDBError getRecord(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, const IDBKeyRangeData&, IDBGetRecordDataType, IDBGetResult& outValue) final;
73     IDBError getAllRecords(const IDBResourceIdentifier& transactionIdentifier, const IDBGetAllRecordsData&, IDBGetAllResult& outValue) final;
74     IDBError getIndexRecord(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t indexIdentifier, IndexedDB::IndexRecordType, const IDBKeyRangeData&, IDBGetResult& outValue) final;
75     IDBError getCount(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t indexIdentifier, const IDBKeyRangeData&, uint64_t& outCount) final;
76     IDBError generateKeyNumber(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t& keyNumber) final;
77     IDBError revertGeneratedKeyNumber(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t keyNumber) final;
78     IDBError maybeUpdateKeyGeneratorNumber(const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, double newKeyNumber) final;
79     IDBError openCursor(const IDBResourceIdentifier& transactionIdentifier, const IDBCursorInfo&, IDBGetResult& outResult) final;
80     IDBError iterateCursor(const IDBResourceIdentifier& transactionIdentifier, const IDBResourceIdentifier& cursorIdentifier, const IDBIterateCursorData&, IDBGetResult& outResult) final;
81     bool prefetchCursor(const IDBResourceIdentifier&, const IDBResourceIdentifier&) final;
82
83     IDBObjectStoreInfo* infoForObjectStore(uint64_t objectStoreIdentifier) final;
84     void deleteBackingStore() final;
85
86     bool supportsSimultaneousTransactions() final { return false; }
87     bool isEphemeral() final { return false; }
88
89     void unregisterCursor(SQLiteIDBCursor&);
90
91     IDBBackingStoreTemporaryFileHandler& temporaryFileHandler() const { return m_temporaryFileHandler; }
92
93     IDBError getBlobRecordsForObjectStoreRecord(int64_t objectStoreRecord, Vector<String>& blobURLs, Vector<String>& blobFilePaths);
94
95     static String databaseNameFromEncodedFilename(const String&);
96     static uint64_t databasesSizeForDirectory(const String& directory);
97
98     String databaseDirectory() const { return m_databaseDirectory; };
99     static String fullDatabasePathForDirectory(const String&);
100     static String databaseNameFromFile(const String&);
101
102     bool hasTransaction(const IDBResourceIdentifier&) const final;
103
104     PAL::SessionID sessionID() const { return m_sessionID; }
105
106 private:
107     String filenameForDatabaseName() const;
108     String fullDatabasePath() const;
109     String fullDatabaseDirectoryWithUpgrade();
110     
111     String databaseRootDirectoryIsolatedCopy() const { return m_databaseRootDirectory.isolatedCopy(); }
112
113     bool ensureValidRecordsTable();
114     bool ensureValidIndexRecordsTable();
115     bool ensureValidIndexRecordsIndex();
116     bool ensureValidBlobTables();
117     std::unique_ptr<IDBDatabaseInfo> createAndPopulateInitialDatabaseInfo();
118     std::unique_ptr<IDBDatabaseInfo> extractExistingDatabaseInfo();
119
120     IDBError deleteRecord(SQLiteIDBTransaction&, int64_t objectStoreID, const IDBKeyData&);
121     IDBError uncheckedGetKeyGeneratorValue(int64_t objectStoreID, uint64_t& outValue);
122     IDBError uncheckedSetKeyGeneratorValue(int64_t objectStoreID, uint64_t value);
123
124     IDBError updateAllIndexesForAddRecord(const IDBObjectStoreInfo&, const IDBKeyData&, const ThreadSafeDataBuffer& value, int64_t recordID);
125     IDBError updateOneIndexForAddRecord(const IDBIndexInfo&, const IDBKeyData&, const ThreadSafeDataBuffer& value, int64_t recordID);
126     IDBError uncheckedPutIndexKey(const IDBIndexInfo&, const IDBKeyData& keyValue, const IndexKey&, int64_t recordID);
127     IDBError uncheckedPutIndexRecord(int64_t objectStoreID, int64_t indexID, const IDBKeyData& keyValue, const IDBKeyData& indexKey, int64_t recordID);
128     IDBError uncheckedHasIndexRecord(const IDBIndexInfo&, const IDBKeyData&, bool& hasRecord);
129     IDBError uncheckedGetIndexRecordForOneKey(int64_t indexeID, int64_t objectStoreID, IndexedDB::IndexRecordType, const IDBKeyData&, IDBGetResult&);
130
131     IDBError deleteUnusedBlobFileRecords(SQLiteIDBTransaction&);
132
133     IDBError getAllObjectStoreRecords(const IDBResourceIdentifier& transactionIdentifier, const IDBGetAllRecordsData&, IDBGetAllResult& outValue);
134     IDBError getAllIndexRecords(const IDBResourceIdentifier& transactionIdentifier, const IDBGetAllRecordsData&, IDBGetAllResult& outValue);
135
136     void closeSQLiteDB();
137     void close() final;
138     
139     uint64_t databaseSize() const final;
140
141     enum class SQL : size_t {
142         CreateObjectStoreInfo,
143         CreateObjectStoreKeyGenerator,
144         DeleteObjectStoreInfo,
145         DeleteObjectStoreKeyGenerator,
146         DeleteObjectStoreRecords,
147         DeleteObjectStoreIndexInfo,
148         DeleteObjectStoreIndexRecords,
149         DeleteObjectStoreBlobRecords,
150         RenameObjectStore,
151         ClearObjectStoreRecords,
152         ClearObjectStoreIndexRecords,
153         CreateIndexInfo,
154         DeleteIndexInfo,
155         HasIndexRecord,
156         PutIndexRecord,
157         GetIndexRecordForOneKey,
158         DeleteIndexRecords,
159         RenameIndex,
160         KeyExistsInObjectStore,
161         GetUnusedBlobFilenames,
162         DeleteUnusedBlobs,
163         GetObjectStoreRecordID,
164         DeleteBlobRecord,
165         DeleteObjectStoreRecord,
166         DeleteObjectStoreIndexRecord,
167         AddObjectStoreRecord,
168         AddBlobRecord,
169         BlobFilenameForBlobURL,
170         AddBlobFilename,
171         GetBlobURL,
172         GetKeyGeneratorValue,
173         SetKeyGeneratorValue,
174         GetAllKeyRecordsLowerOpenUpperOpen,
175         GetAllKeyRecordsLowerOpenUpperClosed,
176         GetAllKeyRecordsLowerClosedUpperOpen,
177         GetAllKeyRecordsLowerClosedUpperClosed,
178         GetValueRecordsLowerOpenUpperOpen,
179         GetValueRecordsLowerOpenUpperClosed,
180         GetValueRecordsLowerClosedUpperOpen,
181         GetValueRecordsLowerClosedUpperClosed,
182         GetKeyRecordsLowerOpenUpperOpen,
183         GetKeyRecordsLowerOpenUpperClosed,
184         GetKeyRecordsLowerClosedUpperOpen,
185         GetKeyRecordsLowerClosedUpperClosed,
186         CountRecordsLowerOpenUpperOpen,
187         CountRecordsLowerOpenUpperClosed,
188         CountRecordsLowerClosedUpperOpen,
189         CountRecordsLowerClosedUpperClosed,
190         CountIndexRecordsLowerOpenUpperOpen,
191         CountIndexRecordsLowerOpenUpperClosed,
192         CountIndexRecordsLowerClosedUpperOpen,
193         CountIndexRecordsLowerClosedUpperClosed,
194         Invalid,
195     };
196
197     SQLiteStatement* cachedStatement(SQL, const char*);
198     SQLiteStatement* cachedStatementForGetAllObjectStoreRecords(const IDBGetAllRecordsData&);
199
200     std::unique_ptr<SQLiteStatement> m_cachedStatements[static_cast<int>(SQL::Invalid)];
201
202     PAL::SessionID m_sessionID;
203     IDBDatabaseIdentifier m_identifier;
204     std::unique_ptr<IDBDatabaseInfo> m_databaseInfo;
205     std::unique_ptr<IDBDatabaseInfo> m_originalDatabaseInfoBeforeVersionChange;
206
207     std::unique_ptr<SQLiteDatabase> m_sqliteDB;
208
209     HashMap<IDBResourceIdentifier, std::unique_ptr<SQLiteIDBTransaction>> m_transactions;
210     HashMap<IDBResourceIdentifier, SQLiteIDBCursor*> m_cursors;
211
212     String m_databaseRootDirectory;
213     String m_databaseDirectory;
214
215     IDBBackingStoreTemporaryFileHandler& m_temporaryFileHandler;
216
217     Ref<IDBSerializationContext> m_serializationContext;
218 };
219
220 } // namespace IDBServer
221 } // namespace WebCore
222
223 #endif // ENABLE(INDEXED_DATABASE)