Bug 1734943 [wpt PR 31170] - Correct scrolling contents cull rect, a=testonly
[gecko.git] / uriloader / prefetch / nsPrefetchService.h
blob7b931ba52c49011f35279d0a9532b0b6fed956e7
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"
16 #include "nsIURI.h"
17 #include "nsWeakReference.h"
18 #include "nsCOMPtr.h"
19 #include "mozilla/Attributes.h"
20 #include <deque>
22 class nsPrefetchService;
23 class nsPrefetchNode;
24 class nsIReferrerInfo;
26 //-----------------------------------------------------------------------------
27 // nsPrefetchService
28 //-----------------------------------------------------------------------------
30 class nsPrefetchService final : public nsIPrefetchService,
31 public nsIWebProgressListener,
32 public nsIObserver,
33 public nsSupportsWeakReference {
34 public:
35 NS_DECL_ISUPPORTS
36 NS_DECL_NSIPREFETCHSERVICE
37 NS_DECL_NSIWEBPROGRESSLISTENER
38 NS_DECL_NSIOBSERVER
40 nsPrefetchService();
42 nsresult Init();
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);
50 private:
51 ~nsPrefetchService();
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);
68 void StopAll();
69 nsresult CheckURIScheme(nsIURI* aURI, nsIReferrerInfo* aReferrerInfo);
71 std::deque<RefPtr<nsPrefetchNode>> mPrefetchQueue;
72 nsTArray<RefPtr<nsPrefetchNode>> mCurrentNodes;
73 int32_t mMaxParallelism;
74 int32_t mStopCount;
75 bool mHaveProcessed;
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.
82 bool mAggressive;
85 //-----------------------------------------------------------------------------
86 // nsPreFetchingNode
87 //-----------------------------------------------------------------------------
89 class nsPrefetchNode final : public nsIStreamListener,
90 public nsIInterfaceRequestor,
91 public nsIChannelEventSink,
92 public nsIRedirectResultListener {
93 public:
94 NS_DECL_ISUPPORTS
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.
118 bool mPreload;
120 private:
121 ~nsPrefetchNode() {}
123 RefPtr<nsPrefetchService> mService;
124 nsCOMPtr<nsIChannel> mChannel;
125 nsCOMPtr<nsIChannel> mRedirectChannel;
126 int64_t mBytesRead;
127 bool mShouldFireLoadEvent;
130 #endif // !nsPrefetchService_h__