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