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. 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 "IDBDatabaseIdentifier.h"
31#include "IDBResourceIdentifier.h"
32#include "IndexedDB.h"
33
34namespace WebCore {
35
36class IDBOpenDBRequest;
37class IDBTransaction;
38
39namespace IndexedDB {
40enum class IndexRecordType;
41}
42
43namespace IDBClient {
44class IDBConnectionProxy;
45class TransactionOperation;
46}
47
48class IDBRequestData {
49public:
50 IDBRequestData(const IDBClient::IDBConnectionProxy&, const IDBOpenDBRequest&);
51 explicit IDBRequestData(IDBClient::TransactionOperation&);
52 IDBRequestData(const IDBRequestData&);
53 IDBRequestData(IDBRequestData&&) = default;
54 IDBRequestData& operator=(IDBRequestData&&) = default;
55
56 enum IsolatedCopyTag { IsolatedCopy };
57 IDBRequestData(const IDBRequestData&, IsolatedCopyTag);
58 IDBRequestData isolatedCopy() const;
59
60 uint64_t serverConnectionIdentifier() const;
61 IDBResourceIdentifier requestIdentifier() const;
62 IDBResourceIdentifier transactionIdentifier() const;
63 uint64_t objectStoreIdentifier() const;
64 uint64_t indexIdentifier() const;
65 IndexedDB::IndexRecordType indexRecordType() const;
66 IDBResourceIdentifier cursorIdentifier() const;
67
68 const IDBDatabaseIdentifier& databaseIdentifier() const { return m_databaseIdentifier; }
69 uint64_t requestedVersion() const;
70
71 bool isOpenRequest() const { return m_requestType == IndexedDB::RequestType::Open; }
72 bool isDeleteRequest() const { return m_requestType == IndexedDB::RequestType::Delete; }
73
74 IDBRequestData isolatedCopy();
75
76 WEBCORE_EXPORT IDBRequestData();
77
78 template<class Encoder> void encode(Encoder&) const;
79 template<class Decoder> static bool decode(Decoder&, IDBRequestData&);
80
81private:
82 static void isolatedCopy(const IDBRequestData& source, IDBRequestData& destination);
83
84 uint64_t m_serverConnectionIdentifier { 0 };
85 std::unique_ptr<IDBResourceIdentifier> m_requestIdentifier;
86 std::unique_ptr<IDBResourceIdentifier> m_transactionIdentifier;
87 std::unique_ptr<IDBResourceIdentifier> m_cursorIdentifier;
88 uint64_t m_objectStoreIdentifier { 0 };
89 uint64_t m_indexIdentifier { 0 };
90 IndexedDB::IndexRecordType m_indexRecordType;
91
92 IDBDatabaseIdentifier m_databaseIdentifier;
93 uint64_t m_requestedVersion { 0 };
94
95 IndexedDB::RequestType m_requestType { IndexedDB::RequestType::Other };
96};
97
98template<class Encoder>
99void IDBRequestData::encode(Encoder& encoder) const
100{
101 encoder << m_serverConnectionIdentifier << m_objectStoreIdentifier << m_indexIdentifier << m_databaseIdentifier << m_requestedVersion;
102
103 encoder.encodeEnum(m_indexRecordType);
104 encoder.encodeEnum(m_requestType);
105
106 encoder << !!m_requestIdentifier;
107 if (m_requestIdentifier)
108 encoder << *m_requestIdentifier;
109
110 encoder << !!m_transactionIdentifier;
111 if (m_transactionIdentifier)
112 encoder << *m_transactionIdentifier;
113
114 encoder << !!m_cursorIdentifier;
115 if (m_cursorIdentifier)
116 encoder << *m_cursorIdentifier;
117}
118
119template<class Decoder>
120bool IDBRequestData::decode(Decoder& decoder, IDBRequestData& request)
121{
122 if (!decoder.decode(request.m_serverConnectionIdentifier))
123 return false;
124
125 if (!decoder.decode(request.m_objectStoreIdentifier))
126 return false;
127
128 if (!decoder.decode(request.m_indexIdentifier))
129 return false;
130
131 Optional<IDBDatabaseIdentifier> databaseIdentifier;
132 decoder >> databaseIdentifier;
133 if (!databaseIdentifier)
134 return false;
135 request.m_databaseIdentifier = WTFMove(*databaseIdentifier);
136
137 if (!decoder.decode(request.m_requestedVersion))
138 return false;
139
140 if (!decoder.decodeEnum(request.m_indexRecordType))
141 return false;
142
143 if (!decoder.decodeEnum(request.m_requestType))
144 return false;
145
146 bool hasObject;
147
148 if (!decoder.decode(hasObject))
149 return false;
150 if (hasObject) {
151 std::unique_ptr<IDBResourceIdentifier> object = std::make_unique<IDBResourceIdentifier>();
152 if (!decoder.decode(*object))
153 return false;
154 request.m_requestIdentifier = WTFMove(object);
155 }
156
157 if (!decoder.decode(hasObject))
158 return false;
159 if (hasObject) {
160 std::unique_ptr<IDBResourceIdentifier> object = std::make_unique<IDBResourceIdentifier>();
161 if (!decoder.decode(*object))
162 return false;
163 request.m_transactionIdentifier = WTFMove(object);
164 }
165
166 if (!decoder.decode(hasObject))
167 return false;
168 if (hasObject) {
169 std::unique_ptr<IDBResourceIdentifier> object = std::make_unique<IDBResourceIdentifier>();
170 if (!decoder.decode(*object))
171 return false;
172 request.m_cursorIdentifier = WTFMove(object);
173 }
174
175 return true;
176}
177
178} // namespace WebCore
179
180#endif // ENABLE(INDEXED_DATABASE)
181