Introduce SPI _WKWebsitePolicies.websiteDataStore
[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 <WebCore/DocumentLoader.h>
31
32 namespace WebKit {
33
34 void WebsitePoliciesData::encode(IPC::Encoder& encoder) const
35 {
36     encoder << contentBlockersEnabled;
37     encoder << autoplayPolicy;
38     encoder << allowedAutoplayQuirks;
39     encoder << customHeaderFields;
40     encoder << websiteDataStoreParameters;
41 }
42
43 std::optional<WebsitePoliciesData> WebsitePoliciesData::decode(IPC::Decoder& decoder)
44 {
45     std::optional<bool> contentBlockersEnabled;
46     decoder >> contentBlockersEnabled;
47     if (!contentBlockersEnabled)
48         return std::nullopt;
49     
50     std::optional<WebsiteAutoplayPolicy> autoplayPolicy;
51     decoder >> autoplayPolicy;
52     if (!autoplayPolicy)
53         return std::nullopt;
54     
55     std::optional<OptionSet<WebsiteAutoplayQuirk>> allowedAutoplayQuirks;
56     decoder >> allowedAutoplayQuirks;
57     if (!allowedAutoplayQuirks)
58         return std::nullopt;
59     
60     std::optional<Vector<WebCore::HTTPHeaderField>> customHeaderFields;
61     decoder >> customHeaderFields;
62     if (!customHeaderFields)
63         return std::nullopt;
64     
65     std::optional<std::optional<WebsiteDataStoreParameters>> websiteDataStoreParameters;
66     decoder >> websiteDataStoreParameters;
67     if (!websiteDataStoreParameters)
68         return std::nullopt;
69     
70     return { {
71         WTFMove(*contentBlockersEnabled),
72         WTFMove(*allowedAutoplayQuirks),
73         WTFMove(*autoplayPolicy),
74         WTFMove(*customHeaderFields),
75         WTFMove(*websiteDataStoreParameters),
76     } };
77 }
78
79 void WebsitePoliciesData::applyToDocumentLoader(WebsitePoliciesData&& websitePolicies, WebCore::DocumentLoader& documentLoader)
80 {
81     documentLoader.setCustomHeaderFields(WTFMove(websitePolicies.customHeaderFields));
82     
83     // Only setUserContentExtensionsEnabled if it hasn't already been disabled by reloading without content blockers.
84     if (documentLoader.userContentExtensionsEnabled())
85         documentLoader.setUserContentExtensionsEnabled(websitePolicies.contentBlockersEnabled);
86
87     OptionSet<WebCore::AutoplayQuirk> quirks;
88     const auto& allowedQuirks = websitePolicies.allowedAutoplayQuirks;
89     
90     if (allowedQuirks.contains(WebsiteAutoplayQuirk::InheritedUserGestures))
91         quirks |= WebCore::AutoplayQuirk::InheritedUserGestures;
92     
93     if (allowedQuirks.contains(WebsiteAutoplayQuirk::SynthesizedPauseEvents))
94         quirks |= WebCore::AutoplayQuirk::SynthesizedPauseEvents;
95     
96     if (allowedQuirks.contains(WebsiteAutoplayQuirk::ArbitraryUserGestures))
97         quirks |= WebCore::AutoplayQuirk::ArbitraryUserGestures;
98
99     documentLoader.setAllowedAutoplayQuirks(quirks);
100
101     switch (websitePolicies.autoplayPolicy) {
102     case WebsiteAutoplayPolicy::Default:
103         documentLoader.setAutoplayPolicy(WebCore::AutoplayPolicy::Default);
104         break;
105     case WebsiteAutoplayPolicy::Allow:
106         documentLoader.setAutoplayPolicy(WebCore::AutoplayPolicy::Allow);
107         break;
108     case WebsiteAutoplayPolicy::AllowWithoutSound:
109         documentLoader.setAutoplayPolicy(WebCore::AutoplayPolicy::AllowWithoutSound);
110         break;
111     case WebsiteAutoplayPolicy::Deny:
112         documentLoader.setAutoplayPolicy(WebCore::AutoplayPolicy::Deny);
113         break;
114     }
115 }
116
117 }
118