Bug 1523562 [wpt PR 15079] - Pass the full path to the flake8 config files, a=testonly
[gecko.git] / uriloader / prefetch / nsPrefetchService.h
blobb4ecea900a33815066b588bed40900180356cd92
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 "nsAutoPtr.h"
20 #include "mozilla/Attributes.h"
21 #include <deque>
23 class nsPrefetchService;
24 class nsPrefetchNode;
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, nsIURI *aReferrerURI, nsINode *aSource,
54 bool aExplicit);
56 nsresult Preload(nsIURI *aURI, nsIURI *aReferrerURI, nsINode *aSource,
57 nsContentPolicyType aPolicyType);
59 void AddProgressListener();
60 void RemoveProgressListener();
61 nsresult EnqueueURI(nsIURI *aURI, nsIURI *aReferrerURI, nsINode *aSource,
62 nsPrefetchNode **node);
63 void EmptyPrefetchQueue();
65 void StartPrefetching();
66 void StopPrefetching();
67 void StopCurrentPrefetchsPreloads(bool aPreload);
68 void StopAll();
69 nsresult CheckURIScheme(nsIURI *aURI, nsIURI *aReferrerURI);
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;
77 bool mPreloadDisabled;
79 // In usual case prefetch does not start until all normal loads are done.
80 // Aggressive mode ignores normal loads and just start prefetch ASAP.
81 // It's mainly for testing purpose and discoraged for normal use;
82 // see https://bugzilla.mozilla.org/show_bug.cgi?id=1281415 for details.
83 bool mAggressive;
86 //-----------------------------------------------------------------------------
87 // nsPreFetchingNode
88 //-----------------------------------------------------------------------------
90 class nsPrefetchNode final : public nsIStreamListener,
91 public nsIInterfaceRequestor,
92 public nsIChannelEventSink,
93 public nsIRedirectResultListener {
94 public:
95 NS_DECL_ISUPPORTS
96 NS_DECL_NSIREQUESTOBSERVER
97 NS_DECL_NSISTREAMLISTENER
98 NS_DECL_NSIINTERFACEREQUESTOR
99 NS_DECL_NSICHANNELEVENTSINK
100 NS_DECL_NSIREDIRECTRESULTLISTENER
102 nsPrefetchNode(nsPrefetchService *aPrefetchService, nsIURI *aURI,
103 nsIURI *aReferrerURI, nsINode *aSource,
104 nsContentPolicyType aPolicyType, bool aPreload);
106 nsresult OpenChannel();
107 nsresult CancelChannel(nsresult error);
109 nsCOMPtr<nsIURI> mURI;
110 nsCOMPtr<nsIURI> mReferrerURI;
111 nsTArray<nsWeakPtr> mSources;
113 // The policy type to be used for fetching the resource.
114 nsContentPolicyType mPolicyType;
115 // nsPrefetchNode is used for prefetching and preloading resource.
116 // mPreload is true if a resource is preloaded. Preloads and
117 // prefetches are fetched in different phases (during load and
118 // after a page load), therefore we need to distinguish them.
119 bool mPreload;
121 private:
122 ~nsPrefetchNode() {}
124 RefPtr<nsPrefetchService> mService;
125 nsCOMPtr<nsIChannel> mChannel;
126 nsCOMPtr<nsIChannel> mRedirectChannel;
127 int64_t mBytesRead;
128 bool mShouldFireLoadEvent;
131 #endif // !nsPrefetchService_h__