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
,
37 const OriginAttributes
& originAttributes
,
38 const uint32_t& 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 const uint16_t& aType
,
62 const OriginAttributes
& aOriginAttributes
,
63 const uint32_t& aFlags
, nsIDNSListener
* aListener
,
64 nsIEventTarget
* target
);
66 void OnRecvCancelDNSRequest(const nsCString
& hostName
,
67 const nsCString
& trrServer
, const uint16_t& type
,
68 const OriginAttributes
& originAttributes
,
69 const uint32_t& flags
,
70 const nsresult
& reason
) override
;
71 bool OnRecvLookupCompleted(const DNSRequestResponse
& reply
) override
;
72 void OnIPCActorDestroy() override
;
74 // Sends IPDL request to DNSRequestHandler
76 void CallOnLookupComplete();
78 DNSRequestSender
* AsDNSRequestSender() override
{ return this; }
79 DNSRequestHandler
* AsDNSRequestHandler() override
{ return nullptr; }
82 friend class ChildDNSService
;
83 virtual ~DNSRequestSender() = default;
85 nsCOMPtr
<nsIDNSListener
> mListener
;
86 nsCOMPtr
<nsIEventTarget
> mTarget
;
87 nsCOMPtr
<nsIDNSRecord
> mResultRecord
;
88 nsresult mResultStatus
;
92 const OriginAttributes mOriginAttributes
;
96 // DNSRequestHandler handles the dns request and sends the result back via IPC.
97 // Note this class could be used both in parent process and socket process.
98 class DNSRequestHandler final
: public DNSRequestBase
, public nsIDNSListener
{
100 NS_DECL_THREADSAFE_ISUPPORTS
101 NS_DECL_NSIDNSLISTENER
105 void DoAsyncResolve(const nsACString
& hostname
, const nsACString
& trrServer
,
106 uint16_t type
, const OriginAttributes
& originAttributes
,
109 void OnRecvCancelDNSRequest(const nsCString
& hostName
,
110 const nsCString
& trrServer
, const uint16_t& type
,
111 const OriginAttributes
& originAttributes
,
112 const uint32_t& flags
,
113 const nsresult
& reason
) override
;
114 bool OnRecvLookupCompleted(const DNSRequestResponse
& reply
) override
;
115 void OnIPCActorDestroy() override
;
117 DNSRequestSender
* AsDNSRequestSender() override
{ return nullptr; }
118 DNSRequestHandler
* AsDNSRequestHandler() override
{ return this; }
121 virtual ~DNSRequestHandler() = default;
126 // Provides some common methods for DNSRequestChild and DNSRequestParent.
127 class DNSRequestActor
{
129 NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
131 explicit DNSRequestActor(DNSRequestBase
* aRequest
) : mDNSRequest(aRequest
) {}
133 virtual bool CanSend() const = 0;
134 virtual DNSRequestChild
* AsDNSRequestChild() = 0;
135 virtual DNSRequestParent
* AsDNSRequestParent() = 0;
137 DNSRequestBase
* GetDNSRequest() { return mDNSRequest
.get(); };
140 virtual ~DNSRequestActor() = default;
142 RefPtr
<DNSRequestBase
> mDNSRequest
;
146 } // namespace mozilla
148 #endif // mozilla_net_DNSRequestBase_h