| 1 | /* |
| 2 | * Copyright (C) 2014-2017 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 <wtf/HashFunctions.h> |
| 29 | #include <wtf/HashTraits.h> |
| 30 | |
| 31 | namespace PAL { |
| 32 | |
| 33 | class SessionID { |
| 34 | public: |
| 35 | SessionID() |
| 36 | : SessionID(emptySessionID()) |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | enum SessionConstants : uint64_t { |
| 41 | EphemeralSessionMask = 0x8000000000000000, |
| 42 | DefaultSessionID = 1, |
| 43 | LegacyPrivateSessionID = DefaultSessionID | EphemeralSessionMask, |
| 44 | HashTableEmptyValueID = 0, |
| 45 | HashTableDeletedValueID = std::numeric_limits<uint64_t>::max(), |
| 46 | }; |
| 47 | |
| 48 | static SessionID emptySessionID() { return SessionID(HashTableEmptyValueID); } |
| 49 | static SessionID hashTableDeletedValue() { return SessionID(HashTableDeletedValueID); } |
| 50 | static SessionID defaultSessionID() { return SessionID(DefaultSessionID); } |
| 51 | static SessionID legacyPrivateSessionID() { return SessionID(LegacyPrivateSessionID); } |
| 52 | |
| 53 | bool isValid() const { return m_sessionID != HashTableEmptyValueID && m_sessionID != HashTableDeletedValueID; } |
| 54 | bool isEphemeral() const { return m_sessionID & EphemeralSessionMask && m_sessionID != HashTableDeletedValueID; } |
| 55 | |
| 56 | PAL_EXPORT static SessionID generateEphemeralSessionID(); |
| 57 | PAL_EXPORT static SessionID generatePersistentSessionID(); |
| 58 | PAL_EXPORT static void enableGenerationProtection(); |
| 59 | |
| 60 | uint64_t sessionID() const { return m_sessionID; } |
| 61 | bool operator==(SessionID sessionID) const { return m_sessionID == sessionID.m_sessionID; } |
| 62 | bool operator!=(SessionID sessionID) const { return m_sessionID != sessionID.m_sessionID; } |
| 63 | bool isAlwaysOnLoggingAllowed() const { return !isEphemeral(); } |
| 64 | |
| 65 | template<class Encoder> void encode(Encoder&) const; |
| 66 | template<class Decoder> static bool decode(Decoder&, SessionID&); |
| 67 | template<class Decoder> static Optional<SessionID> decode(Decoder&); |
| 68 | |
| 69 | SessionID isolatedCopy() const; |
| 70 | |
| 71 | private: |
| 72 | explicit SessionID(uint64_t sessionID) |
| 73 | : m_sessionID(sessionID) |
| 74 | { |
| 75 | } |
| 76 | |
| 77 | uint64_t m_sessionID; |
| 78 | }; |
| 79 | |
| 80 | template<class Encoder> |
| 81 | void SessionID::encode(Encoder& encoder) const |
| 82 | { |
| 83 | // FIXME: Eliminate places that encode invalid SessionIDs, then ASSERT here that the sessionID is valid. |
| 84 | encoder << m_sessionID; |
| 85 | } |
| 86 | |
| 87 | template<class Decoder> |
| 88 | bool SessionID::decode(Decoder& decoder, SessionID& sessionID) |
| 89 | { |
| 90 | Optional<SessionID> decodedSessionID; |
| 91 | decoder >> decodedSessionID; |
| 92 | if (!decodedSessionID) |
| 93 | return false; |
| 94 | |
| 95 | sessionID = decodedSessionID.value(); |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | template<class Decoder> |
| 100 | Optional<SessionID> SessionID::decode(Decoder& decoder) |
| 101 | { |
| 102 | Optional<uint64_t> sessionID; |
| 103 | decoder >> sessionID; |
| 104 | if (!sessionID) |
| 105 | return WTF::nullopt; |
| 106 | |
| 107 | // FIXME: Eliminate places that encode invalid SessionIDs, then fail to decode an invalid sessionID. |
| 108 | return SessionID { *sessionID }; |
| 109 | } |
| 110 | |
| 111 | } // namespace PAL |
| 112 | |
| 113 | namespace WTF { |
| 114 | |
| 115 | struct SessionIDHash { |
| 116 | static unsigned hash(const PAL::SessionID& p) { return intHash(p.sessionID()); } |
| 117 | static bool equal(const PAL::SessionID& a, const PAL::SessionID& b) { return a == b; } |
| 118 | static const bool safeToCompareToEmptyOrDeleted = true; |
| 119 | }; |
| 120 | template<> struct HashTraits<PAL::SessionID> : GenericHashTraits<PAL::SessionID> { |
| 121 | static PAL::SessionID emptyValue() { return PAL::SessionID::emptySessionID(); } |
| 122 | |
| 123 | static void constructDeletedValue(PAL::SessionID& slot) { slot = PAL::SessionID::hashTableDeletedValue(); } |
| 124 | static bool isDeletedValue(const PAL::SessionID& slot) { return slot == PAL::SessionID::hashTableDeletedValue(); } |
| 125 | }; |
| 126 | template<> struct DefaultHash<PAL::SessionID> { |
| 127 | typedef SessionIDHash Hash; |
| 128 | }; |
| 129 | |
| 130 | } // namespace WTF |
| 131 | |