| 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 #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 Loading... |
| 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(); | 115 ~Whitelist(); |
| 116 | 116 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 const char* const feature_name; | 155 const char* const feature_name; |
| 156 | 156 |
| 157 // Controls whether the feature should be available in the platform by | 157 // Controls whether the feature should be available in the platform by |
| 158 // default, in the absence of any declared policy. | 158 // default, in the absence of any declared policy. |
| 159 FeatureDefault default_policy; | 159 FeatureDefault default_policy; |
| 160 }; | 160 }; |
| 161 | 161 |
| 162 using FeatureList = | 162 using FeatureList = |
| 163 std::map<blink::WebFeaturePolicyFeature, const FeaturePolicy::Feature*>; | 163 std::map<blink::WebFeaturePolicyFeature, const FeaturePolicy::Feature*>; |
| 164 | 164 |
| 165 ~FeaturePolicy(); | 165 ~FeaturePolicy() override; |
| 166 | 166 |
| 167 static std::unique_ptr<FeaturePolicy> CreateFromParentPolicy( | 167 static std::unique_ptr<FeaturePolicy> CreateFromParentPolicy( |
| 168 const FeaturePolicy* parent_policy, | 168 const FeaturePolicy* parent_policy, |
| 169 const ParsedFeaturePolicyHeader* container_policy, | 169 const ParsedFeaturePolicyHeader& container_policy, |
| 170 const url::Origin& origin); | 170 const url::Origin& origin); |
| 171 | 171 |
| 172 // Returns whether or not the given feature is enabled by this policy. | 172 // Returns whether or not the given feature is enabled by this policy. |
| 173 bool IsFeatureEnabledForOrigin(blink::WebFeaturePolicyFeature feature, | 173 bool IsFeatureEnabledForOrigin(blink::WebFeaturePolicyFeature feature, |
| 174 const url::Origin& origin) const; | 174 const url::Origin& origin) const; |
| 175 | 175 |
| 176 // Returns whether or not the given feature is enabled for the origin of the | 176 // Returns whether or not the given feature is enabled for the origin of the |
| 177 // document that owns the policy. | 177 // document that owns the policy. |
| 178 bool IsFeatureEnabled(blink::WebFeaturePolicyFeature feature) const; | 178 bool IsFeatureEnabled(blink::WebFeaturePolicyFeature feature) const; |
| 179 | 179 |
| 180 // Sets the declared policy from the parsed Feature-Policy HTTP header. | 180 // Sets the declared policy from the parsed Feature-Policy HTTP header. |
| 181 // Unrecognized features will be ignored. | 181 // Unrecognized features will be ignored. |
| 182 void SetHeaderPolicy(const ParsedFeaturePolicyHeader& parsed_header); | 182 void SetHeaderPolicy(const ParsedFeaturePolicyHeader& parsed_header); |
| 183 | 183 |
| 184 void SetOrigin(url::Origin origin) { origin_ = origin; } |
| 185 |
| 184 private: | 186 private: |
| 185 friend class FeaturePolicyTest; | 187 friend class FeaturePolicyTest; |
| 186 | 188 |
| 187 explicit FeaturePolicy(url::Origin origin); | 189 explicit FeaturePolicy(url::Origin origin); |
| 188 FeaturePolicy(url::Origin origin, const FeatureList& feature_list); | 190 FeaturePolicy(url::Origin origin, const FeatureList& feature_list); |
| 189 static std::unique_ptr<FeaturePolicy> CreateFromParentPolicy( | 191 static std::unique_ptr<FeaturePolicy> CreateFromParentPolicy( |
| 190 const FeaturePolicy* parent_policy, | 192 const FeaturePolicy* parent_policy, |
| 191 const ParsedFeaturePolicyHeader* container_policy, | 193 const ParsedFeaturePolicyHeader& container_policy, |
| 192 const url::Origin& origin, | 194 const url::Origin& origin, |
| 193 const FeatureList& features); | 195 const FeatureList& features); |
| 194 | 196 |
| 195 // Updates the inherited policy with the declarations from the iframe allow* | 197 // Updates the inherited policy with the declarations from the iframe allow* |
| 196 // attributes. | 198 // attributes. |
| 197 void AddContainerPolicy(const ParsedFeaturePolicyHeader* container_policy, | 199 void AddContainerPolicy(const ParsedFeaturePolicyHeader& container_policy, |
| 198 const FeaturePolicy* parent_policy); | 200 const FeaturePolicy* parent_policy); |
| 199 | 201 |
| 200 // Returns the list of features which can be controlled by Feature Policy. | 202 // Returns the list of features which can be controlled by Feature Policy. |
| 201 static const FeatureList& GetDefaultFeatureList(); | 203 static const FeatureList& GetDefaultFeatureList(); |
| 202 | 204 |
| 203 url::Origin origin_; | 205 url::Origin origin_; |
| 204 | 206 |
| 205 // Map of feature names to declared whitelists. Any feature which is missing | 207 // Map of feature names to declared whitelists. Any feature which is missing |
| 206 // from this map should use the inherited policy. | 208 // from this map should use the inherited policy. |
| 207 std::map<blink::WebFeaturePolicyFeature, std::unique_ptr<Whitelist>> | 209 std::map<blink::WebFeaturePolicyFeature, std::unique_ptr<Whitelist>> |
| 208 whitelists_; | 210 whitelists_; |
| 209 | 211 |
| 210 // Records whether or not each feature was enabled for this frame by its | 212 // Records whether or not each feature was enabled for this frame by its |
| 211 // parent frame. | 213 // parent frame. |
| 212 // TODO(iclelland): Generate, instead of this map, a set of bool flags, one | 214 // 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. | 215 // for each feature, as all features are supposed to be represented here. |
| 214 std::map<blink::WebFeaturePolicyFeature, bool> inherited_policies_; | 216 std::map<blink::WebFeaturePolicyFeature, bool> inherited_policies_; |
| 215 | 217 |
| 216 const FeatureList& feature_list_; | 218 const FeatureList& feature_list_; |
| 217 | 219 |
| 218 DISALLOW_COPY_AND_ASSIGN(FeaturePolicy); | 220 DISALLOW_COPY_AND_ASSIGN(FeaturePolicy); |
| 219 }; | 221 }; |
| 220 | 222 |
| 221 } // namespace content | 223 } // namespace content |
| 222 | 224 |
| 223 #endif // CONTENT_COMMON_FEATURE_POLICY_FEATURE_POLICY_H_ | 225 #endif // CONTENT_COMMON_FEATURE_POLICY_FEATURE_POLICY_H_ |
| OLD | NEW |