1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 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_dom_HTMLDNSPrefetch_h___
8 #define mozilla_dom_HTMLDNSPrefetch_h___
11 #include "nsIRequest.h"
13 #include "nsIDNSService.h"
19 class OriginAttributes
;
29 class SupportsDNSPrefetch
;
31 class HTMLDNSPrefetch
{
33 // The required aDocument parameter is the context requesting the prefetch -
34 // under certain circumstances (e.g. headers, or security context) associated
35 // with the context the prefetch will not be performed.
36 static bool IsAllowed(Document
* aDocument
);
38 static nsresult
Initialize();
39 static nsresult
Shutdown();
41 // Call one of the Prefetch* methods to start the lookup.
43 // The URI versions will defer DNS lookup until pageload is
44 // complete, while the string versions submit the lookup to
45 // the DNS system immediately. The URI version is somewhat lighter
46 // weight, but its request is also more likely to be dropped due to a
47 // full queue and it may only be used from the main thread.
49 // If you are planning to use the methods with the OriginAttributes param, be
50 // sure that you pass a partitioned one. See StoragePrincipalHelper.h to know
58 enum class PrefetchSource
{
60 AnchorSpeculativePrefetch
,
62 static nsresult
DeferPrefetch(SupportsDNSPrefetch
& aSupports
,
63 Element
& aElement
, Priority aPriority
);
64 static void SendRequest(Element
& aElement
, nsIDNSService::DNSFlags aFlags
);
65 static nsresult
Prefetch(
66 const nsAString
& host
, bool isHttps
,
67 const OriginAttributes
& aPartitionedPrincipalOriginAttributes
,
68 nsIRequest::TRRMode aTRRMode
, Priority
);
69 static nsresult
CancelPrefetch(
70 const nsAString
& host
, bool isHttps
,
71 const OriginAttributes
& aPartitionedPrincipalOriginAttributes
,
72 nsIRequest::TRRMode aTRRMode
, Priority
, nsresult aReason
);
73 static nsresult
CancelPrefetch(SupportsDNSPrefetch
&, Element
&, Priority
,
75 static void ElementDestroyed(Element
&, SupportsDNSPrefetch
&);
77 static nsIDNSService::DNSFlags
PriorityToDNSServiceFlags(Priority
);
80 static nsresult
Prefetch(
81 const nsAString
& host
, bool isHttps
,
82 const OriginAttributes
& aPartitionedPrincipalOriginAttributes
,
83 nsIDNSService::DNSFlags flags
);
84 static nsresult
CancelPrefetch(
85 const nsAString
& hostname
, bool isHttps
,
86 const OriginAttributes
& aPartitionedPrincipalOriginAttributes
,
87 nsIDNSService::DNSFlags flags
, nsresult aReason
);
89 friend class net::NeckoParent
;
92 // Elements that support DNS prefetch are expected to subclass this.
93 class SupportsDNSPrefetch
{
95 bool IsInDNSPrefetch() { return mInDNSPrefetch
; }
96 void SetIsInDNSPrefetch() { mInDNSPrefetch
= true; }
97 void ClearIsInDNSPrefetch() { mInDNSPrefetch
= false; }
99 void DNSPrefetchRequestStarted() {
100 mDNSPrefetchDeferred
= false;
101 mDNSPrefetchRequested
= true;
104 void DNSPrefetchRequestDeferred() {
105 mDNSPrefetchDeferred
= true;
106 mDNSPrefetchRequested
= false;
109 bool IsDNSPrefetchRequestDeferred() const { return mDNSPrefetchDeferred
; }
111 // This could be a virtual function or something like that, but that would
112 // cause our subclasses to grow by two pointers, rather than just 1 byte at
114 nsIURI
* GetURIForDNSPrefetch(Element
& aOwner
);
117 SupportsDNSPrefetch()
118 : mInDNSPrefetch(false),
119 mDNSPrefetchRequested(false),
120 mDNSPrefetchDeferred(false),
121 mDestroyedCalled(false) {}
123 void CancelDNSPrefetch(Element
& aOwner
);
124 void TryDNSPrefetch(Element
& aOwner
, HTMLDNSPrefetch::PrefetchSource aSource
);
126 // This MUST be called on the destructor of the Element subclass.
127 // Our own destructor ensures that.
128 void Destroyed(Element
& aOwner
) {
129 MOZ_DIAGNOSTIC_ASSERT(!mDestroyedCalled
,
130 "Multiple calls to SupportsDNSPrefetch::Destroyed?");
131 mDestroyedCalled
= true;
132 if (mInDNSPrefetch
) {
133 HTMLDNSPrefetch::ElementDestroyed(aOwner
, *this);
137 ~SupportsDNSPrefetch() {
138 MOZ_DIAGNOSTIC_ASSERT(mDestroyedCalled
,
139 "Need to call SupportsDNSPrefetch::Destroyed "
140 "from the owner element");
144 bool mInDNSPrefetch
: 1;
145 bool mDNSPrefetchRequested
: 1;
146 bool mDNSPrefetchDeferred
: 1;
147 bool mDestroyedCalled
: 1;
151 } // namespace mozilla