tor-browser

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

PrincipalJSONHandler.h (3602B)


      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_PrincipalJSONHandler_h
      7 #define mozilla_PrincipalJSONHandler_h
      8 
      9 #include <stddef.h>  // size_t
     10 #include <stdint.h>  // uint32_t
     11 
     12 #include "js/JSON.h"       // JS::JSONParseHandler
     13 #include "js/TypeDecls.h"  // JS::Latin1Char
     14 
     15 #include "mozilla/AlreadyAddRefed.h"  // already_AddRefed
     16 #include "mozilla/RefPtr.h"           // RefPtr
     17 #include "mozilla/Variant.h"          // Variant
     18 
     19 #include "nsDebug.h"          // NS_WARNING
     20 #include "nsPrintfCString.h"  // nsPrintfCString
     21 
     22 #include "BasePrincipal.h"
     23 #include "ContentPrincipalJSONHandler.h"
     24 #include "ExpandedPrincipalJSONHandler.h"
     25 #include "NullPrincipalJSONHandler.h"
     26 #include "SharedJSONHandler.h"
     27 
     28 namespace mozilla {
     29 
     30 class PrincipalJSONHandlerTypes {
     31 public:
     32  enum class State {
     33    Init,
     34 
     35    // After top-level object's '{'.
     36    StartObject,
     37 
     38    // After the PrincipalKind property key for SystemPrincipal.
     39    SystemPrincipal_Key,
     40    // After the SystemPrincipal's inner object's '{'.
     41    SystemPrincipal_StartObject,
     42    // After the SystemPrincipal's inner object's '}'.
     43    SystemPrincipal_EndObject,
     44 
     45    // After the PrincipalKind property key for NullPrincipal, ContentPrincipal,
     46    // or ExpandedPrincipal, and also the entire inner object.
     47    // Delegates to mInnerHandler until the inner object's '}'.
     48    NullPrincipal_Inner,
     49    ContentPrincipal_Inner,
     50    ExpandedPrincipal_Inner,
     51 
     52    // After top-level object's '}'.
     53    EndObject,
     54 
     55    Error,
     56  };
     57 
     58  using InnerHandlerT =
     59      Maybe<Variant<NullPrincipalJSONHandler, ContentPrincipalJSONHandler,
     60                    ExpandedPrincipalJSONHandler>>;
     61 
     62  static constexpr bool CanContainExpandedPrincipal = true;
     63 };
     64 
     65 // JSON parse handler for the top-level object for principal.
     66 //
     67 //                                 inner object
     68 //                                      |
     69 //       ---------------------------------------------------------
     70 //       |                                                       |
     71 // {"1": {"0": "https://mozilla.com", "2": "^privateBrowsingId=1"}}
     72 //   |
     73 //   |
     74 //   |
     75 // PrincipalKind
     76 //
     77 // For inner object except for the system principal case, this delegates
     78 // to NullPrincipalJSONHandler, ContentPrincipalJSONHandler, or
     79 // ExpandedPrincipalJSONHandler.
     80 //
     81 //// Null principal:
     82 // {"0":{"0":"moz-nullprincipal:{56cac540-864d-47e7-8e25-1614eab5155e}"}}
     83 //
     84 // Content principal:
     85 // {"1":{"0":"https://mozilla.com"}} -> {"0":"https://mozilla.com"}
     86 //
     87 // Expanded principal:
     88 // {"2":{"0":"<base64principal1>,<base64principal2>"}}
     89 //
     90 // System principal:
     91 // {"3":{}}
     92 class PrincipalJSONHandler
     93    : public ContainerPrincipalJSONHandler<PrincipalJSONHandlerTypes> {
     94  using State = PrincipalJSONHandlerTypes::State;
     95  using InnerHandlerT = PrincipalJSONHandlerTypes::InnerHandlerT;
     96 
     97 public:
     98  PrincipalJSONHandler() = default;
     99  virtual ~PrincipalJSONHandler() = default;
    100 
    101  virtual void error(const char* msg, uint32_t line, uint32_t column) override {
    102    NS_WARNING(
    103        nsPrintfCString("JSON Error: %s at line %u column %u of the JSON data",
    104                        msg, line, column)
    105            .get());
    106  }
    107 
    108  already_AddRefed<BasePrincipal> Get() { return mPrincipal.forget(); }
    109 
    110 protected:
    111  virtual void SetErrorState() override { mState = State::Error; }
    112 };
    113 
    114 }  // namespace mozilla
    115 
    116 #endif  // mozilla_PrincipalJSONHandler_h