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#include "config.h"
27#include "UniqueIDBDatabaseIdentifier.h"
28
29#if ENABLE(INDEXED_DATABASE) && ENABLE(DATABASE_PROCESS)
30
31#include <wtf/text/StringBuilder.h>
32
33namespace WebKit {
34
35UniqueIDBDatabaseIdentifier::UniqueIDBDatabaseIdentifier()
36{
37}
38
39UniqueIDBDatabaseIdentifier::UniqueIDBDatabaseIdentifier(const String& databaseName, const SecurityOriginData& openingOrigin, const SecurityOriginData& mainFrameOrigin)
40    : m_databaseName(databaseName)
41    , m_openingOrigin(openingOrigin)
42    , m_mainFrameOrigin(mainFrameOrigin)
43{
44    // While it is valid to have an empty database name, it is not valid to have a null one.
45    ASSERT(!m_databaseName.isNull());
46}
47
48UniqueIDBDatabaseIdentifier::UniqueIDBDatabaseIdentifier(WTF::HashTableDeletedValueType)
49    : m_databaseName(WTF::HashTableDeletedValue)
50{
51}
52
53bool UniqueIDBDatabaseIdentifier::isHashTableDeletedValue() const
54{
55    return m_databaseName.isHashTableDeletedValue();
56}
57
58unsigned UniqueIDBDatabaseIdentifier::hash() const
59{
60    unsigned hashCodes[7] = {
61        m_databaseName.impl() ? m_databaseName.impl()->hash() : 0,
62        m_openingOrigin.protocol.impl() ? m_openingOrigin.protocol.impl()->hash() : 0,
63        m_openingOrigin.host.impl() ? m_openingOrigin.host.impl()->hash() : 0,
64        static_cast<unsigned>(m_openingOrigin.port),
65        m_mainFrameOrigin.protocol.impl() ? m_mainFrameOrigin.protocol.impl()->hash() : 0,
66        m_mainFrameOrigin.host.impl() ? m_mainFrameOrigin.host.impl()->hash() : 0,
67        static_cast<unsigned>(m_mainFrameOrigin.port)
68    };
69    return StringHasher::hashMemory<sizeof(hashCodes)>(hashCodes);
70}
71
72bool UniqueIDBDatabaseIdentifier::isNull() const
73{
74    // Only a default constructed UniqueIDBDatabaseIdentifier can have a null database name.
75    return m_databaseName.isNull();
76}
77
78UniqueIDBDatabaseIdentifier UniqueIDBDatabaseIdentifier::isolatedCopy() const
79{
80    UniqueIDBDatabaseIdentifier result;
81
82    result.m_databaseName = m_databaseName.isolatedCopy();
83    result.m_openingOrigin = m_openingOrigin.isolatedCopy();
84    result.m_mainFrameOrigin = m_mainFrameOrigin.isolatedCopy();
85
86    return result;
87}
88
89bool operator==(const UniqueIDBDatabaseIdentifier& a, const UniqueIDBDatabaseIdentifier& b)
90{
91    if (&a == &b)
92        return true;
93
94    return a.databaseName() == b.databaseName()
95        && a.openingOrigin() == b.openingOrigin()
96        && a.mainFrameOrigin() == b.mainFrameOrigin();
97}
98
99} // namespace WebKit
100
101#endif // ENABLE(INDEXED_DATABASE) && ENABLE(DATABASE_PROCESS)
102