Predictor.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. #ifndef mozilla_net_Predictor_h
  5. #define mozilla_net_Predictor_h
  6. #include "nsINetworkPredictor.h"
  7. #include "nsINetworkPredictorVerifier.h"
  8. #include "nsCOMPtr.h"
  9. #include "nsICacheEntry.h"
  10. #include "nsICacheEntryOpenCallback.h"
  11. #include "nsICacheStorageVisitor.h"
  12. #include "nsIDNSListener.h"
  13. #include "nsIInterfaceRequestor.h"
  14. #include "nsIObserver.h"
  15. #include "nsISpeculativeConnect.h"
  16. #include "nsIStreamListener.h"
  17. #include "mozilla/RefPtr.h"
  18. #include "nsString.h"
  19. #include "nsTArray.h"
  20. #include "mozilla/TimeStamp.h"
  21. class nsICacheStorage;
  22. class nsIDNSService;
  23. class nsIIOService;
  24. class nsILoadContextInfo;
  25. class nsITimer;
  26. namespace mozilla {
  27. namespace net {
  28. class nsHttpRequestHead;
  29. class nsHttpResponseHead;
  30. class Predictor : public nsINetworkPredictor
  31. , public nsIObserver
  32. , public nsISpeculativeConnectionOverrider
  33. , public nsIInterfaceRequestor
  34. , public nsICacheEntryMetaDataVisitor
  35. , public nsINetworkPredictorVerifier
  36. {
  37. public:
  38. NS_DECL_ISUPPORTS
  39. NS_DECL_NSINETWORKPREDICTOR
  40. NS_DECL_NSIOBSERVER
  41. NS_DECL_NSISPECULATIVECONNECTIONOVERRIDER
  42. NS_DECL_NSIINTERFACEREQUESTOR
  43. NS_DECL_NSICACHEENTRYMETADATAVISITOR
  44. NS_DECL_NSINETWORKPREDICTORVERIFIER
  45. Predictor();
  46. nsresult Init();
  47. void Shutdown();
  48. static nsresult Create(nsISupports *outer, const nsIID& iid, void **result);
  49. // Used to update whether a particular URI was cacheable or not.
  50. // sourceURI and targetURI are the same as the arguments to Learn
  51. // and httpStatus is the status code we got while loading targetURI.
  52. static void UpdateCacheability(nsIURI *sourceURI, nsIURI *targetURI,
  53. uint32_t httpStatus,
  54. nsHttpRequestHead &requestHead,
  55. nsHttpResponseHead *reqponseHead,
  56. nsILoadContextInfo *lci);
  57. private:
  58. virtual ~Predictor();
  59. // Stores callbacks for a child process predictor (for test purposes)
  60. nsCOMPtr<nsINetworkPredictorVerifier> mChildVerifier;
  61. union Reason {
  62. PredictorLearnReason mLearn;
  63. PredictorPredictReason mPredict;
  64. };
  65. class DNSListener : public nsIDNSListener
  66. {
  67. public:
  68. NS_DECL_THREADSAFE_ISUPPORTS
  69. NS_DECL_NSIDNSLISTENER
  70. DNSListener()
  71. { }
  72. private:
  73. virtual ~DNSListener()
  74. { }
  75. };
  76. class Action : public nsICacheEntryOpenCallback
  77. {
  78. public:
  79. NS_DECL_THREADSAFE_ISUPPORTS
  80. NS_DECL_NSICACHEENTRYOPENCALLBACK
  81. Action(bool fullUri, bool predict, Reason reason,
  82. nsIURI *targetURI, nsIURI *sourceURI,
  83. nsINetworkPredictorVerifier *verifier, Predictor *predictor);
  84. Action(bool fullUri, bool predict, Reason reason,
  85. nsIURI *targetURI, nsIURI *sourceURI,
  86. nsINetworkPredictorVerifier *verifier, Predictor *predictor,
  87. uint8_t stackCount);
  88. static const bool IS_FULL_URI = true;
  89. static const bool IS_ORIGIN = false;
  90. static const bool DO_PREDICT = true;
  91. static const bool DO_LEARN = false;
  92. private:
  93. virtual ~Action();
  94. bool mFullUri : 1;
  95. bool mPredict : 1;
  96. union {
  97. PredictorPredictReason mPredictReason;
  98. PredictorLearnReason mLearnReason;
  99. };
  100. nsCOMPtr<nsIURI> mTargetURI;
  101. nsCOMPtr<nsIURI> mSourceURI;
  102. nsCOMPtr<nsINetworkPredictorVerifier> mVerifier;
  103. TimeStamp mStartTime;
  104. uint8_t mStackCount;
  105. RefPtr<Predictor> mPredictor;
  106. };
  107. class CacheabilityAction : public nsICacheEntryOpenCallback
  108. , public nsICacheEntryMetaDataVisitor
  109. {
  110. public:
  111. NS_DECL_THREADSAFE_ISUPPORTS
  112. NS_DECL_NSICACHEENTRYOPENCALLBACK
  113. NS_DECL_NSICACHEENTRYMETADATAVISITOR
  114. CacheabilityAction(nsIURI *targetURI, uint32_t httpStatus,
  115. const nsCString &method, Predictor *predictor)
  116. :mTargetURI(targetURI)
  117. ,mHttpStatus(httpStatus)
  118. ,mMethod(method)
  119. ,mPredictor(predictor)
  120. { }
  121. private:
  122. virtual ~CacheabilityAction() { }
  123. nsCOMPtr<nsIURI> mTargetURI;
  124. uint32_t mHttpStatus;
  125. nsCString mMethod;
  126. RefPtr<Predictor> mPredictor;
  127. nsTArray<nsCString> mKeysToCheck;
  128. nsTArray<nsCString> mValuesToCheck;
  129. };
  130. class Resetter : public nsICacheEntryOpenCallback,
  131. public nsICacheEntryMetaDataVisitor,
  132. public nsICacheStorageVisitor
  133. {
  134. public:
  135. NS_DECL_THREADSAFE_ISUPPORTS
  136. NS_DECL_NSICACHEENTRYOPENCALLBACK
  137. NS_DECL_NSICACHEENTRYMETADATAVISITOR
  138. NS_DECL_NSICACHESTORAGEVISITOR
  139. explicit Resetter(Predictor *predictor);
  140. private:
  141. virtual ~Resetter() { }
  142. void Complete();
  143. uint32_t mEntriesToVisit;
  144. nsTArray<nsCString> mKeysToDelete;
  145. RefPtr<Predictor> mPredictor;
  146. nsTArray<nsCOMPtr<nsIURI>> mURIsToVisit;
  147. };
  148. class SpaceCleaner : public nsICacheEntryMetaDataVisitor
  149. {
  150. public:
  151. NS_DECL_ISUPPORTS
  152. NS_DECL_NSICACHEENTRYMETADATAVISITOR
  153. explicit SpaceCleaner(Predictor *predictor)
  154. :mLRUStamp(0)
  155. ,mLRUKeyToDelete(nullptr)
  156. ,mPredictor(predictor)
  157. { }
  158. void Finalize(nsICacheEntry *entry);
  159. private:
  160. virtual ~SpaceCleaner() { }
  161. uint32_t mLRUStamp;
  162. const char *mLRUKeyToDelete;
  163. nsTArray<nsCString> mLongKeysToDelete;
  164. RefPtr<Predictor> mPredictor;
  165. };
  166. class PrefetchListener : public nsIStreamListener
  167. {
  168. public:
  169. NS_DECL_ISUPPORTS
  170. NS_DECL_NSIREQUESTOBSERVER
  171. NS_DECL_NSISTREAMLISTENER
  172. PrefetchListener(nsINetworkPredictorVerifier *verifier, nsIURI *uri,
  173. Predictor *predictor)
  174. :mVerifier(verifier)
  175. ,mURI(uri)
  176. ,mPredictor(predictor)
  177. { }
  178. private:
  179. virtual ~PrefetchListener() { }
  180. nsCOMPtr<nsINetworkPredictorVerifier> mVerifier;
  181. nsCOMPtr<nsIURI> mURI;
  182. RefPtr<Predictor> mPredictor;
  183. TimeStamp mStartTime;
  184. };
  185. // Observer-related stuff
  186. nsresult InstallObserver();
  187. void RemoveObserver();
  188. // Service startup utilities
  189. void MaybeCleanupOldDBFiles();
  190. // The guts of prediction
  191. // This is the top-level driver for doing any prediction that needs
  192. // information from the cache. Returns true if any predictions were queued up
  193. // * reason - What kind of prediction this is/why this prediction is
  194. // happening (pageload, startup)
  195. // * entry - the cache entry with the information we need
  196. // * isNew - whether or not the cache entry is brand new and empty
  197. // * fullUri - whether we are doing predictions based on a full page URI, or
  198. // just the origin of the page
  199. // * targetURI - the URI that we are predicting based upon - IOW, the URI
  200. // that is being loaded or being redirected to
  201. // * verifier - used for testing to verify the expected predictions happen
  202. // * stackCount - used to ensure we don't recurse too far trying to find the
  203. // final redirection in a redirect chain
  204. bool PredictInternal(PredictorPredictReason reason, nsICacheEntry *entry,
  205. bool isNew, bool fullUri, nsIURI *targetURI,
  206. nsINetworkPredictorVerifier *verifier,
  207. uint8_t stackCount);
  208. // Used when predicting because the user's mouse hovered over a link
  209. // * targetURI - the URI target of the link
  210. // * sourceURI - the URI of the page on which the link appears
  211. // * verifier - used for testing to verify the expected predictions happen
  212. void PredictForLink(nsIURI *targetURI,
  213. nsIURI *sourceURI,
  214. nsINetworkPredictorVerifier *verifier);
  215. // Used when predicting because a page is being loaded (which may include
  216. // being the target of a redirect). All arguments are the same as for
  217. // PredictInternal. Returns true if any predictions were queued up.
  218. bool PredictForPageload(nsICacheEntry *entry,
  219. nsIURI *targetURI,
  220. uint8_t stackCount,
  221. bool fullUri,
  222. nsINetworkPredictorVerifier *verifier);
  223. // Used when predicting pages that will be used near browser startup. All
  224. // arguments are the same as for PredictInternal. Returns true if any
  225. // predictions were queued up.
  226. bool PredictForStartup(nsICacheEntry *entry,
  227. bool fullUri,
  228. nsINetworkPredictorVerifier *verifier);
  229. // Utilities related to prediction
  230. // Used to update our rolling load count (how many of the last n loads was a
  231. // partular resource loaded on?)
  232. // * entry - cache entry of page we're loading
  233. // * flags - value that contains our rolling count as the top 20 bits (but
  234. // we may use fewer than those 20 bits for calculations)
  235. // * key - metadata key that we will update on entry
  236. // * hitCount - part of the metadata we need to preserve
  237. // * lastHit - part of the metadata we need to preserve
  238. void UpdateRollingLoadCount(nsICacheEntry *entry, const uint32_t flags,
  239. const char *key, const uint32_t hitCount,
  240. const uint32_t lastHit);
  241. // Used to calculate how much to degrade our confidence for all resources
  242. // on a particular page, because of how long ago the most recent load of that
  243. // page was. Returns a value between 0 (very recent most recent load) and 100
  244. // (very distant most recent load)
  245. // * lastLoad - time stamp of most recent load of a page
  246. int32_t CalculateGlobalDegradation(uint32_t lastLoad);
  247. // Used to calculate how confident we are that a particular resource will be
  248. // used. Returns a value between 0 (no confidence) and 100 (very confident)
  249. // * hitCount - number of times this resource has been seen when loading
  250. // this page
  251. // * hitsPossible - number of times this page has been loaded
  252. // * lastHit - timestamp of the last time this resource was seen when
  253. // loading this page
  254. // * lastPossible - timestamp of the last time this page was loaded
  255. // * globalDegradation - value calculated by CalculateGlobalDegradation for
  256. // this page
  257. int32_t CalculateConfidence(uint32_t hitCount, uint32_t hitsPossible,
  258. uint32_t lastHit, uint32_t lastPossible,
  259. int32_t globalDegradation);
  260. // Used to calculate all confidence values for all resources associated with a
  261. // page.
  262. // * entry - the cache entry with all necessary information about this page
  263. // * referrer - the URI that we are loading (may be null)
  264. // * lastLoad - timestamp of the last time this page was loaded
  265. // * loadCount - number of times this page has been loaded
  266. // * gloablDegradation - value calculated by CalculateGlobalDegradation for
  267. // this page
  268. // * fullUri - whether we're predicting for a full URI or origin-only
  269. void CalculatePredictions(nsICacheEntry *entry, nsIURI *referrer,
  270. uint32_t lastLoad, uint32_t loadCount,
  271. int32_t globalDegradation, bool fullUri);
  272. // Used to prepare any necessary prediction for a resource on a page
  273. // * confidence - value calculated by CalculateConfidence for this resource
  274. // * flags - the flags taken from the resource
  275. // * uri - the URI of the resource
  276. void SetupPrediction(int32_t confidence, uint32_t flags, nsIURI *uri);
  277. // Used to kick off a prefetch from RunPredictions if necessary
  278. // * uri - the URI to prefetch
  279. // * referrer - the URI of the referring page
  280. // * verifier - used for testing to ensure the expected prefetch happens
  281. nsresult Prefetch(nsIURI *uri, nsIURI *referrer, nsINetworkPredictorVerifier *verifier);
  282. // Used to actually perform any predictions set up via SetupPrediction.
  283. // Returns true if any predictions were performed.
  284. // * referrer - the URI we are predicting from
  285. // * verifier - used for testing to ensure the expected predictions happen
  286. bool RunPredictions(nsIURI *referrer, nsINetworkPredictorVerifier *verifier);
  287. // Used to guess whether a page will redirect to another page or not. Returns
  288. // true if a redirection is likely.
  289. // * entry - cache entry with all necessary information about this page
  290. // * loadCount - number of times this page has been loaded
  291. // * lastLoad - timestamp of the last time this page was loaded
  292. // * globalDegradation - value calculated by CalculateGlobalDegradation for
  293. // this page
  294. // * redirectURI - if this returns true, the URI that is likely to be
  295. // redirected to, otherwise null
  296. bool WouldRedirect(nsICacheEntry *entry, uint32_t loadCount,
  297. uint32_t lastLoad, int32_t globalDegradation,
  298. nsIURI **redirectURI);
  299. // The guts of learning information
  300. // This is the top-level driver for doing any updating of our information in
  301. // the cache
  302. // * reason - why this learn is happening (pageload, startup, redirect)
  303. // * entry - the cache entry with the information we need
  304. // * isNew - whether or not the cache entry is brand new and empty
  305. // * fullUri - whether we are doing predictions based on a full page URI, or
  306. // just the origin of the page
  307. // * targetURI - the URI that we are adding to our data - most often a
  308. // resource loaded by a page the user navigated to
  309. // * sourceURI - the URI that caused targetURI to be loaded, usually the
  310. // page the user navigated to
  311. void LearnInternal(PredictorLearnReason reason, nsICacheEntry *entry,
  312. bool isNew, bool fullUri, nsIURI *targetURI,
  313. nsIURI *sourceURI);
  314. // Used when learning about a resource loaded by a page
  315. // * entry - the cache entry with information that needs updating
  316. // * targetURI - the URI of the resource that was loaded by the page
  317. void LearnForSubresource(nsICacheEntry *entry, nsIURI *targetURI);
  318. // Used when learning about a redirect from one page to another
  319. // * entry - the cache entry of the page that was redirected from
  320. // * targetURI - the URI of the redirect target
  321. void LearnForRedirect(nsICacheEntry *entry, nsIURI *targetURI);
  322. // Used to learn about pages loaded close to browser startup. This results in
  323. // LearnForStartup being called if we are, in fact, near browser startup
  324. // * uri - the URI of a page that has been loaded (may not have been near
  325. // browser startup)
  326. // * fullUri - true if this is a full page uri, false if it's an origin
  327. void MaybeLearnForStartup(nsIURI *uri, bool fullUri);
  328. // Used in conjunction with MaybeLearnForStartup to learn about pages loaded
  329. // close to browser startup
  330. // * entry - the cache entry that stores the startup page list
  331. // * targetURI - the URI of a page that was loaded near browser startup
  332. void LearnForStartup(nsICacheEntry *entry, nsIURI *targetURI);
  333. // Used to parse the data we store in cache metadata
  334. // * key - the cache metadata key
  335. // * value - the cache metadata value
  336. // * uri - (out) the URI this metadata entry was about
  337. // * hitCount - (out) the number of times this URI has been seen
  338. // * lastHit - (out) timestamp of the last time this URI was seen
  339. // * flags - (out) flags for this metadata entry
  340. bool ParseMetaDataEntry(const char *key, const char *value, nsIURI **uri,
  341. uint32_t &hitCount, uint32_t &lastHit,
  342. uint32_t &flags);
  343. // Used to update whether a particular URI was cacheable or not.
  344. // sourceURI and targetURI are the same as the arguments to Learn
  345. // and httpStatus is the status code we got while loading targetURI.
  346. void UpdateCacheabilityInternal(nsIURI *sourceURI, nsIURI *targetURI,
  347. uint32_t httpStatus, const nsCString &method);
  348. // Make sure our prefs are in their expected range of values
  349. void SanitizePrefs();
  350. // Our state
  351. bool mInitialized;
  352. bool mEnabled;
  353. bool mEnableHoverOnSSL;
  354. bool mEnablePrefetch;
  355. int32_t mPageDegradationDay;
  356. int32_t mPageDegradationWeek;
  357. int32_t mPageDegradationMonth;
  358. int32_t mPageDegradationYear;
  359. int32_t mPageDegradationMax;
  360. int32_t mSubresourceDegradationDay;
  361. int32_t mSubresourceDegradationWeek;
  362. int32_t mSubresourceDegradationMonth;
  363. int32_t mSubresourceDegradationYear;
  364. int32_t mSubresourceDegradationMax;
  365. int32_t mPrefetchRollingLoadCount;
  366. int32_t mPrefetchMinConfidence;
  367. int32_t mPreconnectMinConfidence;
  368. int32_t mPreresolveMinConfidence;
  369. int32_t mRedirectLikelyConfidence;
  370. int32_t mPrefetchForceValidFor;
  371. int32_t mMaxResourcesPerEntry;
  372. bool mCleanedUp;
  373. nsCOMPtr<nsITimer> mCleanupTimer;
  374. nsTArray<nsCString> mKeysToOperateOn;
  375. nsTArray<nsCString> mValuesToOperateOn;
  376. nsCOMPtr<nsICacheStorage> mCacheDiskStorage;
  377. nsCOMPtr<nsIIOService> mIOService;
  378. nsCOMPtr<nsISpeculativeConnect> mSpeculativeService;
  379. nsCOMPtr<nsIURI> mStartupURI;
  380. uint32_t mStartupTime;
  381. uint32_t mLastStartupTime;
  382. int32_t mStartupCount;
  383. uint32_t mMaxURILength;
  384. nsCOMPtr<nsIDNSService> mDnsService;
  385. RefPtr<DNSListener> mDNSListener;
  386. nsTArray<nsCOMPtr<nsIURI>> mPrefetches;
  387. nsTArray<nsCOMPtr<nsIURI>> mPreconnects;
  388. nsTArray<nsCOMPtr<nsIURI>> mPreresolves;
  389. bool mDoingTests;
  390. static Predictor *sSelf;
  391. };
  392. } // namespace net
  393. } // namespace mozilla
  394. #endif // mozilla_net_Predictor_h