1d1181b1a9addafc5bab2ddcca7764ce626cf65d
[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 << deviceOrientationEventEnabled;
41     encoder << autoplayPolicy;
42     encoder << allowedAutoplayQuirks;
43     encoder << customHeaderFields;
44     encoder << popUpPolicy;
45     encoder << websiteDataStoreParameters;
46     encoder << customUserAgent;
47     encoder << customJavaScriptUserAgent;
48     encoder << customNavigatorPlatform;
49 }
50
51 Optional<WebsitePoliciesData> WebsitePoliciesData::decode(IPC::Decoder& decoder)
52 {
53     Optional<bool> contentBlockersEnabled;
54     decoder >> contentBlockersEnabled;
55     if (!contentBlockersEnabled)
56         return WTF::nullopt;
57
58     Optional<bool> deviceOrientationEventEnabled;
59     decoder >> deviceOrientationEventEnabled;
60     if (!deviceOrientationEventEnabled)
61         return WTF::nullopt;
62     
63     Optional<WebsiteAutoplayPolicy> autoplayPolicy;
64     decoder >> autoplayPolicy;
65     if (!autoplayPolicy)
66         return WTF::nullopt;
67     
68     Optional<OptionSet<WebsiteAutoplayQuirk>> allowedAutoplayQuirks;
69     decoder >> allowedAutoplayQuirks;
70     if (!allowedAutoplayQuirks)
71         return WTF::nullopt;
72     
73     Optional<Vector<WebCore::HTTPHeaderField>> customHeaderFields;
74     decoder >> customHeaderFields;
75     if (!customHeaderFields)
76         return WTF::nullopt;
77
78     Optional<WebsitePopUpPolicy> popUpPolicy;
79     decoder >> popUpPolicy;
80     if (!popUpPolicy)
81         return WTF::nullopt;
82
83     Optional<Optional<WebsiteDataStoreParameters>> websiteDataStoreParameters;
84     decoder >> websiteDataStoreParameters;
85     if (!websiteDataStoreParameters)
86         return WTF::nullopt;
87
88     Optional<String> customUserAgent;
89     decoder >> customUserAgent;
90     if (!customUserAgent)
91         return WTF::nullopt;
92
93     Optional<String> customJavaScriptUserAgent;
94     decoder >> customJavaScriptUserAgent;
95     if (!customJavaScriptUserAgent)
96         return WTF::nullopt;
97
98     Optional<String> customNavigatorPlatform;
99     decoder >> customNavigatorPlatform;
100     if (!customNavigatorPlatform)
101         return WTF::nullopt;
102     
103     return { {
104         WTFMove(*contentBlockersEnabled),
105         WTFMove(*deviceOrientationEventEnabled),
106         WTFMove(*allowedAutoplayQuirks),
107         WTFMove(*autoplayPolicy),
108         WTFMove(*customHeaderFields),
109         WTFMove(*popUpPolicy),
110         WTFMove(*websiteDataStoreParameters),
111         WTFMove(*customUserAgent),
112         WTFMove(*customJavaScriptUserAgent),
113         WTFMove(*customNavigatorPlatform),
114     } };
115 }
116
117 void WebsitePoliciesData::applyToDocumentLoader(WebsitePoliciesData&& websitePolicies, WebCore::DocumentLoader& documentLoader)
118 {
119     documentLoader.setCustomHeaderFields(WTFMove(websitePolicies.customHeaderFields));
120     documentLoader.setCustomUserAgent(websitePolicies.customUserAgent);
121     documentLoader.setCustomJavaScriptUserAgent(websitePolicies.customJavaScriptUserAgent);
122     documentLoader.setCustomNavigatorPlatform(websitePolicies.customNavigatorPlatform);
123     documentLoader.setDeviceOrientationEventEnabled(websitePolicies.deviceOrientationEventEnabled);
124     
125     // Only setUserContentExtensionsEnabled if it hasn't already been disabled by reloading without content blockers.
126     if (documentLoader.userContentExtensionsEnabled())
127         documentLoader.setUserContentExtensionsEnabled(websitePolicies.contentBlockersEnabled);
128
129     OptionSet<WebCore::AutoplayQuirk> quirks;
130     const auto& allowedQuirks = websitePolicies.allowedAutoplayQuirks;
131     
132     if (allowedQuirks.contains(WebsiteAutoplayQuirk::InheritedUserGestures))
133         quirks.add(WebCore::AutoplayQuirk::InheritedUserGestures);
134     
135     if (allowedQuirks.contains(WebsiteAutoplayQuirk::SynthesizedPauseEvents))
136         quirks.add(WebCore::AutoplayQuirk::SynthesizedPauseEvents);
137     
138     if (allowedQuirks.contains(WebsiteAutoplayQuirk::ArbitraryUserGestures))
139         quirks.add(WebCore::AutoplayQuirk::ArbitraryUserGestures);
140
141     if (allowedQuirks.contains(WebsiteAutoplayQuirk::PerDocumentAutoplayBehavior))
142         quirks.add(WebCore::AutoplayQuirk::PerDocumentAutoplayBehavior);
143
144     documentLoader.setAllowedAutoplayQuirks(quirks);
145
146     switch (websitePolicies.autoplayPolicy) {
147     case WebsiteAutoplayPolicy::Default:
148         documentLoader.setAutoplayPolicy(WebCore::AutoplayPolicy::Default);
149         break;
150     case WebsiteAutoplayPolicy::Allow:
151         documentLoader.setAutoplayPolicy(WebCore::AutoplayPolicy::Allow);
152         break;
153     case WebsiteAutoplayPolicy::AllowWithoutSound:
154         documentLoader.setAutoplayPolicy(WebCore::AutoplayPolicy::AllowWithoutSound);
155         break;
156     case WebsiteAutoplayPolicy::Deny:
157         documentLoader.setAutoplayPolicy(WebCore::AutoplayPolicy::Deny);
158         break;
159     }
160
161     switch (websitePolicies.popUpPolicy) {
162     case WebsitePopUpPolicy::Default:
163         documentLoader.setPopUpPolicy(WebCore::PopUpPolicy::Default);
164         break;
165     case WebsitePopUpPolicy::Allow:
166         documentLoader.setPopUpPolicy(WebCore::PopUpPolicy::Allow);
167         break;
168     case WebsitePopUpPolicy::Block:
169         documentLoader.setPopUpPolicy(WebCore::PopUpPolicy::Block);
170         break;
171     }
172
173     if (websitePolicies.websiteDataStoreParameters) {
174         if (auto* frame = documentLoader.frame()) {
175             if (auto* page = frame->page())
176                 page->setSessionID(websitePolicies.websiteDataStoreParameters->networkSessionParameters.sessionID);
177         }
178     }
179 }
180
181 }
182