1 /* -*- Mode: C++; tab-width: 4; 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 nsDNSPrefetch_h___
7 #define nsDNSPrefetch_h___
8 
9 #include <functional>
10 
11 #include "nsIWeakReferenceUtils.h"
12 #include "nsString.h"
13 #include "mozilla/TimeStamp.h"
14 #include "mozilla/Attributes.h"
15 #include "mozilla/BasePrincipal.h"
16 
17 #include "nsIDNSListener.h"
18 #include "nsIRequest.h"
19 
20 class nsIURI;
21 class nsIDNSService;
22 class nsIDNSHTTPSSVCRecord;
23 
24 class nsDNSPrefetch final : public nsIDNSListener {
25   ~nsDNSPrefetch() = default;
26 
27  public:
28   NS_DECL_THREADSAFE_ISUPPORTS
29   NS_DECL_NSIDNSLISTENER
30 
31   nsDNSPrefetch(nsIURI* aURI, mozilla::OriginAttributes& aOriginAttributes,
32                 nsIRequest::TRRMode aTRRMode, nsIDNSListener* aListener,
33                 bool storeTiming);
34   // For fetching HTTPS RR.
35   nsDNSPrefetch(nsIURI* aURI, mozilla::OriginAttributes& aOriginAttributes,
36                 nsIRequest::TRRMode aTRRMode);
TimingsValid()37   bool TimingsValid() const {
38     return !mStartTimestamp.IsNull() && !mEndTimestamp.IsNull();
39   }
40   // Only use the two timings if TimingsValid() returns true
StartTimestamp()41   const mozilla::TimeStamp& StartTimestamp() const { return mStartTimestamp; }
EndTimestamp()42   const mozilla::TimeStamp& EndTimestamp() const { return mEndTimestamp; }
43 
44   static nsresult Initialize(nsIDNSService* aDNSService);
45   static nsresult Shutdown();
46 
47   // Call one of the following methods to start the Prefetch.
48   nsresult PrefetchHigh(bool refreshDNS = false);
49   nsresult PrefetchMedium(bool refreshDNS = false);
50   nsresult PrefetchLow(bool refreshDNS = false);
51 
52   nsresult FetchHTTPSSVC(
53       bool aRefreshDNS, bool aPrefetch,
54       std::function<void(nsIDNSHTTPSSVCRecord*)>&& aCallback);
55 
56  private:
57   nsCString mHostname;
58   mozilla::OriginAttributes mOriginAttributes;
59   bool mStoreTiming;
60   nsIRequest::TRRMode mTRRMode;
61   mozilla::TimeStamp mStartTimestamp;
62   mozilla::TimeStamp mEndTimestamp;
63   nsWeakPtr mListener;
64 
65   nsresult Prefetch(uint32_t flags);
66 };
67 
68 #endif
69