OLD | NEW |
---|---|
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 #include "content/common/feature_policy/feature_policy.h" | 5 #include "content/common/feature_policy/feature_policy.h" |
6 | 6 |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 | 10 |
11 namespace content { | 11 namespace content { |
12 | 12 |
13 namespace { | |
14 | |
15 // Given a string name, return the matching feature struct, or nullptr if it is | |
16 // not the name of a policy-controlled feature. | |
17 blink::WebFeaturePolicyFeature FeatureForName( | |
18 const std::string& feature_name, | |
19 const FeaturePolicy::FeatureList& features) { | |
20 for (const auto& feature_mapping : features) { | |
21 if (feature_name == feature_mapping.second->feature_name) | |
22 return feature_mapping.first; | |
23 } | |
24 return blink::WebFeaturePolicyFeature::NotFound; | |
25 } | |
26 | |
27 // Definitions of all features controlled by Feature Policy should appear here. | 13 // Definitions of all features controlled by Feature Policy should appear here. |
28 const FeaturePolicy::Feature kDocumentCookie{ | 14 const FeaturePolicy::Feature kDocumentCookie{ |
iclelland
2017/03/06 13:55:59
Removing the namespace declaration moves all of th
| |
29 "cookie", FeaturePolicy::FeatureDefault::EnableForAll}; | 15 "cookie", FeaturePolicy::FeatureDefault::EnableForAll}; |
30 const FeaturePolicy::Feature kDocumentDomain{ | 16 const FeaturePolicy::Feature kDocumentDomain{ |
31 "domain", FeaturePolicy::FeatureDefault::EnableForAll}; | 17 "domain", FeaturePolicy::FeatureDefault::EnableForAll}; |
32 const FeaturePolicy::Feature kDocumentWrite{ | 18 const FeaturePolicy::Feature kDocumentWrite{ |
33 "docwrite", FeaturePolicy::FeatureDefault::EnableForAll}; | 19 "docwrite", FeaturePolicy::FeatureDefault::EnableForAll}; |
34 const FeaturePolicy::Feature kFullscreenFeature{ | 20 const FeaturePolicy::Feature kFullscreenFeature{ |
35 "fullscreen", FeaturePolicy::FeatureDefault::EnableForSelf}; | 21 "fullscreen", FeaturePolicy::FeatureDefault::EnableForSelf}; |
36 const FeaturePolicy::Feature kGeolocationFeature{ | 22 const FeaturePolicy::Feature kGeolocationFeature{ |
37 "geolocation", FeaturePolicy::FeatureDefault::EnableForSelf}; | 23 "geolocation", FeaturePolicy::FeatureDefault::EnableForSelf}; |
38 const FeaturePolicy::Feature kMidiFeature{ | 24 const FeaturePolicy::Feature kMidiFeature{ |
(...skipping 20 matching lines...) Expand all Loading... | |
59 const ParsedFeaturePolicyDeclaration& parsed_declaration) { | 45 const ParsedFeaturePolicyDeclaration& parsed_declaration) { |
60 std::unique_ptr<FeaturePolicy::Whitelist> result = | 46 std::unique_ptr<FeaturePolicy::Whitelist> result = |
61 base::WrapUnique(new FeaturePolicy::Whitelist()); | 47 base::WrapUnique(new FeaturePolicy::Whitelist()); |
62 if (parsed_declaration.matches_all_origins) | 48 if (parsed_declaration.matches_all_origins) |
63 result->AddAll(); | 49 result->AddAll(); |
64 for (const auto& origin : parsed_declaration.origins) | 50 for (const auto& origin : parsed_declaration.origins) |
65 result->Add(origin); | 51 result->Add(origin); |
66 return result; | 52 return result; |
67 } | 53 } |
68 | 54 |
69 } // namespace | |
70 | |
71 ParsedFeaturePolicyDeclaration::ParsedFeaturePolicyDeclaration() | 55 ParsedFeaturePolicyDeclaration::ParsedFeaturePolicyDeclaration() |
72 : matches_all_origins(false) {} | 56 : matches_all_origins(false) {} |
73 | 57 |
74 ParsedFeaturePolicyDeclaration::ParsedFeaturePolicyDeclaration( | 58 ParsedFeaturePolicyDeclaration::ParsedFeaturePolicyDeclaration( |
75 std::string feature_name, | 59 blink::WebFeaturePolicyFeature feature, |
76 bool matches_all_origins, | 60 bool matches_all_origins, |
77 std::vector<url::Origin> origins) | 61 std::vector<url::Origin> origins) |
78 : feature_name(feature_name), | 62 : feature(feature), |
79 matches_all_origins(matches_all_origins), | 63 matches_all_origins(matches_all_origins), |
80 origins(origins) {} | 64 origins(origins) {} |
81 | 65 |
82 ParsedFeaturePolicyDeclaration::ParsedFeaturePolicyDeclaration( | 66 ParsedFeaturePolicyDeclaration::ParsedFeaturePolicyDeclaration( |
83 const ParsedFeaturePolicyDeclaration& rhs) = default; | 67 const ParsedFeaturePolicyDeclaration& rhs) = default; |
84 | 68 |
85 ParsedFeaturePolicyDeclaration::~ParsedFeaturePolicyDeclaration() {} | 69 ParsedFeaturePolicyDeclaration::~ParsedFeaturePolicyDeclaration() {} |
86 | 70 |
87 FeaturePolicy::Whitelist::Whitelist() : matches_all_origins_(false) {} | 71 FeaturePolicy::Whitelist::Whitelist() : matches_all_origins_(false) {} |
88 | 72 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
165 return (&origin_ == &origin) || origin_.IsSameOriginWith(origin); | 149 return (&origin_ == &origin) || origin_.IsSameOriginWith(origin); |
166 } | 150 } |
167 return false; | 151 return false; |
168 } | 152 } |
169 | 153 |
170 void FeaturePolicy::SetHeaderPolicy( | 154 void FeaturePolicy::SetHeaderPolicy( |
171 const ParsedFeaturePolicyHeader& parsed_header) { | 155 const ParsedFeaturePolicyHeader& parsed_header) { |
172 DCHECK(whitelists_.empty()); | 156 DCHECK(whitelists_.empty()); |
173 for (const ParsedFeaturePolicyDeclaration& parsed_declaration : | 157 for (const ParsedFeaturePolicyDeclaration& parsed_declaration : |
174 parsed_header) { | 158 parsed_header) { |
175 blink::WebFeaturePolicyFeature feature = | 159 blink::WebFeaturePolicyFeature feature = parsed_declaration.feature; |
176 FeatureForName(parsed_declaration.feature_name, feature_list_); | |
177 if (feature == blink::WebFeaturePolicyFeature::NotFound) | 160 if (feature == blink::WebFeaturePolicyFeature::NotFound) |
178 continue; | 161 continue; |
179 whitelists_[feature] = WhitelistFromDeclaration(parsed_declaration); | 162 whitelists_[feature] = WhitelistFromDeclaration(parsed_declaration); |
180 } | 163 } |
181 } | 164 } |
182 | 165 |
183 FeaturePolicy::FeaturePolicy(url::Origin origin, | 166 FeaturePolicy::FeaturePolicy(url::Origin origin, |
184 const FeatureList& feature_list) | 167 const FeatureList& feature_list) |
185 : origin_(origin), feature_list_(feature_list) {} | 168 : origin_(origin), feature_list_(feature_list) {} |
186 | 169 |
(...skipping 25 matching lines...) Expand all Loading... | |
212 | 195 |
213 void FeaturePolicy::AddContainerPolicy( | 196 void FeaturePolicy::AddContainerPolicy( |
214 const ParsedFeaturePolicyHeader& container_policy, | 197 const ParsedFeaturePolicyHeader& container_policy, |
215 const FeaturePolicy* parent_policy) { | 198 const FeaturePolicy* parent_policy) { |
216 DCHECK(parent_policy); | 199 DCHECK(parent_policy); |
217 for (const ParsedFeaturePolicyDeclaration& parsed_declaration : | 200 for (const ParsedFeaturePolicyDeclaration& parsed_declaration : |
218 container_policy) { | 201 container_policy) { |
219 // If a feature is enabled in the parent frame, and the parent chooses to | 202 // If a feature is enabled in the parent frame, and the parent chooses to |
220 // delegate it to the child frame, using the iframe attribute, then the | 203 // delegate it to the child frame, using the iframe attribute, then the |
221 // feature should be enabled in the child frame. | 204 // feature should be enabled in the child frame. |
222 blink::WebFeaturePolicyFeature feature = | 205 blink::WebFeaturePolicyFeature feature = parsed_declaration.feature; |
223 FeatureForName(parsed_declaration.feature_name, feature_list_); | |
224 if (feature == blink::WebFeaturePolicyFeature::NotFound) | 206 if (feature == blink::WebFeaturePolicyFeature::NotFound) |
225 continue; | 207 continue; |
226 if (WhitelistFromDeclaration(parsed_declaration)->Contains(origin_) && | 208 if (WhitelistFromDeclaration(parsed_declaration)->Contains(origin_) && |
227 parent_policy->IsFeatureEnabled(feature)) { | 209 parent_policy->IsFeatureEnabled(feature)) { |
228 inherited_policies_[feature] = true; | 210 inherited_policies_[feature] = true; |
229 } else { | 211 } else { |
230 inherited_policies_[feature] = false; | 212 inherited_policies_[feature] = false; |
231 } | 213 } |
232 } | 214 } |
233 } | 215 } |
(...skipping 13 matching lines...) Expand all Loading... | |
247 {blink::WebFeaturePolicyFeature::Push, &kPushFeature}, | 229 {blink::WebFeaturePolicyFeature::Push, &kPushFeature}, |
248 {blink::WebFeaturePolicyFeature::SyncScript, &kSyncScript}, | 230 {blink::WebFeaturePolicyFeature::SyncScript, &kSyncScript}, |
249 {blink::WebFeaturePolicyFeature::SyncXHR, &kSyncXHR}, | 231 {blink::WebFeaturePolicyFeature::SyncXHR, &kSyncXHR}, |
250 {blink::WebFeaturePolicyFeature::Usermedia, &kUsermedia}, | 232 {blink::WebFeaturePolicyFeature::Usermedia, &kUsermedia}, |
251 {blink::WebFeaturePolicyFeature::Vibrate, &kVibrateFeature}, | 233 {blink::WebFeaturePolicyFeature::Vibrate, &kVibrateFeature}, |
252 {blink::WebFeaturePolicyFeature::WebRTC, &kWebRTC}})); | 234 {blink::WebFeaturePolicyFeature::WebRTC, &kWebRTC}})); |
253 return default_feature_list; | 235 return default_feature_list; |
254 } | 236 } |
255 | 237 |
256 } // namespace content | 238 } // namespace content |
OLD | NEW |