Bug 1550804 - Add color scheme simulation to the inspector. r=pbro
[gecko.git] / uriloader / prefetch / nsPrefetchService.h
blob6e156df7c5492018256c535e29fcc21f4e343179
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;
25 class nsIReferrerInfo;
27 //-----------------------------------------------------------------------------
28 // nsPrefetchService
29 //-----------------------------------------------------------------------------
31 class nsPrefetchService final : public nsIPrefetchService,
32 public nsIWebProgressListener,
33 public nsIObserver,
34 public nsSupportsWeakReference {
35 public:
36 NS_DECL_ISUPPORTS
37 NS_DECL_NSIPREFETCHSERVICE
38 NS_DECL_NSIWEBPROGRESSLISTENER
39 NS_DECL_NSIOBSERVER
41 nsPrefetchService();
43 nsresult Init();
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);
51 private:
52 ~nsPrefetchService();
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);
69 void StopAll();
70 nsresult CheckURIScheme(nsIURI* aURI, nsIReferrerInfo* aReferrerInfo);
72 std::deque<RefPtr<nsPrefetchNode>> mPrefetchQueue;
73 nsTArray<RefPtr<nsPrefetchNode>> mCurrentNodes;
74 int32_t mMaxParallelism;
75 int32_t mStopCount;
76 bool mHaveProcessed;
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.
84 bool mAggressive;
87 //-----------------------------------------------------------------------------
88 // nsPreFetchingNode
89 //-----------------------------------------------------------------------------
91 class nsPrefetchNode final : public nsIStreamListener,
92 public nsIInterfaceRequestor,
93 public nsIChannelEventSink,
94 public nsIRedirectResultListener {
95 public:
96 NS_DECL_ISUPPORTS
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.
120 bool mPreload;
122 private:
123 ~nsPrefetchNode() {}
125 RefPtr<nsPrefetchService> mService;
126 nsCOMPtr<nsIChannel> mChannel;
127 nsCOMPtr<nsIChannel> mRedirectChannel;
128 int64_t mBytesRead;
129 bool mShouldFireLoadEvent;
132 #endif // !nsPrefetchService_h__