|
1 /* vim: set ts=2 sts=2 et sw=2: */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #ifndef mozilla_net_Seer_h |
|
7 #define mozilla_net_Seer_h |
|
8 |
|
9 #include "nsINetworkSeer.h" |
|
10 |
|
11 #include "nsCOMPtr.h" |
|
12 #include "nsIDNSListener.h" |
|
13 #include "nsIInterfaceRequestor.h" |
|
14 #include "nsIObserver.h" |
|
15 #include "nsISpeculativeConnect.h" |
|
16 #include "nsProxyRelease.h" |
|
17 |
|
18 #include "mozilla/Mutex.h" |
|
19 #include "mozilla/storage/StatementCache.h" |
|
20 #include "mozilla/TimeStamp.h" |
|
21 |
|
22 class nsIDNSService; |
|
23 class nsINetworkSeerVerifier; |
|
24 class nsIThread; |
|
25 class nsITimer; |
|
26 |
|
27 class mozIStorageConnection; |
|
28 class mozIStorageService; |
|
29 class mozIStorageStatement; |
|
30 |
|
31 namespace mozilla { |
|
32 namespace net { |
|
33 |
|
34 typedef nsMainThreadPtrHandle<nsINetworkSeerVerifier> SeerVerifierHandle; |
|
35 |
|
36 class SeerPredictionRunner; |
|
37 struct SeerTelemetryAccumulators; |
|
38 class SeerDNSListener; |
|
39 |
|
40 class Seer : public nsINetworkSeer |
|
41 , public nsIObserver |
|
42 , public nsISpeculativeConnectionOverrider |
|
43 , public nsIInterfaceRequestor |
|
44 { |
|
45 public: |
|
46 NS_DECL_ISUPPORTS |
|
47 NS_DECL_NSINETWORKSEER |
|
48 NS_DECL_NSIOBSERVER |
|
49 NS_DECL_NSISPECULATIVECONNECTIONOVERRIDER |
|
50 NS_DECL_NSIINTERFACEREQUESTOR |
|
51 |
|
52 Seer(); |
|
53 virtual ~Seer(); |
|
54 |
|
55 nsresult Init(); |
|
56 void Shutdown(); |
|
57 static nsresult Create(nsISupports *outer, const nsIID& iid, void **result); |
|
58 |
|
59 private: |
|
60 friend class SeerPredictionEvent; |
|
61 friend class SeerLearnEvent; |
|
62 friend class SeerResetEvent; |
|
63 friend class SeerPredictionRunner; |
|
64 friend class SeerDBShutdownRunner; |
|
65 friend class SeerCommitTimerInitEvent; |
|
66 friend class SeerNewTransactionEvent; |
|
67 friend class SeerCleanupEvent; |
|
68 |
|
69 void CheckForAndDeleteOldDBFile(); |
|
70 nsresult EnsureInitStorage(); |
|
71 |
|
72 // This is a proxy for the information we need from an nsIURI |
|
73 struct UriInfo { |
|
74 nsAutoCString spec; |
|
75 nsAutoCString origin; |
|
76 }; |
|
77 |
|
78 void PredictForLink(nsIURI *targetURI, |
|
79 nsIURI *sourceURI, |
|
80 nsINetworkSeerVerifier *verifier); |
|
81 void PredictForPageload(const UriInfo &dest, |
|
82 SeerVerifierHandle &verifier, |
|
83 int stackCount, |
|
84 TimeStamp &predictStartTime); |
|
85 void PredictForStartup(SeerVerifierHandle &verifier, |
|
86 TimeStamp &predictStartTime); |
|
87 |
|
88 // Whether we're working on a page or an origin |
|
89 enum QueryType { |
|
90 QUERY_PAGE = 0, |
|
91 QUERY_ORIGIN |
|
92 }; |
|
93 |
|
94 // Holds info from the db about a top-level page or origin |
|
95 struct TopLevelInfo { |
|
96 int32_t id; |
|
97 int32_t loadCount; |
|
98 PRTime lastLoad; |
|
99 }; |
|
100 |
|
101 // Holds info from the db about a subresource |
|
102 struct SubresourceInfo { |
|
103 int32_t id; |
|
104 int32_t hitCount; |
|
105 PRTime lastHit; |
|
106 }; |
|
107 |
|
108 nsresult ReserveSpaceInQueue(); |
|
109 void FreeSpaceInQueue(); |
|
110 |
|
111 int CalculateGlobalDegradation(PRTime now, |
|
112 PRTime lastLoad); |
|
113 int CalculateConfidence(int baseConfidence, |
|
114 PRTime lastHit, |
|
115 PRTime lastPossible, |
|
116 int globalDegradation); |
|
117 void SetupPrediction(int confidence, |
|
118 const nsACString &uri, |
|
119 SeerPredictionRunner *runner); |
|
120 |
|
121 bool LookupTopLevel(QueryType queryType, |
|
122 const nsACString &key, |
|
123 TopLevelInfo &info); |
|
124 void AddTopLevel(QueryType queryType, |
|
125 const nsACString &key, |
|
126 PRTime now); |
|
127 void UpdateTopLevel(QueryType queryType, |
|
128 const TopLevelInfo &info, |
|
129 PRTime now); |
|
130 bool TryPredict(QueryType queryType, |
|
131 const TopLevelInfo &info, |
|
132 PRTime now, |
|
133 SeerVerifierHandle &verifier, |
|
134 TimeStamp &predictStartTime); |
|
135 bool WouldRedirect(const TopLevelInfo &info, |
|
136 PRTime now, |
|
137 UriInfo &newUri); |
|
138 |
|
139 bool LookupSubresource(QueryType queryType, |
|
140 const int32_t parentId, |
|
141 const nsACString &key, |
|
142 SubresourceInfo &info); |
|
143 void AddSubresource(QueryType queryType, |
|
144 const int32_t parentId, |
|
145 const nsACString &key, PRTime now); |
|
146 void UpdateSubresource(QueryType queryType, |
|
147 const SubresourceInfo &info, |
|
148 PRTime now); |
|
149 |
|
150 void MaybeLearnForStartup(const UriInfo &uri, const PRTime now); |
|
151 |
|
152 void LearnForToplevel(const UriInfo &uri); |
|
153 void LearnForSubresource(const UriInfo &targetURI, const UriInfo &sourceURI); |
|
154 void LearnForRedirect(const UriInfo &targetURI, const UriInfo &sourceURI); |
|
155 void LearnForStartup(const UriInfo &uri); |
|
156 |
|
157 void ResetInternal(); |
|
158 |
|
159 void BeginTransaction() |
|
160 { |
|
161 mDB->BeginTransaction(); |
|
162 } |
|
163 |
|
164 void CommitTransaction() |
|
165 { |
|
166 mDB->CommitTransaction(); |
|
167 } |
|
168 |
|
169 int64_t GetDBFileSize(); |
|
170 int64_t GetDBFileSizeAfterVacuum(); |
|
171 void MaybeScheduleCleanup(); |
|
172 void Cleanup(); |
|
173 void CleanupOrigins(PRTime now); |
|
174 void CleanupStartupPages(PRTime now); |
|
175 int32_t GetSubresourceCount(); |
|
176 |
|
177 void VacuumDatabase(); |
|
178 |
|
179 // Observer-related stuff |
|
180 nsresult InstallObserver(); |
|
181 void RemoveObserver(); |
|
182 |
|
183 bool mInitialized; |
|
184 |
|
185 bool mEnabled; |
|
186 bool mEnableHoverOnSSL; |
|
187 |
|
188 int mPageDegradationDay; |
|
189 int mPageDegradationWeek; |
|
190 int mPageDegradationMonth; |
|
191 int mPageDegradationYear; |
|
192 int mPageDegradationMax; |
|
193 |
|
194 int mSubresourceDegradationDay; |
|
195 int mSubresourceDegradationWeek; |
|
196 int mSubresourceDegradationMonth; |
|
197 int mSubresourceDegradationYear; |
|
198 int mSubresourceDegradationMax; |
|
199 |
|
200 int mPreconnectMinConfidence; |
|
201 int mPreresolveMinConfidence; |
|
202 int mRedirectLikelyConfidence; |
|
203 |
|
204 int32_t mMaxQueueSize; |
|
205 |
|
206 nsCOMPtr<nsIThread> mIOThread; |
|
207 |
|
208 nsCOMPtr<nsISpeculativeConnect> mSpeculativeService; |
|
209 |
|
210 nsCOMPtr<nsIFile> mDBFile; |
|
211 nsCOMPtr<mozIStorageService> mStorageService; |
|
212 nsCOMPtr<mozIStorageConnection> mDB; |
|
213 mozilla::storage::StatementCache<mozIStorageStatement> mStatements; |
|
214 |
|
215 PRTime mStartupTime; |
|
216 PRTime mLastStartupTime; |
|
217 int32_t mStartupCount; |
|
218 |
|
219 nsCOMPtr<nsIDNSService> mDnsService; |
|
220 |
|
221 int32_t mQueueSize; |
|
222 mozilla::Mutex mQueueSizeLock; |
|
223 |
|
224 nsAutoPtr<SeerTelemetryAccumulators> mAccumulators; |
|
225 |
|
226 nsRefPtr<SeerDNSListener> mDNSListener; |
|
227 |
|
228 nsCOMPtr<nsITimer> mCommitTimer; |
|
229 |
|
230 #ifdef SEER_TESTS |
|
231 friend class SeerPrepareForDnsTestEvent; |
|
232 void PrepareForDnsTestInternal(int64_t timestamp, const nsACString &uri); |
|
233 #endif |
|
234 |
|
235 bool mCleanupScheduled; |
|
236 int32_t mMaxDBSize; |
|
237 int32_t mPreservePercentage; |
|
238 PRTime mLastCleanupTime; |
|
239 }; |
|
240 |
|
241 } // ::mozilla::net |
|
242 } // ::mozilla |
|
243 |
|
244 #endif // mozilla_net_Seer_h |