nsAboutProtocolHandler.h (4086B)
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 nsAboutProtocolHandler_h___ 7 #define nsAboutProtocolHandler_h___ 8 9 #include "nsIProtocolHandler.h" 10 #include "nsSimpleNestedURI.h" 11 #include "nsWeakReference.h" 12 #include "nsIURIMutator.h" 13 14 class nsIURI; 15 16 namespace mozilla { 17 namespace net { 18 19 class nsAboutProtocolHandler : public nsIProtocolHandlerWithDynamicFlags, 20 public nsIProtocolHandler, 21 public nsSupportsWeakReference { 22 public: 23 NS_DECL_ISUPPORTS 24 25 // nsIProtocolHandler methods: 26 NS_DECL_NSIPROTOCOLHANDLER 27 NS_DECL_NSIPROTOCOLHANDLERWITHDYNAMICFLAGS 28 29 // nsAboutProtocolHandler methods: 30 nsAboutProtocolHandler() = default; 31 32 static nsresult CreateNewURI(const nsACString& aSpec, const char* aCharset, 33 nsIURI* aBaseURI, nsIURI** result); 34 35 private: 36 virtual ~nsAboutProtocolHandler() = default; 37 }; 38 39 class nsSafeAboutProtocolHandler final : public nsIProtocolHandler, 40 public nsSupportsWeakReference { 41 public: 42 NS_DECL_ISUPPORTS 43 44 // nsIProtocolHandler methods: 45 NS_DECL_NSIPROTOCOLHANDLER 46 47 // nsSafeAboutProtocolHandler methods: 48 nsSafeAboutProtocolHandler() = default; 49 50 private: 51 ~nsSafeAboutProtocolHandler() = default; 52 }; 53 54 // Class to allow us to propagate the base URI to about:blank correctly 55 class nsNestedAboutURI final : public nsSimpleNestedURI { 56 private: 57 nsNestedAboutURI(nsIURI* aInnerURI, nsIURI* aBaseURI) 58 : nsSimpleNestedURI(aInnerURI), mBaseURI(aBaseURI) {} 59 nsNestedAboutURI() {} 60 virtual ~nsNestedAboutURI() = default; 61 62 public: 63 // Override QI so we can QI to our CID as needed 64 NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override; 65 66 // Override StartClone(), the nsISerializable methods, and 67 virtual already_AddRefed<nsSimpleURI> StartClone() override; 68 NS_IMETHOD Mutate(nsIURIMutator** _retval) override; 69 NS_IMETHOD_(void) Serialize(ipc::URIParams& aParams) override; 70 71 // nsISerializable 72 NS_IMETHOD Read(nsIObjectInputStream* aStream) override; 73 NS_IMETHOD Write(nsIObjectOutputStream* aStream) override; 74 75 nsIURI* GetBaseURI() const { return mBaseURI; } 76 77 protected: 78 nsCOMPtr<nsIURI> mBaseURI; 79 bool Deserialize(const mozilla::ipc::URIParams&); 80 nsresult ReadPrivate(nsIObjectInputStream* stream); 81 82 public: 83 class Mutator final : public nsIURIMutator, 84 public BaseURIMutator<nsNestedAboutURI>, 85 public nsISerializable, 86 public nsINestedAboutURIMutator { 87 NS_DECL_ISUPPORTS 88 NS_FORWARD_SAFE_NSIURISETTERS_RET(mURI) 89 90 explicit Mutator() = default; 91 92 private: 93 virtual ~Mutator() = default; 94 95 [[nodiscard]] NS_IMETHOD Deserialize( 96 const mozilla::ipc::URIParams& aParams) override { 97 return InitFromIPCParams(aParams); 98 } 99 100 NS_IMETHOD 101 Write(nsIObjectOutputStream* aOutputStream) override { 102 return NS_ERROR_NOT_IMPLEMENTED; 103 } 104 105 [[nodiscard]] NS_IMETHOD Read(nsIObjectInputStream* aStream) override { 106 return InitFromInputStream(aStream); 107 } 108 109 [[nodiscard]] NS_IMETHOD Finalize(nsIURI** aURI) override { 110 mURI.forget(aURI); 111 return NS_OK; 112 } 113 114 [[nodiscard]] NS_IMETHOD SetSpec(const nsACString& aSpec, 115 nsIURIMutator** aMutator) override { 116 if (aMutator) { 117 NS_ADDREF(*aMutator = this); 118 } 119 return InitFromSpec(aSpec); 120 } 121 122 [[nodiscard]] NS_IMETHOD InitWithBase(nsIURI* aInnerURI, 123 nsIURI* aBaseURI) override { 124 mURI = new nsNestedAboutURI(aInnerURI, aBaseURI); 125 return NS_OK; 126 } 127 128 friend class nsNestedAboutURI; 129 }; 130 131 friend BaseURIMutator<nsNestedAboutURI>; 132 }; 133 134 } // namespace net 135 } // namespace mozilla 136 137 #endif /* nsAboutProtocolHandler_h___ */