1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef nsPrefetchService_h__
6 #define nsPrefetchService_h__
8 #include "nsIObserver.h"
9 #include "nsIInterfaceRequestor.h"
10 #include "nsIChannelEventSink.h"
11 #include "nsIPrefetchService.h"
12 #include "nsIRedirectResultListener.h"
13 #include "nsIWebProgressListener.h"
14 #include "nsIStreamListener.h"
15 #include "nsIChannel.h"
17 #include "nsWeakReference.h"
19 #include "mozilla/Attributes.h"
22 class nsPrefetchService
;
24 class nsIReferrerInfo
;
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 class nsPrefetchService final
: public nsIPrefetchService
,
31 public nsIWebProgressListener
,
33 public nsSupportsWeakReference
{
36 NS_DECL_NSIPREFETCHSERVICE
37 NS_DECL_NSIWEBPROGRESSLISTENER
43 void RemoveNodeAndMaybeStartNextPrefetchURI(nsPrefetchNode
* aFinished
);
44 void ProcessNextPrefetchURI();
46 void NotifyLoadRequested(nsPrefetchNode
* node
);
47 void NotifyLoadCompleted(nsPrefetchNode
* node
);
48 void DispatchEvent(nsPrefetchNode
* node
, bool aSuccess
);
53 nsresult
Prefetch(nsIURI
* aURI
, nsIReferrerInfo
* aReferrerInfo
,
54 nsINode
* aSource
, bool aExplicit
);
56 nsresult
Preload(nsIURI
* aURI
, nsIReferrerInfo
* aReferrerInfo
,
57 nsINode
* aSource
, nsContentPolicyType aPolicyType
);
59 void AddProgressListener();
60 void RemoveProgressListener();
61 nsresult
EnqueueURI(nsIURI
* aURI
, nsIReferrerInfo
* aReferrerInfo
,
62 nsINode
* aSource
, nsPrefetchNode
** node
);
63 void EmptyPrefetchQueue();
65 void StartPrefetching();
66 void StopPrefetching();
67 void StopCurrentPrefetchsPreloads(bool aPreload
);
69 nsresult
CheckURIScheme(nsIURI
* aURI
, nsIReferrerInfo
* aReferrerInfo
);
71 std::deque
<RefPtr
<nsPrefetchNode
>> mPrefetchQueue
;
72 nsTArray
<RefPtr
<nsPrefetchNode
>> mCurrentNodes
;
73 int32_t mMaxParallelism
;
76 bool mPrefetchDisabled
;
78 // In usual case prefetch does not start until all normal loads are done.
79 // Aggressive mode ignores normal loads and just start prefetch ASAP.
80 // It's mainly for testing purpose and discoraged for normal use;
81 // see https://bugzilla.mozilla.org/show_bug.cgi?id=1281415 for details.
85 //-----------------------------------------------------------------------------
87 //-----------------------------------------------------------------------------
89 class nsPrefetchNode final
: public nsIStreamListener
,
90 public nsIInterfaceRequestor
,
91 public nsIChannelEventSink
,
92 public nsIRedirectResultListener
{
95 NS_DECL_NSIREQUESTOBSERVER
96 NS_DECL_NSISTREAMLISTENER
97 NS_DECL_NSIINTERFACEREQUESTOR
98 NS_DECL_NSICHANNELEVENTSINK
99 NS_DECL_NSIREDIRECTRESULTLISTENER
101 nsPrefetchNode(nsPrefetchService
* aPrefetchService
, nsIURI
* aURI
,
102 nsIReferrerInfo
* aReferrerInfo
, nsINode
* aSource
,
103 nsContentPolicyType aPolicyType
, bool aPreload
);
105 nsresult
OpenChannel();
106 nsresult
CancelChannel(nsresult error
);
108 nsCOMPtr
<nsIURI
> mURI
;
109 nsCOMPtr
<nsIReferrerInfo
> mReferrerInfo
;
110 nsTArray
<nsWeakPtr
> mSources
;
112 // The policy type to be used for fetching the resource.
113 nsContentPolicyType mPolicyType
;
114 // nsPrefetchNode is used for prefetching and preloading resource.
115 // mPreload is true if a resource is preloaded. Preloads and
116 // prefetches are fetched in different phases (during load and
117 // after a page load), therefore we need to distinguish them.
123 RefPtr
<nsPrefetchService
> mService
;
124 nsCOMPtr
<nsIChannel
> mChannel
;
125 nsCOMPtr
<nsIChannel
> mRedirectChannel
;
127 bool mShouldFireLoadEvent
;
130 #endif // !nsPrefetchService_h__