1/*
2 * Copyright (C) 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 "RegistrableDomain.h"
29#include "SecurityOriginData.h"
30#include <wtf/HashTraits.h>
31#include <wtf/URL.h>
32
33namespace WebCore {
34
35struct ClientOrigin {
36 static ClientOrigin emptyKey() { return { }; }
37
38 unsigned hash() const;
39 bool operator==(const ClientOrigin&) const;
40
41 template<class Encoder> void encode(Encoder&) const;
42 template<class Decoder> static Optional<ClientOrigin> decode(Decoder&);
43
44 ClientOrigin isolatedCopy() const;
45 bool isRelated(const SecurityOriginData& other) const { return topOrigin == other || clientOrigin == other; }
46
47 RegistrableDomain clientRegistrableDomain() const { return RegistrableDomain::uncheckedCreateFromHost(clientOrigin.host); }
48
49 SecurityOriginData topOrigin;
50 SecurityOriginData clientOrigin;
51};
52
53inline unsigned ClientOrigin::hash() const
54{
55 unsigned hashes[2];
56 hashes[0] = SecurityOriginDataHash::hash(topOrigin);
57 hashes[1] = SecurityOriginDataHash::hash(clientOrigin);
58
59 return StringHasher::hashMemory(hashes, sizeof(hashes));
60}
61
62inline bool ClientOrigin::operator==(const ClientOrigin& other) const
63{
64 return topOrigin == other.topOrigin && clientOrigin == other.clientOrigin;
65}
66
67inline ClientOrigin ClientOrigin::isolatedCopy() const
68{
69 return { topOrigin.isolatedCopy(), clientOrigin.isolatedCopy() };
70}
71
72template<class Encoder> inline void ClientOrigin::encode(Encoder& encoder) const
73{
74 encoder << topOrigin;
75 encoder << clientOrigin;
76}
77
78template<class Decoder> inline Optional<ClientOrigin> ClientOrigin::decode(Decoder& decoder)
79{
80 Optional<SecurityOriginData> topOrigin;
81 Optional<SecurityOriginData> clientOrigin;
82 decoder >> topOrigin;
83 if (!topOrigin)
84 return WTF::nullopt;
85 decoder >> clientOrigin;
86 if (!clientOrigin)
87 return WTF::nullopt;
88
89 return ClientOrigin { WTFMove(*topOrigin), WTFMove(*clientOrigin) };
90}
91
92} // namespace WebCore
93
94namespace WTF {
95
96struct ClientOriginKeyHash {
97 static unsigned hash(const WebCore::ClientOrigin& key) { return key.hash(); }
98 static bool equal(const WebCore::ClientOrigin& a, const WebCore::ClientOrigin& b) { return a == b; }
99 static const bool safeToCompareToEmptyOrDeleted = false;
100};
101
102template<> struct HashTraits<WebCore::ClientOrigin> : GenericHashTraits<WebCore::ClientOrigin> {
103 static WebCore::ClientOrigin emptyValue() { return WebCore::ClientOrigin::emptyKey(); }
104
105 static void constructDeletedValue(WebCore::ClientOrigin& slot) { slot.topOrigin = WebCore::SecurityOriginData(HashTableDeletedValue); }
106 static bool isDeletedValue(const WebCore::ClientOrigin& slot) { return slot.topOrigin.isHashTableDeletedValue(); }
107};
108
109template<> struct DefaultHash<WebCore::ClientOrigin> {
110 typedef ClientOriginKeyHash Hash;
111};
112
113} // namespace WTF
114