summaryrefslogtreecommitdiffstats
path: root/caps/SharedJSONHandler.h
blob: baf05810fe47766328748b0f8f1579b60b6a0e47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* -*- 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 http://mozilla.org/MPL/2.0/. */

#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 {
 public:
  // 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");
    SetErrorState();
    return false;
  };

  virtual bool stringValue(const char16_t* str, size_t length) override {
    NS_WARNING("Principal JSON shouldn't use non-ASCII");
    SetErrorState();
    return true;
  }

  virtual bool numberValue(double d) override {
    NS_WARNING("Unexpected number value");
    SetErrorState();
    return false;
  }

  virtual bool booleanValue(bool v) override {
    NS_WARNING("Unexpected boolean value");
    SetErrorState();
    return false;
  }

  virtual bool nullValue() override {
    NS_WARNING("Unexpected null value");
    SetErrorState();
    return false;
  }

  virtual void error(const char* msg, uint32_t line, uint32_t column) override {
    // Unused.
  }

 protected:
  // Set to the error state for the above handlers.
  virtual void SetErrorState() = 0;

 public:
  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 =
      HandlerTypesT::CanContainExpandedPrincipal;

 public:
  // 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;

 private:
  bool ProcessInnerResult(bool aResult);

  template <class Func>
  bool CallOnInner(Func&& aFunc) {
    return mInnerHandler->match([&](auto& aInner) {
      bool result = aFunc(aInner);
      return ProcessInnerResult(result);
    });
  }

 protected:
  State mState = State::Init;

  InnerHandlerT mInnerHandler;
};

}  // namespace mozilla

#endif  // mozilla_SharedJSONHandler_h