Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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/. */
6 #ifndef mozilla_net_Seer_h
7 #define mozilla_net_Seer_h
9 #include "nsINetworkSeer.h"
11 #include "nsCOMPtr.h"
12 #include "nsIDNSListener.h"
13 #include "nsIInterfaceRequestor.h"
14 #include "nsIObserver.h"
15 #include "nsISpeculativeConnect.h"
16 #include "nsProxyRelease.h"
18 #include "mozilla/Mutex.h"
19 #include "mozilla/storage/StatementCache.h"
20 #include "mozilla/TimeStamp.h"
22 class nsIDNSService;
23 class nsINetworkSeerVerifier;
24 class nsIThread;
25 class nsITimer;
27 class mozIStorageConnection;
28 class mozIStorageService;
29 class mozIStorageStatement;
31 namespace mozilla {
32 namespace net {
34 typedef nsMainThreadPtrHandle<nsINetworkSeerVerifier> SeerVerifierHandle;
36 class SeerPredictionRunner;
37 struct SeerTelemetryAccumulators;
38 class SeerDNSListener;
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
52 Seer();
53 virtual ~Seer();
55 nsresult Init();
56 void Shutdown();
57 static nsresult Create(nsISupports *outer, const nsIID& iid, void **result);
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;
69 void CheckForAndDeleteOldDBFile();
70 nsresult EnsureInitStorage();
72 // This is a proxy for the information we need from an nsIURI
73 struct UriInfo {
74 nsAutoCString spec;
75 nsAutoCString origin;
76 };
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);
88 // Whether we're working on a page or an origin
89 enum QueryType {
90 QUERY_PAGE = 0,
91 QUERY_ORIGIN
92 };
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 };
101 // Holds info from the db about a subresource
102 struct SubresourceInfo {
103 int32_t id;
104 int32_t hitCount;
105 PRTime lastHit;
106 };
108 nsresult ReserveSpaceInQueue();
109 void FreeSpaceInQueue();
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);
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);
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);
150 void MaybeLearnForStartup(const UriInfo &uri, const PRTime now);
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);
157 void ResetInternal();
159 void BeginTransaction()
160 {
161 mDB->BeginTransaction();
162 }
164 void CommitTransaction()
165 {
166 mDB->CommitTransaction();
167 }
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();
177 void VacuumDatabase();
179 // Observer-related stuff
180 nsresult InstallObserver();
181 void RemoveObserver();
183 bool mInitialized;
185 bool mEnabled;
186 bool mEnableHoverOnSSL;
188 int mPageDegradationDay;
189 int mPageDegradationWeek;
190 int mPageDegradationMonth;
191 int mPageDegradationYear;
192 int mPageDegradationMax;
194 int mSubresourceDegradationDay;
195 int mSubresourceDegradationWeek;
196 int mSubresourceDegradationMonth;
197 int mSubresourceDegradationYear;
198 int mSubresourceDegradationMax;
200 int mPreconnectMinConfidence;
201 int mPreresolveMinConfidence;
202 int mRedirectLikelyConfidence;
204 int32_t mMaxQueueSize;
206 nsCOMPtr<nsIThread> mIOThread;
208 nsCOMPtr<nsISpeculativeConnect> mSpeculativeService;
210 nsCOMPtr<nsIFile> mDBFile;
211 nsCOMPtr<mozIStorageService> mStorageService;
212 nsCOMPtr<mozIStorageConnection> mDB;
213 mozilla::storage::StatementCache<mozIStorageStatement> mStatements;
215 PRTime mStartupTime;
216 PRTime mLastStartupTime;
217 int32_t mStartupCount;
219 nsCOMPtr<nsIDNSService> mDnsService;
221 int32_t mQueueSize;
222 mozilla::Mutex mQueueSizeLock;
224 nsAutoPtr<SeerTelemetryAccumulators> mAccumulators;
226 nsRefPtr<SeerDNSListener> mDNSListener;
228 nsCOMPtr<nsITimer> mCommitTimer;
230 #ifdef SEER_TESTS
231 friend class SeerPrepareForDnsTestEvent;
232 void PrepareForDnsTestInternal(int64_t timestamp, const nsACString &uri);
233 #endif
235 bool mCleanupScheduled;
236 int32_t mMaxDBSize;
237 int32_t mPreservePercentage;
238 PRTime mLastCleanupTime;
239 };
241 } // ::mozilla::net
242 } // ::mozilla
244 #endif // mozilla_net_Seer_h