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 "IDBIndexInfo.h"
31#include "IDBKeyPath.h"
32#include <wtf/HashMap.h>
33#include <wtf/text/WTFString.h>
34
35namespace WebCore {
36
37class IDBObjectStoreInfo {
38public:
39 WEBCORE_EXPORT IDBObjectStoreInfo();
40 IDBObjectStoreInfo(uint64_t identifier, const String& name, Optional<IDBKeyPath>&&, bool autoIncrement);
41
42 uint64_t identifier() const { return m_identifier; }
43 const String& name() const { return m_name; }
44 const Optional<IDBKeyPath>& keyPath() const { return m_keyPath; }
45 bool autoIncrement() const { return m_autoIncrement; }
46 uint64_t maxIndexID() const { return m_maxIndexID; }
47
48 void rename(const String& newName) { m_name = newName; }
49
50 IDBObjectStoreInfo isolatedCopy() const;
51
52 IDBIndexInfo createNewIndex(const String& name, IDBKeyPath&&, bool unique, bool multiEntry);
53 void addExistingIndex(const IDBIndexInfo&);
54 bool hasIndex(const String& name) const;
55 bool hasIndex(uint64_t indexIdentifier) const;
56 IDBIndexInfo* infoForExistingIndex(const String& name);
57 IDBIndexInfo* infoForExistingIndex(uint64_t identifier);
58
59 Vector<String> indexNames() const;
60 const HashMap<uint64_t, IDBIndexInfo>& indexMap() const { return m_indexMap; }
61
62 void deleteIndex(const String& indexName);
63 void deleteIndex(uint64_t indexIdentifier);
64
65 template<class Encoder> void encode(Encoder&) const;
66 template<class Decoder> static bool decode(Decoder&, IDBObjectStoreInfo&);
67
68#if !LOG_DISABLED
69 String loggingString(int indent = 0) const;
70 String condensedLoggingString() const;
71#endif
72
73private:
74 uint64_t m_identifier { 0 };
75 String m_name;
76 Optional<IDBKeyPath> m_keyPath;
77 bool m_autoIncrement { false };
78 uint64_t m_maxIndexID { 0 };
79
80 HashMap<uint64_t, IDBIndexInfo> m_indexMap;
81};
82
83template<class Encoder>
84void IDBObjectStoreInfo::encode(Encoder& encoder) const
85{
86 encoder << m_identifier << m_name << m_keyPath << m_autoIncrement << m_maxIndexID << m_indexMap;
87}
88
89template<class Decoder>
90bool IDBObjectStoreInfo::decode(Decoder& decoder, IDBObjectStoreInfo& info)
91{
92 if (!decoder.decode(info.m_identifier))
93 return false;
94
95 if (!decoder.decode(info.m_name))
96 return false;
97
98 if (!decoder.decode(info.m_keyPath))
99 return false;
100
101 if (!decoder.decode(info.m_autoIncrement))
102 return false;
103
104 if (!decoder.decode(info.m_maxIndexID))
105 return false;
106
107 if (!decoder.decode(info.m_indexMap))
108 return false;
109
110 return true;
111}
112
113} // namespace WebCore
114
115#endif // ENABLE(INDEXED_DATABASE)
116