Chromium Code Reviews| Index: third_party/WebKit/Source/platform/feature_policy/FeaturePolicy.h |
| diff --git a/third_party/WebKit/Source/platform/feature_policy/FeaturePolicy.h b/third_party/WebKit/Source/platform/feature_policy/FeaturePolicy.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4dc5c4aa3b143a05e078d853fd5eac4827c97b1c |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/platform/feature_policy/FeaturePolicy.h |
| @@ -0,0 +1,143 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef FeaturePolicy_h |
| +#define FeaturePolicy_h |
| + |
| +#include "platform/PlatformExport.h" |
| +#include "platform/weborigin/SecurityOrigin.h" |
| +#include "wtf/RefPtr.h" |
| +#include "wtf/Vector.h" |
| +#include "wtf/text/WTFString.h" |
| + |
| +#include <memory> |
| + |
| +namespace blink { |
| + |
| +// The FeaturePolicyFeatureDefault enum defines the default enable state for a |
| +// feature when neither it nor any parent frame have declared an explicit |
| +// policy. The three possibilities map directly to Feature Policy Whitelist |
| +// semantics. |
| +enum class FeaturePolicyFeatureDefault { |
| + // Equivalent to []. The feature is never available by default, and can only |
| + // be enabled by an explicit policy. |
| + DisableForAll, |
| + |
| + // Equivalent to ["self"]. The feature is enabled for top-level frames, and |
| + // their same-origin children. It must be explicitly delegated to cross-origin |
| + // child frames. |
|
raymes
2016/10/23 23:31:05
Preface: Since this is just comments I don't feel
iclelland
2016/10/24 05:14:32
Thanks, I'll incorporate that -- I think it may ma
|
| + EnableForSelf, |
| + |
| + // Equivalent to ["*"]. The feature is enabled by default for all frames. |
| + EnableForAll |
| +}; |
| + |
| +// The FeaturePolicyFeature struct is used to define all features under control |
| +// of Feature Policy. There should only be one instance of this struct for any |
| +// given feature (declared below). |
| +struct FeaturePolicyFeature { |
|
raymes
2016/10/23 23:31:05
nit (optional): if we move the enum above and this
iclelland
2016/10/24 05:14:32
Done.
|
| + // The name of the feature, as it should appear in a policy string |
| + const char* featureName; |
| + |
| + // Controls whether the feature should be available in the platform by |
| + // default, in the absence of any declared policy. |
| + FeaturePolicyFeatureDefault defaultPolicy; |
| +}; |
| + |
| +// Declarations for all features currently under control of the Feature Policy |
| +// mechanism should be placed here. |
| +extern const PLATFORM_EXPORT FeaturePolicyFeature kDocumentCookie; |
| +extern const PLATFORM_EXPORT FeaturePolicyFeature kDocumentDomain; |
| +extern const PLATFORM_EXPORT FeaturePolicyFeature kDocumentWrite; |
| +extern const PLATFORM_EXPORT FeaturePolicyFeature kGeolocationFeature; |
| +extern const PLATFORM_EXPORT FeaturePolicyFeature kMidiFeature; |
| +extern const PLATFORM_EXPORT FeaturePolicyFeature kNotificationsFeature; |
| +extern const PLATFORM_EXPORT FeaturePolicyFeature kPaymentFeature; |
| +extern const PLATFORM_EXPORT FeaturePolicyFeature kPushFeature; |
| +extern const PLATFORM_EXPORT FeaturePolicyFeature kSyncScript; |
| +extern const PLATFORM_EXPORT FeaturePolicyFeature kSyncXHR; |
| +extern const PLATFORM_EXPORT FeaturePolicyFeature kUsermedia; |
| +extern const PLATFORM_EXPORT FeaturePolicyFeature kVibrateFeature; |
| +extern const PLATFORM_EXPORT FeaturePolicyFeature kWebRTC; |
| + |
| +using FeatureList = const Vector<const FeaturePolicyFeature*>; |
| + |
| +class PLATFORM_EXPORT FeaturePolicy final { |
| + public: |
| + // Represents a collection of origins which make up a whitelist in a feature |
| + // policy. This collection may be set to match every origin (corresponding to |
| + // the "*" syntax in the policy string, in which case the contains() method |
| + // will always return true. |
| + class Whitelist final { |
| + public: |
| + Whitelist(); |
| + |
| + // Adds a single origin to the whitelist. |
| + void add(RefPtr<SecurityOrigin>); |
| + |
| + // Adds all origins to the whitelist. |
| + void addAll(); |
| + |
| + // Returns true if the given origin has been added to the whitelist. |
| + bool contains(const SecurityOrigin&) const; |
| + String toString(); |
| + |
| + private: |
| + bool m_matchesAllOrigins; |
| + Vector<RefPtr<SecurityOrigin>> m_origins; |
| + }; |
| + |
| + static FeaturePolicy* createFromParentPolicy(const FeaturePolicy* parent, |
| + RefPtr<SecurityOrigin>, |
| + FeatureList& features); |
|
raymes
2016/10/23 23:31:05
Is this just for testing? It might be better to ma
iclelland
2016/10/24 05:14:32
Yes, this interface is just for testing, although
|
| + |
| + static FeaturePolicy* createFromParentPolicy(const FeaturePolicy* parent, |
| + RefPtr<SecurityOrigin>); |
| + |
| + // Sets the declared policy from the Feature-Policy HTTP header. If the header |
| + // cannot be parsed, errors will be appended to the |messages| vector. |
| + void setHeaderPolicy(const String&, Vector<String>& messages); |
| + |
| + // Returns whether or not the given feature is enabled by this policy. |
| + bool isFeatureEnabledForOrigin(const FeaturePolicyFeature*, |
| + const SecurityOrigin&) const; |
| + |
| + // Returns whether or not the given feature is enabled for the frame that owns |
| + // the policy. |
| + bool isFeatureEnabled(const FeaturePolicyFeature*) const; |
| + |
| + // Returns the list of features which can be controlled by Feature Policy. |
| + static FeatureList& getDefaultFeatureList(); |
| + |
| + String toString(); |
| + |
| + private: |
| + FeaturePolicy(RefPtr<SecurityOrigin>, FeatureList& features); |
| + |
| + // Parses a policy string into a set of whitelists for features. |
| + HashMap<const FeaturePolicyFeature*, std::unique_ptr<Whitelist>> parse( |
| + const String&); |
| + |
| + RefPtr<SecurityOrigin> m_origin; |
| + |
| + // Records whether or not each feature was enabled for this frame by its |
| + // parent frame. |
| + // TODO(iclelland): Generate, instead of this map, a set of bool flags, one |
| + // for each feature, as all features are supposed to be represented here. |
| + HashMap<const FeaturePolicyFeature*, bool> m_inheritedFeatures; |
| + |
| + // Map of feature names to declared whitelists. Any feature which is missing |
| + // from this map should use the inherited policy. |
| + HashMap<const FeaturePolicyFeature*, std::unique_ptr<Whitelist>> |
| + m_headerWhitelists; |
| + |
| + // Contains the set of all features which can be controlled by this policy. |
| + FeatureList& m_features; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(FeaturePolicy); |
| +}; |
| + |
| +} // namespace blink |
| + |
| +#endif // FeaturePolicy_h |