1/*
2 * Copyright (C) 2015 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 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#pragma once
27
28#if ENABLE(INDEXED_DATABASE)
29
30#include "IDBDatabaseInfo.h"
31#include "IDBKeyData.h"
32#include "IDBTransactionInfo.h"
33#include "IndexValueStore.h"
34#include "ThreadSafeDataBuffer.h"
35#include <wtf/HashMap.h>
36#include <wtf/HashSet.h>
37
38namespace WebCore {
39namespace IDBServer {
40
41class MemoryIDBBackingStore;
42class MemoryIndex;
43class MemoryObjectStore;
44
45typedef HashMap<IDBKeyData, ThreadSafeDataBuffer, IDBKeyDataHash, IDBKeyDataHashTraits> KeyValueMap;
46
47class MemoryBackingStoreTransaction {
48 WTF_MAKE_FAST_ALLOCATED;
49public:
50 static std::unique_ptr<MemoryBackingStoreTransaction> create(MemoryIDBBackingStore&, const IDBTransactionInfo&);
51
52 MemoryBackingStoreTransaction(MemoryIDBBackingStore&, const IDBTransactionInfo&);
53 ~MemoryBackingStoreTransaction();
54
55 bool isVersionChange() const { return m_info.mode() == IDBTransactionMode::Versionchange; }
56 bool isWriting() const { return m_info.mode() != IDBTransactionMode::Readonly; }
57 bool isAborting() const { return m_isAborting; }
58
59 const IDBDatabaseInfo& originalDatabaseInfo() const;
60
61 void addNewObjectStore(MemoryObjectStore&);
62 void addExistingObjectStore(MemoryObjectStore&);
63
64 void recordValueChanged(MemoryObjectStore&, const IDBKeyData&, ThreadSafeDataBuffer*);
65 void objectStoreDeleted(Ref<MemoryObjectStore>&&);
66 void objectStoreCleared(MemoryObjectStore&, std::unique_ptr<KeyValueMap>&&, std::unique_ptr<IDBKeyDataSet>&&);
67 void objectStoreRenamed(MemoryObjectStore&, const String& oldName);
68 void indexRenamed(MemoryIndex&, const String& oldName);
69 void indexCleared(MemoryIndex&, std::unique_ptr<IndexValueStore>&&);
70
71 void addNewIndex(MemoryIndex&);
72 void addExistingIndex(MemoryIndex&);
73 void indexDeleted(Ref<MemoryIndex>&&);
74
75 void abort();
76 void commit();
77
78private:
79 void finish();
80
81 MemoryIDBBackingStore& m_backingStore;
82 IDBTransactionInfo m_info;
83
84 std::unique_ptr<IDBDatabaseInfo> m_originalDatabaseInfo;
85
86 bool m_inProgress { true };
87 bool m_isAborting { false };
88
89 HashSet<RefPtr<MemoryObjectStore>> m_objectStores;
90 HashSet<RefPtr<MemoryObjectStore>> m_versionChangeAddedObjectStores;
91 HashSet<RefPtr<MemoryIndex>> m_indexes;
92 HashSet<RefPtr<MemoryIndex>> m_versionChangeAddedIndexes;
93
94 HashMap<MemoryObjectStore*, uint64_t> m_originalKeyGenerators;
95 HashMap<String, RefPtr<MemoryObjectStore>> m_deletedObjectStores;
96 HashMap<String, RefPtr<MemoryIndex>> m_deletedIndexes;
97 HashMap<MemoryObjectStore*, std::unique_ptr<KeyValueMap>> m_originalValues;
98 HashMap<MemoryObjectStore*, std::unique_ptr<KeyValueMap>> m_clearedKeyValueMaps;
99 HashMap<MemoryObjectStore*, std::unique_ptr<IDBKeyDataSet>> m_clearedOrderedKeys;
100 HashMap<MemoryObjectStore*, String> m_originalObjectStoreNames;
101 HashMap<MemoryIndex*, String> m_originalIndexNames;
102 HashMap<MemoryIndex*, std::unique_ptr<IndexValueStore>> m_clearedIndexValueStores;
103};
104
105} // namespace IDBServer
106} // namespace WebCore
107
108#endif // ENABLE(INDEXED_DATABASE)
109