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 "nsAutoPtr.h"
20 #include "mozilla/Attributes.h"
23 class nsPrefetchService
;
25 class nsIReferrerInfo
;
27 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
31 class nsPrefetchService final
: public nsIPrefetchService
,
32 public nsIWebProgressListener
,
34 public nsSupportsWeakReference
{
37 NS_DECL_NSIPREFETCHSERVICE
38 NS_DECL_NSIWEBPROGRESSLISTENER
44 void RemoveNodeAndMaybeStartNextPrefetchURI(nsPrefetchNode
* aFinished
);
45 void ProcessNextPrefetchURI();
47 void NotifyLoadRequested(nsPrefetchNode
* node
);
48 void NotifyLoadCompleted(nsPrefetchNode
* node
);
49 void DispatchEvent(nsPrefetchNode
* node
, bool aSuccess
);
54 nsresult
Prefetch(nsIURI
* aURI
, nsIReferrerInfo
* aReferrerInfo
,
55 nsINode
* aSource
, bool aExplicit
);
57 nsresult
Preload(nsIURI
* aURI
, nsIReferrerInfo
* aReferrerInfo
,
58 nsINode
* aSource
, nsContentPolicyType aPolicyType
);
60 void AddProgressListener();
61 void RemoveProgressListener();
62 nsresult
EnqueueURI(nsIURI
* aURI
, nsIReferrerInfo
* aReferrerInfo
,
63 nsINode
* aSource
, nsPrefetchNode
** node
);
64 void EmptyPrefetchQueue();
66 void StartPrefetching();
67 void StopPrefetching();
68 void StopCurrentPrefetchsPreloads(bool aPreload
);
70 nsresult
CheckURIScheme(nsIURI
* aURI
, nsIReferrerInfo
* aReferrerInfo
);
72 std::deque
<RefPtr
<nsPrefetchNode
>> mPrefetchQueue
;
73 nsTArray
<RefPtr
<nsPrefetchNode
>> mCurrentNodes
;
74 int32_t mMaxParallelism
;
77 bool mPrefetchDisabled
;
78 bool mPreloadDisabled
;
80 // In usual case prefetch does not start until all normal loads are done.
81 // Aggressive mode ignores normal loads and just start prefetch ASAP.
82 // It's mainly for testing purpose and discoraged for normal use;
83 // see https://bugzilla.mozilla.org/show_bug.cgi?id=1281415 for details.
87 //-----------------------------------------------------------------------------
89 //-----------------------------------------------------------------------------
91 class nsPrefetchNode final
: public nsIStreamListener
,
92 public nsIInterfaceRequestor
,
93 public nsIChannelEventSink
,
94 public nsIRedirectResultListener
{
97 NS_DECL_NSIREQUESTOBSERVER
98 NS_DECL_NSISTREAMLISTENER
99 NS_DECL_NSIINTERFACEREQUESTOR
100 NS_DECL_NSICHANNELEVENTSINK
101 NS_DECL_NSIREDIRECTRESULTLISTENER
103 nsPrefetchNode(nsPrefetchService
* aPrefetchService
, nsIURI
* aURI
,
104 nsIReferrerInfo
* aReferrerInfo
, nsINode
* aSource
,
105 nsContentPolicyType aPolicyType
, bool aPreload
);
107 nsresult
OpenChannel();
108 nsresult
CancelChannel(nsresult error
);
110 nsCOMPtr
<nsIURI
> mURI
;
111 nsCOMPtr
<nsIReferrerInfo
> mReferrerInfo
;
112 nsTArray
<nsWeakPtr
> mSources
;
114 // The policy type to be used for fetching the resource.
115 nsContentPolicyType mPolicyType
;
116 // nsPrefetchNode is used for prefetching and preloading resource.
117 // mPreload is true if a resource is preloaded. Preloads and
118 // prefetches are fetched in different phases (during load and
119 // after a page load), therefore we need to distinguish them.
125 RefPtr
<nsPrefetchService
> mService
;
126 nsCOMPtr
<nsIChannel
> mChannel
;
127 nsCOMPtr
<nsIChannel
> mRedirectChannel
;
129 bool mShouldFireLoadEvent
;
132 #endif // !nsPrefetchService_h__