Remove IDBBackingStoreTemporaryFileHandler
[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);
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     IDBError getBlobRecordsForObjectStoreRecord(int64_t objectStoreRecord, Vector<String>& blobURLs, Vector<String>& blobFilePaths);
92
93     static String databaseNameFromEncodedFilename(const String&);
94     static uint64_t databasesSizeForDirectory(const String& directory);
95
96     String databaseDirectory() const { return m_databaseDirectory; };
97     static String fullDatabasePathForDirectory(const String&);
98     static String databaseNameFromFile(const String&);
99
100     bool hasTransaction(const IDBResourceIdentifier&) const final;
101
102     PAL::SessionID sessionID() const { return m_sessionID; }
103
104 private:
105     String filenameForDatabaseName() const;
106     String fullDatabasePath() const;
107     String fullDatabaseDirectoryWithUpgrade();
108     
109     String databaseRootDirectoryIsolatedCopy() const { return m_databaseRootDirectory.isolatedCopy(); }
110
111     bool ensureValidRecordsTable();
112     bool ensureValidIndexRecordsTable();
113     bool ensureValidIndexRecordsIndex();
114     bool ensureValidBlobTables();
115     std::unique_ptr<IDBDatabaseInfo> createAndPopulateInitialDatabaseInfo();
116     std::unique_ptr<IDBDatabaseInfo> extractExistingDatabaseInfo();
117
118     IDBError deleteRecord(SQLiteIDBTransaction&, int64_t objectStoreID, const IDBKeyData&);
119     IDBError uncheckedGetKeyGeneratorValue(int64_t objectStoreID, uint64_t& outValue);
120     IDBError uncheckedSetKeyGeneratorValue(int64_t objectStoreID, uint64_t value);
121
122     IDBError updateAllIndexesForAddRecord(const IDBObjectStoreInfo&, const IDBKeyData&, const ThreadSafeDataBuffer& value, int64_t recordID);
123     IDBError updateOneIndexForAddRecord(const IDBIndexInfo&, const IDBKeyData&, const ThreadSafeDataBuffer& value, int64_t recordID);
124     IDBError uncheckedPutIndexKey(const IDBIndexInfo&, const IDBKeyData& keyValue, const IndexKey&, int64_t recordID);
125     IDBError uncheckedPutIndexRecord(int64_t objectStoreID, int64_t indexID, const IDBKeyData& keyValue, const IDBKeyData& indexKey, int64_t recordID);
126     IDBError uncheckedHasIndexRecord(const IDBIndexInfo&, const IDBKeyData&, bool& hasRecord);
127     IDBError uncheckedGetIndexRecordForOneKey(int64_t indexeID, int64_t objectStoreID, IndexedDB::IndexRecordType, const IDBKeyData&, IDBGetResult&);
128
129     IDBError deleteUnusedBlobFileRecords(SQLiteIDBTransaction&);
130
131     IDBError getAllObjectStoreRecords(const IDBResourceIdentifier& transactionIdentifier, const IDBGetAllRecordsData&, IDBGetAllResult& outValue);
132     IDBError getAllIndexRecords(const IDBResourceIdentifier& transactionIdentifier, const IDBGetAllRecordsData&, IDBGetAllResult& outValue);
133
134     void closeSQLiteDB();
135     void close() final;
136     
137     uint64_t databaseSize() const final;
138
139     enum class SQL : size_t {
140         CreateObjectStoreInfo,
141         CreateObjectStoreKeyGenerator,
142         DeleteObjectStoreInfo,
143         DeleteObjectStoreKeyGenerator,
144         DeleteObjectStoreRecords,
145         DeleteObjectStoreIndexInfo,
146         DeleteObjectStoreIndexRecords,
147         DeleteObjectStoreBlobRecords,
148         RenameObjectStore,
149         ClearObjectStoreRecords,
150         ClearObjectStoreIndexRecords,
151         CreateIndexInfo,
152         DeleteIndexInfo,
153         HasIndexRecord,
154         PutIndexRecord,
155         GetIndexRecordForOneKey,
156         DeleteIndexRecords,
157         RenameIndex,
158         KeyExistsInObjectStore,
159         GetUnusedBlobFilenames,
160         DeleteUnusedBlobs,
161         GetObjectStoreRecordID,
162         DeleteBlobRecord,
163         DeleteObjectStoreRecord,
164         DeleteObjectStoreIndexRecord,
165         AddObjectStoreRecord,
166         AddBlobRecord,
167         BlobFilenameForBlobURL,
168         AddBlobFilename,
169         GetBlobURL,
170         GetKeyGeneratorValue,
171         SetKeyGeneratorValue,
172         GetAllKeyRecordsLowerOpenUpperOpen,
173         GetAllKeyRecordsLowerOpenUpperClosed,
174         GetAllKeyRecordsLowerClosedUpperOpen,
175         GetAllKeyRecordsLowerClosedUpperClosed,
176         GetValueRecordsLowerOpenUpperOpen,
177         GetValueRecordsLowerOpenUpperClosed,
178         GetValueRecordsLowerClosedUpperOpen,
179         GetValueRecordsLowerClosedUpperClosed,
180         GetKeyRecordsLowerOpenUpperOpen,
181         GetKeyRecordsLowerOpenUpperClosed,
182         GetKeyRecordsLowerClosedUpperOpen,
183         GetKeyRecordsLowerClosedUpperClosed,
184         CountRecordsLowerOpenUpperOpen,
185         CountRecordsLowerOpenUpperClosed,
186         CountRecordsLowerClosedUpperOpen,
187         CountRecordsLowerClosedUpperClosed,
188         CountIndexRecordsLowerOpenUpperOpen,
189         CountIndexRecordsLowerOpenUpperClosed,
190         CountIndexRecordsLowerClosedUpperOpen,
191         CountIndexRecordsLowerClosedUpperClosed,
192         Invalid,
193     };
194
195     SQLiteStatement* cachedStatement(SQL, const char*);
196     SQLiteStatement* cachedStatementForGetAllObjectStoreRecords(const IDBGetAllRecordsData&);
197
198     std::unique_ptr<SQLiteStatement> m_cachedStatements[static_cast<int>(SQL::Invalid)];
199
200     PAL::SessionID m_sessionID;
201     IDBDatabaseIdentifier m_identifier;
202     std::unique_ptr<IDBDatabaseInfo> m_databaseInfo;
203     std::unique_ptr<IDBDatabaseInfo> m_originalDatabaseInfoBeforeVersionChange;
204
205     std::unique_ptr<SQLiteDatabase> m_sqliteDB;
206
207     HashMap<IDBResourceIdentifier, std::unique_ptr<SQLiteIDBTransaction>> m_transactions;
208     HashMap<IDBResourceIdentifier, SQLiteIDBCursor*> m_cursors;
209
210     String m_databaseRootDirectory;
211     String m_databaseDirectory;
212
213     Ref<IDBSerializationContext> m_serializationContext;
214 };
215
216 } // namespace IDBServer
217 } // namespace WebCore
218
219 #endif // ENABLE(INDEXED_DATABASE)