Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(41)

Side by Side Diff: third_party/WebKit/Source/platform/feature_policy/FeaturePolicy.h

Issue 2656533004: Remove unused blink::FeaturePolicy class. (Closed)
Patch Set: Rebase Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef FeaturePolicy_h 5 #ifndef FeaturePolicy_h
6 #define FeaturePolicy_h 6 #define FeaturePolicy_h
7 7
8 #include "platform/PlatformExport.h" 8 #include "platform/PlatformExport.h"
9 #include "platform/weborigin/SecurityOrigin.h" 9 #include "platform/weborigin/SecurityOrigin.h"
10 #include "public/platform/WebFeaturePolicy.h" 10 #include "public/platform/WebFeaturePolicy.h"
11 #include "wtf/RefPtr.h" 11 #include "wtf/RefPtr.h"
12 #include "wtf/Vector.h" 12 #include "wtf/Vector.h"
13 #include "wtf/text/WTFString.h" 13 #include "wtf/text/WTFString.h"
14 14
15 #include <memory> 15 #include <memory>
16 16
17 namespace blink { 17 namespace blink {
18 18
19 // Feature Policy is a mechanism for controlling the availability of web 19 // Converts a JSON feature policy string into a vector of whitelists, one for
20 // platform features in a frame, including all embedded frames. It can be used 20 // each feature specified. Unrecognized features are parsed and included
21 // to remove features, automatically refuse API permission requests, or modify 21 // but will be filtered out when the policy is constructed. If |messages| is
22 // the behaviour of features. (The specific changes which are made depend on the 22 // not null, then any errors in the input will cause an error message to be
23 // feature; see the specification for details). 23 // appended to it.
24 // 24 PLATFORM_EXPORT WebParsedFeaturePolicyHeader
25 // Policies can be defined in the HTTP header stream, with the |Feature-Policy| 25 parseFeaturePolicy(const String& policy,
26 // HTTP header, or can be set by |enable| and |disable| attributes on the iframe 26 RefPtr<SecurityOrigin>,
27 // element which embeds the document. 27 Vector<String>* messages);
28 //
29 // See https://wicg.github.io/FeaturePolicy/
30 //
31 // Key concepts:
32 //
33 // Features
34 // --------
35 // Features which can be controlled by policy are defined as instances of the
36 // FeaturePoliicy::Feature struct. The features are referenced by pointer, so
37 // only a single instance of each feature should be defined. The features which
38 // are declared in the feature policy specification are all defined in
39 // |FeaturePolicy.cpp|.
40 //
41 // Whitelists
42 // ----------
43 // Policies are defined as a mapping of feaure names to whitelists. Whitelists
44 // are collections of origins, although two special terms can be used when
45 // declaring them:
46 // "self" refers to the orgin of the frame which is declaring the policy.
47 // "*" refers to all origins; any origin will match a whitelist which contains
48 // it.
49 //
50 // Defaults
51 // --------
52 // Each defined feature has a default policy, which determines whether the
53 // feature is available when no policy has been declared, ans determines how the
54 // feature is inherited across origin boundaries.
55 //
56 // If the default policy is in effect for a frame, then it controls how the
57 // feature is inherited by any cross-origin iframes embedded by the frame. (See
58 // the comments below in FeaturePolicy::DefaultPolicy for specifics)
59 //
60 // Policy Inheritance
61 // ------------------
62 // Policies in effect for a frame are inherited by any child frames it embeds.
63 // Unless another policy is declared in the child, all same-origin children will
64 // receive the same set of enables features as the parent frame. Whether or not
65 // features are inherited by cross-origin iframes without an explicit policy is
66 // determined by the feature's default policy. (Again, see the comments in
67 // FeaturePolicy::DefaultPolicy for details)
68
69 class PLATFORM_EXPORT FeaturePolicy final {
70 public:
71 // Represents a collection of origins which make up a whitelist in a feature
72 // policy. This collection may be set to match every origin (corresponding to
73 // the "*" syntax in the policy string, in which case the contains() method
74 // will always return true.
75 class Whitelist final {
76 public:
77 static std::unique_ptr<Whitelist> from(
78 const WebParsedFeaturePolicyDeclaration&);
79
80 Whitelist();
81
82 // Adds a single origin to the whitelist.
83 void add(RefPtr<SecurityOrigin>);
84
85 // Adds all origins to the whitelist.
86 void addAll();
87
88 // Returns true if the given origin has been added to the whitelist.
89 bool contains(const SecurityOrigin&) const;
90 String toString();
91
92 private:
93 bool m_matchesAllOrigins;
94 Vector<RefPtr<SecurityOrigin>> m_origins;
95 };
96
97 // The FeaturePolicy::FeatureDefault enum defines the default enable state for
98 // a feature when neither it nor any parent frame have declared an explicit
99 // policy. The three possibilities map directly to Feature Policy Whitelist
100 // semantics.
101 enum class FeatureDefault {
102 // Equivalent to []. If this default policy is in effect for a frame, then
103 // the feature will not be enabled for that frame or any of its children.
104 DisableForAll,
105
106 // Equivalent to ["self"]. If this default policy is in effect for a frame,
107 // then the feature will be enabled for that frame, and any same-origin
108 // child frames, but not for any cross-origin child frames.
109 EnableForSelf,
110
111 // Equivalent to ["*"]. If in effect for a frame, then the feature is
112 // enabled for that frame and all of its children.
113 EnableForAll
114 };
115
116 // The FeaturePolicy::Feature struct is used to define all features under
117 // control of Feature Policy. There should only be one instance of this struct
118 // for any given feature (declared below).
119 struct Feature {
120 // The name of the feature, as it should appear in a policy string
121 const char* const featureName;
122
123 // Controls whether the feature should be available in the platform by
124 // default, in the absence of any declared policy.
125 FeatureDefault defaultPolicy;
126 };
127
128 using FeatureList = const Vector<const FeaturePolicy::Feature*>;
129
130 // Converts a JSON feature policy string into a vector of whitelists, one for
131 // each feature specified. Unrecognized features are parsed and included
132 // but will be filtered out when the policy is constructed. If |messages| is
133 // not null, then any errors in the input will cause an error message to be
134 // appended to it.
135 static WebParsedFeaturePolicyHeader parseFeaturePolicy(
136 const String& policy,
137 RefPtr<SecurityOrigin>,
138 Vector<String>* messages);
139
140 static std::unique_ptr<FeaturePolicy> createFromParentPolicy(
141 const FeaturePolicy* parent,
142 RefPtr<SecurityOrigin>);
143
144 // Sets the declared policy from the parsed Feature-Policy HTTP header.
145 // Unrecognized features will be ignored.
146 void setHeaderPolicy(const WebParsedFeaturePolicyHeader&);
147
148 // Returns whether or not the given feature is enabled by this policy.
149 bool isFeatureEnabledForOrigin(const Feature&, const SecurityOrigin&) const;
150
151 // Returns whether or not the given feature is enabled for the frame that owns
152 // the policy.
153 bool isFeatureEnabled(const Feature&) const;
154
155 // Returns the list of features which can be controlled by Feature Policy.
156 static FeatureList& getDefaultFeatureList();
157
158 String toString();
159
160 private:
161 friend class FeaturePolicyTest;
162 friend class FeaturePolicyInFrameTest;
163
164 FeaturePolicy(RefPtr<SecurityOrigin>, FeatureList& features);
165
166 static std::unique_ptr<FeaturePolicy> createFromParentPolicy(
167 const FeaturePolicy* parent,
168 RefPtr<SecurityOrigin>,
169 FeatureList& features);
170
171 RefPtr<SecurityOrigin> m_origin;
172
173 // Records whether or not each feature was enabled for this frame by its
174 // parent frame.
175 // TODO(iclelland): Generate, instead of this map, a set of bool flags, one
176 // for each feature, as all features are supposed to be represented here.
177 HashMap<const Feature*, bool> m_inheritedFeatures;
178
179 // Map of feature names to declared whitelists. Any feature which is missing
180 // from this map should use the inherited policy.
181 HashMap<const Feature*, std::unique_ptr<Whitelist>> m_headerWhitelists;
182
183 // Contains the set of all features which can be controlled by this policy.
184 FeatureList& m_features;
185
186 DISALLOW_COPY_AND_ASSIGN(FeaturePolicy);
187 };
188
189 // Declarations for all features currently under control of the Feature Policy
190 // mechanism should be placed here.
191 extern const PLATFORM_EXPORT FeaturePolicy::Feature kDocumentCookie;
192 extern const PLATFORM_EXPORT FeaturePolicy::Feature kDocumentDomain;
193 extern const PLATFORM_EXPORT FeaturePolicy::Feature kDocumentWrite;
194 extern const PLATFORM_EXPORT FeaturePolicy::Feature kGeolocationFeature;
195 extern const PLATFORM_EXPORT FeaturePolicy::Feature kFullscreenFeature;
196 extern const PLATFORM_EXPORT FeaturePolicy::Feature kMidiFeature;
197 extern const PLATFORM_EXPORT FeaturePolicy::Feature kNotificationsFeature;
198 extern const PLATFORM_EXPORT FeaturePolicy::Feature kPaymentFeature;
199 extern const PLATFORM_EXPORT FeaturePolicy::Feature kPushFeature;
200 extern const PLATFORM_EXPORT FeaturePolicy::Feature kSyncScript;
201 extern const PLATFORM_EXPORT FeaturePolicy::Feature kSyncXHR;
202 extern const PLATFORM_EXPORT FeaturePolicy::Feature kUsermedia;
203 extern const PLATFORM_EXPORT FeaturePolicy::Feature kVibrateFeature;
204 extern const PLATFORM_EXPORT FeaturePolicy::Feature kWebRTC;
205 28
206 } // namespace blink 29 } // namespace blink
207 30
208 #endif // FeaturePolicy_h 31 #endif // FeaturePolicy_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698