Bug 1877661 - use non-shippable geckoview for android source tests. r=bhearsum,releng...
[gecko.git] / caps / PrincipalJSONHandler.h
blobd9ef7725b3b70d3a464b7225241078d407d02e74
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_PrincipalJSONHandler_h
7 #define mozilla_PrincipalJSONHandler_h
9 #include <stddef.h> // size_t
10 #include <stdint.h> // uint32_t
12 #include "js/JSON.h" // JS::JSONParseHandler
13 #include "js/TypeDecls.h" // JS::Latin1Char
15 #include "mozilla/AlreadyAddRefed.h" // already_AddRefed
16 #include "mozilla/RefPtr.h" // RefPtr
17 #include "mozilla/Variant.h" // Variant
19 #include "nsDebug.h" // NS_WARNING
20 #include "nsPrintfCString.h" // nsPrintfCString
22 #include "BasePrincipal.h"
23 #include "ContentPrincipalJSONHandler.h"
24 #include "ExpandedPrincipalJSONHandler.h"
25 #include "NullPrincipalJSONHandler.h"
26 #include "SharedJSONHandler.h"
28 namespace mozilla {
30 class PrincipalJSONHandlerTypes {
31 public:
32 enum class State {
33 Init,
35 // After top-level object's '{'.
36 StartObject,
38 // After the PrincipalKind property key for SystemPrincipal.
39 SystemPrincipal_Key,
40 // After the SystemPrincipal's inner object's '{'.
41 SystemPrincipal_StartObject,
42 // After the SystemPrincipal's inner object's '}'.
43 SystemPrincipal_EndObject,
45 // After the PrincipalKind property key for NullPrincipal, ContentPrincipal,
46 // or ExpandedPrincipal, and also the entire inner object.
47 // Delegates to mInnerHandler until the inner object's '}'.
48 NullPrincipal_Inner,
49 ContentPrincipal_Inner,
50 ExpandedPrincipal_Inner,
52 // After top-level object's '}'.
53 EndObject,
55 Error,
58 using InnerHandlerT =
59 Maybe<Variant<NullPrincipalJSONHandler, ContentPrincipalJSONHandler,
60 ExpandedPrincipalJSONHandler>>;
62 static constexpr bool CanContainExpandedPrincipal = true;
65 // JSON parse handler for the top-level object for principal.
67 // inner object
68 // |
69 // ---------------------------------------------------------
70 // | |
71 // {"1": {"0": "https://mozilla.com", "2": "^privateBrowsingId=1"}}
72 // |
73 // |
74 // |
75 // PrincipalKind
77 // For inner object except for the system principal case, this delegates
78 // to NullPrincipalJSONHandler, ContentPrincipalJSONHandler, or
79 // ExpandedPrincipalJSONHandler.
81 //// Null principal:
82 // {"0":{"0":"moz-nullprincipal:{56cac540-864d-47e7-8e25-1614eab5155e}"}}
84 // Content principal:
85 // {"1":{"0":"https://mozilla.com"}} -> {"0":"https://mozilla.com"}
87 // Expanded principal:
88 // {"2":{"0":"<base64principal1>,<base64principal2>"}}
90 // System principal:
91 // {"3":{}}
92 class PrincipalJSONHandler
93 : public ContainerPrincipalJSONHandler<PrincipalJSONHandlerTypes> {
94 using State = PrincipalJSONHandlerTypes::State;
95 using InnerHandlerT = PrincipalJSONHandlerTypes::InnerHandlerT;
97 public:
98 PrincipalJSONHandler() = default;
99 virtual ~PrincipalJSONHandler() = default;
101 virtual void error(const char* msg, uint32_t line, uint32_t column) override {
102 NS_WARNING(
103 nsPrintfCString("JSON Error: %s at line %u column %u of the JSON data",
104 msg, line, column)
105 .get());
108 already_AddRefed<BasePrincipal> Get() { return mPrincipal.forget(); }
110 protected:
111 virtual void SetErrorState() override { mState = State::Error; }
114 } // namespace mozilla
116 #endif // mozilla_PrincipalJSONHandler_h