1/*
2 * Copyright (C) 2013 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#ifndef IDBIdentifier_h
27#define IDBIdentifier_h
28
29#if ENABLE(INDEXED_DATABASE) && ENABLE(DATABASE_PROCESS)
30
31#include <wtf/HashTraits.h>
32#include <wtf/StringHasher.h>
33
34namespace WebKit {
35
36class DatabaseProcessIDBConnection;
37
38class IDBIdentifier {
39public:
40    IDBIdentifier()
41        : m_connection(nullptr)
42        , m_identifier(0)
43    {
44    }
45
46    IDBIdentifier(DatabaseProcessIDBConnection& connection, int64_t identifier)
47        : m_connection(&connection)
48        , m_identifier(identifier)
49    {
50    }
51
52    IDBIdentifier isolatedCopy() const
53    {
54        return *this;
55    }
56
57    bool isEmpty() const
58    {
59        return !m_connection && !m_identifier;
60    }
61
62    unsigned hash() const
63    {
64        uint64_t hashCodes[2] = { reinterpret_cast<uint64_t>(m_connection), static_cast<uint64_t>(m_identifier) };
65        return StringHasher::hashMemory<sizeof(hashCodes)>(hashCodes);
66    }
67
68    bool operator==(const IDBIdentifier& other) const
69    {
70        return m_connection == other.m_connection && m_identifier == other.m_identifier;
71    }
72
73    IDBIdentifier(WTF::HashTableDeletedValueType)
74        : m_connection(nullptr)
75        , m_identifier(-1)
76    {
77    }
78
79    bool isHashTableDeletedValue() const
80    {
81        return !m_connection && m_identifier == -1;
82    }
83
84    DatabaseProcessIDBConnection& connection() const
85    {
86        ASSERT(m_connection);
87        return *m_connection;
88    }
89
90    int64_t id() const { return m_identifier; }
91
92private:
93    // If any members are added that cannot be safely copied across threads, isolatedCopy() must be updated.
94    DatabaseProcessIDBConnection* m_connection;
95    int64_t m_identifier;
96};
97
98struct IDBIdentifierHash {
99    static unsigned hash(const IDBIdentifier& a) { return a.hash(); }
100    static bool equal(const IDBIdentifier& a, const IDBIdentifier& b) { return a == b; }
101    static const bool safeToCompareToEmptyOrDeleted = false;
102};
103
104struct IDBIdentifierHashTraits : WTF::SimpleClassHashTraits<IDBIdentifier> {
105    static const bool hasIsEmptyValueFunction = true;
106    static bool isEmptyValue(const IDBIdentifier& info) { return info.isEmpty(); }
107};
108
109} // namespace WebKit
110
111namespace WTF {
112
113template<> struct HashTraits<WebKit::IDBIdentifier> : WebKit::IDBIdentifierHashTraits { };
114template<> struct DefaultHash<WebKit::IDBIdentifier> {
115    typedef WebKit::IDBIdentifierHash Hash;
116};
117
118} // namespace WTF
119
120#endif // ENABLE(INDEXED_DATABASE) && ENABLE(DATABASE_PROCESS)
121#endif // IDBIdentifier_h
122