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