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"
30 class PrincipalJSONHandlerTypes
{
35 // After top-level object's '{'.
38 // After the PrincipalKind property key for SystemPrincipal.
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 '}'.
49 ContentPrincipal_Inner
,
50 ExpandedPrincipal_Inner
,
52 // After top-level object's '}'.
59 Maybe
<Variant
<NullPrincipalJSONHandler
, ContentPrincipalJSONHandler
,
60 ExpandedPrincipalJSONHandler
>>;
62 static constexpr bool CanContainExpandedPrincipal
= true;
65 // JSON parse handler for the top-level object for principal.
69 // ---------------------------------------------------------
71 // {"1": {"0": "https://mozilla.com", "2": "^privateBrowsingId=1"}}
77 // For inner object except for the system principal case, this delegates
78 // to NullPrincipalJSONHandler, ContentPrincipalJSONHandler, or
79 // ExpandedPrincipalJSONHandler.
82 // {"0":{"0":"moz-nullprincipal:{56cac540-864d-47e7-8e25-1614eab5155e}"}}
85 // {"1":{"0":"https://mozilla.com"}} -> {"0":"https://mozilla.com"}
87 // Expanded principal:
88 // {"2":{"0":"<base64principal1>,<base64principal2>"}}
92 class PrincipalJSONHandler
93 : public ContainerPrincipalJSONHandler
<PrincipalJSONHandlerTypes
> {
94 using State
= PrincipalJSONHandlerTypes::State
;
95 using InnerHandlerT
= PrincipalJSONHandlerTypes::InnerHandlerT
;
98 PrincipalJSONHandler() = default;
99 virtual ~PrincipalJSONHandler() = default;
101 virtual void error(const char* msg
, uint32_t line
, uint32_t column
) override
{
103 nsPrintfCString("JSON Error: %s at line %u column %u of the JSON data",
108 already_AddRefed
<BasePrincipal
> Get() { return mPrincipal
.forget(); }
111 virtual void SetErrorState() override
{ mState
= State::Error
; }
114 } // namespace mozilla
116 #endif // mozilla_PrincipalJSONHandler_h