Source code

Revision control

Copy as Markdown

Other Tools

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at */
#ifndef mozilla_SharedJSONHandler_h
#define mozilla_SharedJSONHandler_h
#include "js/JSON.h" // JS::JSONParseHandler
#include "mozilla/RefPtr.h" // RefPtr
#include "BasePrincipal.h" // BasePrincipal
namespace mozilla {
// Base class of all principal JSON parse handlers.
class PrincipalJSONHandlerShared : public JS::JSONParseHandler {
// Common handlers for inner objects.
// NOTE: propertyName and stringValue are partial overloads.
// Subclasses should put the following:
// * `using PrincipalJSONHandlerShared::propertyName`
// * `using PrincipalJSONHandlerShared::stringValue`
virtual bool propertyName(const char16_t* name, size_t length) override {
NS_WARNING("Principal JSON shouldn't use non-ASCII");
return false;
virtual bool stringValue(const char16_t* str, size_t length) override {
NS_WARNING("Principal JSON shouldn't use non-ASCII");
return true;
virtual bool numberValue(double d) override {
NS_WARNING("Unexpected number value");
return false;
virtual bool booleanValue(bool v) override {
NS_WARNING("Unexpected boolean value");
return false;
virtual bool nullValue() override {
NS_WARNING("Unexpected null value");
return false;
virtual void error(const char* msg, uint32_t line, uint32_t column) override {
// Unused.
// Set to the error state for the above handlers.
virtual void SetErrorState() = 0;
RefPtr<BasePrincipal> mPrincipal;
// Base class shared between PrincipalJSONHandler and
// SubsumedPrincipalJSONHandler.
// This implements the common code for them, absorbing the difference about
// whether it can contain ExpandedPrincipal or not.
template <typename HandlerTypesT>
class ContainerPrincipalJSONHandler : public PrincipalJSONHandlerShared {
using State = typename HandlerTypesT::State;
using InnerHandlerT = typename HandlerTypesT::InnerHandlerT;
static constexpr bool CanContainExpandedPrincipal =
// Common handlers.
virtual bool startObject() override;
using PrincipalJSONHandlerShared::propertyName;
virtual bool propertyName(const JS::Latin1Char* name, size_t length) override;
virtual bool endObject() override;
virtual bool startArray() override;
virtual bool endArray() override;
using PrincipalJSONHandlerShared::stringValue;
virtual bool stringValue(const JS::Latin1Char* str, size_t length) override;
bool ProcessInnerResult(bool aResult);
template <class Func>
bool CallOnInner(Func&& aFunc) {
return mInnerHandler->match([&](auto& aInner) {
bool result = aFunc(aInner);
return ProcessInnerResult(result);
State mState = State::Init;
InnerHandlerT mInnerHandler;
} // namespace mozilla
#endif // mozilla_SharedJSONHandler_h