Add infrastructure for adding custom headers to requests per website
[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 @implementation _WKWebsitePolicies
32
33 - (void)dealloc
34 {
35     _websitePolicies->API::WebsitePolicies::~WebsitePolicies();
36     
37     [super dealloc];
38 }
39
40 - (instancetype)init
41 {
42     if (!(self = [super init]))
43         return nil;
44     
45     API::Object::constructInWrapper<API::WebsitePolicies>(self);
46     
47     return self;
48 }
49
50 - (void)setContentBlockersEnabled:(BOOL)contentBlockersEnabled
51 {
52     _websitePolicies->setContentBlockersEnabled(contentBlockersEnabled);
53 }
54
55 - (BOOL)contentBlockersEnabled
56 {
57     return _websitePolicies->contentBlockersEnabled();
58 }
59
60 - (void)setAllowedAutoplayQuirks:(_WKWebsiteAutoplayQuirk)allowedQuirks
61 {
62     OptionSet<WebKit::WebsiteAutoplayQuirk> quirks;
63
64     if (allowedQuirks & _WKWebsiteAutoplayQuirkInheritedUserGestures)
65         quirks |= WebKit::WebsiteAutoplayQuirk::InheritedUserGestures;
66
67     if (allowedQuirks & _WKWebsiteAutoplayQuirkSynthesizedPauseEvents)
68         quirks |= WebKit::WebsiteAutoplayQuirk::SynthesizedPauseEvents;
69
70     _websitePolicies->setAllowedAutoplayQuirks(quirks);
71 }
72
73 - (_WKWebsiteAutoplayQuirk)allowedAutoplayQuirks
74 {
75     _WKWebsiteAutoplayQuirk quirks = 0;
76     auto allowedQuirks = _websitePolicies->allowedAutoplayQuirks();
77
78     if (allowedQuirks.contains(WebKit::WebsiteAutoplayQuirk::InheritedUserGestures))
79         quirks |= _WKWebsiteAutoplayQuirkInheritedUserGestures;
80
81     if (allowedQuirks.contains(WebKit::WebsiteAutoplayQuirk::SynthesizedPauseEvents))
82         quirks |= _WKWebsiteAutoplayQuirkSynthesizedPauseEvents;
83
84     return quirks;
85 }
86
87 - (void)setAutoplayPolicy:(_WKWebsiteAutoplayPolicy)policy
88 {
89     switch (policy) {
90     case _WKWebsiteAutoplayPolicyDefault:
91         _websitePolicies->setAutoplayPolicy(WebKit::WebsiteAutoplayPolicy::Default);
92         break;
93     case _WKWebsiteAutoplayPolicyAllow:
94         _websitePolicies->setAutoplayPolicy(WebKit::WebsiteAutoplayPolicy::Allow);
95         break;
96     case _WKWebsiteAutoplayPolicyAllowWithoutSound:
97         _websitePolicies->setAutoplayPolicy(WebKit::WebsiteAutoplayPolicy::AllowWithoutSound);
98         break;
99     case _WKWebsiteAutoplayPolicyDeny:
100         _websitePolicies->setAutoplayPolicy(WebKit::WebsiteAutoplayPolicy::Deny);
101         break;
102     }
103 }
104
105 - (_WKWebsiteAutoplayPolicy)autoplayPolicy
106 {
107     switch (_websitePolicies->autoplayPolicy()) {
108     case WebKit::WebsiteAutoplayPolicy::Default:
109         return _WKWebsiteAutoplayPolicyDefault;
110     case WebKit::WebsiteAutoplayPolicy::Allow:
111         return _WKWebsiteAutoplayPolicyAllow;
112     case WebKit::WebsiteAutoplayPolicy::AllowWithoutSound:
113         return _WKWebsiteAutoplayPolicyAllowWithoutSound;
114     case WebKit::WebsiteAutoplayPolicy::Deny:
115         return _WKWebsiteAutoplayPolicyDeny;
116     }
117 }
118
119 - (NSArray<NSString *> *)customHeaderFields
120 {
121     const auto& fields = _websitePolicies->customHeaderFields();
122     NSMutableArray *array = [[[NSMutableArray alloc] initWithCapacity:fields.size()] autorelease];
123     for (const auto& field : fields)
124         [array addObject:field.field()];
125     return array;
126 }
127
128 - (void)setCustomHeaderFields:(NSArray<NSString *> *)fields
129 {
130     Vector<WebCore::HTTPHeaderField> parsedFields;
131     parsedFields.reserveInitialCapacity(fields.count);
132     for (NSString *string in fields) {
133         WebCore::HTTPHeaderField parsedField(string);
134         if (!parsedField.field().isNull()
135             && parsedField.field().startsWithIgnoringASCIICase("X-")) // Let's just pretend RFC6648 never happened.
136             parsedFields.uncheckedAppend(WTFMove(parsedField));
137     }
138     _websitePolicies->setCustomHeaderFields(WTFMove(parsedFields));
139 }
140
141 - (NSString *)description
142 {
143     return [NSString stringWithFormat:@"<%@: %p; contentBlockersEnabled = %d>", NSStringFromClass(self.class), self, self.contentBlockersEnabled];
144 }
145
146 - (API::Object&)_apiObject
147 {
148     return *_websitePolicies;
149 }
150
151 @end
152
153 #endif