Check for navigation to app-bound domain during script evaluation
[WebKit-https.git] / Source / WebKit / Shared / PolicyDecision.h
1 /*
2  * Copyright (C) 2020 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 #pragma once
27
28 #include "DownloadID.h"
29 #include "WebsitePoliciesData.h"
30 #include <wtf/Forward.h>
31
32 namespace WebKit {
33
34 enum class NavigatingToAppBoundDomain { Yes, No };
35 enum class NavigatedAwayFromAppBoundDomain { Yes, No};
36
37 struct PolicyDecision {
38     WebCore::PolicyCheckIdentifier identifier { };
39     NavigatingToAppBoundDomain isNavigatingToAppBoundDomain { NavigatingToAppBoundDomain::No };
40     NavigatedAwayFromAppBoundDomain hasNavigatedAwayFromAppBoundDomain { NavigatedAwayFromAppBoundDomain::No };
41     WebCore::PolicyAction policyAction { WebCore::PolicyAction::Ignore };
42     uint64_t navigationID { 0 };
43     DownloadID downloadID { 0 };
44     Optional<WebsitePoliciesData> websitePoliciesData { WTF::nullopt };
45
46     template<class Encoder>
47     void encode(Encoder& encoder) const
48     {
49         encoder << identifier;
50         encoder << isNavigatingToAppBoundDomain;
51         encoder << hasNavigatedAwayFromAppBoundDomain;
52         encoder << policyAction;
53         encoder << navigationID;
54         encoder << downloadID;
55         encoder << websitePoliciesData;
56     }
57
58     template<class Decoder>
59     static Optional<PolicyDecision> decode(Decoder& decoder)
60     {
61         Optional<WebCore::PolicyCheckIdentifier> decodedIdentifier;
62         decoder >> decodedIdentifier;
63         if (!decodedIdentifier)
64             return WTF::nullopt;
65         
66         Optional<NavigatingToAppBoundDomain> decodedIsNavigatingToAppBoundDomain;
67         decoder >> decodedIsNavigatingToAppBoundDomain;
68         if (!decodedIsNavigatingToAppBoundDomain)
69             return WTF::nullopt;
70         
71         Optional<NavigatedAwayFromAppBoundDomain> decodedHasNavigatedAwayFromAppBoundDomain;
72         decoder >> decodedHasNavigatedAwayFromAppBoundDomain;
73         if (!decodedHasNavigatedAwayFromAppBoundDomain)
74             return WTF::nullopt;
75
76         Optional<WebCore::PolicyAction> decodedPolicyAction;
77         decoder >> decodedPolicyAction;
78         if (!decodedPolicyAction)
79             return WTF::nullopt;
80
81         Optional<uint64_t> decodedNavigationID;
82         decoder >> decodedNavigationID;
83         if (!decodedNavigationID)
84             return WTF::nullopt;
85
86         Optional<DownloadID> decodedDownloadID;
87         decoder >> decodedDownloadID;
88         if (!decodedDownloadID)
89             return WTF::nullopt;
90
91         Optional<Optional<WebsitePoliciesData>> decodedWebsitePoliciesData;
92         decoder >> decodedWebsitePoliciesData;
93         if (!decodedWebsitePoliciesData)
94             return WTF::nullopt;
95
96         return {{ WTFMove(*decodedIdentifier), WTFMove(*decodedIsNavigatingToAppBoundDomain), WTFMove(*decodedHasNavigatedAwayFromAppBoundDomain), WTFMove(*decodedPolicyAction), WTFMove(*decodedNavigationID), WTFMove(*decodedDownloadID), WTFMove(*decodedWebsitePoliciesData) }};
97     }
98 };
99
100 } // namespace WebKit
101
102 namespace WTF {
103
104 template<> struct EnumTraits<WebKit::NavigatingToAppBoundDomain> {
105     using values = EnumValues<
106         WebKit::NavigatingToAppBoundDomain,
107         WebKit::NavigatingToAppBoundDomain::Yes,
108         WebKit::NavigatingToAppBoundDomain::No
109     >;
110 };
111
112 template<> struct EnumTraits<WebKit::NavigatedAwayFromAppBoundDomain> {
113     using values = EnumValues<
114         WebKit::NavigatedAwayFromAppBoundDomain,
115         WebKit::NavigatedAwayFromAppBoundDomain::Yes,
116         WebKit::NavigatedAwayFromAppBoundDomain::No
117     >;
118 };
119
120 }