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 "nsString.h"
11 #include <functional>
12 
13 #include "mozilla/dom/FeaturePolicy.h"
14 
15 class PickleIterator;
16 
17 namespace IPC {
18 class Message;
19 }
20 
21 namespace mozilla {
22 namespace dom {
23 
24 class Document;
25 
26 class FeaturePolicyUtils final {
27  public:
28   enum FeaturePolicyValue {
29     // Feature always allowed.
30     eAll,
31 
32     // Feature allowed for documents that are same-origin with this one.
33     eSelf,
34 
35     // Feature denied.
36     eNone,
37   };
38 
39   // This method returns true if aFeatureName is allowed for aDocument.
40   // Use this method everywhere you need to check feature-policy directives.
41   static bool IsFeatureAllowed(Document* aDocument,
42                                const nsAString& aFeatureName);
43 
44   // Returns true if aFeatureName is a known feature policy name.
45   static bool IsSupportedFeature(const nsAString& aFeatureName);
46 
47   // Returns true if aFeatureName is a experimental feature policy name.
48   static bool IsExperimentalFeature(const nsAString& aFeatureName);
49 
50   // Runs aCallback for each known feature policy, with the feature name as
51   // argument.
52   static void ForEachFeature(const std::function<void(const char*)>& aCallback);
53 
54   // Returns the default policy value for aFeatureName.
55   static FeaturePolicyValue DefaultAllowListFeature(
56       const nsAString& aFeatureName);
57 
58   // This method returns true if aFeatureName is in unsafe allowed "*" case.
59   // We are in "unsafe" case when there is 'allow "*"' presents for an origin
60   // that's not presented in the ancestor feature policy chain, via src, via
61   // explicitly listed in allow, and not being the top-level origin.
62   static bool IsFeatureUnsafeAllowedAll(Document* aDocument,
63                                         const nsAString& aFeatureName);
64 
65  private:
66   static void ReportViolation(Document* aDocument,
67                               const nsAString& aFeatureName);
68 };
69 
70 }  // namespace dom
71 
72 namespace ipc {
73 
74 class IProtocol;
75 
76 template <typename T>
77 struct IPDLParamTraits;
78 
79 template <>
80 struct IPDLParamTraits<mozilla::dom::FeaturePolicy*> {
81   static void Write(IPC::Message* aMsg, IProtocol* aActor,
82                     mozilla::dom::FeaturePolicy* aParam);
83   static bool Read(const IPC::Message* aMsg, PickleIterator* aIter,
84                    IProtocol* aActor,
85                    RefPtr<mozilla::dom::FeaturePolicy>* aResult);
86 };
87 }  // namespace ipc
88 }  // namespace mozilla
89 
90 #endif  // mozilla_dom_FeaturePolicyUtils_h
91