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_SharedJSONHandler_h
7 #define mozilla_SharedJSONHandler_h
9 #include "js/JSON.h" // JS::JSONParseHandler
11 #include "mozilla/RefPtr.h" // RefPtr
13 #include "BasePrincipal.h" // BasePrincipal
17 // Base class of all principal JSON parse handlers.
18 class PrincipalJSONHandlerShared
: public JS::JSONParseHandler
{
20 // Common handlers for inner objects.
22 // NOTE: propertyName and stringValue are partial overloads.
23 // Subclasses should put the following:
24 // * `using PrincipalJSONHandlerShared::propertyName`
25 // * `using PrincipalJSONHandlerShared::stringValue`
26 virtual bool propertyName(const char16_t
* name
, size_t length
) override
{
27 NS_WARNING("Principal JSON shouldn't use non-ASCII");
32 virtual bool stringValue(const char16_t
* str
, size_t length
) override
{
33 NS_WARNING("Principal JSON shouldn't use non-ASCII");
38 virtual bool numberValue(double d
) override
{
39 NS_WARNING("Unexpected number value");
44 virtual bool booleanValue(bool v
) override
{
45 NS_WARNING("Unexpected boolean value");
50 virtual bool nullValue() override
{
51 NS_WARNING("Unexpected null value");
56 virtual void error(const char* msg
, uint32_t line
, uint32_t column
) override
{
61 // Set to the error state for the above handlers.
62 virtual void SetErrorState() = 0;
65 RefPtr
<BasePrincipal
> mPrincipal
;
68 // Base class shared between PrincipalJSONHandler and
69 // SubsumedPrincipalJSONHandler.
70 // This implements the common code for them, absorbing the difference about
71 // whether it can contain ExpandedPrincipal or not.
72 template <typename HandlerTypesT
>
73 class ContainerPrincipalJSONHandler
: public PrincipalJSONHandlerShared
{
74 using State
= typename
HandlerTypesT::State
;
75 using InnerHandlerT
= typename
HandlerTypesT::InnerHandlerT
;
76 static constexpr bool CanContainExpandedPrincipal
=
77 HandlerTypesT::CanContainExpandedPrincipal
;
82 virtual bool startObject() override
;
84 using PrincipalJSONHandlerShared::propertyName
;
85 virtual bool propertyName(const JS::Latin1Char
* name
, size_t length
) override
;
86 virtual bool endObject() override
;
88 virtual bool startArray() override
;
90 virtual bool endArray() override
;
92 using PrincipalJSONHandlerShared::stringValue
;
93 virtual bool stringValue(const JS::Latin1Char
* str
, size_t length
) override
;
96 bool ProcessInnerResult(bool aResult
);
99 bool CallOnInner(Func
&& aFunc
) {
100 return mInnerHandler
->match([&](auto& aInner
) {
101 bool result
= aFunc(aInner
);
102 return ProcessInnerResult(result
);
107 State mState
= State::Init
;
109 InnerHandlerT mInnerHandler
;
112 } // namespace mozilla
114 #endif // mozilla_SharedJSONHandler_h