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#include "config.h"
27#include "WebsitePoliciesData.h"
28
29#include "ArgumentCoders.h"
30#include "WebProcess.h"
31#include <WebCore/CustomHeaderFields.h>
32#include <WebCore/DocumentLoader.h>
33#include <WebCore/Frame.h>
34#include <WebCore/Page.h>
35
36namespace WebKit {
37
38void WebsitePoliciesData::encode(IPC::Encoder& encoder) const
39{
40 encoder << contentBlockersEnabled;
41 encoder << autoplayPolicy;
42#if ENABLE(DEVICE_ORIENTATION)
43 encoder << deviceOrientationAndMotionAccessState;
44#endif
45 encoder << allowedAutoplayQuirks;
46 encoder << customHeaderFields;
47 encoder << popUpPolicy;
48 encoder << websiteDataStoreParameters;
49 encoder << customUserAgent;
50 encoder << customJavaScriptUserAgentAsSiteSpecificQuirks;
51 encoder << customNavigatorPlatform;
52 encoder << metaViewportPolicy;
53 encoder << mediaSourcePolicy;
54 encoder << simulatedMouseEventsDispatchPolicy;
55 encoder << legacyOverflowScrollingTouchPolicy;
56}
57
58Optional<WebsitePoliciesData> WebsitePoliciesData::decode(IPC::Decoder& decoder)
59{
60 Optional<bool> contentBlockersEnabled;
61 decoder >> contentBlockersEnabled;
62 if (!contentBlockersEnabled)
63 return WTF::nullopt;
64
65 Optional<WebsiteAutoplayPolicy> autoplayPolicy;
66 decoder >> autoplayPolicy;
67 if (!autoplayPolicy)
68 return WTF::nullopt;
69
70#if ENABLE(DEVICE_ORIENTATION)
71 Optional<DeviceOrientationOrMotionPermissionState> deviceOrientationAndMotionAccessState;
72 decoder >> deviceOrientationAndMotionAccessState;
73 if (!deviceOrientationAndMotionAccessState)
74 return WTF::nullopt;
75#endif
76
77 Optional<OptionSet<WebsiteAutoplayQuirk>> allowedAutoplayQuirks;
78 decoder >> allowedAutoplayQuirks;
79 if (!allowedAutoplayQuirks)
80 return WTF::nullopt;
81
82 Optional<Vector<WebCore::CustomHeaderFields>> customHeaderFields;
83 decoder >> customHeaderFields;
84 if (!customHeaderFields)
85 return WTF::nullopt;
86
87 Optional<WebsitePopUpPolicy> popUpPolicy;
88 decoder >> popUpPolicy;
89 if (!popUpPolicy)
90 return WTF::nullopt;
91
92 Optional<Optional<WebsiteDataStoreParameters>> websiteDataStoreParameters;
93 decoder >> websiteDataStoreParameters;
94 if (!websiteDataStoreParameters)
95 return WTF::nullopt;
96
97 Optional<String> customUserAgent;
98 decoder >> customUserAgent;
99 if (!customUserAgent)
100 return WTF::nullopt;
101
102 Optional<String> customJavaScriptUserAgentAsSiteSpecificQuirks;
103 decoder >> customJavaScriptUserAgentAsSiteSpecificQuirks;
104 if (!customJavaScriptUserAgentAsSiteSpecificQuirks)
105 return WTF::nullopt;
106
107 Optional<String> customNavigatorPlatform;
108 decoder >> customNavigatorPlatform;
109 if (!customNavigatorPlatform)
110 return WTF::nullopt;
111
112 Optional<WebsiteMetaViewportPolicy> metaViewportPolicy;
113 decoder >> metaViewportPolicy;
114 if (!metaViewportPolicy)
115 return WTF::nullopt;
116
117 Optional<WebsiteMediaSourcePolicy> mediaSourcePolicy;
118 decoder >> mediaSourcePolicy;
119 if (!mediaSourcePolicy)
120 return WTF::nullopt;
121
122 Optional<WebsiteSimulatedMouseEventsDispatchPolicy> simulatedMouseEventsDispatchPolicy;
123 decoder >> simulatedMouseEventsDispatchPolicy;
124 if (!simulatedMouseEventsDispatchPolicy)
125 return WTF::nullopt;
126
127 Optional<WebsiteLegacyOverflowScrollingTouchPolicy> legacyOverflowScrollingTouchPolicy;
128 decoder >> legacyOverflowScrollingTouchPolicy;
129 if (!legacyOverflowScrollingTouchPolicy)
130 return WTF::nullopt;
131
132 return { {
133 WTFMove(*contentBlockersEnabled),
134 WTFMove(*allowedAutoplayQuirks),
135 WTFMove(*autoplayPolicy),
136#if ENABLE(DEVICE_ORIENTATION)
137 WTFMove(*deviceOrientationAndMotionAccessState),
138#endif
139 WTFMove(*customHeaderFields),
140 WTFMove(*popUpPolicy),
141 WTFMove(*websiteDataStoreParameters),
142 WTFMove(*customUserAgent),
143 WTFMove(*customJavaScriptUserAgentAsSiteSpecificQuirks),
144 WTFMove(*customNavigatorPlatform),
145 WTFMove(*metaViewportPolicy),
146 WTFMove(*mediaSourcePolicy),
147 WTFMove(*simulatedMouseEventsDispatchPolicy),
148 WTFMove(*legacyOverflowScrollingTouchPolicy),
149 } };
150}
151
152void WebsitePoliciesData::applyToDocumentLoader(WebsitePoliciesData&& websitePolicies, WebCore::DocumentLoader& documentLoader)
153{
154 documentLoader.setCustomHeaderFields(WTFMove(websitePolicies.customHeaderFields));
155 documentLoader.setCustomUserAgent(websitePolicies.customUserAgent);
156 documentLoader.setCustomJavaScriptUserAgentAsSiteSpecificQuirks(websitePolicies.customJavaScriptUserAgentAsSiteSpecificQuirks);
157 documentLoader.setCustomNavigatorPlatform(websitePolicies.customNavigatorPlatform);
158
159#if ENABLE(DEVICE_ORIENTATION)
160 documentLoader.setDeviceOrientationAndMotionAccessState(websitePolicies.deviceOrientationAndMotionAccessState);
161#endif
162
163 // Only setUserContentExtensionsEnabled if it hasn't already been disabled by reloading without content blockers.
164 if (documentLoader.userContentExtensionsEnabled())
165 documentLoader.setUserContentExtensionsEnabled(websitePolicies.contentBlockersEnabled);
166
167 OptionSet<WebCore::AutoplayQuirk> quirks;
168 const auto& allowedQuirks = websitePolicies.allowedAutoplayQuirks;
169
170 if (allowedQuirks.contains(WebsiteAutoplayQuirk::InheritedUserGestures))
171 quirks.add(WebCore::AutoplayQuirk::InheritedUserGestures);
172
173 if (allowedQuirks.contains(WebsiteAutoplayQuirk::SynthesizedPauseEvents))
174 quirks.add(WebCore::AutoplayQuirk::SynthesizedPauseEvents);
175
176 if (allowedQuirks.contains(WebsiteAutoplayQuirk::ArbitraryUserGestures))
177 quirks.add(WebCore::AutoplayQuirk::ArbitraryUserGestures);
178
179 if (allowedQuirks.contains(WebsiteAutoplayQuirk::PerDocumentAutoplayBehavior))
180 quirks.add(WebCore::AutoplayQuirk::PerDocumentAutoplayBehavior);
181
182 documentLoader.setAllowedAutoplayQuirks(quirks);
183
184 switch (websitePolicies.autoplayPolicy) {
185 case WebsiteAutoplayPolicy::Default:
186 documentLoader.setAutoplayPolicy(WebCore::AutoplayPolicy::Default);
187 break;
188 case WebsiteAutoplayPolicy::Allow:
189 documentLoader.setAutoplayPolicy(WebCore::AutoplayPolicy::Allow);
190 break;
191 case WebsiteAutoplayPolicy::AllowWithoutSound:
192 documentLoader.setAutoplayPolicy(WebCore::AutoplayPolicy::AllowWithoutSound);
193 break;
194 case WebsiteAutoplayPolicy::Deny:
195 documentLoader.setAutoplayPolicy(WebCore::AutoplayPolicy::Deny);
196 break;
197 }
198
199 switch (websitePolicies.popUpPolicy) {
200 case WebsitePopUpPolicy::Default:
201 documentLoader.setPopUpPolicy(WebCore::PopUpPolicy::Default);
202 break;
203 case WebsitePopUpPolicy::Allow:
204 documentLoader.setPopUpPolicy(WebCore::PopUpPolicy::Allow);
205 break;
206 case WebsitePopUpPolicy::Block:
207 documentLoader.setPopUpPolicy(WebCore::PopUpPolicy::Block);
208 break;
209 }
210
211 switch (websitePolicies.metaViewportPolicy) {
212 case WebsiteMetaViewportPolicy::Default:
213 documentLoader.setMetaViewportPolicy(WebCore::MetaViewportPolicy::Default);
214 break;
215 case WebsiteMetaViewportPolicy::Respect:
216 documentLoader.setMetaViewportPolicy(WebCore::MetaViewportPolicy::Respect);
217 break;
218 case WebsiteMetaViewportPolicy::Ignore:
219 documentLoader.setMetaViewportPolicy(WebCore::MetaViewportPolicy::Ignore);
220 break;
221 }
222
223 switch (websitePolicies.mediaSourcePolicy) {
224 case WebsiteMediaSourcePolicy::Default:
225 documentLoader.setMediaSourcePolicy(WebCore::MediaSourcePolicy::Default);
226 break;
227 case WebsiteMediaSourcePolicy::Disable:
228 documentLoader.setMediaSourcePolicy(WebCore::MediaSourcePolicy::Disable);
229 break;
230 case WebsiteMediaSourcePolicy::Enable:
231 documentLoader.setMediaSourcePolicy(WebCore::MediaSourcePolicy::Enable);
232 break;
233 }
234
235 switch (websitePolicies.simulatedMouseEventsDispatchPolicy) {
236 case WebsiteSimulatedMouseEventsDispatchPolicy::Default:
237 documentLoader.setSimulatedMouseEventsDispatchPolicy(WebCore::SimulatedMouseEventsDispatchPolicy::Default);
238 break;
239 case WebsiteSimulatedMouseEventsDispatchPolicy::Allow:
240 documentLoader.setSimulatedMouseEventsDispatchPolicy(WebCore::SimulatedMouseEventsDispatchPolicy::Allow);
241 break;
242 case WebsiteSimulatedMouseEventsDispatchPolicy::Deny:
243 documentLoader.setSimulatedMouseEventsDispatchPolicy(WebCore::SimulatedMouseEventsDispatchPolicy::Deny);
244 break;
245 }
246
247 switch (websitePolicies.legacyOverflowScrollingTouchPolicy) {
248 case WebsiteLegacyOverflowScrollingTouchPolicy::Default:
249 documentLoader.setLegacyOverflowScrollingTouchPolicy(WebCore::LegacyOverflowScrollingTouchPolicy::Default);
250 break;
251 case WebsiteLegacyOverflowScrollingTouchPolicy::Disable:
252 documentLoader.setLegacyOverflowScrollingTouchPolicy(WebCore::LegacyOverflowScrollingTouchPolicy::Disable);
253 break;
254 case WebsiteLegacyOverflowScrollingTouchPolicy::Enable:
255 documentLoader.setLegacyOverflowScrollingTouchPolicy(WebCore::LegacyOverflowScrollingTouchPolicy::Enable);
256 break;
257 }
258
259 auto* frame = documentLoader.frame();
260 if (!frame)
261 return;
262
263 documentLoader.applyPoliciesToSettings();
264
265 auto* page = frame->page();
266 if (!page)
267 return;
268
269 if (websitePolicies.websiteDataStoreParameters)
270 page->setSessionID(websitePolicies.websiteDataStoreParameters->networkSessionParameters.sessionID);
271}
272
273}
274
275