Add SPI to set a list of hosts to which to send custom header fields cross-origin
[WebKit-https.git] / Source / WebKit / UIProcess / API / APIWebsitePolicies.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 "APIWebsitePolicies.h"
28
29 #include "APIWebsiteDataStore.h"
30 #include "WebsitePoliciesData.h"
31
32 namespace API {
33
34 WebsitePolicies::WebsitePolicies() = default;
35
36 WebsitePolicies::WebsitePolicies(bool contentBlockersEnabled, OptionSet<WebKit::WebsiteAutoplayQuirk> allowedAutoplayQuirks, WebKit::WebsiteAutoplayPolicy autoplayPolicy, Vector<WebCore::HTTPHeaderField>&& legacyCustomHeaderFields, Vector<WebCore::CustomHeaderFields>&& customHeaderFields, WebKit::WebsitePopUpPolicy popUpPolicy, RefPtr<WebsiteDataStore>&& websiteDataStore)
37     : m_contentBlockersEnabled(contentBlockersEnabled)
38     , m_allowedAutoplayQuirks(allowedAutoplayQuirks)
39     , m_autoplayPolicy(autoplayPolicy)
40     , m_legacyCustomHeaderFields(WTFMove(legacyCustomHeaderFields))
41     , m_customHeaderFields(WTFMove(customHeaderFields))
42     , m_popUpPolicy(popUpPolicy)
43     , m_websiteDataStore(WTFMove(websiteDataStore))
44 { }
45
46 Ref<WebsitePolicies> WebsitePolicies::copy() const
47 {
48     auto policies = WebsitePolicies::create();
49     policies->setContentBlockersEnabled(m_contentBlockersEnabled);
50     policies->setAllowedAutoplayQuirks(m_allowedAutoplayQuirks);
51     policies->setAutoplayPolicy(m_autoplayPolicy);
52 #if ENABLE(DEVICE_ORIENTATION)
53     policies->setDeviceOrientationAndMotionAccessState(m_deviceOrientationAndMotionAccessState);
54 #endif
55     policies->setPopUpPolicy(m_popUpPolicy);
56     policies->setWebsiteDataStore(m_websiteDataStore.get());
57     policies->setCustomUserAgent(m_customUserAgent);
58     policies->setCustomJavaScriptUserAgentAsSiteSpecificQuirks(m_customJavaScriptUserAgentAsSiteSpecificQuirks);
59     policies->setCustomNavigatorPlatform(m_customNavigatorPlatform);
60     policies->setPreferredCompatibilityMode(m_preferredCompatibilityMode);
61     policies->setMetaViewportPolicy(m_metaViewportPolicy);
62     policies->setMediaSourcePolicy(m_mediaSourcePolicy);
63     policies->setSimulatedMouseEventsDispatchPolicy(m_simulatedMouseEventsDispatchPolicy);
64     
65     Vector<WebCore::HTTPHeaderField> legacyCustomHeaderFields;
66     legacyCustomHeaderFields.reserveInitialCapacity(m_legacyCustomHeaderFields.size());
67     for (auto& field : m_legacyCustomHeaderFields)
68         legacyCustomHeaderFields.uncheckedAppend(field);
69     policies->setLegacyCustomHeaderFields(WTFMove(legacyCustomHeaderFields));
70
71     Vector<WebCore::CustomHeaderFields> customHeaderFields;
72     customHeaderFields.reserveInitialCapacity(m_customHeaderFields.size());
73     for (auto& field : m_customHeaderFields)
74         customHeaderFields.uncheckedAppend(field);
75     policies->setCustomHeaderFields(WTFMove(customHeaderFields));
76     policies->setAllowSiteSpecificQuirksToOverrideCompatibilityMode(m_allowSiteSpecificQuirksToOverrideCompatibilityMode);
77     return policies;
78 }
79
80 WebsitePolicies::~WebsitePolicies()
81 {
82 }
83
84 void WebsitePolicies::setWebsiteDataStore(RefPtr<WebsiteDataStore>&& websiteDataStore)
85 {
86     m_websiteDataStore = WTFMove(websiteDataStore);
87 }
88
89 WebKit::WebsitePoliciesData WebsitePolicies::data()
90 {
91     bool hasLegacyCustomHeaderFields = legacyCustomHeaderFields().size();
92     Vector<WebCore::CustomHeaderFields> customHeaderFields;
93     customHeaderFields.reserveInitialCapacity(this->customHeaderFields().size() + hasLegacyCustomHeaderFields);
94     for (auto& field : this->customHeaderFields())
95         customHeaderFields.uncheckedAppend(field);
96     if (hasLegacyCustomHeaderFields)
97         customHeaderFields.uncheckedAppend({ legacyCustomHeaderFields(), { }});
98
99     return {
100         contentBlockersEnabled(),
101         allowedAutoplayQuirks(),
102         autoplayPolicy(),
103 #if ENABLE(DEVICE_ORIENTATION)
104         deviceOrientationAndMotionAccessState(),
105 #endif
106         WTFMove(customHeaderFields),
107         popUpPolicy(),
108         m_websiteDataStore ? Optional<WebKit::WebsiteDataStoreParameters> { m_websiteDataStore->websiteDataStore().parameters() } : WTF::nullopt,
109         m_customUserAgent,
110         m_customJavaScriptUserAgentAsSiteSpecificQuirks,
111         m_customNavigatorPlatform,
112         m_metaViewportPolicy,
113         m_mediaSourcePolicy,
114         m_simulatedMouseEventsDispatchPolicy,
115     };
116 }
117
118 }
119