| 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/HashTraits.h> |
| 30 | #include <wtf/ObjectIdentifier.h> |
| 31 | |
| 32 | namespace WebCore { |
| 33 | |
| 34 | enum WindowIdentifierType { }; |
| 35 | using WindowIdentifier = ObjectIdentifier<WindowIdentifierType>; |
| 36 | |
| 37 | // Window identifier that is unique across all WebContent processes. |
| 38 | struct GlobalWindowIdentifier { |
| 39 | ProcessIdentifier processIdentifier; |
| 40 | WindowIdentifier windowIdentifier; |
| 41 | |
| 42 | unsigned hash() const; |
| 43 | |
| 44 | template<class Encoder> void encode(Encoder&) const; |
| 45 | template<class Decoder> static Optional<GlobalWindowIdentifier> decode(Decoder&); |
| 46 | }; |
| 47 | |
| 48 | inline bool operator==(const GlobalWindowIdentifier& a, const GlobalWindowIdentifier& b) |
| 49 | { |
| 50 | return a.processIdentifier == b.processIdentifier && a.windowIdentifier == b.windowIdentifier; |
| 51 | } |
| 52 | |
| 53 | inline unsigned GlobalWindowIdentifier::hash() const |
| 54 | { |
| 55 | uint64_t identifiers[2]; |
| 56 | identifiers[0] = processIdentifier.toUInt64(); |
| 57 | identifiers[1] = windowIdentifier.toUInt64(); |
| 58 | |
| 59 | return StringHasher::hashMemory(identifiers, sizeof(identifiers)); |
| 60 | } |
| 61 | |
| 62 | template<class Encoder> |
| 63 | void GlobalWindowIdentifier::encode(Encoder& encoder) const |
| 64 | { |
| 65 | encoder << processIdentifier << windowIdentifier; |
| 66 | } |
| 67 | |
| 68 | template<class Decoder> |
| 69 | Optional<GlobalWindowIdentifier> GlobalWindowIdentifier::decode(Decoder& decoder) |
| 70 | { |
| 71 | Optional<ProcessIdentifier> processIdentifier; |
| 72 | decoder >> processIdentifier; |
| 73 | if (!processIdentifier) |
| 74 | return WTF::nullopt; |
| 75 | |
| 76 | Optional<WindowIdentifier> windowIdentifier; |
| 77 | decoder >> windowIdentifier; |
| 78 | if (!windowIdentifier) |
| 79 | return WTF::nullopt; |
| 80 | |
| 81 | return { { WTFMove(*processIdentifier), WTFMove(*windowIdentifier) } }; |
| 82 | } |
| 83 | |
| 84 | } // namespace WebCore |
| 85 | |
| 86 | namespace WTF { |
| 87 | |
| 88 | struct GlobalWindowIdentifierHash { |
| 89 | static unsigned hash(const WebCore::GlobalWindowIdentifier& key) { return key.hash(); } |
| 90 | static bool equal(const WebCore::GlobalWindowIdentifier& a, const WebCore::GlobalWindowIdentifier& b) { return a == b; } |
| 91 | static const bool safeToCompareToEmptyOrDeleted = true; |
| 92 | }; |
| 93 | |
| 94 | template<> struct HashTraits<WebCore::GlobalWindowIdentifier> : GenericHashTraits<WebCore::GlobalWindowIdentifier> { |
| 95 | static WebCore::GlobalWindowIdentifier emptyValue() { return { }; } |
| 96 | |
| 97 | static void constructDeletedValue(WebCore::GlobalWindowIdentifier& slot) { slot.windowIdentifier = makeObjectIdentifier<WebCore::WindowIdentifierType>(std::numeric_limits<uint64_t>::max()); } |
| 98 | static bool isDeletedValue(const WebCore::GlobalWindowIdentifier& slot) { return slot.windowIdentifier.toUInt64() == std::numeric_limits<uint64_t>::max(); } |
| 99 | }; |
| 100 | |
| 101 | template<> struct DefaultHash<WebCore::GlobalWindowIdentifier> { |
| 102 | typedef GlobalWindowIdentifierHash Hash; |
| 103 | }; |
| 104 | |
| 105 | } // namespace WTF |
| 106 | |