FeaturePolicyUtils.h (2760B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_dom_FeaturePolicyUtils_h 8 #define mozilla_dom_FeaturePolicyUtils_h 9 10 #include <functional> 11 12 #include "mozilla/dom/FeaturePolicy.h" 13 14 class PickleIterator; 15 16 namespace IPC { 17 class Message; 18 class MessageReader; 19 class MessageWriter; 20 } // namespace IPC 21 22 namespace mozilla { 23 namespace dom { 24 25 class Document; 26 27 class FeaturePolicyUtils final { 28 public: 29 enum FeaturePolicyValue { 30 // Feature always allowed. 31 eAll, 32 33 // Feature allowed for documents that are same-origin with this one. 34 eSelf, 35 36 // Feature denied. 37 eNone, 38 }; 39 40 // This method returns true if aFeatureName is allowed for aDocument. 41 // Use this method everywhere you need to check feature-policy directives. 42 static bool IsFeatureAllowed(Document* aDocument, 43 const nsAString& aFeatureName); 44 45 // Returns true if aFeatureName is a known feature policy name. 46 static bool IsSupportedFeature(const nsAString& aFeatureName); 47 48 // Returns true if aFeatureName is a experimental feature policy name. 49 static bool IsExperimentalFeature(const nsAString& aFeatureName); 50 51 // Runs aCallback for each known feature policy, with the feature name as 52 // argument. 53 static void ForEachFeature(const std::function<void(const char*)>& aCallback); 54 55 // Returns the default policy value for aFeatureName. 56 static FeaturePolicyValue DefaultAllowListFeature( 57 const nsAString& aFeatureName); 58 59 // This method returns true if aFeatureName is in unsafe allowed "*" case. 60 // We are in "unsafe" case when there is 'allow "*"' presents for an origin 61 // that's not presented in the ancestor feature policy chain, via src, via 62 // explicitly listed in allow, and not being the top-level origin. 63 static bool IsFeatureUnsafeAllowedAll(Document* aDocument, 64 const nsAString& aFeatureName); 65 66 private: 67 static void ReportViolation(Document* aDocument, 68 const nsAString& aFeatureName); 69 }; 70 71 } // namespace dom 72 } // namespace mozilla 73 74 namespace IPC { 75 76 template <typename T> 77 struct ParamTraits; 78 79 template <> 80 struct ParamTraits<mozilla::dom::FeaturePolicyInfo> { 81 static void Write(MessageWriter* aWriter, 82 const mozilla::dom::FeaturePolicyInfo& aParam); 83 static bool Read(MessageReader* aReader, 84 mozilla::dom::FeaturePolicyInfo* aResult); 85 }; 86 87 } // namespace IPC 88 89 #endif // mozilla_dom_FeaturePolicyUtils_h