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

Side by Side Diff: content/common/feature_policy/feature_policy.h

Issue 2651883008: Make content::FeaturePolicy implement WebFeaturePolicy, and use it in blink code (Closed)
Patch Set: Duplicate FP object rather than modifying in-place Created 3 years, 9 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 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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 CONTENT_COMMON_FEATURE_POLICY_FEATURE_POLICY_H_ 5 #ifndef CONTENT_COMMON_FEATURE_POLICY_FEATURE_POLICY_H_
6 #define CONTENT_COMMON_FEATURE_POLICY_FEATURE_POLICY_H_ 6 #define CONTENT_COMMON_FEATURE_POLICY_FEATURE_POLICY_H_
7 7
8 #include <map> 8 #include <map>
9 #include <memory> 9 #include <memory>
10 #include <string> 10 #include <string>
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 ParsedFeaturePolicyDeclaration(const ParsedFeaturePolicyDeclaration& rhs); 96 ParsedFeaturePolicyDeclaration(const ParsedFeaturePolicyDeclaration& rhs);
97 ~ParsedFeaturePolicyDeclaration(); 97 ~ParsedFeaturePolicyDeclaration();
98 98
99 std::string feature_name; 99 std::string feature_name;
100 bool matches_all_origins; 100 bool matches_all_origins;
101 std::vector<url::Origin> origins; 101 std::vector<url::Origin> origins;
102 }; 102 };
103 103
104 using ParsedFeaturePolicyHeader = std::vector<ParsedFeaturePolicyDeclaration>; 104 using ParsedFeaturePolicyHeader = std::vector<ParsedFeaturePolicyDeclaration>;
105 105
106 class CONTENT_EXPORT FeaturePolicy { 106 class CONTENT_EXPORT FeaturePolicy : public blink::WebFeaturePolicy {
107 public: 107 public:
108 // Represents a collection of origins which make up a whitelist in a feature 108 // Represents a collection of origins which make up a whitelist in a feature
109 // policy. This collection may be set to match every origin (corresponding to 109 // policy. This collection may be set to match every origin (corresponding to
110 // the "*" syntax in the policy string, in which case the Contains() method 110 // the "*" syntax in the policy string, in which case the Contains() method
111 // will always return true. 111 // will always return true.
112 class Whitelist final { 112 class Whitelist final {
113 public: 113 public:
114 Whitelist(); 114 Whitelist();
115 Whitelist(const Whitelist& rhs);
115 ~Whitelist(); 116 ~Whitelist();
116 117
117 // Adds a single origin to the whitelist. 118 // Adds a single origin to the whitelist.
118 void Add(const url::Origin& origin); 119 void Add(const url::Origin& origin);
119 120
120 // Adds all origins to the whitelist. 121 // Adds all origins to the whitelist.
121 void AddAll(); 122 void AddAll();
122 123
123 // Returns true if the given origin has been added to the whitelist. 124 // Returns true if the given origin has been added to the whitelist.
124 bool Contains(const url::Origin& origin) const; 125 bool Contains(const url::Origin& origin) const;
(...skipping 30 matching lines...) Expand all
155 const char* const feature_name; 156 const char* const feature_name;
156 157
157 // Controls whether the feature should be available in the platform by 158 // Controls whether the feature should be available in the platform by
158 // default, in the absence of any declared policy. 159 // default, in the absence of any declared policy.
159 FeatureDefault default_policy; 160 FeatureDefault default_policy;
160 }; 161 };
161 162
162 using FeatureList = 163 using FeatureList =
163 std::map<blink::WebFeaturePolicyFeature, const FeaturePolicy::Feature*>; 164 std::map<blink::WebFeaturePolicyFeature, const FeaturePolicy::Feature*>;
164 165
165 ~FeaturePolicy(); 166 ~FeaturePolicy() override;
166 167
167 static std::unique_ptr<FeaturePolicy> CreateFromParentPolicy( 168 static std::unique_ptr<FeaturePolicy> CreateFromParentPolicy(
168 const FeaturePolicy* parent_policy, 169 const FeaturePolicy* parent_policy,
169 const ParsedFeaturePolicyHeader* container_policy, 170 const ParsedFeaturePolicyHeader& container_policy,
170 const url::Origin& origin); 171 const url::Origin& origin);
171 172
172 // Returns whether or not the given feature is enabled by this policy. 173 static std::unique_ptr<FeaturePolicy> CreateFromPolicyWithOrigin(
174 const FeaturePolicy& policy,
175 const url::Origin& origin);
176
177 // WebFeaturePolicy implementation
178 bool IsFeatureEnabled(blink::WebFeaturePolicyFeature feature) const override;
179
180 // Returns whether or not the given feature is enabled by this policy for a
181 // specific origin.
173 bool IsFeatureEnabledForOrigin(blink::WebFeaturePolicyFeature feature, 182 bool IsFeatureEnabledForOrigin(blink::WebFeaturePolicyFeature feature,
174 const url::Origin& origin) const; 183 const url::Origin& origin) const;
175 184
176 // Returns whether or not the given feature is enabled for the origin of the
177 // document that owns the policy.
178 bool IsFeatureEnabled(blink::WebFeaturePolicyFeature feature) const;
179
180 // Sets the declared policy from the parsed Feature-Policy HTTP header. 185 // Sets the declared policy from the parsed Feature-Policy HTTP header.
181 // Unrecognized features will be ignored. 186 // Unrecognized features will be ignored.
182 void SetHeaderPolicy(const ParsedFeaturePolicyHeader& parsed_header); 187 void SetHeaderPolicy(const ParsedFeaturePolicyHeader& parsed_header);
183 188
184 private: 189 private:
185 friend class FeaturePolicyTest; 190 friend class FeaturePolicyTest;
186 191
187 explicit FeaturePolicy(url::Origin origin); 192 explicit FeaturePolicy(url::Origin origin);
188 FeaturePolicy(url::Origin origin, const FeatureList& feature_list); 193 FeaturePolicy(url::Origin origin, const FeatureList& feature_list);
189 static std::unique_ptr<FeaturePolicy> CreateFromParentPolicy( 194 static std::unique_ptr<FeaturePolicy> CreateFromParentPolicy(
190 const FeaturePolicy* parent_policy, 195 const FeaturePolicy* parent_policy,
191 const ParsedFeaturePolicyHeader* container_policy, 196 const ParsedFeaturePolicyHeader& container_policy,
192 const url::Origin& origin, 197 const url::Origin& origin,
193 const FeatureList& features); 198 const FeatureList& features);
194 199
195 // Updates the inherited policy with the declarations from the iframe allow* 200 // Updates the inherited policy with the declarations from the iframe allow*
196 // attributes. 201 // attributes.
197 void AddContainerPolicy(const ParsedFeaturePolicyHeader* container_policy, 202 void AddContainerPolicy(const ParsedFeaturePolicyHeader& container_policy,
198 const FeaturePolicy* parent_policy); 203 const FeaturePolicy* parent_policy);
199 204
200 // Returns the list of features which can be controlled by Feature Policy. 205 // Returns the list of features which can be controlled by Feature Policy.
201 static const FeatureList& GetDefaultFeatureList(); 206 static const FeatureList& GetDefaultFeatureList();
202 207
203 url::Origin origin_; 208 url::Origin origin_;
204 209
205 // Map of feature names to declared whitelists. Any feature which is missing 210 // Map of feature names to declared whitelists. Any feature which is missing
206 // from this map should use the inherited policy. 211 // from this map should use the inherited policy.
207 std::map<blink::WebFeaturePolicyFeature, std::unique_ptr<Whitelist>> 212 std::map<blink::WebFeaturePolicyFeature, std::unique_ptr<Whitelist>>
208 whitelists_; 213 whitelists_;
209 214
210 // Records whether or not each feature was enabled for this frame by its 215 // Records whether or not each feature was enabled for this frame by its
211 // parent frame. 216 // parent frame.
212 // TODO(iclelland): Generate, instead of this map, a set of bool flags, one 217 // TODO(iclelland): Generate, instead of this map, a set of bool flags, one
213 // for each feature, as all features are supposed to be represented here. 218 // for each feature, as all features are supposed to be represented here.
214 std::map<blink::WebFeaturePolicyFeature, bool> inherited_policies_; 219 std::map<blink::WebFeaturePolicyFeature, bool> inherited_policies_;
215 220
216 const FeatureList& feature_list_; 221 const FeatureList& feature_list_;
217 222
218 DISALLOW_COPY_AND_ASSIGN(FeaturePolicy); 223 DISALLOW_COPY_AND_ASSIGN(FeaturePolicy);
219 }; 224 };
220 225
221 } // namespace content 226 } // namespace content
222 227
223 #endif // CONTENT_COMMON_FEATURE_POLICY_FEATURE_POLICY_H_ 228 #endif // CONTENT_COMMON_FEATURE_POLICY_FEATURE_POLICY_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698