Line data Source code
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/. */
6 :
7 : #ifndef nsHTMLDNSPrefetch_h___
8 : #define nsHTMLDNSPrefetch_h___
9 :
10 : #include "nsCOMPtr.h"
11 : #include "nsString.h"
12 :
13 : #include "nsIDNSListener.h"
14 : #include "nsIWebProgressListener.h"
15 : #include "nsWeakReference.h"
16 : #include "nsIObserver.h"
17 :
18 : class nsIDocument;
19 : class nsITimer;
20 : namespace mozilla {
21 : namespace dom {
22 : class Link;
23 : } // namespace dom
24 : } // namespace mozilla
25 :
26 : namespace mozilla {
27 : namespace net {
28 : class NeckoParent;
29 : } // namespace net
30 : } // namespace mozilla
31 :
32 : class nsHTMLDNSPrefetch
33 : {
34 : public:
35 : // The required aDocument parameter is the context requesting the prefetch - under
36 : // certain circumstances (e.g. headers, or security context) associated with
37 : // the context the prefetch will not be performed.
38 : static bool IsAllowed(nsIDocument *aDocument);
39 :
40 : static nsresult Initialize();
41 : static nsresult Shutdown();
42 :
43 : // Call one of the Prefetch* methods to start the lookup.
44 : //
45 : // The URI versions will defer DNS lookup until pageload is
46 : // complete, while the string versions submit the lookup to
47 : // the DNS system immediately. The URI version is somewhat lighter
48 : // weight, but its request is also more likely to be dropped due to a
49 : // full queue and it may only be used from the main thread.
50 :
51 : static nsresult PrefetchHigh(mozilla::dom::Link *aElement);
52 : static nsresult PrefetchMedium(mozilla::dom::Link *aElement);
53 : static nsresult PrefetchLow(mozilla::dom::Link *aElement);
54 : static nsresult PrefetchHigh(const nsAString &host,
55 : const mozilla::OriginAttributes &aOriginAttributes);
56 : static nsresult PrefetchMedium(const nsAString &host,
57 : const mozilla::OriginAttributes &aOriginAttributes);
58 : static nsresult PrefetchLow(const nsAString &host,
59 : const mozilla::OriginAttributes &aOriginAttributes);
60 : static nsresult CancelPrefetchLow(const nsAString &host,
61 : const mozilla::OriginAttributes &aOriginAttributes,
62 : nsresult aReason);
63 : static nsresult CancelPrefetchLow(mozilla::dom::Link *aElement,
64 : nsresult aReason);
65 :
66 : static void LinkDestroyed(mozilla::dom::Link* aLink);
67 :
68 : private:
69 : static nsresult Prefetch(const nsAString &host,
70 : const mozilla::OriginAttributes &aOriginAttributes,
71 : uint16_t flags);
72 : static nsresult Prefetch(mozilla::dom::Link *aElement, uint16_t flags);
73 : static nsresult CancelPrefetch(const nsAString &hostname,
74 : const mozilla::OriginAttributes &aOriginAttributes,
75 : uint16_t flags,
76 : nsresult aReason);
77 : static nsresult CancelPrefetch(mozilla::dom::Link *aElement,
78 : uint16_t flags,
79 : nsresult aReason);
80 :
81 : public:
82 : class nsListener final : public nsIDNSListener
83 : {
84 : // This class exists to give a safe callback no-op DNSListener
85 : public:
86 : NS_DECL_THREADSAFE_ISUPPORTS
87 : NS_DECL_NSIDNSLISTENER
88 :
89 3 : nsListener() {}
90 : private:
91 0 : ~nsListener() {}
92 : };
93 :
94 : class nsDeferrals final: public nsIWebProgressListener
95 : , public nsSupportsWeakReference
96 : , public nsIObserver
97 : {
98 : public:
99 : NS_DECL_ISUPPORTS
100 : NS_DECL_NSIWEBPROGRESSLISTENER
101 : NS_DECL_NSIOBSERVER
102 :
103 : nsDeferrals();
104 :
105 : void Activate();
106 : nsresult Add(uint16_t flags, mozilla::dom::Link *aElement);
107 :
108 : void RemoveUnboundLinks();
109 :
110 : private:
111 : ~nsDeferrals();
112 : void Flush();
113 :
114 : void SubmitQueue();
115 :
116 : uint16_t mHead;
117 : uint16_t mTail;
118 : uint32_t mActiveLoaderCount;
119 :
120 : nsCOMPtr<nsITimer> mTimer;
121 : bool mTimerArmed;
122 : static void Tick(nsITimer *aTimer, void *aClosure);
123 :
124 : static const int sMaxDeferred = 512; // keep power of 2 for masking
125 : static const int sMaxDeferredMask = (sMaxDeferred - 1);
126 :
127 : struct deferred_entry
128 : {
129 : uint16_t mFlags;
130 : // Link implementation clears this raw pointer in its destructor.
131 : mozilla::dom::Link* mElement;
132 : } mEntries[sMaxDeferred];
133 : };
134 :
135 : friend class mozilla::net::NeckoParent;
136 : };
137 :
138 : #endif
|