[WTF] Add makeUnique<T>, which ensures T is fast-allocated, makeUnique / makeUniqueWi...
[WebKit-https.git] / Source / WebCore / Modules / indexeddb / shared / IDBResultData.cpp
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 #include "config.h"
27 #include "IDBResultData.h"
28
29 #if ENABLE(INDEXED_DATABASE)
30
31 #include "UniqueIDBDatabase.h"
32 #include "UniqueIDBDatabaseConnection.h"
33 #include "UniqueIDBDatabaseTransaction.h"
34
35 namespace WebCore {
36
37 IDBResultData::IDBResultData()
38 {
39 }
40
41 IDBResultData::IDBResultData(const IDBResourceIdentifier& requestIdentifier)
42     : m_requestIdentifier(requestIdentifier)
43 {
44 }
45
46 IDBResultData::IDBResultData(IDBResultType type, const IDBResourceIdentifier& requestIdentifier)
47     : m_type(type)
48     , m_requestIdentifier(requestIdentifier)
49 {
50 }
51
52 IDBResultData::IDBResultData(const IDBResultData& other)
53     : m_type(other.m_type)
54     , m_requestIdentifier(other.m_requestIdentifier)
55     , m_error(other.m_error)
56     , m_databaseConnectionIdentifier(other.m_databaseConnectionIdentifier)
57     , m_resultInteger(other.m_resultInteger)
58 {
59     if (other.m_databaseInfo)
60         m_databaseInfo = makeUnique<IDBDatabaseInfo>(*other.m_databaseInfo);
61     if (other.m_transactionInfo)
62         m_transactionInfo = makeUnique<IDBTransactionInfo>(*other.m_transactionInfo);
63     if (other.m_resultKey)
64         m_resultKey = makeUnique<IDBKeyData>(*other.m_resultKey);
65     if (other.m_getResult)
66         m_getResult = makeUnique<IDBGetResult>(*other.m_getResult);
67     if (other.m_getAllResult)
68         m_getAllResult = makeUnique<IDBGetAllResult>(*other.m_getAllResult);
69 }
70
71 IDBResultData::IDBResultData(const IDBResultData& that, IsolatedCopyTag)
72 {
73     isolatedCopy(that, *this);
74 }
75
76 IDBResultData IDBResultData::isolatedCopy() const
77 {
78     return { *this, IsolatedCopy };
79 }
80
81 void IDBResultData::isolatedCopy(const IDBResultData& source, IDBResultData& destination)
82 {
83     destination.m_type = source.m_type;
84     destination.m_requestIdentifier = source.m_requestIdentifier.isolatedCopy();
85     destination.m_error = source.m_error.isolatedCopy();
86     destination.m_databaseConnectionIdentifier = source.m_databaseConnectionIdentifier;
87     destination.m_resultInteger = source.m_resultInteger;
88
89     if (source.m_databaseInfo)
90         destination.m_databaseInfo = makeUnique<IDBDatabaseInfo>(*source.m_databaseInfo, IDBDatabaseInfo::IsolatedCopy);
91     if (source.m_transactionInfo)
92         destination.m_transactionInfo = makeUnique<IDBTransactionInfo>(*source.m_transactionInfo, IDBTransactionInfo::IsolatedCopy);
93     if (source.m_resultKey)
94         destination.m_resultKey = makeUnique<IDBKeyData>(*source.m_resultKey, IDBKeyData::IsolatedCopy);
95     if (source.m_getResult)
96         destination.m_getResult = makeUnique<IDBGetResult>(*source.m_getResult, IDBGetResult::IsolatedCopy);
97     if (source.m_getAllResult)
98         destination.m_getAllResult = makeUnique<IDBGetAllResult>(*source.m_getAllResult, IDBGetAllResult::IsolatedCopy);
99 }
100
101 IDBResultData IDBResultData::error(const IDBResourceIdentifier& requestIdentifier, const IDBError& error)
102 {
103     IDBResultData result { requestIdentifier };
104     result.m_type = IDBResultType::Error;
105     result.m_error = error;
106     return result;
107 }
108
109 IDBResultData IDBResultData::openDatabaseSuccess(const IDBResourceIdentifier& requestIdentifier, IDBServer::UniqueIDBDatabaseConnection& connection)
110 {
111     IDBResultData result { requestIdentifier };
112     result.m_type = IDBResultType::OpenDatabaseSuccess;
113     result.m_databaseConnectionIdentifier = connection.identifier();
114     result.m_databaseInfo = makeUnique<IDBDatabaseInfo>(connection.database()->info());
115     return result;
116 }
117
118
119 IDBResultData IDBResultData::openDatabaseUpgradeNeeded(const IDBResourceIdentifier& requestIdentifier, IDBServer::UniqueIDBDatabaseTransaction& transaction)
120 {
121     IDBResultData result { requestIdentifier };
122     result.m_type = IDBResultType::OpenDatabaseUpgradeNeeded;
123     result.m_databaseConnectionIdentifier = transaction.databaseConnection().identifier();
124     result.m_databaseInfo = makeUnique<IDBDatabaseInfo>(transaction.databaseConnection().database()->info());
125     result.m_transactionInfo = makeUnique<IDBTransactionInfo>(transaction.info());
126     return result;
127 }
128
129 IDBResultData IDBResultData::deleteDatabaseSuccess(const IDBResourceIdentifier& requestIdentifier, const IDBDatabaseInfo& info)
130 {
131     IDBResultData result {IDBResultType::DeleteDatabaseSuccess, requestIdentifier };
132     result.m_databaseInfo = makeUnique<IDBDatabaseInfo>(info);
133     return result;
134 }
135
136 IDBResultData IDBResultData::createObjectStoreSuccess(const IDBResourceIdentifier& requestIdentifier)
137 {
138     return { IDBResultType::CreateObjectStoreSuccess, requestIdentifier };
139 }
140
141 IDBResultData IDBResultData::deleteObjectStoreSuccess(const IDBResourceIdentifier& requestIdentifier)
142 {
143     return { IDBResultType::DeleteObjectStoreSuccess, requestIdentifier };
144 }
145
146 IDBResultData IDBResultData::renameObjectStoreSuccess(const IDBResourceIdentifier& requestIdentifier)
147 {
148     return { IDBResultType::RenameObjectStoreSuccess, requestIdentifier };
149 }
150
151 IDBResultData IDBResultData::clearObjectStoreSuccess(const IDBResourceIdentifier& requestIdentifier)
152 {
153     return { IDBResultType::ClearObjectStoreSuccess, requestIdentifier };
154 }
155
156 IDBResultData IDBResultData::createIndexSuccess(const IDBResourceIdentifier& requestIdentifier)
157 {
158     return { IDBResultType::CreateIndexSuccess, requestIdentifier };
159 }
160
161 IDBResultData IDBResultData::deleteIndexSuccess(const IDBResourceIdentifier& requestIdentifier)
162 {
163     return { IDBResultType::DeleteIndexSuccess, requestIdentifier };
164 }
165
166 IDBResultData IDBResultData::renameIndexSuccess(const IDBResourceIdentifier& requestIdentifier)
167 {
168     return { IDBResultType::RenameIndexSuccess, requestIdentifier };
169 }
170
171 IDBResultData IDBResultData::putOrAddSuccess(const IDBResourceIdentifier& requestIdentifier, const IDBKeyData& resultKey)
172 {
173     IDBResultData result { IDBResultType::PutOrAddSuccess, requestIdentifier };
174     result.m_resultKey = makeUnique<IDBKeyData>(resultKey);
175     return result;
176 }
177
178 IDBResultData IDBResultData::getRecordSuccess(const IDBResourceIdentifier& requestIdentifier, const IDBGetResult& getResult)
179 {
180     IDBResultData result { IDBResultType::GetRecordSuccess, requestIdentifier };
181     result.m_getResult = makeUnique<IDBGetResult>(getResult);
182     return result;
183 }
184
185 IDBResultData IDBResultData::getAllRecordsSuccess(const IDBResourceIdentifier& requestIdentifier, const IDBGetAllResult& getAllResult)
186 {
187     IDBResultData result { IDBResultType::GetAllRecordsSuccess, requestIdentifier };
188     result.m_getAllResult = makeUnique<IDBGetAllResult>(getAllResult);
189     return result;
190 }
191
192 IDBResultData IDBResultData::getCountSuccess(const IDBResourceIdentifier& requestIdentifier, uint64_t count)
193 {
194     IDBResultData result { IDBResultType::GetRecordSuccess, requestIdentifier };
195     result.m_resultInteger = count;
196     return result;
197 }
198
199 IDBResultData IDBResultData::deleteRecordSuccess(const IDBResourceIdentifier& requestIdentifier)
200 {
201     return { IDBResultType::DeleteRecordSuccess, requestIdentifier };
202 }
203
204 IDBResultData IDBResultData::openCursorSuccess(const IDBResourceIdentifier& requestIdentifier, const IDBGetResult& getResult)
205 {
206     IDBResultData result { IDBResultType::OpenCursorSuccess, requestIdentifier };
207     result.m_getResult = makeUnique<IDBGetResult>(getResult);
208     return result;
209 }
210
211 IDBResultData IDBResultData::iterateCursorSuccess(const IDBResourceIdentifier& requestIdentifier, const IDBGetResult& getResult)
212 {
213     IDBResultData result { IDBResultType::IterateCursorSuccess, requestIdentifier };
214     result.m_getResult = makeUnique<IDBGetResult>(getResult);
215     return result;
216 }
217
218 const IDBDatabaseInfo& IDBResultData::databaseInfo() const
219 {
220     RELEASE_ASSERT(m_databaseInfo);
221     return *m_databaseInfo;
222 }
223
224 const IDBTransactionInfo& IDBResultData::transactionInfo() const
225 {
226     RELEASE_ASSERT(m_transactionInfo);
227     return *m_transactionInfo;
228 }
229
230 const IDBGetResult& IDBResultData::getResult() const
231 {
232     RELEASE_ASSERT(m_getResult);
233     return *m_getResult;
234 }
235
236 IDBGetResult& IDBResultData::getResultRef()
237 {
238     RELEASE_ASSERT(m_getResult);
239     return *m_getResult;
240 }
241
242 const IDBGetAllResult& IDBResultData::getAllResult() const
243 {
244     RELEASE_ASSERT(m_getAllResult);
245     return *m_getAllResult;
246 }
247
248 } // namespace WebCore
249
250 #endif // ENABLE(INDEXED_DATABASE)