1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/base/src/Seer.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,244 @@ 1.4 +/* vim: set ts=2 sts=2 et sw=2: */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef mozilla_net_Seer_h 1.10 +#define mozilla_net_Seer_h 1.11 + 1.12 +#include "nsINetworkSeer.h" 1.13 + 1.14 +#include "nsCOMPtr.h" 1.15 +#include "nsIDNSListener.h" 1.16 +#include "nsIInterfaceRequestor.h" 1.17 +#include "nsIObserver.h" 1.18 +#include "nsISpeculativeConnect.h" 1.19 +#include "nsProxyRelease.h" 1.20 + 1.21 +#include "mozilla/Mutex.h" 1.22 +#include "mozilla/storage/StatementCache.h" 1.23 +#include "mozilla/TimeStamp.h" 1.24 + 1.25 +class nsIDNSService; 1.26 +class nsINetworkSeerVerifier; 1.27 +class nsIThread; 1.28 +class nsITimer; 1.29 + 1.30 +class mozIStorageConnection; 1.31 +class mozIStorageService; 1.32 +class mozIStorageStatement; 1.33 + 1.34 +namespace mozilla { 1.35 +namespace net { 1.36 + 1.37 +typedef nsMainThreadPtrHandle<nsINetworkSeerVerifier> SeerVerifierHandle; 1.38 + 1.39 +class SeerPredictionRunner; 1.40 +struct SeerTelemetryAccumulators; 1.41 +class SeerDNSListener; 1.42 + 1.43 +class Seer : public nsINetworkSeer 1.44 + , public nsIObserver 1.45 + , public nsISpeculativeConnectionOverrider 1.46 + , public nsIInterfaceRequestor 1.47 +{ 1.48 +public: 1.49 + NS_DECL_ISUPPORTS 1.50 + NS_DECL_NSINETWORKSEER 1.51 + NS_DECL_NSIOBSERVER 1.52 + NS_DECL_NSISPECULATIVECONNECTIONOVERRIDER 1.53 + NS_DECL_NSIINTERFACEREQUESTOR 1.54 + 1.55 + Seer(); 1.56 + virtual ~Seer(); 1.57 + 1.58 + nsresult Init(); 1.59 + void Shutdown(); 1.60 + static nsresult Create(nsISupports *outer, const nsIID& iid, void **result); 1.61 + 1.62 +private: 1.63 + friend class SeerPredictionEvent; 1.64 + friend class SeerLearnEvent; 1.65 + friend class SeerResetEvent; 1.66 + friend class SeerPredictionRunner; 1.67 + friend class SeerDBShutdownRunner; 1.68 + friend class SeerCommitTimerInitEvent; 1.69 + friend class SeerNewTransactionEvent; 1.70 + friend class SeerCleanupEvent; 1.71 + 1.72 + void CheckForAndDeleteOldDBFile(); 1.73 + nsresult EnsureInitStorage(); 1.74 + 1.75 + // This is a proxy for the information we need from an nsIURI 1.76 + struct UriInfo { 1.77 + nsAutoCString spec; 1.78 + nsAutoCString origin; 1.79 + }; 1.80 + 1.81 + void PredictForLink(nsIURI *targetURI, 1.82 + nsIURI *sourceURI, 1.83 + nsINetworkSeerVerifier *verifier); 1.84 + void PredictForPageload(const UriInfo &dest, 1.85 + SeerVerifierHandle &verifier, 1.86 + int stackCount, 1.87 + TimeStamp &predictStartTime); 1.88 + void PredictForStartup(SeerVerifierHandle &verifier, 1.89 + TimeStamp &predictStartTime); 1.90 + 1.91 + // Whether we're working on a page or an origin 1.92 + enum QueryType { 1.93 + QUERY_PAGE = 0, 1.94 + QUERY_ORIGIN 1.95 + }; 1.96 + 1.97 + // Holds info from the db about a top-level page or origin 1.98 + struct TopLevelInfo { 1.99 + int32_t id; 1.100 + int32_t loadCount; 1.101 + PRTime lastLoad; 1.102 + }; 1.103 + 1.104 + // Holds info from the db about a subresource 1.105 + struct SubresourceInfo { 1.106 + int32_t id; 1.107 + int32_t hitCount; 1.108 + PRTime lastHit; 1.109 + }; 1.110 + 1.111 + nsresult ReserveSpaceInQueue(); 1.112 + void FreeSpaceInQueue(); 1.113 + 1.114 + int CalculateGlobalDegradation(PRTime now, 1.115 + PRTime lastLoad); 1.116 + int CalculateConfidence(int baseConfidence, 1.117 + PRTime lastHit, 1.118 + PRTime lastPossible, 1.119 + int globalDegradation); 1.120 + void SetupPrediction(int confidence, 1.121 + const nsACString &uri, 1.122 + SeerPredictionRunner *runner); 1.123 + 1.124 + bool LookupTopLevel(QueryType queryType, 1.125 + const nsACString &key, 1.126 + TopLevelInfo &info); 1.127 + void AddTopLevel(QueryType queryType, 1.128 + const nsACString &key, 1.129 + PRTime now); 1.130 + void UpdateTopLevel(QueryType queryType, 1.131 + const TopLevelInfo &info, 1.132 + PRTime now); 1.133 + bool TryPredict(QueryType queryType, 1.134 + const TopLevelInfo &info, 1.135 + PRTime now, 1.136 + SeerVerifierHandle &verifier, 1.137 + TimeStamp &predictStartTime); 1.138 + bool WouldRedirect(const TopLevelInfo &info, 1.139 + PRTime now, 1.140 + UriInfo &newUri); 1.141 + 1.142 + bool LookupSubresource(QueryType queryType, 1.143 + const int32_t parentId, 1.144 + const nsACString &key, 1.145 + SubresourceInfo &info); 1.146 + void AddSubresource(QueryType queryType, 1.147 + const int32_t parentId, 1.148 + const nsACString &key, PRTime now); 1.149 + void UpdateSubresource(QueryType queryType, 1.150 + const SubresourceInfo &info, 1.151 + PRTime now); 1.152 + 1.153 + void MaybeLearnForStartup(const UriInfo &uri, const PRTime now); 1.154 + 1.155 + void LearnForToplevel(const UriInfo &uri); 1.156 + void LearnForSubresource(const UriInfo &targetURI, const UriInfo &sourceURI); 1.157 + void LearnForRedirect(const UriInfo &targetURI, const UriInfo &sourceURI); 1.158 + void LearnForStartup(const UriInfo &uri); 1.159 + 1.160 + void ResetInternal(); 1.161 + 1.162 + void BeginTransaction() 1.163 + { 1.164 + mDB->BeginTransaction(); 1.165 + } 1.166 + 1.167 + void CommitTransaction() 1.168 + { 1.169 + mDB->CommitTransaction(); 1.170 + } 1.171 + 1.172 + int64_t GetDBFileSize(); 1.173 + int64_t GetDBFileSizeAfterVacuum(); 1.174 + void MaybeScheduleCleanup(); 1.175 + void Cleanup(); 1.176 + void CleanupOrigins(PRTime now); 1.177 + void CleanupStartupPages(PRTime now); 1.178 + int32_t GetSubresourceCount(); 1.179 + 1.180 + void VacuumDatabase(); 1.181 + 1.182 + // Observer-related stuff 1.183 + nsresult InstallObserver(); 1.184 + void RemoveObserver(); 1.185 + 1.186 + bool mInitialized; 1.187 + 1.188 + bool mEnabled; 1.189 + bool mEnableHoverOnSSL; 1.190 + 1.191 + int mPageDegradationDay; 1.192 + int mPageDegradationWeek; 1.193 + int mPageDegradationMonth; 1.194 + int mPageDegradationYear; 1.195 + int mPageDegradationMax; 1.196 + 1.197 + int mSubresourceDegradationDay; 1.198 + int mSubresourceDegradationWeek; 1.199 + int mSubresourceDegradationMonth; 1.200 + int mSubresourceDegradationYear; 1.201 + int mSubresourceDegradationMax; 1.202 + 1.203 + int mPreconnectMinConfidence; 1.204 + int mPreresolveMinConfidence; 1.205 + int mRedirectLikelyConfidence; 1.206 + 1.207 + int32_t mMaxQueueSize; 1.208 + 1.209 + nsCOMPtr<nsIThread> mIOThread; 1.210 + 1.211 + nsCOMPtr<nsISpeculativeConnect> mSpeculativeService; 1.212 + 1.213 + nsCOMPtr<nsIFile> mDBFile; 1.214 + nsCOMPtr<mozIStorageService> mStorageService; 1.215 + nsCOMPtr<mozIStorageConnection> mDB; 1.216 + mozilla::storage::StatementCache<mozIStorageStatement> mStatements; 1.217 + 1.218 + PRTime mStartupTime; 1.219 + PRTime mLastStartupTime; 1.220 + int32_t mStartupCount; 1.221 + 1.222 + nsCOMPtr<nsIDNSService> mDnsService; 1.223 + 1.224 + int32_t mQueueSize; 1.225 + mozilla::Mutex mQueueSizeLock; 1.226 + 1.227 + nsAutoPtr<SeerTelemetryAccumulators> mAccumulators; 1.228 + 1.229 + nsRefPtr<SeerDNSListener> mDNSListener; 1.230 + 1.231 + nsCOMPtr<nsITimer> mCommitTimer; 1.232 + 1.233 +#ifdef SEER_TESTS 1.234 + friend class SeerPrepareForDnsTestEvent; 1.235 + void PrepareForDnsTestInternal(int64_t timestamp, const nsACString &uri); 1.236 +#endif 1.237 + 1.238 + bool mCleanupScheduled; 1.239 + int32_t mMaxDBSize; 1.240 + int32_t mPreservePercentage; 1.241 + PRTime mLastCleanupTime; 1.242 +}; 1.243 + 1.244 +} // ::mozilla::net 1.245 +} // ::mozilla 1.246 + 1.247 +#endif // mozilla_net_Seer_h