1/*
2 * Copyright (C) 2019 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/KeyValuePair.h>
29#include <wtf/Vector.h>
30#include <wtf/text/WTFString.h>
31
32namespace WebCore {
33
34struct ContentRuleListResults {
35 struct Result {
36 bool blockedLoad { false };
37 bool madeHTTPS { false };
38 bool blockedCookies { false };
39 Vector<String> notifications;
40
41 bool shouldNotifyApplication() const
42 {
43 return blockedLoad
44 || madeHTTPS
45 || blockedCookies
46 || !notifications.isEmpty();
47 }
48
49 template<class Encoder> void encode(Encoder&) const;
50 template<class Decoder> static Optional<Result> decode(Decoder&);
51 };
52 struct Summary {
53 bool blockedLoad { false };
54 bool madeHTTPS { false };
55 bool blockedCookies { false };
56 bool hasNotifications { false };
57
58 template<class Encoder> void encode(Encoder&) const;
59 template<class Decoder> static Optional<Summary> decode(Decoder&);
60 };
61 using ContentRuleListIdentifier = String;
62
63 Summary summary;
64 Vector<std::pair<ContentRuleListIdentifier, Result>> results;
65
66 bool shouldNotifyApplication() const
67 {
68 return summary.blockedLoad
69 || summary.madeHTTPS
70 || summary.blockedCookies
71 || summary.hasNotifications;
72 }
73
74 template<class Encoder> void encode(Encoder& encoder) const
75 {
76 encoder << summary;
77 encoder << results;
78 }
79 template<class Decoder> static Optional<ContentRuleListResults> decode(Decoder& decoder)
80 {
81 Optional<Summary> summary;
82 decoder >> summary;
83 if (!summary)
84 return WTF::nullopt;
85
86 Optional<Vector<std::pair<ContentRuleListIdentifier, Result>>> results;
87 decoder >> results;
88 if (!results)
89 return WTF::nullopt;
90
91 return {{
92 WTFMove(*summary),
93 WTFMove(*results)
94 }};
95 }
96};
97
98template<class Encoder> void ContentRuleListResults::Result::encode(Encoder& encoder) const
99{
100 encoder << blockedLoad;
101 encoder << madeHTTPS;
102 encoder << blockedCookies;
103 encoder << notifications;
104}
105
106template<class Decoder> auto ContentRuleListResults::Result::decode(Decoder& decoder) -> Optional<Result>
107{
108 Optional<bool> blockedLoad;
109 decoder >> blockedLoad;
110 if (!blockedLoad)
111 return WTF::nullopt;
112
113 Optional<bool> madeHTTPS;
114 decoder >> madeHTTPS;
115 if (!madeHTTPS)
116 return WTF::nullopt;
117
118 Optional<bool> blockedCookies;
119 decoder >> blockedCookies;
120 if (!blockedCookies)
121 return WTF::nullopt;
122
123 Optional<Vector<String>> notifications;
124 decoder >> notifications;
125 if (!notifications)
126 return WTF::nullopt;
127
128 return {{
129 WTFMove(*blockedLoad),
130 WTFMove(*madeHTTPS),
131 WTFMove(*blockedCookies),
132 WTFMove(*notifications)
133 }};
134}
135
136template<class Encoder> void ContentRuleListResults::Summary::encode(Encoder& encoder) const
137{
138 encoder << blockedLoad;
139 encoder << madeHTTPS;
140 encoder << blockedCookies;
141 encoder << hasNotifications;
142}
143
144template<class Decoder> auto ContentRuleListResults::Summary::decode(Decoder& decoder) -> Optional<Summary>
145{
146 Optional<bool> blockedLoad;
147 decoder >> blockedLoad;
148 if (!blockedLoad)
149 return WTF::nullopt;
150
151 Optional<bool> madeHTTPS;
152 decoder >> madeHTTPS;
153 if (!madeHTTPS)
154 return WTF::nullopt;
155
156 Optional<bool> blockedCookies;
157 decoder >> blockedCookies;
158 if (!blockedCookies)
159 return WTF::nullopt;
160
161 Optional<bool> hasNotifications;
162 decoder >> hasNotifications;
163 if (!hasNotifications)
164 return WTF::nullopt;
165
166 return {{
167 WTFMove(*blockedLoad),
168 WTFMove(*madeHTTPS),
169 WTFMove(*blockedCookies),
170 WTFMove(*hasNotifications),
171 }};
172}
173
174}
175