| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ | 5 #ifndef CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ |
| 6 #define CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ | 6 #define CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 | 35 |
| 36 class PredictorsHandler; | 36 class PredictorsHandler; |
| 37 class Profile; | 37 class Profile; |
| 38 | 38 |
| 39 namespace net { | 39 namespace net { |
| 40 class URLRequest; | 40 class URLRequest; |
| 41 } | 41 } |
| 42 | 42 |
| 43 namespace predictors { | 43 namespace predictors { |
| 44 | 44 |
| 45 struct OriginRequestSummary; |
| 46 struct URLRequestSummary; |
| 47 struct PageRequestSummary; |
| 48 |
| 45 namespace internal { | 49 namespace internal { |
| 46 constexpr char kResourcePrefetchPredictorPrefetchingDurationHistogram[] = | 50 constexpr char kResourcePrefetchPredictorPrefetchingDurationHistogram[] = |
| 47 "ResourcePrefetchPredictor.PrefetchingDuration"; | 51 "ResourcePrefetchPredictor.PrefetchingDuration"; |
| 48 | 52 |
| 49 const uint32_t kVersionedRemovedExperiment = 0x03ff25e3; | 53 const uint32_t kVersionedRemovedExperiment = 0x03ff25e3; |
| 50 const uint32_t kUnusedRemovedExperiment = 0xf7f77166; | 54 const uint32_t kUnusedRemovedExperiment = 0xf7f77166; |
| 51 const uint32_t kNoStoreRemovedExperiment = 0xd90a199a; | 55 const uint32_t kNoStoreRemovedExperiment = 0xd90a199a; |
| 52 | 56 |
| 53 struct ManifestCompare { | 57 struct ManifestCompare { |
| 54 bool operator()(const precache::PrecacheManifest& lhs, | 58 bool operator()(const precache::PrecacheManifest& lhs, |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 // ResourcePrefetcherManager, and issues net::URLRequest to fetch resources. | 101 // ResourcePrefetcherManager, and issues net::URLRequest to fetch resources. |
| 98 // | 102 // |
| 99 // TODO(zhenw): Currently only main frame requests/redirects/responses are | 103 // TODO(zhenw): Currently only main frame requests/redirects/responses are |
| 100 // recorded. Consider recording sub-frame responses independently or together | 104 // recorded. Consider recording sub-frame responses independently or together |
| 101 // with main frame. | 105 // with main frame. |
| 102 class ResourcePrefetchPredictor | 106 class ResourcePrefetchPredictor |
| 103 : public history::HistoryServiceObserver, | 107 : public history::HistoryServiceObserver, |
| 104 public base::SupportsWeakPtr<ResourcePrefetchPredictor>, | 108 public base::SupportsWeakPtr<ResourcePrefetchPredictor>, |
| 105 public precache::PrecacheManager::Delegate { | 109 public precache::PrecacheManager::Delegate { |
| 106 public: | 110 public: |
| 107 // Data collected for origin-based prediction, for a single origin during a | |
| 108 // page load (see PageRequestSummary). | |
| 109 struct OriginRequestSummary { | |
| 110 OriginRequestSummary(); | |
| 111 OriginRequestSummary(const OriginRequestSummary& other); | |
| 112 ~OriginRequestSummary(); | |
| 113 | |
| 114 GURL origin; | |
| 115 bool always_access_network; | |
| 116 bool accessed_network; | |
| 117 int first_occurrence; | |
| 118 }; | |
| 119 | |
| 120 // Stores the data that we need to get from the URLRequest. | |
| 121 struct URLRequestSummary { | |
| 122 URLRequestSummary(); | |
| 123 URLRequestSummary(const URLRequestSummary& other); | |
| 124 ~URLRequestSummary(); | |
| 125 | |
| 126 NavigationID navigation_id; | |
| 127 GURL resource_url; | |
| 128 GURL request_url; // URL after all redirects. | |
| 129 content::ResourceType resource_type; | |
| 130 net::RequestPriority priority; | |
| 131 base::TimeTicks response_time; | |
| 132 bool before_first_contentful_paint; | |
| 133 | |
| 134 // Only for responses. | |
| 135 std::string mime_type; | |
| 136 bool was_cached; | |
| 137 GURL redirect_url; // Empty unless request was redirected to a valid url. | |
| 138 | |
| 139 bool has_validators; | |
| 140 bool always_revalidate; | |
| 141 bool is_no_store; | |
| 142 bool network_accessed; | |
| 143 | |
| 144 // Initializes a |URLRequestSummary| from a |URLRequest| response. | |
| 145 // Returns true for success. Note: NavigationID is NOT initialized | |
| 146 // by this function. | |
| 147 static bool SummarizeResponse(const net::URLRequest& request, | |
| 148 URLRequestSummary* summary); | |
| 149 }; | |
| 150 | |
| 151 // Stores the data learned from a single navigation. | |
| 152 struct PageRequestSummary { | |
| 153 explicit PageRequestSummary(const GURL& main_frame_url); | |
| 154 PageRequestSummary(const PageRequestSummary& other); | |
| 155 ~PageRequestSummary(); | |
| 156 | |
| 157 GURL main_frame_url; | |
| 158 GURL initial_url; | |
| 159 base::TimeTicks first_contentful_paint; | |
| 160 | |
| 161 // Stores all subresource requests within a single navigation, from initial | |
| 162 // main frame request to navigation completion. | |
| 163 std::vector<URLRequestSummary> subresource_requests; | |
| 164 // Map of origin -> OriginRequestSummary. Only one instance of each origin | |
| 165 // is kept per navigation, but the summary is updated several times. | |
| 166 std::map<GURL, OriginRequestSummary> origins; | |
| 167 }; | |
| 168 | |
| 169 // Stores a result of prediction. Essentially, |subresource_urls| is main | 111 // Stores a result of prediction. Essentially, |subresource_urls| is main |
| 170 // result and other fields are used for diagnosis and histograms reporting. | 112 // result and other fields are used for diagnosis and histograms reporting. |
| 171 struct Prediction { | 113 struct Prediction { |
| 172 Prediction(); | 114 Prediction(); |
| 173 Prediction(const Prediction& other); | 115 Prediction(const Prediction& other); |
| 174 ~Prediction(); | 116 ~Prediction(); |
| 175 | 117 |
| 176 bool is_host; | 118 bool is_host; |
| 177 bool is_redirected; | 119 bool is_redirected; |
| 178 std::string main_frame_key; | 120 std::string main_frame_key; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 193 | 135 |
| 194 ResourcePrefetchPredictor(const LoadingPredictorConfig& config, | 136 ResourcePrefetchPredictor(const LoadingPredictorConfig& config, |
| 195 Profile* profile); | 137 Profile* profile); |
| 196 ~ResourcePrefetchPredictor() override; | 138 ~ResourcePrefetchPredictor() override; |
| 197 | 139 |
| 198 // Starts initialization by posting a task to the DB thread to read the | 140 // Starts initialization by posting a task to the DB thread to read the |
| 199 // predictor database. Virtual for testing. | 141 // predictor database. Virtual for testing. |
| 200 virtual void StartInitialization(); | 142 virtual void StartInitialization(); |
| 201 virtual void Shutdown(); | 143 virtual void Shutdown(); |
| 202 | 144 |
| 203 // Determines the resource type from the declared one, falling back to MIME | |
| 204 // type detection when it is not explicit. | |
| 205 static content::ResourceType GetResourceType( | |
| 206 content::ResourceType resource_type, | |
| 207 const std::string& mime_type); | |
| 208 | |
| 209 // Determines the ResourceType from the mime type, defaulting to the | |
| 210 // |fallback| if the ResourceType could not be determined. | |
| 211 static content::ResourceType GetResourceTypeFromMimeType( | |
| 212 const std::string& mime_type, | |
| 213 content::ResourceType fallback); | |
| 214 | |
| 215 // Called when ResourcePrefetcher is finished, i.e. there is nothing pending | 145 // Called when ResourcePrefetcher is finished, i.e. there is nothing pending |
| 216 // in flight. | 146 // in flight. |
| 217 void OnPrefetchingFinished( | 147 void OnPrefetchingFinished( |
| 218 const GURL& main_frame_url, | 148 const GURL& main_frame_url, |
| 219 std::unique_ptr<ResourcePrefetcher::PrefetcherStats> stats); | 149 std::unique_ptr<ResourcePrefetcher::PrefetcherStats> stats); |
| 220 | 150 |
| 221 // Returns true if prefetching data exists for the |main_frame_url|. | 151 // Returns true if prefetching data exists for the |main_frame_url|. |
| 222 virtual bool IsUrlPrefetchable(const GURL& main_frame_url) const; | 152 virtual bool IsUrlPrefetchable(const GURL& main_frame_url) const; |
| 223 | 153 |
| 224 // Returns true iff |resource| has sufficient confidence level and required | 154 // Returns true iff |resource| has sufficient confidence level and required |
| (...skipping 11 matching lines...) Expand all Loading... |
| 236 | 166 |
| 237 // Sets the |stats_collector| which is used to report histograms. | 167 // Sets the |stats_collector| which is used to report histograms. |
| 238 void SetStatsCollector(LoadingStatsCollector* stats_collector); | 168 void SetStatsCollector(LoadingStatsCollector* stats_collector); |
| 239 | 169 |
| 240 // Returns true iff there is PrefetchData that can be used for a | 170 // Returns true iff there is PrefetchData that can be used for a |
| 241 // |main_frame_url| and fills |prediction| with resources that need to be | 171 // |main_frame_url| and fills |prediction| with resources that need to be |
| 242 // prefetched. |prediction| pointer may be nullptr to get return value only. | 172 // prefetched. |prediction| pointer may be nullptr to get return value only. |
| 243 virtual bool GetPrefetchData(const GURL& main_frame_url, | 173 virtual bool GetPrefetchData(const GURL& main_frame_url, |
| 244 Prediction* prediction) const; | 174 Prediction* prediction) const; |
| 245 | 175 |
| 176 // Called by the collector after a page has finished loading resources and |
| 177 // assembled a PageRequestSummary. |
| 178 void HandlePageRequestSummary(std::unique_ptr<PageRequestSummary> summary); |
| 179 |
| 246 private: | 180 private: |
| 247 // 'LoadingPredictorObserver' calls the below functions to inform the | |
| 248 // predictor of main frame and resource requests. Should only be called if the | |
| 249 // corresponding Should* functions return true. | |
| 250 void RecordURLRequest(const URLRequestSummary& request); | |
| 251 void RecordURLResponse(const URLRequestSummary& response); | |
| 252 void RecordURLRedirect(const URLRequestSummary& response); | |
| 253 | |
| 254 // Called when the main frame of a page completes loading. | |
| 255 void RecordMainFrameLoadComplete(const NavigationID& navigation_id); | |
| 256 | |
| 257 // Called after the main frame's first contentful paint. | |
| 258 void RecordFirstContentfulPaint( | |
| 259 const NavigationID& navigation_id, | |
| 260 const base::TimeTicks& first_contentful_paint); | |
| 261 | |
| 262 // Starts prefetching for |main_frame_url| from a |prediction|. | 181 // Starts prefetching for |main_frame_url| from a |prediction|. |
| 263 void StartPrefetching(const GURL& main_frame_url, | 182 void StartPrefetching(const GURL& main_frame_url, |
| 264 const Prediction& prediction); | 183 const Prediction& prediction); |
| 265 | 184 |
| 266 // Stops prefetching that may be in progress corresponding to | 185 // Stops prefetching that may be in progress corresponding to |
| 267 // |main_frame_url|. | 186 // |main_frame_url|. |
| 268 void StopPrefetching(const GURL& main_frame_url); | 187 void StopPrefetching(const GURL& main_frame_url); |
| 269 | 188 |
| 270 friend class LoadingPredictor; | 189 friend class LoadingPredictor; |
| 271 friend class ::PredictorsHandler; | 190 friend class ::PredictorsHandler; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 internal::ManifestCompare> | 241 internal::ManifestCompare> |
| 323 ManifestDataMap; | 242 ManifestDataMap; |
| 324 typedef GlowplugKeyValueData<OriginData, internal::LastVisitTimeCompare> | 243 typedef GlowplugKeyValueData<OriginData, internal::LastVisitTimeCompare> |
| 325 OriginDataMap; | 244 OriginDataMap; |
| 326 typedef std::map<NavigationID, std::unique_ptr<PageRequestSummary>> | 245 typedef std::map<NavigationID, std::unique_ptr<PageRequestSummary>> |
| 327 NavigationMap; | 246 NavigationMap; |
| 328 | 247 |
| 329 // Returns true if the request (should have a response in it) is "no-store". | 248 // Returns true if the request (should have a response in it) is "no-store". |
| 330 static bool IsNoStore(const net::URLRequest& request); | 249 static bool IsNoStore(const net::URLRequest& request); |
| 331 | 250 |
| 332 // Functions called on different network events pertaining to the loading of | |
| 333 // main frame resource or sub resources. | |
| 334 void OnMainFrameRequest(const URLRequestSummary& request); | |
| 335 void OnMainFrameRedirect(const URLRequestSummary& response); | |
| 336 void OnSubresourceResponse(const URLRequestSummary& response); | |
| 337 void OnSubresourceRedirect(const URLRequestSummary& response); | |
| 338 | |
| 339 // Called when onload completes for a navigation. We treat this point as the | |
| 340 // "completion" of the navigation. The resources requested by the page up to | |
| 341 // this point are the only ones considered for prefetching. | |
| 342 void OnNavigationComplete(const NavigationID& nav_id_without_timing_info); | |
| 343 | |
| 344 // Returns true iff one of the following conditions is true | 251 // Returns true iff one of the following conditions is true |
| 345 // * |redirect_data| contains confident redirect endpoint for |entry_point| | 252 // * |redirect_data| contains confident redirect endpoint for |entry_point| |
| 346 // and assigns it to the |redirect_endpoint| | 253 // and assigns it to the |redirect_endpoint| |
| 347 // | 254 // |
| 348 // * |redirect_data| doens't contain an entry for |entry_point| and assigns | 255 // * |redirect_data| doens't contain an entry for |entry_point| and assigns |
| 349 // |entry_point| to the |redirect_endpoint|. | 256 // |entry_point| to the |redirect_endpoint|. |
| 350 bool GetRedirectEndpoint(const std::string& entry_point, | 257 bool GetRedirectEndpoint(const std::string& entry_point, |
| 351 const RedirectDataMap& redirect_data, | 258 const RedirectDataMap& redirect_data, |
| 352 std::string* redirect_endpoint) const; | 259 std::string* redirect_endpoint) const; |
| 353 | 260 |
| 354 // Returns true iff the |resource_data| contains PrefetchData that can be used | 261 // Returns true iff the |resource_data| contains PrefetchData that can be used |
| 355 // for a |main_frame_key| and fills |urls| with resources that need to be | 262 // for a |main_frame_key| and fills |urls| with resources that need to be |
| 356 // prefetched. |urls| may be nullptr to get the return value only. | 263 // prefetched. |urls| may be nullptr to get the return value only. |
| 357 bool PopulatePrefetcherRequest(const std::string& main_frame_key, | 264 bool PopulatePrefetcherRequest(const std::string& main_frame_key, |
| 358 const PrefetchDataMap& resource_data, | 265 const PrefetchDataMap& resource_data, |
| 359 std::vector<GURL>* urls) const; | 266 std::vector<GURL>* urls) const; |
| 360 | 267 |
| 361 // Returns true iff the manifest table contains PrecacheManifest that can be | 268 // Returns true iff the manifest table contains PrecacheManifest that can be |
| 362 // used for a |manifest_host| and fills |urls| with resources that need to be | 269 // used for a |manifest_host| and fills |urls| with resources that need to be |
| 363 // prefetched. |urls| may be nullptr to get the return value only. | 270 // prefetched. |urls| may be nullptr to get the return value only. |
| 364 bool PopulateFromManifest(const std::string& manifest_host, | 271 bool PopulateFromManifest(const std::string& manifest_host, |
| 365 std::vector<GURL>* urls) const; | 272 std::vector<GURL>* urls) const; |
| 366 | 273 |
| 367 void InitializeOnDBThread(); | 274 void InitializeOnDBThread(); |
| 368 | 275 |
| 369 // Called during initialization when history is read and the predictor | 276 // Called during initialization when history is read and the predictor |
| 370 // database has been read. | 277 // database has been read. |
| 371 void OnHistoryAndCacheLoaded(); | 278 void OnHistoryAndCacheLoaded(); |
| 372 | 279 |
| 373 // Cleanup inflight_navigations_ and call a cleanup for stats_collector_. | |
| 374 void CleanupAbandonedNavigations(const NavigationID& navigation_id); | |
| 375 | |
| 376 // Deletes all URLs from the predictor database, the caches and removes all | 280 // Deletes all URLs from the predictor database, the caches and removes all |
| 377 // inflight navigations. | 281 // inflight navigations. |
| 378 void DeleteAllUrls(); | 282 void DeleteAllUrls(); |
| 379 | 283 |
| 380 // Deletes data for the input |urls| and their corresponding hosts from the | 284 // Deletes data for the input |urls| and their corresponding hosts from the |
| 381 // predictor database and caches. | 285 // predictor database and caches. |
| 382 void DeleteUrls(const history::URLRows& urls); | 286 void DeleteUrls(const history::URLRows& urls); |
| 383 | 287 |
| 384 // Callback for GetUrlVisitCountTask. | 288 // Callback for GetUrlVisitCountTask. |
| 385 void OnVisitCountLookup(size_t url_visit_count, | 289 void OnVisitCountLookup(size_t url_visit_count, |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 460 | 364 |
| 461 // An interface used to notify that data in the ResourcePrefetchPredictor | 365 // An interface used to notify that data in the ResourcePrefetchPredictor |
| 462 // has changed. All methods are invoked on the UI thread. | 366 // has changed. All methods are invoked on the UI thread. |
| 463 class TestObserver { | 367 class TestObserver { |
| 464 public: | 368 public: |
| 465 // De-registers itself from |predictor_| on destruction. | 369 // De-registers itself from |predictor_| on destruction. |
| 466 virtual ~TestObserver(); | 370 virtual ~TestObserver(); |
| 467 | 371 |
| 468 virtual void OnPredictorInitialized() {} | 372 virtual void OnPredictorInitialized() {} |
| 469 | 373 |
| 470 virtual void OnNavigationLearned( | 374 virtual void OnNavigationLearned(size_t url_visit_count, |
| 471 size_t url_visit_count, | 375 const PageRequestSummary& summary) {} |
| 472 const ResourcePrefetchPredictor::PageRequestSummary& summary) {} | |
| 473 | 376 |
| 474 virtual void OnPrefetchingStarted(const GURL& main_frame_url) {} | 377 virtual void OnPrefetchingStarted(const GURL& main_frame_url) {} |
| 475 | 378 |
| 476 virtual void OnPrefetchingStopped(const GURL& main_frame_url) {} | 379 virtual void OnPrefetchingStopped(const GURL& main_frame_url) {} |
| 477 | 380 |
| 478 virtual void OnPrefetchingFinished(const GURL& main_frame_url) {} | 381 virtual void OnPrefetchingFinished(const GURL& main_frame_url) {} |
| 479 | 382 |
| 480 protected: | 383 protected: |
| 481 // |predictor| must be non-NULL and has to outlive the TestObserver. | 384 // |predictor| must be non-NULL and has to outlive the TestObserver. |
| 482 // Also the predictor must not have a TestObserver set. | 385 // Also the predictor must not have a TestObserver set. |
| 483 explicit TestObserver(ResourcePrefetchPredictor* predictor); | 386 explicit TestObserver(ResourcePrefetchPredictor* predictor); |
| 484 | 387 |
| 485 private: | 388 private: |
| 486 ResourcePrefetchPredictor* predictor_; | 389 ResourcePrefetchPredictor* predictor_; |
| 487 | 390 |
| 488 DISALLOW_COPY_AND_ASSIGN(TestObserver); | 391 DISALLOW_COPY_AND_ASSIGN(TestObserver); |
| 489 }; | 392 }; |
| 490 | 393 |
| 491 } // namespace predictors | 394 } // namespace predictors |
| 492 | 395 |
| 493 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ | 396 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ |
| OLD | NEW |