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"
18 class OriginAttributes
;
28 class SupportsDNSPrefetch
;
30 class HTMLDNSPrefetch
{
32 // The required aDocument parameter is the context requesting the prefetch -
33 // under certain circumstances (e.g. headers, or security context) associated
34 // with the context the prefetch will not be performed.
35 static bool IsAllowed(Document
* aDocument
);
37 static nsresult
Initialize();
38 static nsresult
Shutdown();
40 // Call one of the Prefetch* methods to start the lookup.
42 // The URI versions will defer DNS lookup until pageload is
43 // complete, while the string versions submit the lookup to
44 // the DNS system immediately. The URI version is somewhat lighter
45 // weight, but its request is also more likely to be dropped due to a
46 // full queue and it may only be used from the main thread.
48 // If you are planning to use the methods with the OriginAttributes param, be
49 // sure that you pass a partitioned one. See StoragePrincipalHelper.h to know
57 static nsresult
Prefetch(SupportsDNSPrefetch
&, Element
&, Priority
);
58 static nsresult
Prefetch(
59 const nsAString
& host
, bool isHttps
,
60 const OriginAttributes
& aPartitionedPrincipalOriginAttributes
,
61 nsIRequest::TRRMode aTRRMode
, Priority
);
62 static nsresult
CancelPrefetch(
63 const nsAString
& host
, bool isHttps
,
64 const OriginAttributes
& aPartitionedPrincipalOriginAttributes
,
65 nsIRequest::TRRMode aTRRMode
, Priority
, nsresult aReason
);
66 static nsresult
CancelPrefetch(SupportsDNSPrefetch
&, Element
&, Priority
,
68 static void ElementDestroyed(Element
&, SupportsDNSPrefetch
&);
71 static uint32_t PriorityToDNSServiceFlags(Priority
);
73 static nsresult
Prefetch(
74 const nsAString
& host
, bool isHttps
,
75 const OriginAttributes
& aPartitionedPrincipalOriginAttributes
,
77 static nsresult
CancelPrefetch(
78 const nsAString
& hostname
, bool isHttps
,
79 const OriginAttributes
& aPartitionedPrincipalOriginAttributes
,
80 uint32_t flags
, nsresult aReason
);
82 friend class net::NeckoParent
;
85 // Elements that support DNS prefetch are expected to subclass this.
86 class SupportsDNSPrefetch
{
88 bool IsInDNSPrefetch() { return mInDNSPrefetch
; }
89 void SetIsInDNSPrefetch() { mInDNSPrefetch
= true; }
90 void ClearIsInDNSPrefetch() { mInDNSPrefetch
= false; }
92 void DNSPrefetchRequestStarted() {
93 mDNSPrefetchDeferred
= false;
94 mDNSPrefetchRequested
= true;
97 void DNSPrefetchRequestDeferred() {
98 mDNSPrefetchDeferred
= true;
99 mDNSPrefetchRequested
= false;
102 bool IsDNSPrefetchRequestDeferred() const { return mDNSPrefetchDeferred
; }
104 // This could be a virtual function or something like that, but that would
105 // cause our subclasses to grow by two pointers, rather than just 1 byte at
107 nsIURI
* GetURIForDNSPrefetch(Element
& aOwner
);
110 SupportsDNSPrefetch()
111 : mInDNSPrefetch(false),
112 mDNSPrefetchRequested(false),
113 mDNSPrefetchDeferred(false),
114 mDestroyedCalled(false) {}
116 void CancelDNSPrefetch(Element
&);
117 void TryDNSPrefetch(Element
&);
119 // This MUST be called on the destructor of the Element subclass.
120 // Our own destructor ensures that.
121 void Destroyed(Element
& aOwner
) {
122 MOZ_DIAGNOSTIC_ASSERT(!mDestroyedCalled
,
123 "Multiple calls to SupportsDNSPrefetch::Destroyed?");
124 mDestroyedCalled
= true;
125 if (mInDNSPrefetch
) {
126 HTMLDNSPrefetch::ElementDestroyed(aOwner
, *this);
130 ~SupportsDNSPrefetch() {
131 MOZ_DIAGNOSTIC_ASSERT(mDestroyedCalled
,
132 "Need to call SupportsDNSPrefetch::Destroyed "
133 "from the owner element");
137 bool mInDNSPrefetch
: 1;
138 bool mDNSPrefetchRequested
: 1;
139 bool mDNSPrefetchDeferred
: 1;
140 bool mDestroyedCalled
: 1;
144 } // namespace mozilla