User agent string override for navigator.userAgent should be site specific quirks
[WebKit-https.git] / Source / WebKit / UIProcess / API / Cocoa / _WKWebsitePolicies.mm
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 #import "config.h"
27 #import "_WKWebsitePoliciesInternal.h"
28
29 #if WK_API_ENABLED
30
31 #import "WKWebsiteDataStoreInternal.h"
32
33 @implementation _WKWebsitePolicies
34
35 - (void)dealloc
36 {
37     _websitePolicies->API::WebsitePolicies::~WebsitePolicies();
38     
39     [super dealloc];
40 }
41
42 - (instancetype)init
43 {
44     if (!(self = [super init]))
45         return nil;
46     
47     API::Object::constructInWrapper<API::WebsitePolicies>(self);
48     
49     return self;
50 }
51
52 - (void)setContentBlockersEnabled:(BOOL)contentBlockersEnabled
53 {
54     _websitePolicies->setContentBlockersEnabled(contentBlockersEnabled);
55 }
56
57 - (BOOL)contentBlockersEnabled
58 {
59     return _websitePolicies->contentBlockersEnabled();
60 }
61
62 - (void)setDeviceOrientationEventEnabled:(BOOL)deviceOrientationEventEnabled
63 {
64     _websitePolicies->setDeviceOrientationEventEnabled(deviceOrientationEventEnabled);
65 }
66
67 - (BOOL)deviceOrientationEventEnabled
68 {
69     return _websitePolicies->deviceOrientationEventEnabled();
70 }
71
72 - (void)setAllowedAutoplayQuirks:(_WKWebsiteAutoplayQuirk)allowedQuirks
73 {
74     OptionSet<WebKit::WebsiteAutoplayQuirk> quirks;
75
76     if (allowedQuirks & _WKWebsiteAutoplayQuirkInheritedUserGestures)
77         quirks.add(WebKit::WebsiteAutoplayQuirk::InheritedUserGestures);
78
79     if (allowedQuirks & _WKWebsiteAutoplayQuirkSynthesizedPauseEvents)
80         quirks.add(WebKit::WebsiteAutoplayQuirk::SynthesizedPauseEvents);
81
82     if (allowedQuirks & _WKWebsiteAutoplayQuirkArbitraryUserGestures)
83         quirks.add(WebKit::WebsiteAutoplayQuirk::ArbitraryUserGestures);
84
85     if (allowedQuirks & _WKWebsiteAutoplayQuirkPerDocumentAutoplayBehavior)
86         quirks.add(WebKit::WebsiteAutoplayQuirk::PerDocumentAutoplayBehavior);
87
88     _websitePolicies->setAllowedAutoplayQuirks(quirks);
89 }
90
91 - (_WKWebsiteAutoplayQuirk)allowedAutoplayQuirks
92 {
93     _WKWebsiteAutoplayQuirk quirks = 0;
94     auto allowedQuirks = _websitePolicies->allowedAutoplayQuirks();
95
96     if (allowedQuirks.contains(WebKit::WebsiteAutoplayQuirk::InheritedUserGestures))
97         quirks |= _WKWebsiteAutoplayQuirkInheritedUserGestures;
98
99     if (allowedQuirks.contains(WebKit::WebsiteAutoplayQuirk::SynthesizedPauseEvents))
100         quirks |= _WKWebsiteAutoplayQuirkSynthesizedPauseEvents;
101
102     if (allowedQuirks.contains(WebKit::WebsiteAutoplayQuirk::ArbitraryUserGestures))
103         quirks |= _WKWebsiteAutoplayQuirkArbitraryUserGestures;
104
105     if (allowedQuirks.contains(WebKit::WebsiteAutoplayQuirk::PerDocumentAutoplayBehavior))
106         quirks |= _WKWebsiteAutoplayQuirkPerDocumentAutoplayBehavior;
107
108     return quirks;
109 }
110
111 - (void)setAutoplayPolicy:(_WKWebsiteAutoplayPolicy)policy
112 {
113     switch (policy) {
114     case _WKWebsiteAutoplayPolicyDefault:
115         _websitePolicies->setAutoplayPolicy(WebKit::WebsiteAutoplayPolicy::Default);
116         break;
117     case _WKWebsiteAutoplayPolicyAllow:
118         _websitePolicies->setAutoplayPolicy(WebKit::WebsiteAutoplayPolicy::Allow);
119         break;
120     case _WKWebsiteAutoplayPolicyAllowWithoutSound:
121         _websitePolicies->setAutoplayPolicy(WebKit::WebsiteAutoplayPolicy::AllowWithoutSound);
122         break;
123     case _WKWebsiteAutoplayPolicyDeny:
124         _websitePolicies->setAutoplayPolicy(WebKit::WebsiteAutoplayPolicy::Deny);
125         break;
126     }
127 }
128
129 - (_WKWebsiteAutoplayPolicy)autoplayPolicy
130 {
131     switch (_websitePolicies->autoplayPolicy()) {
132     case WebKit::WebsiteAutoplayPolicy::Default:
133         return _WKWebsiteAutoplayPolicyDefault;
134     case WebKit::WebsiteAutoplayPolicy::Allow:
135         return _WKWebsiteAutoplayPolicyAllow;
136     case WebKit::WebsiteAutoplayPolicy::AllowWithoutSound:
137         return _WKWebsiteAutoplayPolicyAllowWithoutSound;
138     case WebKit::WebsiteAutoplayPolicy::Deny:
139         return _WKWebsiteAutoplayPolicyDeny;
140     }
141 }
142
143 - (void)setPopUpPolicy:(_WKWebsitePopUpPolicy)policy
144 {
145     switch (policy) {
146     case _WKWebsitePopUpPolicyDefault:
147         _websitePolicies->setPopUpPolicy(WebKit::WebsitePopUpPolicy::Default);
148         break;
149     case _WKWebsitePopUpPolicyAllow:
150         _websitePolicies->setPopUpPolicy(WebKit::WebsitePopUpPolicy::Allow);
151         break;
152     case _WKWebsitePopUpPolicyBlock:
153         _websitePolicies->setPopUpPolicy(WebKit::WebsitePopUpPolicy::Block);
154         break;
155     }
156 }
157
158 - (_WKWebsitePopUpPolicy)popUpPolicy
159 {
160     switch (_websitePolicies->popUpPolicy()) {
161     case WebKit::WebsitePopUpPolicy::Default:
162         return _WKWebsitePopUpPolicyDefault;
163     case WebKit::WebsitePopUpPolicy::Allow:
164         return _WKWebsitePopUpPolicyAllow;
165     case WebKit::WebsitePopUpPolicy::Block:
166         return _WKWebsitePopUpPolicyBlock;
167     }
168 }
169
170 - (NSDictionary<NSString *, NSString *> *)customHeaderFields
171 {
172     const auto& fields = _websitePolicies->customHeaderFields();
173     NSMutableDictionary *dictionary = [[[NSMutableDictionary alloc] initWithCapacity:fields.size()] autorelease];
174     for (const auto& field : fields)
175         [dictionary setObject:field.value() forKey:field.name()];
176     return dictionary;
177 }
178
179 - (void)setCustomHeaderFields:(NSDictionary<NSString *, NSString *> *)fields
180 {
181     Vector<WebCore::HTTPHeaderField> parsedFields;
182     parsedFields.reserveInitialCapacity(fields.count);
183     
184     for (NSString* name in fields) {
185         auto field = WebCore::HTTPHeaderField::create(name, [fields objectForKey:name]);
186         if (field && startsWithLettersIgnoringASCIICase(field->name(), "x-"))
187             parsedFields.uncheckedAppend(WTFMove(*field));
188     }
189     _websitePolicies->setCustomHeaderFields(WTFMove(parsedFields));
190 }
191
192 - (WKWebsiteDataStore *)websiteDataStore
193 {
194     return wrapper(_websitePolicies->websiteDataStore());
195 }
196
197 - (void)setWebsiteDataStore:(WKWebsiteDataStore *)websiteDataStore
198 {
199     _websitePolicies->setWebsiteDataStore(websiteDataStore->_websiteDataStore.get());
200 }
201
202 - (void)setCustomUserAgent:(NSString *)customUserAgent
203 {
204     _websitePolicies->setCustomUserAgent(customUserAgent);
205 }
206
207 - (NSString *)customUserAgent
208 {
209     return _websitePolicies->customUserAgent();
210 }
211
212 - (void)setCustomJavaScriptUserAgentAsSiteSpecificQuirks:(NSString *)customUserAgent
213 {
214     _websitePolicies->setCustomJavaScriptUserAgentAsSiteSpecificQuirks(customUserAgent);
215 }
216
217 - (NSString *)customJavaScriptUserAgentAsSiteSpecificQuirks
218 {
219     return _websitePolicies->customJavaScriptUserAgentAsSiteSpecificQuirks();
220 }
221
222 - (void)setCustomNavigatorPlatform:(NSString *)customNavigatorPlatform
223 {
224     _websitePolicies->setCustomNavigatorPlatform(customNavigatorPlatform);
225 }
226
227 - (NSString *)customNavigatorPlatform
228 {
229     return _websitePolicies->customNavigatorPlatform();
230 }
231
232 - (NSString *)description
233 {
234     return [NSString stringWithFormat:@"<%@: %p; contentBlockersEnabled = %d>", NSStringFromClass(self.class), self, self.contentBlockersEnabled];
235 }
236
237 - (API::Object&)_apiObject
238 {
239     return *_websitePolicies;
240 }
241
242 @end
243
244 #endif