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
32namespace API {
33
34WebsitePolicies::WebsitePolicies() = default;
35
36WebsitePolicies::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
46Ref<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->setPreferredContentMode(m_preferredContentMode);
61 policies->setMetaViewportPolicy(m_metaViewportPolicy);
62 policies->setMediaSourcePolicy(m_mediaSourcePolicy);
63 policies->setSimulatedMouseEventsDispatchPolicy(m_simulatedMouseEventsDispatchPolicy);
64 policies->setLegacyOverflowScrollingTouchPolicy(m_legacyOverflowScrollingTouchPolicy);
65
66 Vector<WebCore::HTTPHeaderField> legacyCustomHeaderFields;
67 legacyCustomHeaderFields.reserveInitialCapacity(m_legacyCustomHeaderFields.size());
68 for (auto& field : m_legacyCustomHeaderFields)
69 legacyCustomHeaderFields.uncheckedAppend(field);
70 policies->setLegacyCustomHeaderFields(WTFMove(legacyCustomHeaderFields));
71
72 Vector<WebCore::CustomHeaderFields> customHeaderFields;
73 customHeaderFields.reserveInitialCapacity(m_customHeaderFields.size());
74 for (auto& field : m_customHeaderFields)
75 customHeaderFields.uncheckedAppend(field);
76 policies->setCustomHeaderFields(WTFMove(customHeaderFields));
77 policies->setAllowSiteSpecificQuirksToOverrideContentMode(m_allowSiteSpecificQuirksToOverrideContentMode);
78 policies->setApplicationNameForDesktopUserAgent(m_applicationNameForDesktopUserAgent);
79 return policies;
80}
81
82WebsitePolicies::~WebsitePolicies()
83{
84}
85
86void WebsitePolicies::setWebsiteDataStore(RefPtr<WebsiteDataStore>&& websiteDataStore)
87{
88 m_websiteDataStore = WTFMove(websiteDataStore);
89}
90
91WebKit::WebsitePoliciesData WebsitePolicies::data()
92{
93 bool hasLegacyCustomHeaderFields = legacyCustomHeaderFields().size();
94 Vector<WebCore::CustomHeaderFields> customHeaderFields;
95 customHeaderFields.reserveInitialCapacity(this->customHeaderFields().size() + hasLegacyCustomHeaderFields);
96 for (auto& field : this->customHeaderFields())
97 customHeaderFields.uncheckedAppend(field);
98 if (hasLegacyCustomHeaderFields)
99 customHeaderFields.uncheckedAppend({ legacyCustomHeaderFields(), { }});
100
101 return {
102 contentBlockersEnabled(),
103 allowedAutoplayQuirks(),
104 autoplayPolicy(),
105#if ENABLE(DEVICE_ORIENTATION)
106 deviceOrientationAndMotionAccessState(),
107#endif
108 WTFMove(customHeaderFields),
109 popUpPolicy(),
110 m_websiteDataStore ? Optional<WebKit::WebsiteDataStoreParameters> { m_websiteDataStore->websiteDataStore().parameters() } : WTF::nullopt,
111 m_customUserAgent,
112 m_customJavaScriptUserAgentAsSiteSpecificQuirks,
113 m_customNavigatorPlatform,
114 m_metaViewportPolicy,
115 m_mediaSourcePolicy,
116 m_simulatedMouseEventsDispatchPolicy,
117 m_legacyOverflowScrollingTouchPolicy,
118 };
119}
120
121}
122
123