SharedJSONHandler.h (3294B)
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/. */ 5 6 #ifndef mozilla_SharedJSONHandler_h 7 #define mozilla_SharedJSONHandler_h 8 9 #include "js/JSON.h" // JS::JSONParseHandler 10 11 #include "mozilla/RefPtr.h" // RefPtr 12 13 #include "BasePrincipal.h" // BasePrincipal 14 15 namespace mozilla { 16 17 // Base class of all principal JSON parse handlers. 18 class PrincipalJSONHandlerShared : public JS::JSONParseHandler { 19 public: 20 // Common handlers for inner objects. 21 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; 30 }; 31 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; 36 } 37 38 virtual bool numberValue(double d) override { 39 NS_WARNING("Unexpected number value"); 40 SetErrorState(); 41 return false; 42 } 43 44 virtual bool booleanValue(bool v) override { 45 NS_WARNING("Unexpected boolean value"); 46 SetErrorState(); 47 return false; 48 } 49 50 virtual bool nullValue() override { 51 NS_WARNING("Unexpected null value"); 52 SetErrorState(); 53 return false; 54 } 55 56 virtual void error(const char* msg, uint32_t line, uint32_t column) override { 57 // Unused. 58 } 59 60 protected: 61 // Set to the error state for the above handlers. 62 virtual void SetErrorState() = 0; 63 64 public: 65 RefPtr<BasePrincipal> mPrincipal; 66 }; 67 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; 78 79 public: 80 // Common handlers. 81 82 virtual bool startObject() override; 83 84 using PrincipalJSONHandlerShared::propertyName; 85 virtual bool propertyName(const JS::Latin1Char* name, size_t length) override; 86 virtual bool endObject() override; 87 88 virtual bool startArray() override; 89 90 virtual bool endArray() override; 91 92 using PrincipalJSONHandlerShared::stringValue; 93 virtual bool stringValue(const JS::Latin1Char* str, size_t length) override; 94 95 private: 96 bool ProcessInnerResult(bool aResult); 97 98 template <class Func> 99 bool CallOnInner(Func&& aFunc) { 100 return mInnerHandler->match([&](auto& aInner) { 101 bool result = aFunc(aInner); 102 return ProcessInnerResult(result); 103 }); 104 } 105 106 protected: 107 State mState = State::Init; 108 109 InnerHandlerT mInnerHandler; 110 }; 111 112 } // namespace mozilla 113 114 #endif // mozilla_SharedJSONHandler_h