Bug 1867190 - Initialise the PHC allocate delay later r=glandium
[gecko.git] / dom / html / HTMLDNSPrefetch.h
blob5820a6ecb2f9bfdac94ec914418afe12814fe603
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___
10 #include "nsCOMPtr.h"
11 #include "nsIRequest.h"
12 #include "nsString.h"
13 #include "nsIDNSService.h"
15 class nsITimer;
16 class nsIURI;
17 namespace mozilla {
19 class OriginAttributes;
21 namespace net {
22 class NeckoParent;
23 } // namespace net
25 namespace dom {
26 class Document;
27 class Element;
29 class SupportsDNSPrefetch;
31 class HTMLDNSPrefetch {
32 public:
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
51 // more.
53 enum class Priority {
54 Low,
55 Medium,
56 High,
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,
68 nsresult aReason);
69 static void ElementDestroyed(Element&, SupportsDNSPrefetch&);
71 private:
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 {
88 public:
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
107 // most.
108 nsIURI* GetURIForDNSPrefetch(Element& aOwner);
110 protected:
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");
137 private:
138 bool mInDNSPrefetch : 1;
139 bool mDNSPrefetchRequested : 1;
140 bool mDNSPrefetchDeferred : 1;
141 bool mDestroyedCalled : 1;
144 } // namespace dom
145 } // namespace mozilla
147 #endif