ContentPrincipalJSONHandler.h (2424B)
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_ContentPrincipalJSONHandler_h 7 #define mozilla_ContentPrincipalJSONHandler_h 8 9 #include <stddef.h> // size_t 10 #include <stdint.h> // uint32_t 11 12 #include "js/TypeDecls.h" // JS::Latin1Char 13 14 #include "mozilla/RefPtr.h" // RefPtr 15 16 #include "nsCOMPtr.h" // nsCOMPtr 17 #include "nsDebug.h" // NS_WARNING 18 #include "nsIURI.h" // nsIURI 19 20 #include "ContentPrincipal.h" 21 #include "OriginAttributes.h" 22 #include "SharedJSONHandler.h" 23 24 namespace mozilla { 25 26 // JSON parse handler for an inner object for ContentPrincipal. 27 // Used by PrincipalJSONHandler or SubsumedPrincipalJSONHandler. 28 class ContentPrincipalJSONHandler : public PrincipalJSONHandlerShared { 29 enum class State { 30 Init, 31 32 // After the inner object's '{'. 33 StartObject, 34 35 // After the property key for eURI. 36 URIKey, 37 38 // After the property key for eDomain. 39 DomainKey, 40 41 // After the property key for eSuffix. 42 SuffixKey, 43 44 // After the property value for eURI, eDomain, or eSuffix. 45 AfterPropertyValue, 46 47 // After the inner object's '}'. 48 EndObject, 49 50 Error, 51 }; 52 53 public: 54 ContentPrincipalJSONHandler() = default; 55 virtual ~ContentPrincipalJSONHandler() = default; 56 57 virtual bool startObject() override; 58 59 using PrincipalJSONHandlerShared::propertyName; 60 virtual bool propertyName(const JS::Latin1Char* name, size_t length) override; 61 62 virtual bool endObject() override; 63 64 virtual bool startArray() override { 65 NS_WARNING("Unexpected array value"); 66 mState = State::Error; 67 return false; 68 } 69 virtual bool endArray() override { 70 NS_WARNING("Unexpected array value"); 71 mState = State::Error; 72 return false; 73 } 74 75 using PrincipalJSONHandlerShared::stringValue; 76 virtual bool stringValue(const JS::Latin1Char* str, size_t length) override; 77 78 bool HasAccepted() const { return mState == State::EndObject; } 79 80 protected: 81 virtual void SetErrorState() override { mState = State::Error; } 82 83 private: 84 State mState = State::Init; 85 86 nsCOMPtr<nsIURI> mPrincipalURI; 87 nsCOMPtr<nsIURI> mDomain; 88 OriginAttributes mAttrs; 89 }; 90 91 } // namespace mozilla 92 93 #endif // mozilla_ContentPrincipalJSONHandler_h