Line data Source code
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/. */
4 :
5 : #ifndef nsPrefetchService_h__
6 : #define nsPrefetchService_h__
7 :
8 : #include "nsCPrefetchService.h"
9 : #include "nsIObserver.h"
10 : #include "nsIInterfaceRequestor.h"
11 : #include "nsIChannelEventSink.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>
22 :
23 : class nsPrefetchService;
24 : class nsPrefetchNode;
25 :
26 : //-----------------------------------------------------------------------------
27 : // nsPrefetchService
28 : //-----------------------------------------------------------------------------
29 :
30 : class nsPrefetchService final : public nsIPrefetchService
31 : , public nsIWebProgressListener
32 : , public nsIObserver
33 : , public nsSupportsWeakReference
34 : {
35 : public:
36 : NS_DECL_ISUPPORTS
37 : NS_DECL_NSIPREFETCHSERVICE
38 : NS_DECL_NSIWEBPROGRESSLISTENER
39 : NS_DECL_NSIOBSERVER
40 :
41 : nsPrefetchService();
42 :
43 : nsresult Init();
44 : void RemoveNodeAndMaybeStartNextPrefetchURI(nsPrefetchNode *aFinished);
45 : void ProcessNextPrefetchURI();
46 :
47 : void NotifyLoadRequested(nsPrefetchNode *node);
48 : void NotifyLoadCompleted(nsPrefetchNode *node);
49 : void DispatchEvent(nsPrefetchNode *node, bool aSuccess);
50 :
51 : private:
52 : ~nsPrefetchService();
53 :
54 : nsresult Prefetch(nsIURI *aURI,
55 : nsIURI *aReferrerURI,
56 : nsIDOMNode *aSource,
57 : bool aExplicit);
58 :
59 : nsresult Preload(nsIURI *aURI,
60 : nsIURI *aReferrerURI,
61 : nsIDOMNode *aSource,
62 : nsContentPolicyType aPolicyType);
63 :
64 : void AddProgressListener();
65 : void RemoveProgressListener();
66 : nsresult EnqueueURI(nsIURI *aURI, nsIURI *aReferrerURI,
67 : nsIDOMNode *aSource, nsPrefetchNode **node);
68 : void EmptyPrefetchQueue();
69 :
70 : void StartPrefetching();
71 : void StopPrefetching();
72 : void StopCurrentPrefetchsPreloads(bool aPreload);
73 : void StopAll();
74 : nsresult CheckURIScheme(nsIURI *aURI, nsIURI *aReferrerURI);
75 :
76 : std::deque<RefPtr<nsPrefetchNode>> mPrefetchQueue;
77 : nsTArray<RefPtr<nsPrefetchNode>> mCurrentNodes;
78 : int32_t mMaxParallelism;
79 : int32_t mStopCount;
80 : bool mHaveProcessed;
81 : bool mPrefetchDisabled;
82 : bool mPreloadDisabled;
83 :
84 : // In usual case prefetch does not start until all normal loads are done.
85 : // Aggressive mode ignores normal loads and just start prefetch ASAP.
86 : // It's mainly for testing purpose and discoraged for normal use;
87 : // see https://bugzilla.mozilla.org/show_bug.cgi?id=1281415 for details.
88 : bool mAggressive;
89 : };
90 :
91 : //-----------------------------------------------------------------------------
92 : // nsPreFetchingNode
93 : //-----------------------------------------------------------------------------
94 :
95 : class nsPrefetchNode final : public nsIStreamListener
96 : , public nsIInterfaceRequestor
97 : , public nsIChannelEventSink
98 : , public nsIRedirectResultListener
99 : {
100 : public:
101 : NS_DECL_ISUPPORTS
102 : NS_DECL_NSIREQUESTOBSERVER
103 : NS_DECL_NSISTREAMLISTENER
104 : NS_DECL_NSIINTERFACEREQUESTOR
105 : NS_DECL_NSICHANNELEVENTSINK
106 : NS_DECL_NSIREDIRECTRESULTLISTENER
107 :
108 : nsPrefetchNode(nsPrefetchService *aPrefetchService,
109 : nsIURI *aURI,
110 : nsIURI *aReferrerURI,
111 : nsIDOMNode *aSource,
112 : nsContentPolicyType aPolicyType,
113 : bool aPreload);
114 :
115 : nsresult OpenChannel();
116 : nsresult CancelChannel(nsresult error);
117 :
118 : nsCOMPtr<nsIURI> mURI;
119 : nsCOMPtr<nsIURI> mReferrerURI;
120 : nsTArray<nsCOMPtr<nsIWeakReference>> mSources;
121 :
122 : // The policy type to be used for fetching the resource.
123 : nsContentPolicyType mPolicyType;
124 : // nsPrefetchNode is used for prefetching and preloading resource.
125 : // mPreload is true if a resource is preloaded. Preloads and
126 : // prefetches are fetched in different phases (during load and
127 : // after a page load), therefore we need to distinguish them.
128 : bool mPreload;
129 :
130 : private:
131 0 : ~nsPrefetchNode() {}
132 :
133 : RefPtr<nsPrefetchService> mService;
134 : nsCOMPtr<nsIChannel> mChannel;
135 : nsCOMPtr<nsIChannel> mRedirectChannel;
136 : int64_t mBytesRead;
137 : bool mShouldFireLoadEvent;
138 : };
139 :
140 : #endif // !nsPrefetchService_h__
|