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 "IDBDatabaseInfo.h" |
31 | #include "IDBResourceIdentifier.h" |
32 | #include "IDBTransactionMode.h" |
33 | #include "IndexedDB.h" |
34 | #include <wtf/Vector.h> |
35 | |
36 | namespace WebCore { |
37 | |
38 | namespace IDBClient { |
39 | class IDBConnectionProxy; |
40 | } |
41 | |
42 | namespace IDBServer { |
43 | class IDBConnectionToClient; |
44 | } |
45 | |
46 | class IDBTransactionInfo { |
47 | WTF_MAKE_FAST_ALLOCATED; |
48 | public: |
49 | static IDBTransactionInfo clientTransaction(const IDBClient::IDBConnectionProxy&, const Vector<String>& objectStores, IDBTransactionMode); |
50 | static IDBTransactionInfo versionChange(const IDBServer::IDBConnectionToClient&, const IDBDatabaseInfo& originalDatabaseInfo, uint64_t newVersion); |
51 | |
52 | IDBTransactionInfo(const IDBTransactionInfo&); |
53 | IDBTransactionInfo(IDBTransactionInfo&&) = default; |
54 | IDBTransactionInfo& operator=(IDBTransactionInfo&&) = default; |
55 | |
56 | enum IsolatedCopyTag { IsolatedCopy }; |
57 | IDBTransactionInfo(const IDBTransactionInfo&, IsolatedCopyTag); |
58 | |
59 | IDBTransactionInfo isolatedCopy() const; |
60 | |
61 | const IDBResourceIdentifier& identifier() const { return m_identifier; } |
62 | |
63 | IDBTransactionMode mode() const { return m_mode; } |
64 | uint64_t newVersion() const { return m_newVersion; } |
65 | |
66 | const Vector<String>& objectStores() const { return m_objectStores; } |
67 | |
68 | IDBDatabaseInfo* originalDatabaseInfo() const { return m_originalDatabaseInfo.get(); } |
69 | |
70 | WEBCORE_EXPORT IDBTransactionInfo(); |
71 | template<class Encoder> void encode(Encoder&) const; |
72 | template<class Decoder> static bool decode(Decoder&, IDBTransactionInfo&); |
73 | |
74 | #if !LOG_DISABLED |
75 | String loggingString() const; |
76 | #endif |
77 | |
78 | private: |
79 | IDBTransactionInfo(const IDBResourceIdentifier&); |
80 | |
81 | static void isolatedCopy(const IDBTransactionInfo& source, IDBTransactionInfo& destination); |
82 | |
83 | IDBResourceIdentifier m_identifier; |
84 | |
85 | IDBTransactionMode m_mode { IDBTransactionMode::Readonly }; |
86 | uint64_t m_newVersion { 0 }; |
87 | Vector<String> m_objectStores; |
88 | std::unique_ptr<IDBDatabaseInfo> m_originalDatabaseInfo; |
89 | }; |
90 | |
91 | template<class Encoder> |
92 | void IDBTransactionInfo::encode(Encoder& encoder) const |
93 | { |
94 | encoder << m_identifier << m_newVersion << m_objectStores; |
95 | encoder.encodeEnum(m_mode); |
96 | |
97 | encoder << !!m_originalDatabaseInfo; |
98 | if (m_originalDatabaseInfo) |
99 | encoder << *m_originalDatabaseInfo; |
100 | } |
101 | |
102 | template<class Decoder> |
103 | bool IDBTransactionInfo::decode(Decoder& decoder, IDBTransactionInfo& info) |
104 | { |
105 | if (!decoder.decode(info.m_identifier)) |
106 | return false; |
107 | |
108 | if (!decoder.decode(info.m_newVersion)) |
109 | return false; |
110 | |
111 | if (!decoder.decode(info.m_objectStores)) |
112 | return false; |
113 | |
114 | if (!decoder.decodeEnum(info.m_mode)) |
115 | return false; |
116 | |
117 | bool hasObject; |
118 | if (!decoder.decode(hasObject)) |
119 | return false; |
120 | |
121 | if (hasObject) { |
122 | std::unique_ptr<IDBDatabaseInfo> object = std::make_unique<IDBDatabaseInfo>(); |
123 | if (!decoder.decode(*object)) |
124 | return false; |
125 | info.m_originalDatabaseInfo = WTFMove(object); |
126 | } |
127 | |
128 | return true; |
129 | } |
130 | |
131 | } // namespace WebCore |
132 | |
133 | #endif // ENABLE(INDEXED_DATABASE) |
134 | |