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