| 1 | /* |
| 2 | * Copyright (C) 2018 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 | #include "ProcessIdentifier.h" |
| 29 | #include <wtf/DebugUtilities.h> |
| 30 | #include <wtf/Hasher.h> |
| 31 | #include <wtf/text/StringConcatenateNumbers.h> |
| 32 | |
| 33 | namespace WebCore { |
| 34 | |
| 35 | struct BackForwardItemIdentifier { |
| 36 | ProcessIdentifier processIdentifier; |
| 37 | enum ItemIdentifierType { }; |
| 38 | ObjectIdentifier<ItemIdentifierType> itemIdentifier; |
| 39 | |
| 40 | unsigned hash() const; |
| 41 | |
| 42 | template<class Encoder> void encode(Encoder&) const; |
| 43 | template<class Decoder> static Optional<BackForwardItemIdentifier> decode(Decoder&); |
| 44 | |
| 45 | #if !LOG_DISABLED |
| 46 | const char* logString() const; |
| 47 | #endif |
| 48 | }; |
| 49 | |
| 50 | #if !LOG_DISABLED |
| 51 | |
| 52 | inline const char* BackForwardItemIdentifier::logString() const |
| 53 | { |
| 54 | return debugString(makeString(processIdentifier.toUInt64(), '-', itemIdentifier.toUInt64())); |
| 55 | } |
| 56 | |
| 57 | #endif |
| 58 | |
| 59 | inline bool operator==(const BackForwardItemIdentifier& a, const BackForwardItemIdentifier& b) |
| 60 | { |
| 61 | return a.processIdentifier == b.processIdentifier && a.itemIdentifier == b.itemIdentifier; |
| 62 | } |
| 63 | |
| 64 | template<class Encoder> |
| 65 | void BackForwardItemIdentifier::encode(Encoder& encoder) const |
| 66 | { |
| 67 | encoder << processIdentifier << itemIdentifier; |
| 68 | } |
| 69 | |
| 70 | template<class Decoder> |
| 71 | Optional<BackForwardItemIdentifier> BackForwardItemIdentifier::decode(Decoder& decoder) |
| 72 | { |
| 73 | Optional<ProcessIdentifier> processIdentifier; |
| 74 | decoder >> processIdentifier; |
| 75 | if (!processIdentifier) |
| 76 | return WTF::nullopt; |
| 77 | |
| 78 | Optional<ObjectIdentifier<ItemIdentifierType>> itemIdentifier; |
| 79 | decoder >> itemIdentifier; |
| 80 | if (!itemIdentifier) |
| 81 | return WTF::nullopt; |
| 82 | |
| 83 | return { { WTFMove(*processIdentifier), WTFMove(*itemIdentifier) } }; |
| 84 | } |
| 85 | |
| 86 | inline unsigned BackForwardItemIdentifier::hash() const |
| 87 | { |
| 88 | return computeHash(processIdentifier.toUInt64(), itemIdentifier.toUInt64()); |
| 89 | } |
| 90 | |
| 91 | } // namespace WebCore |
| 92 | |
| 93 | namespace WTF { |
| 94 | |
| 95 | struct BackForwardItemIdentifierHash { |
| 96 | static unsigned hash(const WebCore::BackForwardItemIdentifier& key) { return key.hash(); } |
| 97 | static bool equal(const WebCore::BackForwardItemIdentifier& a, const WebCore::BackForwardItemIdentifier& b) { return a == b; } |
| 98 | static const bool safeToCompareToEmptyOrDeleted = true; |
| 99 | }; |
| 100 | |
| 101 | template<> struct HashTraits<WebCore::BackForwardItemIdentifier> : GenericHashTraits<WebCore::BackForwardItemIdentifier> { |
| 102 | static WebCore::BackForwardItemIdentifier emptyValue() { return { }; } |
| 103 | |
| 104 | static void constructDeletedValue(WebCore::BackForwardItemIdentifier& slot) { slot.processIdentifier = ObjectIdentifier<WebCore::ProcessIdentifierType>(HashTableDeletedValue); } |
| 105 | |
| 106 | static bool isDeletedValue(const WebCore::BackForwardItemIdentifier& slot) { return slot.processIdentifier.toUInt64() == std::numeric_limits<uint64_t>::max(); } |
| 107 | }; |
| 108 | |
| 109 | template<> struct DefaultHash<WebCore::BackForwardItemIdentifier> { |
| 110 | typedef BackForwardItemIdentifierHash Hash; |
| 111 | }; |
| 112 | |
| 113 | } // namespace WTF |
| 114 | |