Bug 1857841 - pt 3. Add a new page kind named "fresh" r=glandium
[gecko.git] / caps / SharedJSONHandler.h
blobbaf05810fe47766328748b0f8f1579b60b6a0e47
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
15 namespace mozilla {
17 // Base class of all principal JSON parse handlers.
18 class PrincipalJSONHandlerShared : public JS::JSONParseHandler {
19 public:
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");
28 SetErrorState();
29 return false;
32 virtual bool stringValue(const char16_t* str, size_t length) override {
33 NS_WARNING("Principal JSON shouldn't use non-ASCII");
34 SetErrorState();
35 return true;
38 virtual bool numberValue(double d) override {
39 NS_WARNING("Unexpected number value");
40 SetErrorState();
41 return false;
44 virtual bool booleanValue(bool v) override {
45 NS_WARNING("Unexpected boolean value");
46 SetErrorState();
47 return false;
50 virtual bool nullValue() override {
51 NS_WARNING("Unexpected null value");
52 SetErrorState();
53 return false;
56 virtual void error(const char* msg, uint32_t line, uint32_t column) override {
57 // Unused.
60 protected:
61 // Set to the error state for the above handlers.
62 virtual void SetErrorState() = 0;
64 public:
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;
79 public:
80 // Common handlers.
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;
95 private:
96 bool ProcessInnerResult(bool aResult);
98 template <class Func>
99 bool CallOnInner(Func&& aFunc) {
100 return mInnerHandler->match([&](auto& aInner) {
101 bool result = aFunc(aInner);
102 return ProcessInnerResult(result);
106 protected:
107 State mState = State::Init;
109 InnerHandlerT mInnerHandler;
112 } // namespace mozilla
114 #endif // mozilla_SharedJSONHandler_h