tor-browser

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

nsJSProtocolHandler.h (3969B)


      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 nsJSProtocolHandler_h___
      7 #define nsJSProtocolHandler_h___
      8 
      9 #include "nsIClassInfo.h"
     10 #include "nsINestedURI.h"
     11 #include "nsIProtocolHandler.h"
     12 #include "nsISerializable.h"
     13 #include "nsIURI.h"
     14 #include "nsSimpleURI.h"
     15 
     16 #define NS_JSPROTOCOLHANDLER_CID              \
     17  {/* bfc310d2-38a0-11d3-8cd3-0060b0fc14a3 */ \
     18   0xbfc310d2,                                \
     19   0x38a0,                                    \
     20   0x11d3,                                    \
     21   {0x8c, 0xd3, 0x00, 0x60, 0xb0, 0xfc, 0x14, 0xa3}}
     22 
     23 #define NS_JSURI_CID                          \
     24  {/* 58f089ee-512a-42d2-a935-d0c874128930 */ \
     25   0x58f089ee,                                \
     26   0x512a,                                    \
     27   0x42d2,                                    \
     28   {0xa9, 0x35, 0xd0, 0xc8, 0x74, 0x12, 0x89, 0x30}}
     29 
     30 #define NS_JSURIMUTATOR_CID                   \
     31  {/* 574ce83e-fe9f-4095-b85c-7909abbf7c37 */ \
     32   0x574ce83e,                                \
     33   0xfe9f,                                    \
     34   0x4095,                                    \
     35   {0xb8, 0x5c, 0x79, 0x09, 0xab, 0xbf, 0x7c, 0x37}}
     36 
     37 #define NS_JSPROTOCOLHANDLER_CONTRACTID \
     38  NS_NETWORK_PROTOCOL_CONTRACTID_PREFIX "javascript"
     39 
     40 class nsJSProtocolHandler : public nsIProtocolHandler {
     41 public:
     42  NS_DECL_ISUPPORTS
     43 
     44  // nsIProtocolHandler methods:
     45  NS_DECL_NSIPROTOCOLHANDLER
     46 
     47  // nsJSProtocolHandler methods:
     48  nsJSProtocolHandler();
     49 
     50  static nsresult CreateNewURI(const nsACString& aSpec, const char* aCharset,
     51                               nsIURI* aBaseURI, nsIURI** result);
     52 
     53 protected:
     54  virtual ~nsJSProtocolHandler();
     55 
     56  static nsresult EnsureUTF8Spec(const nsCString& aSpec, const char* aCharset,
     57                                 nsACString& aUTF8Spec);
     58 };
     59 
     60 class nsJSURI final : public mozilla::net::nsSimpleURI {
     61 public:
     62  using mozilla::net::nsSimpleURI::Read;
     63  using mozilla::net::nsSimpleURI::Write;
     64 
     65  nsIURI* GetBaseURI() const { return mBaseURI; }
     66 
     67  NS_DECL_ISUPPORTS_INHERITED
     68 
     69  // nsIURI overrides
     70  virtual already_AddRefed<mozilla::net::nsSimpleURI> StartClone() override;
     71  NS_IMETHOD Mutate(nsIURIMutator** _retval) override;
     72  NS_IMETHOD_(void) Serialize(mozilla::ipc::URIParams& aParams) override;
     73 
     74  // nsISerializable overrides
     75  NS_IMETHOD Read(nsIObjectInputStream* aStream) override;
     76  NS_IMETHOD Write(nsIObjectOutputStream* aStream) override;
     77 
     78 protected:
     79  nsJSURI() = default;
     80  explicit nsJSURI(nsIURI* aBaseURI) : mBaseURI(aBaseURI) {}
     81 
     82  virtual ~nsJSURI() = default;
     83 
     84  virtual nsresult EqualsInternal(nsIURI* other,
     85                                  RefHandlingEnum refHandlingMode,
     86                                  bool* result) override;
     87  bool Deserialize(const mozilla::ipc::URIParams&);
     88  nsresult ReadPrivate(nsIObjectInputStream* aStream);
     89 
     90 private:
     91  nsCOMPtr<nsIURI> mBaseURI;
     92 
     93 public:
     94  class Mutator final : public nsIURIMutator,
     95                        public BaseURIMutator<nsJSURI>,
     96                        public nsISerializable,
     97                        public nsIJSURIMutator {
     98    NS_DECL_ISUPPORTS
     99    NS_FORWARD_SAFE_NSIURISETTERS_RET(mURI)
    100    NS_DEFINE_NSIMUTATOR_COMMON
    101 
    102    NS_IMETHOD
    103    Write(nsIObjectOutputStream* aOutputStream) override {
    104      return NS_ERROR_NOT_IMPLEMENTED;
    105    }
    106 
    107    [[nodiscard]] NS_IMETHOD Read(nsIObjectInputStream* aStream) override {
    108      return InitFromInputStream(aStream);
    109    }
    110 
    111    [[nodiscard]] NS_IMETHOD SetBase(nsIURI* aBaseURI) override {
    112      mURI = new nsJSURI(aBaseURI);
    113      return NS_OK;
    114    }
    115 
    116    explicit Mutator() = default;
    117 
    118   private:
    119    virtual ~Mutator() = default;
    120 
    121    friend class nsJSURI;
    122  };
    123 
    124  friend BaseURIMutator<nsJSURI>;
    125 };
    126 
    127 #endif /* nsJSProtocolHandler_h___ */