1/*
2 * Copyright (C) 2014 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 "IDBKey.h"
31#include "IDBKeyData.h"
32#include "IDBKeyPath.h"
33#include "IDBValue.h"
34#include "SharedBuffer.h"
35
36namespace WebCore {
37
38class IDBGetResult {
39 WTF_MAKE_FAST_ALLOCATED;
40public:
41 IDBGetResult()
42 : m_isDefined(false)
43 {
44 }
45
46 IDBGetResult(const IDBKeyData& keyData)
47 : m_keyData(keyData)
48 {
49 }
50
51 IDBGetResult(const IDBKeyData& keyData, const IDBKeyData& primaryKeyData)
52 : m_keyData(keyData)
53 , m_primaryKeyData(primaryKeyData)
54 {
55 }
56
57 IDBGetResult(const IDBKeyData& keyData, const ThreadSafeDataBuffer& buffer, const Optional<IDBKeyPath>& keyPath)
58 : m_value(buffer)
59 , m_keyData(keyData)
60 , m_keyPath(keyPath)
61 {
62 }
63
64 IDBGetResult(const IDBKeyData& keyData, IDBValue&& value, const Optional<IDBKeyPath>& keyPath)
65 : m_value(WTFMove(value))
66 , m_keyData(keyData)
67 , m_keyPath(keyPath)
68 {
69 }
70
71 IDBGetResult(const IDBKeyData& keyData, const IDBKeyData& primaryKeyData, IDBValue&& value, const Optional<IDBKeyPath>& keyPath)
72 : m_value(WTFMove(value))
73 , m_keyData(keyData)
74 , m_primaryKeyData(primaryKeyData)
75 , m_keyPath(keyPath)
76 {
77 }
78
79 enum IsolatedCopyTag { IsolatedCopy };
80 IDBGetResult(const IDBGetResult&, IsolatedCopyTag);
81
82 IDBGetResult isolatedCopy() const;
83
84 void setValue(IDBValue&&);
85
86 const IDBValue& value() const { return m_value; }
87 const IDBKeyData& keyData() const { return m_keyData; }
88 const IDBKeyData& primaryKeyData() const { return m_primaryKeyData; }
89 const Optional<IDBKeyPath>& keyPath() const { return m_keyPath; }
90 bool isDefined() const { return m_isDefined; }
91
92 template<class Encoder> void encode(Encoder&) const;
93 template<class Decoder> static bool decode(Decoder&, IDBGetResult&);
94
95private:
96 void dataFromBuffer(SharedBuffer&);
97
98 static void isolatedCopy(const IDBGetResult& source, IDBGetResult& destination);
99
100 IDBValue m_value;
101 IDBKeyData m_keyData;
102 IDBKeyData m_primaryKeyData;
103 Optional<IDBKeyPath> m_keyPath;
104 bool m_isDefined { true };
105};
106
107template<class Encoder>
108void IDBGetResult::encode(Encoder& encoder) const
109{
110 encoder << m_keyData << m_primaryKeyData << m_keyPath << m_isDefined << m_value;
111}
112
113template<class Decoder>
114bool IDBGetResult::decode(Decoder& decoder, IDBGetResult& result)
115{
116 Optional<IDBKeyData> keyData;
117 decoder >> keyData;
118 if (!keyData)
119 return false;
120 result.m_keyData = WTFMove(*keyData);
121
122 Optional<IDBKeyData> primaryKeyData;
123 decoder >> primaryKeyData;
124 if (!primaryKeyData)
125 return false;
126 result.m_primaryKeyData = WTFMove(*primaryKeyData);
127
128 if (!decoder.decode(result.m_keyPath))
129 return false;
130
131 if (!decoder.decode(result.m_isDefined))
132 return false;
133
134 Optional<IDBValue> value;
135 decoder >> value;
136 if (!value)
137 return false;
138 result.m_value = WTFMove(*value);
139
140 return true;
141}
142
143} // namespace WebCore
144
145#endif // ENABLE(INDEXED_DATABASE)
146