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 "IDBObjectStoreInfo.h"
31#include <wtf/HashMap.h>
32
33namespace WebCore {
34
35class IDBDatabaseInfo {
36 WTF_MAKE_FAST_ALLOCATED;
37public:
38 IDBDatabaseInfo(const String& name, uint64_t version);
39
40 enum IsolatedCopyTag { IsolatedCopy };
41 IDBDatabaseInfo(const IDBDatabaseInfo&, IsolatedCopyTag);
42
43 IDBDatabaseInfo isolatedCopy() const;
44
45 const String& name() const { return m_name; }
46
47 void setVersion(uint64_t version) { m_version = version; }
48 uint64_t version() const { return m_version; }
49
50 bool hasObjectStore(const String& name) const;
51 IDBObjectStoreInfo createNewObjectStore(const String& name, Optional<IDBKeyPath>&&, bool autoIncrement);
52 void addExistingObjectStore(const IDBObjectStoreInfo&);
53 IDBObjectStoreInfo* infoForExistingObjectStore(uint64_t objectStoreIdentifier);
54 IDBObjectStoreInfo* infoForExistingObjectStore(const String& objectStoreName);
55 const IDBObjectStoreInfo* infoForExistingObjectStore(uint64_t objectStoreIdentifier) const;
56 const IDBObjectStoreInfo* infoForExistingObjectStore(const String& objectStoreName) const;
57
58 void renameObjectStore(uint64_t objectStoreIdentifier, const String& newName);
59
60 Vector<String> objectStoreNames() const;
61
62 void deleteObjectStore(const String& objectStoreName);
63 void deleteObjectStore(uint64_t objectStoreIdentifier);
64
65 WEBCORE_EXPORT IDBDatabaseInfo();
66
67 template<class Encoder> void encode(Encoder&) const;
68 template<class Decoder> static bool decode(Decoder&, IDBDatabaseInfo&);
69
70#if !LOG_DISABLED
71 String loggingString() const;
72#endif
73
74private:
75 IDBObjectStoreInfo* getInfoForExistingObjectStore(const String& objectStoreName);
76 IDBObjectStoreInfo* getInfoForExistingObjectStore(uint64_t objectStoreIdentifier);
77
78 String m_name;
79 uint64_t m_version { 0 };
80 uint64_t m_maxObjectStoreID { 0 };
81
82 HashMap<uint64_t, IDBObjectStoreInfo> m_objectStoreMap;
83
84};
85
86template<class Encoder>
87void IDBDatabaseInfo::encode(Encoder& encoder) const
88{
89 encoder << m_name << m_version << m_maxObjectStoreID << m_objectStoreMap;
90}
91
92template<class Decoder>
93bool IDBDatabaseInfo::decode(Decoder& decoder, IDBDatabaseInfo& info)
94{
95 if (!decoder.decode(info.m_name))
96 return false;
97
98 if (!decoder.decode(info.m_version))
99 return false;
100
101 if (!decoder.decode(info.m_maxObjectStoreID))
102 return false;
103
104 if (!decoder.decode(info.m_objectStoreMap))
105 return false;
106
107 return true;
108}
109
110} // namespace WebCore
111
112#endif // ENABLE(INDEXED_DATABASE)
113