6eb4be2c3dd338a9d38d2cd604bafe62ac45eafd
[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     encoder << allowedAutoplayQuirks;
42     encoder << customHeaderFields;
43     encoder << popUpPolicy;
44     encoder << websiteDataStoreParameters;
45 }
46
47 std::optional<WebsitePoliciesData> WebsitePoliciesData::decode(IPC::Decoder& decoder)
48 {
49     std::optional<bool> contentBlockersEnabled;
50     decoder >> contentBlockersEnabled;
51     if (!contentBlockersEnabled)
52         return std::nullopt;
53     
54     std::optional<WebsiteAutoplayPolicy> autoplayPolicy;
55     decoder >> autoplayPolicy;
56     if (!autoplayPolicy)
57         return std::nullopt;
58     
59     std::optional<OptionSet<WebsiteAutoplayQuirk>> allowedAutoplayQuirks;
60     decoder >> allowedAutoplayQuirks;
61     if (!allowedAutoplayQuirks)
62         return std::nullopt;
63     
64     std::optional<Vector<WebCore::HTTPHeaderField>> customHeaderFields;
65     decoder >> customHeaderFields;
66     if (!customHeaderFields)
67         return std::nullopt;
68
69     std::optional<WebsitePopUpPolicy> popUpPolicy;
70     decoder >> popUpPolicy;
71     if (!popUpPolicy)
72         return std::nullopt;
73
74     std::optional<std::optional<WebsiteDataStoreParameters>> websiteDataStoreParameters;
75     decoder >> websiteDataStoreParameters;
76     if (!websiteDataStoreParameters)
77         return std::nullopt;
78     
79     return { {
80         WTFMove(*contentBlockersEnabled),
81         WTFMove(*allowedAutoplayQuirks),
82         WTFMove(*autoplayPolicy),
83         WTFMove(*customHeaderFields),
84         WTFMove(*popUpPolicy),
85         WTFMove(*websiteDataStoreParameters),
86     } };
87 }
88
89 void WebsitePoliciesData::applyToDocumentLoader(WebsitePoliciesData&& websitePolicies, WebCore::DocumentLoader& documentLoader)
90 {
91     documentLoader.setCustomHeaderFields(WTFMove(websitePolicies.customHeaderFields));
92     
93     // Only setUserContentExtensionsEnabled if it hasn't already been disabled by reloading without content blockers.
94     if (documentLoader.userContentExtensionsEnabled())
95         documentLoader.setUserContentExtensionsEnabled(websitePolicies.contentBlockersEnabled);
96
97     OptionSet<WebCore::AutoplayQuirk> quirks;
98     const auto& allowedQuirks = websitePolicies.allowedAutoplayQuirks;
99     
100     if (allowedQuirks.contains(WebsiteAutoplayQuirk::InheritedUserGestures))
101         quirks |= WebCore::AutoplayQuirk::InheritedUserGestures;
102     
103     if (allowedQuirks.contains(WebsiteAutoplayQuirk::SynthesizedPauseEvents))
104         quirks |= WebCore::AutoplayQuirk::SynthesizedPauseEvents;
105     
106     if (allowedQuirks.contains(WebsiteAutoplayQuirk::ArbitraryUserGestures))
107         quirks |= WebCore::AutoplayQuirk::ArbitraryUserGestures;
108
109     documentLoader.setAllowedAutoplayQuirks(quirks);
110
111     switch (websitePolicies.autoplayPolicy) {
112     case WebsiteAutoplayPolicy::Default:
113         documentLoader.setAutoplayPolicy(WebCore::AutoplayPolicy::Default);
114         break;
115     case WebsiteAutoplayPolicy::Allow:
116         documentLoader.setAutoplayPolicy(WebCore::AutoplayPolicy::Allow);
117         break;
118     case WebsiteAutoplayPolicy::AllowWithoutSound:
119         documentLoader.setAutoplayPolicy(WebCore::AutoplayPolicy::AllowWithoutSound);
120         break;
121     case WebsiteAutoplayPolicy::Deny:
122         documentLoader.setAutoplayPolicy(WebCore::AutoplayPolicy::Deny);
123         break;
124     }
125
126     switch (websitePolicies.popUpPolicy) {
127     case WebsitePopUpPolicy::Default:
128         documentLoader.setPopUpPolicy(WebCore::PopUpPolicy::Default);
129         break;
130     case WebsitePopUpPolicy::Allow:
131         documentLoader.setPopUpPolicy(WebCore::PopUpPolicy::Allow);
132         break;
133     case WebsitePopUpPolicy::Block:
134         documentLoader.setPopUpPolicy(WebCore::PopUpPolicy::Block);
135         break;
136     }
137
138     if (websitePolicies.websiteDataStoreParameters) {
139         if (auto* frame = documentLoader.frame()) {
140             if (auto* page = frame->page()) {
141                 auto sessionID = websitePolicies.websiteDataStoreParameters->networkSessionParameters.sessionID;
142                 WebProcess::singleton().addWebsiteDataStore(WTFMove(*websitePolicies.websiteDataStoreParameters));
143                 page->setSessionID(sessionID);
144             }
145         }
146     }
147 }
148
149 }
150