Replace OptionSet |= and -= operators with add() and remove() functions
[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     toImpl(websitePolicies)->setAllowedAutoplayQuirks(quirks);
95 }
96
97 WKWebsiteAutoplayQuirk WKWebsitePoliciesGetAllowedAutoplayQuirks(WKWebsitePoliciesRef websitePolicies)
98 {
99     WKWebsiteAutoplayQuirk quirks = 0;
100     auto allowedQuirks = toImpl(websitePolicies)->allowedAutoplayQuirks();
101
102     if (allowedQuirks.contains(WebsiteAutoplayQuirk::SynthesizedPauseEvents))
103         quirks |= kWKWebsiteAutoplayQuirkSynthesizedPauseEvents;
104
105     if (allowedQuirks.contains(WebsiteAutoplayQuirk::InheritedUserGestures))
106         quirks |= kWKWebsiteAutoplayQuirkInheritedUserGestures;
107
108     if (allowedQuirks.contains(WebsiteAutoplayQuirk::ArbitraryUserGestures))
109         quirks |= kWKWebsiteAutoplayQuirkArbitraryUserGestures;
110
111     return quirks;
112 }
113
114 WKWebsiteAutoplayPolicy WKWebsitePoliciesGetAutoplayPolicy(WKWebsitePoliciesRef websitePolicies)
115 {
116     switch (toImpl(websitePolicies)->autoplayPolicy()) {
117     case WebKit::WebsiteAutoplayPolicy::Default:
118         return kWKWebsiteAutoplayPolicyDefault;
119     case WebsiteAutoplayPolicy::Allow:
120         return kWKWebsiteAutoplayPolicyAllow;
121     case WebsiteAutoplayPolicy::AllowWithoutSound:
122         return kWKWebsiteAutoplayPolicyAllowWithoutSound;
123     case WebsiteAutoplayPolicy::Deny:
124         return kWKWebsiteAutoplayPolicyDeny;
125     }
126     ASSERT_NOT_REACHED();
127     return kWKWebsiteAutoplayPolicyDefault;
128 }
129
130 void WKWebsitePoliciesSetAutoplayPolicy(WKWebsitePoliciesRef websitePolicies, WKWebsiteAutoplayPolicy policy)
131 {
132     switch (policy) {
133     case kWKWebsiteAutoplayPolicyDefault:
134         toImpl(websitePolicies)->setAutoplayPolicy(WebsiteAutoplayPolicy::Default);
135         return;
136     case kWKWebsiteAutoplayPolicyAllow:
137         toImpl(websitePolicies)->setAutoplayPolicy(WebsiteAutoplayPolicy::Allow);
138         return;
139     case kWKWebsiteAutoplayPolicyAllowWithoutSound:
140         toImpl(websitePolicies)->setAutoplayPolicy(WebsiteAutoplayPolicy::AllowWithoutSound);
141         return;
142     case kWKWebsiteAutoplayPolicyDeny:
143         toImpl(websitePolicies)->setAutoplayPolicy(WebsiteAutoplayPolicy::Deny);
144         return;
145     }
146     ASSERT_NOT_REACHED();
147 }
148
149 WKWebsitePopUpPolicy WKWebsitePoliciesGetPopUpPolicy(WKWebsitePoliciesRef websitePolicies)
150 {
151     switch (toImpl(websitePolicies)->popUpPolicy()) {
152     case WebsitePopUpPolicy::Default:
153         return kWKWebsitePopUpPolicyDefault;
154     case WebsitePopUpPolicy::Allow:
155         return kWKWebsitePopUpPolicyAllow;
156     case WebsitePopUpPolicy::Block:
157         return kWKWebsitePopUpPolicyBlock;
158     }
159     ASSERT_NOT_REACHED();
160     return kWKWebsitePopUpPolicyDefault;
161 }
162
163 void WKWebsitePoliciesSetPopUpPolicy(WKWebsitePoliciesRef websitePolicies, WKWebsitePopUpPolicy policy)
164 {
165     switch (policy) {
166     case kWKWebsitePopUpPolicyDefault:
167         toImpl(websitePolicies)->setPopUpPolicy(WebsitePopUpPolicy::Default);
168         return;
169     case kWKWebsitePopUpPolicyAllow:
170         toImpl(websitePolicies)->setPopUpPolicy(WebsitePopUpPolicy::Allow);
171         return;
172     case kWKWebsitePopUpPolicyBlock:
173         toImpl(websitePolicies)->setPopUpPolicy(WebsitePopUpPolicy::Block);
174         return;
175     }
176     ASSERT_NOT_REACHED();
177 }
178
179 WKWebsiteDataStoreRef WKWebsitePoliciesGetDataStore(WKWebsitePoliciesRef websitePolicies)
180 {
181     return toAPI(toImpl(websitePolicies)->websiteDataStore());
182 }
183
184 void WKWebsitePoliciesSetDataStore(WKWebsitePoliciesRef websitePolicies, WKWebsiteDataStoreRef websiteDataStore)
185 {
186     toImpl(websitePolicies)->setWebsiteDataStore(toImpl(websiteDataStore));
187 }
188