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 static nsresult
Prefetch(SupportsDNSPrefetch
&, Element
&, Priority
);
59 static nsresult
Prefetch(
60 const nsAString
& host
, bool isHttps
,
61 const OriginAttributes
& aPartitionedPrincipalOriginAttributes
,
62 nsIRequest::TRRMode aTRRMode
, Priority
);
63 static nsresult
CancelPrefetch(
64 const nsAString
& host
, bool isHttps
,
65 const OriginAttributes
& aPartitionedPrincipalOriginAttributes
,
66 nsIRequest::TRRMode aTRRMode
, Priority
, nsresult aReason
);
67 static nsresult
CancelPrefetch(SupportsDNSPrefetch
&, Element
&, Priority
,
69 static void ElementDestroyed(Element
&, SupportsDNSPrefetch
&);
72 static nsIDNSService::DNSFlags
PriorityToDNSServiceFlags(Priority
);
74 static nsresult
Prefetch(
75 const nsAString
& host
, bool isHttps
,
76 const OriginAttributes
& aPartitionedPrincipalOriginAttributes
,
77 nsIDNSService::DNSFlags flags
);
78 static nsresult
CancelPrefetch(
79 const nsAString
& hostname
, bool isHttps
,
80 const OriginAttributes
& aPartitionedPrincipalOriginAttributes
,
81 nsIDNSService::DNSFlags flags
, nsresult aReason
);
83 friend class net::NeckoParent
;
86 // Elements that support DNS prefetch are expected to subclass this.
87 class SupportsDNSPrefetch
{
89 bool IsInDNSPrefetch() { return mInDNSPrefetch
; }
90 void SetIsInDNSPrefetch() { mInDNSPrefetch
= true; }
91 void ClearIsInDNSPrefetch() { mInDNSPrefetch
= false; }
93 void DNSPrefetchRequestStarted() {
94 mDNSPrefetchDeferred
= false;
95 mDNSPrefetchRequested
= true;
98 void DNSPrefetchRequestDeferred() {
99 mDNSPrefetchDeferred
= true;
100 mDNSPrefetchRequested
= false;
103 bool IsDNSPrefetchRequestDeferred() const { return mDNSPrefetchDeferred
; }
105 // This could be a virtual function or something like that, but that would
106 // cause our subclasses to grow by two pointers, rather than just 1 byte at
108 nsIURI
* GetURIForDNSPrefetch(Element
& aOwner
);
111 SupportsDNSPrefetch()
112 : mInDNSPrefetch(false),
113 mDNSPrefetchRequested(false),
114 mDNSPrefetchDeferred(false),
115 mDestroyedCalled(false) {}
117 void CancelDNSPrefetch(Element
&);
118 void TryDNSPrefetch(Element
&);
120 // This MUST be called on the destructor of the Element subclass.
121 // Our own destructor ensures that.
122 void Destroyed(Element
& aOwner
) {
123 MOZ_DIAGNOSTIC_ASSERT(!mDestroyedCalled
,
124 "Multiple calls to SupportsDNSPrefetch::Destroyed?");
125 mDestroyedCalled
= true;
126 if (mInDNSPrefetch
) {
127 HTMLDNSPrefetch::ElementDestroyed(aOwner
, *this);
131 ~SupportsDNSPrefetch() {
132 MOZ_DIAGNOSTIC_ASSERT(mDestroyedCalled
,
133 "Need to call SupportsDNSPrefetch::Destroyed "
134 "from the owner element");
138 bool mInDNSPrefetch
: 1;
139 bool mDNSPrefetchRequested
: 1;
140 bool mDNSPrefetchDeferred
: 1;
141 bool mDestroyedCalled
: 1;
145 } // namespace mozilla