| 1 | /* |
| 2 | * Copyright (C) 2015, 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 "IDBConnectionToClient.h" |
| 31 | #include "IDBDatabaseIdentifier.h" |
| 32 | #include "StorageQuotaManager.h" |
| 33 | #include "StorageQuotaUser.h" |
| 34 | #include "UniqueIDBDatabase.h" |
| 35 | #include "UniqueIDBDatabaseConnection.h" |
| 36 | #include <pal/HysteresisActivity.h> |
| 37 | #include <pal/SessionID.h> |
| 38 | #include <wtf/CrossThreadTaskHandler.h> |
| 39 | #include <wtf/HashMap.h> |
| 40 | #include <wtf/Lock.h> |
| 41 | #include <wtf/Ref.h> |
| 42 | #include <wtf/RefCounted.h> |
| 43 | #include <wtf/RefPtr.h> |
| 44 | #include <wtf/WeakPtr.h> |
| 45 | |
| 46 | namespace WebCore { |
| 47 | |
| 48 | class IDBCursorInfo; |
| 49 | class IDBRequestData; |
| 50 | class IDBValue; |
| 51 | class StorageQuotaManager; |
| 52 | |
| 53 | struct IDBGetRecordData; |
| 54 | |
| 55 | namespace IDBServer { |
| 56 | |
| 57 | const uint64_t defaultPerOriginQuota = 500 * MB; |
| 58 | |
| 59 | class IDBBackingStoreTemporaryFileHandler; |
| 60 | |
| 61 | enum class ShouldForceStop : bool { No, Yes }; |
| 62 | |
| 63 | class IDBServer : public RefCounted<IDBServer>, public CrossThreadTaskHandler, public CanMakeWeakPtr<IDBServer> { |
| 64 | public: |
| 65 | using QuotaManagerGetter = WTF::Function<StorageQuotaManager*(PAL::SessionID, const ClientOrigin&)>; |
| 66 | static Ref<IDBServer> create(PAL::SessionID, IDBBackingStoreTemporaryFileHandler&, QuotaManagerGetter&&); |
| 67 | WEBCORE_EXPORT static Ref<IDBServer> create(PAL::SessionID, const String& databaseDirectoryPath, IDBBackingStoreTemporaryFileHandler&, QuotaManagerGetter&&); |
| 68 | |
| 69 | WEBCORE_EXPORT void registerConnection(IDBConnectionToClient&); |
| 70 | WEBCORE_EXPORT void unregisterConnection(IDBConnectionToClient&); |
| 71 | |
| 72 | // Operations requested by the client. |
| 73 | WEBCORE_EXPORT void openDatabase(const IDBRequestData&); |
| 74 | WEBCORE_EXPORT void deleteDatabase(const IDBRequestData&); |
| 75 | WEBCORE_EXPORT void abortTransaction(const IDBResourceIdentifier&); |
| 76 | WEBCORE_EXPORT void commitTransaction(const IDBResourceIdentifier&); |
| 77 | WEBCORE_EXPORT void didFinishHandlingVersionChangeTransaction(uint64_t databaseConnectionIdentifier, const IDBResourceIdentifier&); |
| 78 | WEBCORE_EXPORT void createObjectStore(const IDBRequestData&, const IDBObjectStoreInfo&); |
| 79 | WEBCORE_EXPORT void renameObjectStore(const IDBRequestData&, uint64_t objectStoreIdentifier, const String& newName); |
| 80 | WEBCORE_EXPORT void deleteObjectStore(const IDBRequestData&, const String& objectStoreName); |
| 81 | WEBCORE_EXPORT void clearObjectStore(const IDBRequestData&, uint64_t objectStoreIdentifier); |
| 82 | WEBCORE_EXPORT void createIndex(const IDBRequestData&, const IDBIndexInfo&); |
| 83 | WEBCORE_EXPORT void deleteIndex(const IDBRequestData&, uint64_t objectStoreIdentifier, const String& indexName); |
| 84 | WEBCORE_EXPORT void renameIndex(const IDBRequestData&, uint64_t objectStoreIdentifier, uint64_t indexIdentifier, const String& newName); |
| 85 | WEBCORE_EXPORT void putOrAdd(const IDBRequestData&, const IDBKeyData&, const IDBValue&, IndexedDB::ObjectStoreOverwriteMode); |
| 86 | WEBCORE_EXPORT void getRecord(const IDBRequestData&, const IDBGetRecordData&); |
| 87 | WEBCORE_EXPORT void getAllRecords(const IDBRequestData&, const IDBGetAllRecordsData&); |
| 88 | WEBCORE_EXPORT void getCount(const IDBRequestData&, const IDBKeyRangeData&); |
| 89 | WEBCORE_EXPORT void deleteRecord(const IDBRequestData&, const IDBKeyRangeData&); |
| 90 | WEBCORE_EXPORT void openCursor(const IDBRequestData&, const IDBCursorInfo&); |
| 91 | WEBCORE_EXPORT void iterateCursor(const IDBRequestData&, const IDBIterateCursorData&); |
| 92 | |
| 93 | WEBCORE_EXPORT void establishTransaction(uint64_t databaseConnectionIdentifier, const IDBTransactionInfo&); |
| 94 | WEBCORE_EXPORT void databaseConnectionPendingClose(uint64_t databaseConnectionIdentifier); |
| 95 | WEBCORE_EXPORT void databaseConnectionClosed(uint64_t databaseConnectionIdentifier); |
| 96 | WEBCORE_EXPORT void abortOpenAndUpgradeNeeded(uint64_t databaseConnectionIdentifier, const IDBResourceIdentifier& transactionIdentifier); |
| 97 | WEBCORE_EXPORT void didFireVersionChangeEvent(uint64_t databaseConnectionIdentifier, const IDBResourceIdentifier& requestIdentifier); |
| 98 | WEBCORE_EXPORT void openDBRequestCancelled(const IDBRequestData&); |
| 99 | WEBCORE_EXPORT void confirmDidCloseFromServer(uint64_t databaseConnectionIdentifier); |
| 100 | |
| 101 | WEBCORE_EXPORT void getAllDatabaseNames(uint64_t serverConnectionIdentifier, const SecurityOriginData& mainFrameOrigin, const SecurityOriginData& openingOrigin, uint64_t callbackID); |
| 102 | |
| 103 | void postDatabaseTask(CrossThreadTask&&); |
| 104 | void postDatabaseTaskReply(CrossThreadTask&&); |
| 105 | |
| 106 | void registerDatabaseConnection(UniqueIDBDatabaseConnection&); |
| 107 | void unregisterDatabaseConnection(UniqueIDBDatabaseConnection&); |
| 108 | void registerTransaction(UniqueIDBDatabaseTransaction&); |
| 109 | void unregisterTransaction(UniqueIDBDatabaseTransaction&); |
| 110 | |
| 111 | std::unique_ptr<UniqueIDBDatabase> closeAndTakeUniqueIDBDatabase(UniqueIDBDatabase&); |
| 112 | |
| 113 | std::unique_ptr<IDBBackingStore> createBackingStore(const IDBDatabaseIdentifier&); |
| 114 | |
| 115 | WEBCORE_EXPORT void closeAndDeleteDatabasesModifiedSince(WallTime, Function<void ()>&& completionHandler); |
| 116 | WEBCORE_EXPORT void closeAndDeleteDatabasesForOrigins(const Vector<SecurityOriginData>&, Function<void ()>&& completionHandler); |
| 117 | |
| 118 | uint64_t perOriginQuota() const { return m_perOriginQuota; } |
| 119 | WEBCORE_EXPORT void setPerOriginQuota(uint64_t); |
| 120 | |
| 121 | void requestSpace(const ClientOrigin&, uint64_t taskSize, CompletionHandler<void(StorageQuotaManager::Decision)>&&); |
| 122 | void increasePotentialSpaceUsed(const ClientOrigin&, uint64_t taskSize); |
| 123 | void decreasePotentialSpaceUsed(const ClientOrigin&, uint64_t taskSize); |
| 124 | void setSpaceUsed(const ClientOrigin&, uint64_t spaceUsed); |
| 125 | void resetSpaceUsed(const ClientOrigin&); |
| 126 | |
| 127 | void initializeQuotaUser(const ClientOrigin& origin) { ensureQuotaUser(origin); } |
| 128 | |
| 129 | WEBCORE_EXPORT void tryStop(ShouldForceStop); |
| 130 | WEBCORE_EXPORT void resume(); |
| 131 | |
| 132 | private: |
| 133 | IDBServer(PAL::SessionID, IDBBackingStoreTemporaryFileHandler&, QuotaManagerGetter&&); |
| 134 | IDBServer(PAL::SessionID, const String& databaseDirectoryPath, IDBBackingStoreTemporaryFileHandler&, QuotaManagerGetter&&); |
| 135 | |
| 136 | UniqueIDBDatabase& getOrCreateUniqueIDBDatabase(const IDBDatabaseIdentifier&); |
| 137 | |
| 138 | void performGetAllDatabaseNames(uint64_t serverConnectionIdentifier, const SecurityOriginData& mainFrameOrigin, const SecurityOriginData& openingOrigin, uint64_t callbackID); |
| 139 | void didGetAllDatabaseNames(uint64_t serverConnectionIdentifier, uint64_t callbackID, const Vector<String>& databaseNames); |
| 140 | |
| 141 | void performCloseAndDeleteDatabasesModifiedSince(WallTime, uint64_t callbackID); |
| 142 | void performCloseAndDeleteDatabasesForOrigins(const Vector<SecurityOriginData>&, uint64_t callbackID); |
| 143 | void didPerformCloseAndDeleteDatabases(uint64_t callbackID); |
| 144 | |
| 145 | void upgradeFilesIfNecessary(); |
| 146 | void removeDatabasesModifiedSinceForVersion(WallTime, const String&); |
| 147 | void removeDatabasesWithOriginsForVersion(const Vector<SecurityOriginData>&, const String&); |
| 148 | |
| 149 | class QuotaUser final : public StorageQuotaUser { |
| 150 | WTF_MAKE_FAST_ALLOCATED; |
| 151 | public: |
| 152 | QuotaUser(IDBServer&, StorageQuotaManager*, ClientOrigin&&); |
| 153 | ~QuotaUser(); |
| 154 | |
| 155 | StorageQuotaManager* manager() { return m_manager.get(); } |
| 156 | |
| 157 | void setSpaceUsed(uint64_t spaceUsed) { m_spaceUsed = spaceUsed; } |
| 158 | void resetSpaceUsed(); |
| 159 | |
| 160 | void increasePotentialSpaceUsed(uint64_t increase) { m_estimatedSpaceIncrease += increase; } |
| 161 | void decreasePotentialSpaceUsed(uint64_t decrease) |
| 162 | { |
| 163 | ASSERT(m_estimatedSpaceIncrease >= decrease); |
| 164 | m_estimatedSpaceIncrease -= decrease; |
| 165 | } |
| 166 | |
| 167 | void initializeSpaceUsed(uint64_t spaceUsed); |
| 168 | |
| 169 | private: |
| 170 | uint64_t spaceUsed() const final { return m_spaceUsed + m_estimatedSpaceIncrease; } |
| 171 | void whenInitialized(CompletionHandler<void()>&&) final; |
| 172 | |
| 173 | IDBServer& m_server; |
| 174 | WeakPtr<StorageQuotaManager> m_manager; |
| 175 | ClientOrigin m_origin; |
| 176 | bool m_isInitialized { false }; |
| 177 | uint64_t m_spaceUsed { 0 }; |
| 178 | uint64_t m_estimatedSpaceIncrease { 0 }; |
| 179 | CompletionHandler<void()> m_initializationCallback; |
| 180 | }; |
| 181 | |
| 182 | WEBCORE_EXPORT QuotaUser& ensureQuotaUser(const ClientOrigin&); |
| 183 | void startComputingSpaceUsedForOrigin(const ClientOrigin&); |
| 184 | void computeSpaceUsedForOrigin(const ClientOrigin&); |
| 185 | void finishComputingSpaceUsedForOrigin(const ClientOrigin&, uint64_t spaceUsed); |
| 186 | |
| 187 | PAL::SessionID m_sessionID; |
| 188 | HashMap<uint64_t, RefPtr<IDBConnectionToClient>> m_connectionMap; |
| 189 | HashMap<IDBDatabaseIdentifier, std::unique_ptr<UniqueIDBDatabase>> m_uniqueIDBDatabaseMap; |
| 190 | |
| 191 | HashMap<uint64_t, UniqueIDBDatabaseConnection*> m_databaseConnections; |
| 192 | HashMap<IDBResourceIdentifier, UniqueIDBDatabaseTransaction*> m_transactions; |
| 193 | |
| 194 | HashMap<uint64_t, Function<void ()>> m_deleteDatabaseCompletionHandlers; |
| 195 | |
| 196 | String m_databaseDirectoryPath; |
| 197 | IDBBackingStoreTemporaryFileHandler& m_backingStoreTemporaryFileHandler; |
| 198 | |
| 199 | uint64_t m_perOriginQuota { defaultPerOriginQuota }; |
| 200 | |
| 201 | HashMap<ClientOrigin, std::unique_ptr<QuotaUser>> m_quotaUsers; |
| 202 | QuotaManagerGetter m_quotaManagerGetter; |
| 203 | }; |
| 204 | |
| 205 | } // namespace IDBServer |
| 206 | } // namespace WebCore |
| 207 | |
| 208 | #endif // ENABLE(INDEXED_DATABASE) |
| 209 | |