Bug 1861709 replace AudioCallbackDriver::ThreadRunning() assertions that mean to...
[gecko.git] / netwerk / dns / DNSRequestBase.h
blob7b26846f5e26fb379011dd56438c2377e5c803eb
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/. */
7 #ifndef mozilla_net_DNSRequestBase_h
8 #define mozilla_net_DNSRequestBase_h
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"
17 namespace mozilla {
18 namespace net {
20 class DNSRequestActor;
21 class DNSRequestChild;
22 class DNSRequestHandler;
23 class DNSRequestParent;
24 class DNSRequestSender;
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;
32 void SetIPCActor(DNSRequestActor* aActor);
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;
43 virtual DNSRequestSender* AsDNSRequestSender() = 0;
44 virtual DNSRequestHandler* AsDNSRequestHandler() = 0;
46 protected:
47 virtual ~DNSRequestBase() = default;
49 RefPtr<DNSRequestActor> mIPCActor;
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
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);
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;
75 // Sends IPDL request to DNSRequestHandler
76 void StartRequest();
77 void CallOnLookupComplete();
79 DNSRequestSender* AsDNSRequestSender() override { return this; }
80 DNSRequestHandler* AsDNSRequestHandler() override { return nullptr; }
82 private:
83 friend class ChildDNSService;
84 virtual ~DNSRequestSender() = default;
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;
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
105 DNSRequestHandler() = default;
107 void DoAsyncResolve(const nsACString& hostname, const nsACString& trrServer,
108 int32_t port, uint16_t type,
109 const OriginAttributes& originAttributes,
110 nsIDNSService::DNSFlags flags);
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;
121 DNSRequestSender* AsDNSRequestSender() override { return nullptr; }
122 DNSRequestHandler* AsDNSRequestHandler() override { return this; }
124 private:
125 virtual ~DNSRequestHandler() = default;
127 nsIDNSService::DNSFlags mFlags = nsIDNSService::RESOLVE_DEFAULT_FLAGS;
130 // Provides some common methods for DNSRequestChild and DNSRequestParent.
131 class DNSRequestActor {
132 public:
133 NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
135 explicit DNSRequestActor(DNSRequestBase* aRequest) : mDNSRequest(aRequest) {}
137 virtual bool CanSend() const = 0;
138 virtual DNSRequestChild* AsDNSRequestChild() = 0;
139 virtual DNSRequestParent* AsDNSRequestParent() = 0;
141 DNSRequestBase* GetDNSRequest() { return mDNSRequest.get(); };
143 protected:
144 virtual ~DNSRequestActor() = default;
146 RefPtr<DNSRequestBase> mDNSRequest;
149 } // namespace net
150 } // namespace mozilla
152 #endif // mozilla_net_DNSRequestBase_h