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"
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
{
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;
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
{
57 NS_DECL_THREADSAFE_ISUPPORTS
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
,
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
77 void CallOnLookupComplete();
79 DNSRequestSender
* AsDNSRequestSender() override
{ return this; }
80 DNSRequestHandler
* AsDNSRequestHandler() override
{ return nullptr; }
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
;
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
{
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; }
125 virtual ~DNSRequestHandler() = default;
127 nsIDNSService::DNSFlags mFlags
= nsIDNSService::RESOLVE_DEFAULT_FLAGS
;
130 // Provides some common methods for DNSRequestChild and DNSRequestParent.
131 class DNSRequestActor
{
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(); };
144 virtual ~DNSRequestActor() = default;
146 RefPtr
<DNSRequestBase
> mDNSRequest
;
150 } // namespace mozilla
152 #endif // mozilla_net_DNSRequestBase_h