1 /*
2 * Copyright (C) 2011, 2015 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/URL.h>
29
30namespace WebCore {
31
32class Frame;
33class SecurityOrigin;
34
35struct SecurityOriginData {
36 SecurityOriginData() = default;
37 SecurityOriginData(const String& protocol, const String& host, Optional<uint16_t> port)
38 : protocol(protocol)
39 , host(host)
40 , port(port)
41 {
42 }
43 SecurityOriginData(WTF::HashTableDeletedValueType)
44 : protocol(WTF::HashTableDeletedValue)
45 {
46 }
47
48 WEBCORE_EXPORT static SecurityOriginData fromFrame(Frame*);
49 static SecurityOriginData fromURL(const URL& url)
50 {
51 return SecurityOriginData {
52 url.protocol().isNull() ? emptyString() : url.protocol().convertToASCIILowercase(),
53 url.host().isNull() ? emptyString() : url.host().convertToASCIILowercase(),
54 url.port()
55 };
56 }
57
58 WEBCORE_EXPORT Ref<SecurityOrigin> securityOrigin() const;
59
60 // FIXME <rdar://9018386>: We should be sending more state across the wire than just the protocol,
61 // host, and port.
62
63 String protocol;
64 String host;
65 Optional<uint16_t> port;
66
67 WEBCORE_EXPORT SecurityOriginData isolatedCopy() const;
68
69 // Serialize the security origin to a string that could be used as part of
70 // file names. This format should be used in storage APIs only.
71 WEBCORE_EXPORT String databaseIdentifier() const;
72 WEBCORE_EXPORT static Optional<SecurityOriginData> fromDatabaseIdentifier(const String&);
73
74 template<class Encoder> void encode(Encoder&) const;
75 template<class Decoder> static Optional<SecurityOriginData> decode(Decoder&);
76
77 bool isEmpty() const
78 {
79 return protocol.isNull() && host.isNull() && port == WTF::nullopt;
80 }
81
82 bool isHashTableDeletedValue() const
83 {
84 return protocol.isHashTableDeletedValue();
85 }
86
87 WEBCORE_EXPORT String toString() const;
88
89#if !LOG_DISABLED
90 String debugString() const { return toString(); }
91#endif
92};
93
94WEBCORE_EXPORT bool operator==(const SecurityOriginData&, const SecurityOriginData&);
95inline bool operator!=(const SecurityOriginData& first, const SecurityOriginData& second) { return !(first == second); }
96
97template<class Encoder>
98void SecurityOriginData::encode(Encoder& encoder) const
99{
100 encoder << protocol;
101 encoder << host;
102 encoder << port;
103}
104
105template<class Decoder>
106Optional<SecurityOriginData> SecurityOriginData::decode(Decoder& decoder)
107{
108 Optional<String> protocol;
109 decoder >> protocol;
110 if (!protocol)
111 return WTF::nullopt;
112
113 Optional<String> host;
114 decoder >> host;
115 if (!host)
116 return WTF::nullopt;
117
118 Optional<uint16_t> port;
119 if (!decoder.decode(port))
120 return WTF::nullopt;
121
122 return {{ WTFMove(*protocol), WTFMove(*host), WTFMove(port) }};
123}
124
125struct SecurityOriginDataHashTraits : WTF::SimpleClassHashTraits<SecurityOriginData> {
126 static const bool hasIsEmptyValueFunction = true;
127 static const bool emptyValueIsZero = false;
128 static bool isEmptyValue(const SecurityOriginData& data) { return data.isEmpty(); }
129};
130
131struct SecurityOriginDataHash {
132 static unsigned hash(const SecurityOriginData& data)
133 {
134 unsigned hashCodes[3] = {
135 data.protocol.impl() ? data.protocol.impl()->hash() : 0,
136 data.host.impl() ? data.host.impl()->hash() : 0,
137 data.port.valueOr(0)
138 };
139 return StringHasher::hashMemory<sizeof(hashCodes)>(hashCodes);
140 }
141 static bool equal(const SecurityOriginData& a, const SecurityOriginData& b) { return a == b; }
142 static const bool safeToCompareToEmptyOrDeleted = false;
143};
144
145} // namespace WebCore
146
147namespace WTF {
148
149template<> struct HashTraits<WebCore::SecurityOriginData> : WebCore::SecurityOriginDataHashTraits { };
150template<> struct DefaultHash<WebCore::SecurityOriginData> {
151 typedef WebCore::SecurityOriginDataHash Hash;
152};
153
154} // namespace WTF
155