tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

NullPrincipalJSONHandler.h (2360B)


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