| 1 | /* |
| 2 | * Copyright (C) 2014-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 | #ifndef NetworkCacheKey_h |
| 27 | #define NetworkCacheKey_h |
| 28 | |
| 29 | #include "NetworkCacheData.h" |
| 30 | #include <wtf/SHA1.h> |
| 31 | #include <wtf/persistence/PersistentCoder.h> |
| 32 | #include <wtf/text/WTFString.h> |
| 33 | |
| 34 | namespace WebKit { |
| 35 | namespace NetworkCache { |
| 36 | |
| 37 | struct DataKey { |
| 38 | String partition; |
| 39 | String type; |
| 40 | SHA1::Digest identifier; |
| 41 | |
| 42 | template <class Encoder> void encode(Encoder& encoder) const |
| 43 | { |
| 44 | encoder << partition << type << identifier; |
| 45 | } |
| 46 | |
| 47 | template <class Decoder> static bool decode(Decoder& decoder, DataKey& dataKey) |
| 48 | { |
| 49 | return decoder.decode(dataKey.partition) && decoder.decode(dataKey.type) && decoder.decode(dataKey.identifier); |
| 50 | } |
| 51 | }; |
| 52 | |
| 53 | class Key { |
| 54 | public: |
| 55 | typedef SHA1::Digest HashType; |
| 56 | |
| 57 | Key() { } |
| 58 | Key(const Key&); |
| 59 | Key(Key&&) = default; |
| 60 | Key(const String& partition, const String& type, const String& range, const String& identifier, const Salt&); |
| 61 | Key(const DataKey&, const Salt&); |
| 62 | |
| 63 | Key& operator=(const Key&); |
| 64 | Key& operator=(Key&&) = default; |
| 65 | |
| 66 | Key(WTF::HashTableDeletedValueType); |
| 67 | bool isHashTableDeletedValue() const { return m_identifier.isHashTableDeletedValue(); } |
| 68 | |
| 69 | bool isNull() const { return m_identifier.isNull(); } |
| 70 | |
| 71 | const String& partition() const { return m_partition; } |
| 72 | const String& identifier() const { return m_identifier; } |
| 73 | const String& type() const { return m_type; } |
| 74 | const String& range() const { return m_range; } |
| 75 | |
| 76 | const HashType& hash() const { return m_hash; } |
| 77 | const HashType& partitionHash() const { return m_partitionHash; } |
| 78 | |
| 79 | static bool stringToHash(const String&, HashType&); |
| 80 | |
| 81 | static size_t hashStringLength() { return 2 * sizeof(m_hash); } |
| 82 | String hashAsString() const { return hashAsString(m_hash); } |
| 83 | String partitionHashAsString() const { return hashAsString(m_partitionHash); } |
| 84 | |
| 85 | void encode(WTF::Persistence::Encoder&) const; |
| 86 | static bool decode(WTF::Persistence::Decoder&, Key&); |
| 87 | |
| 88 | bool operator==(const Key&) const; |
| 89 | bool operator!=(const Key& other) const { return !(*this == other); } |
| 90 | |
| 91 | private: |
| 92 | static String hashAsString(const HashType&); |
| 93 | HashType computeHash(const Salt&) const; |
| 94 | HashType computePartitionHash(const Salt&) const; |
| 95 | |
| 96 | String m_partition; |
| 97 | String m_type; |
| 98 | String m_identifier; |
| 99 | String m_range; |
| 100 | HashType m_hash; |
| 101 | HashType m_partitionHash; |
| 102 | }; |
| 103 | |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | namespace WTF { |
| 108 | |
| 109 | struct NetworkCacheKeyHash { |
| 110 | static unsigned hash(const WebKit::NetworkCache::Key& key) |
| 111 | { |
| 112 | static_assert(SHA1::hashSize >= sizeof(unsigned), "Hash size must be greater than sizeof(unsigned)" ); |
| 113 | return *reinterpret_cast<const unsigned*>(key.hash().data()); |
| 114 | } |
| 115 | |
| 116 | static bool equal(const WebKit::NetworkCache::Key& a, const WebKit::NetworkCache::Key& b) |
| 117 | { |
| 118 | return a == b; |
| 119 | } |
| 120 | |
| 121 | static const bool safeToCompareToEmptyOrDeleted = false; |
| 122 | }; |
| 123 | |
| 124 | template<typename T> struct DefaultHash; |
| 125 | template<> struct DefaultHash<WebKit::NetworkCache::Key> { |
| 126 | typedef NetworkCacheKeyHash Hash; |
| 127 | }; |
| 128 | |
| 129 | template<> struct HashTraits<WebKit::NetworkCache::Key> : SimpleClassHashTraits<WebKit::NetworkCache::Key> { |
| 130 | static const bool emptyValueIsZero = false; |
| 131 | |
| 132 | static const bool hasIsEmptyValueFunction = true; |
| 133 | static bool isEmptyValue(const WebKit::NetworkCache::Key& key) { return key.isNull(); } |
| 134 | }; |
| 135 | |
| 136 | } // namespace WTF |
| 137 | |
| 138 | #endif |
| 139 | |