Bug 1890277: part 1) Add CSP parser tests for `require-trusted-types-for`. r=tschuster
[gecko.git] / caps / SubsumedPrincipalJSONHandler.h
blob67311a516d7f4ad1436841572cfebae247732598
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef mozilla_SubsumedPrincipalJSONHandler_h
7 #define mozilla_SubsumedPrincipalJSONHandler_h
9 #include <stddef.h> // size_t
10 #include <stdint.h> // uint32_t
12 #include "js/TypeDecls.h" // JS::Latin1Char
14 #include "mozilla/AlreadyAddRefed.h" // already_AddRefed
15 #include "mozilla/Variant.h" // Variant
16 #include "mozilla/RefPtr.h" // RefPtr
18 #include "nsDebug.h" // NS_WARNING
20 #include "BasePrincipal.h"
21 #include "ContentPrincipalJSONHandler.h"
22 #include "NullPrincipalJSONHandler.h"
23 #include "SharedJSONHandler.h"
25 namespace mozilla {
27 class SubsumedPrincipalJSONHandlerTypes {
28 public:
29 enum class State {
30 Init,
32 // After the subsumed principal object's '{'.
33 StartObject,
35 // After the PrincipalKind property key for SystemPrincipal.
36 SystemPrincipal_Key,
37 // After the SystemPrincipal's inner object's '{'.
38 SystemPrincipal_StartObject,
39 // After the SystemPrincipal's inner object's '}'.
40 SystemPrincipal_EndObject,
42 // After the PrincipalKind property key for NullPrincipal or
43 // ContentPrincipal, and also the entire inner object.
44 // Delegates to mInnerHandler until the inner object's '}'.
46 // Unlike PrincipalJSONHandler, subsumed principal doesn't contain
47 // ExpandedPrincipal.
48 NullPrincipal_Inner,
49 ContentPrincipal_Inner,
51 // After the subsumed principal object's '}'.
52 EndObject,
54 Error,
57 using InnerHandlerT =
58 Maybe<Variant<NullPrincipalJSONHandler, ContentPrincipalJSONHandler>>;
60 static constexpr bool CanContainExpandedPrincipal = false;
63 // JSON parse handler for subsumed principal object inside ExpandedPrincipal's
64 // new format.
66 // Subsumed principal object
67 // |
68 // ----------------------------------
69 // | |
70 // {"2": {"0": [{"1": {"0": https://mozilla.com"}}]}}
71 // | | |
72 // | ---------------------------
73 // | |
74 // | inner object
75 // PrincipalKind
77 // For inner object except for the system principal case, this delegates
78 // to NullPrincipalJSONHandler or ContentPrincipalJSONHandler.
79 class SubsumedPrincipalJSONHandler
80 : public ContainerPrincipalJSONHandler<SubsumedPrincipalJSONHandlerTypes> {
81 using State = SubsumedPrincipalJSONHandlerTypes::State;
82 using InnerHandlerT = SubsumedPrincipalJSONHandlerTypes::InnerHandlerT;
84 public:
85 SubsumedPrincipalJSONHandler() = default;
86 virtual ~SubsumedPrincipalJSONHandler() = default;
88 bool HasAccepted() const { return mState == State::EndObject; }
90 protected:
91 virtual void SetErrorState() override { mState = State::Error; }
94 } // namespace mozilla
96 #endif // mozilla_SubsumedPrincipalJSONHandler_h