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