Unreviewed, rolling out r245401.
[WebKit-https.git] / Source / WebKit / UIProcess / API / C / WKWebsitePolicies.cpp
1 /*
2  * Copyright (C) 2016 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 "WKWebsitePolicies.h"
28
29 #include "APIDictionary.h"
30 #include "APIWebsiteDataStore.h"
31 #include "APIWebsitePolicies.h"
32 #include "WKAPICast.h"
33 #include "WKArray.h"
34 #include "WKDictionary.h"
35 #include "WKRetainPtr.h"
36
37 using namespace WebKit;
38
39 WKTypeID WKWebsitePoliciesGetTypeID()
40 {
41     return toAPI(API::WebsitePolicies::APIType);
42 }
43
44 WKWebsitePoliciesRef WKWebsitePoliciesCreate()
45 {
46     return toAPI(&API::WebsitePolicies::create().leakRef());
47 }
48
49 void WKWebsitePoliciesSetContentBlockersEnabled(WKWebsitePoliciesRef websitePolicies, bool enabled)
50 {
51     toImpl(websitePolicies)->setContentBlockersEnabled(enabled);
52 }
53
54 bool WKWebsitePoliciesGetContentBlockersEnabled(WKWebsitePoliciesRef websitePolicies)
55 {
56     return toImpl(websitePolicies)->contentBlockersEnabled();
57 }
58
59 WK_EXPORT WKDictionaryRef WKWebsitePoliciesCopyCustomHeaderFields(WKWebsitePoliciesRef websitePolicies)
60 {
61     HashMap<WTF::String, RefPtr<API::Object>> fields;
62     for (const auto& field : toImpl(websitePolicies)->customHeaderFields())
63         fields.add(field.name(), API::String::create(field.value()));
64     return toAPI(API::Dictionary::create(WTFMove(fields)).ptr());
65 }
66
67 WK_EXPORT void WKWebsitePoliciesSetCustomHeaderFields(WKWebsitePoliciesRef websitePolicies, WKDictionaryRef dictionary)
68 {
69     auto keys = adoptWK(WKDictionaryCopyKeys(dictionary));
70     size_t length = WKArrayGetSize(keys.get());
71     Vector<WebCore::HTTPHeaderField> fields;
72     fields.reserveInitialCapacity(length);
73     for (size_t i = 0; i < length; ++i) {
74         WKStringRef name = static_cast<WKStringRef>(WKArrayGetItemAtIndex(keys.get(), i));
75         auto field = WebCore::HTTPHeaderField::create(toImpl(name)->string(), toImpl(static_cast<WKStringRef>(WKDictionaryGetItemForKey(dictionary, name)))->string());
76         if (field && startsWithLettersIgnoringASCIICase(field->name(), "x-"))
77             fields.uncheckedAppend(WTFMove(*field));
78     }
79     toImpl(websitePolicies)->setCustomHeaderFields(WTFMove(fields));
80 }
81
82 void WKWebsitePoliciesSetAllowedAutoplayQuirks(WKWebsitePoliciesRef websitePolicies, WKWebsiteAutoplayQuirk allowedQuirks)
83 {
84     OptionSet<WebsiteAutoplayQuirk> quirks;
85     if (allowedQuirks & kWKWebsiteAutoplayQuirkInheritedUserGestures)
86         quirks.add(WebsiteAutoplayQuirk::InheritedUserGestures);
87
88     if (allowedQuirks & kWKWebsiteAutoplayQuirkSynthesizedPauseEvents)
89         quirks.add(WebsiteAutoplayQuirk::SynthesizedPauseEvents);
90
91     if (allowedQuirks & kWKWebsiteAutoplayQuirkArbitraryUserGestures)
92         quirks.add(WebsiteAutoplayQuirk::ArbitraryUserGestures);
93
94     if (allowedQuirks & kWKWebsiteAutoplayQuirkPerDocumentAutoplayBehavior)
95         quirks.add(WebsiteAutoplayQuirk::PerDocumentAutoplayBehavior);
96
97     toImpl(websitePolicies)->setAllowedAutoplayQuirks(quirks);
98 }
99
100 WKWebsiteAutoplayQuirk WKWebsitePoliciesGetAllowedAutoplayQuirks(WKWebsitePoliciesRef websitePolicies)
101 {
102     WKWebsiteAutoplayQuirk quirks = 0;
103     auto allowedQuirks = toImpl(websitePolicies)->allowedAutoplayQuirks();
104
105     if (allowedQuirks.contains(WebsiteAutoplayQuirk::SynthesizedPauseEvents))
106         quirks |= kWKWebsiteAutoplayQuirkSynthesizedPauseEvents;
107
108     if (allowedQuirks.contains(WebsiteAutoplayQuirk::InheritedUserGestures))
109         quirks |= kWKWebsiteAutoplayQuirkInheritedUserGestures;
110
111     if (allowedQuirks.contains(WebsiteAutoplayQuirk::ArbitraryUserGestures))
112         quirks |= kWKWebsiteAutoplayQuirkArbitraryUserGestures;
113
114     if (allowedQuirks.contains(WebsiteAutoplayQuirk::PerDocumentAutoplayBehavior))
115         quirks |= kWKWebsiteAutoplayQuirkPerDocumentAutoplayBehavior;
116
117     return quirks;
118 }
119
120 WKWebsiteAutoplayPolicy WKWebsitePoliciesGetAutoplayPolicy(WKWebsitePoliciesRef websitePolicies)
121 {
122     switch (toImpl(websitePolicies)->autoplayPolicy()) {
123     case WebKit::WebsiteAutoplayPolicy::Default:
124         return kWKWebsiteAutoplayPolicyDefault;
125     case WebsiteAutoplayPolicy::Allow:
126         return kWKWebsiteAutoplayPolicyAllow;
127     case WebsiteAutoplayPolicy::AllowWithoutSound:
128         return kWKWebsiteAutoplayPolicyAllowWithoutSound;
129     case WebsiteAutoplayPolicy::Deny:
130         return kWKWebsiteAutoplayPolicyDeny;
131     }
132     ASSERT_NOT_REACHED();
133     return kWKWebsiteAutoplayPolicyDefault;
134 }
135
136 void WKWebsitePoliciesSetAutoplayPolicy(WKWebsitePoliciesRef websitePolicies, WKWebsiteAutoplayPolicy policy)
137 {
138     switch (policy) {
139     case kWKWebsiteAutoplayPolicyDefault:
140         toImpl(websitePolicies)->setAutoplayPolicy(WebsiteAutoplayPolicy::Default);
141         return;
142     case kWKWebsiteAutoplayPolicyAllow:
143         toImpl(websitePolicies)->setAutoplayPolicy(WebsiteAutoplayPolicy::Allow);
144         return;
145     case kWKWebsiteAutoplayPolicyAllowWithoutSound:
146         toImpl(websitePolicies)->setAutoplayPolicy(WebsiteAutoplayPolicy::AllowWithoutSound);
147         return;
148     case kWKWebsiteAutoplayPolicyDeny:
149         toImpl(websitePolicies)->setAutoplayPolicy(WebsiteAutoplayPolicy::Deny);
150         return;
151     }
152     ASSERT_NOT_REACHED();
153 }
154
155 WKWebsitePopUpPolicy WKWebsitePoliciesGetPopUpPolicy(WKWebsitePoliciesRef websitePolicies)
156 {
157     switch (toImpl(websitePolicies)->popUpPolicy()) {
158     case WebsitePopUpPolicy::Default:
159         return kWKWebsitePopUpPolicyDefault;
160     case WebsitePopUpPolicy::Allow:
161         return kWKWebsitePopUpPolicyAllow;
162     case WebsitePopUpPolicy::Block:
163         return kWKWebsitePopUpPolicyBlock;
164     }
165     ASSERT_NOT_REACHED();
166     return kWKWebsitePopUpPolicyDefault;
167 }
168
169 void WKWebsitePoliciesSetPopUpPolicy(WKWebsitePoliciesRef websitePolicies, WKWebsitePopUpPolicy policy)
170 {
171     switch (policy) {
172     case kWKWebsitePopUpPolicyDefault:
173         toImpl(websitePolicies)->setPopUpPolicy(WebsitePopUpPolicy::Default);
174         return;
175     case kWKWebsitePopUpPolicyAllow:
176         toImpl(websitePolicies)->setPopUpPolicy(WebsitePopUpPolicy::Allow);
177         return;
178     case kWKWebsitePopUpPolicyBlock:
179         toImpl(websitePolicies)->setPopUpPolicy(WebsitePopUpPolicy::Block);
180         return;
181     }
182     ASSERT_NOT_REACHED();
183 }
184
185 WKWebsiteDataStoreRef WKWebsitePoliciesGetDataStore(WKWebsitePoliciesRef websitePolicies)
186 {
187     return toAPI(toImpl(websitePolicies)->websiteDataStore());
188 }
189
190 void WKWebsitePoliciesSetDataStore(WKWebsitePoliciesRef websitePolicies, WKWebsiteDataStoreRef websiteDataStore)
191 {
192     toImpl(websitePolicies)->setWebsiteDataStore(toImpl(websiteDataStore));
193 }
194