DNSRequestBase.h (5556B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set sw=2 ts=8 et tw=80 : */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_net_DNSRequestBase_h 8 #define mozilla_net_DNSRequestBase_h 9 10 #include "mozilla/net/PDNSRequestParent.h" 11 #include "nsICancelable.h" 12 #include "nsIDNSRecord.h" 13 #include "nsIDNSListener.h" 14 #include "nsIDNSByTypeRecord.h" 15 #include "nsIEventTarget.h" 16 17 namespace mozilla { 18 namespace net { 19 20 class DNSRequestActor; 21 class DNSRequestChild; 22 class DNSRequestHandler; 23 class DNSRequestParent; 24 class DNSRequestSender; 25 26 // A base class for DNSRequestSender and DNSRequestHandler. 27 // Provide interfaces for processing DNS requests. 28 class DNSRequestBase : public nsISupports { 29 public: 30 explicit DNSRequestBase() = default; 31 32 void SetIPCActor(DNSRequestActor* aActor); 33 34 virtual void OnRecvCancelDNSRequest(const nsCString& hostName, 35 const nsCString& trrServer, 36 const int32_t& port, const uint16_t& type, 37 const OriginAttributes& originAttributes, 38 const nsIDNSService::DNSFlags& flags, 39 const nsresult& reason) = 0; 40 virtual bool OnRecvLookupCompleted(const DNSRequestResponse& reply) = 0; 41 virtual void OnIPCActorDestroy() = 0; 42 43 virtual DNSRequestSender* AsDNSRequestSender() = 0; 44 virtual DNSRequestHandler* AsDNSRequestHandler() = 0; 45 46 protected: 47 virtual ~DNSRequestBase() = default; 48 49 RefPtr<DNSRequestActor> mIPCActor; 50 }; 51 52 // DNSRequestSender is used to send an IPC request to DNSRequestHandler and 53 // deliver the result to nsIDNSListener. 54 // Note this class could be used both in content process and parent process. 55 class DNSRequestSender final : public DNSRequestBase, public nsICancelable { 56 public: 57 NS_DECL_THREADSAFE_ISUPPORTS 58 NS_DECL_NSICANCELABLE 59 60 DNSRequestSender(const nsACString& aHost, const nsACString& aTrrServer, 61 int32_t aPort, const uint16_t& aType, 62 const OriginAttributes& aOriginAttributes, 63 const nsIDNSService::DNSFlags& aFlags, 64 nsIDNSListener* aListener, nsIEventTarget* target); 65 66 void OnRecvCancelDNSRequest(const nsCString& hostName, 67 const nsCString& trrServer, const int32_t& port, 68 const uint16_t& type, 69 const OriginAttributes& originAttributes, 70 const nsIDNSService::DNSFlags& flags, 71 const nsresult& reason) override; 72 bool OnRecvLookupCompleted(const DNSRequestResponse& reply) override; 73 void OnIPCActorDestroy() override; 74 75 // Sends IPDL request to DNSRequestHandler 76 void StartRequest(); 77 void CallOnLookupComplete(); 78 79 DNSRequestSender* AsDNSRequestSender() override { return this; } 80 DNSRequestHandler* AsDNSRequestHandler() override { return nullptr; } 81 82 private: 83 friend class ChildDNSService; 84 virtual ~DNSRequestSender() = default; 85 86 nsCOMPtr<nsIDNSListener> mListener; 87 nsCOMPtr<nsIEventTarget> mTarget; 88 nsCOMPtr<nsIDNSRecord> mResultRecord; 89 nsresult mResultStatus = NS_OK; 90 nsCString mHost; 91 nsCString mTrrServer; 92 int32_t mPort; 93 uint16_t mType = 0; 94 const OriginAttributes mOriginAttributes; 95 nsIDNSService::DNSFlags mFlags = nsIDNSService::RESOLVE_DEFAULT_FLAGS; 96 }; 97 98 // DNSRequestHandler handles the dns request and sends the result back via IPC. 99 // Note this class could be used both in parent process and socket process. 100 class DNSRequestHandler final : public DNSRequestBase, public nsIDNSListener { 101 public: 102 NS_DECL_THREADSAFE_ISUPPORTS 103 NS_DECL_NSIDNSLISTENER 104 105 DNSRequestHandler() = default; 106 107 void DoAsyncResolve(const nsACString& hostname, const nsACString& trrServer, 108 int32_t port, uint16_t type, 109 const OriginAttributes& originAttributes, 110 nsIDNSService::DNSFlags flags); 111 112 void OnRecvCancelDNSRequest(const nsCString& hostName, 113 const nsCString& trrServer, const int32_t& port, 114 const uint16_t& type, 115 const OriginAttributes& originAttributes, 116 const nsIDNSService::DNSFlags& flags, 117 const nsresult& reason) override; 118 bool OnRecvLookupCompleted(const DNSRequestResponse& reply) override; 119 void OnIPCActorDestroy() override; 120 121 DNSRequestSender* AsDNSRequestSender() override { return nullptr; } 122 DNSRequestHandler* AsDNSRequestHandler() override { return this; } 123 124 private: 125 virtual ~DNSRequestHandler() = default; 126 127 nsIDNSService::DNSFlags mFlags = nsIDNSService::RESOLVE_DEFAULT_FLAGS; 128 }; 129 130 // Provides some common methods for DNSRequestChild and DNSRequestParent. 131 class DNSRequestActor { 132 public: 133 NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING 134 135 explicit DNSRequestActor(DNSRequestBase* aRequest) : mDNSRequest(aRequest) {} 136 137 virtual bool CanSend() const = 0; 138 virtual DNSRequestChild* AsDNSRequestChild() = 0; 139 virtual DNSRequestParent* AsDNSRequestParent() = 0; 140 141 DNSRequestBase* GetDNSRequest() { return mDNSRequest.get(); }; 142 143 protected: 144 virtual ~DNSRequestActor() = default; 145 146 RefPtr<DNSRequestBase> mDNSRequest; 147 }; 148 149 } // namespace net 150 } // namespace mozilla 151 152 #endif // mozilla_net_DNSRequestBase_h