Chromium Code Reviews| 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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 107 // * ResourcePrefetcher - Lives entirely on the IO thread, owned by the | 111 // * ResourcePrefetcher - Lives entirely on the IO thread, owned by the |
| 108 // ResourcePrefetcherManager, and issues net::URLRequest to fetch resources. | 112 // ResourcePrefetcherManager, and issues net::URLRequest to fetch resources. |
| 109 // | 113 // |
| 110 // TODO(zhenw): Currently only main frame requests/redirects/responses are | 114 // TODO(zhenw): Currently only main frame requests/redirects/responses are |
| 111 // recorded. Consider recording sub-frame responses independently or together | 115 // recorded. Consider recording sub-frame responses independently or together |
| 112 // with main frame. | 116 // with main frame. |
| 113 class ResourcePrefetchPredictor | 117 class ResourcePrefetchPredictor |
| 114 : public history::HistoryServiceObserver, | 118 : public history::HistoryServiceObserver, |
| 115 public precache::PrecacheManager::Delegate { | 119 public precache::PrecacheManager::Delegate { |
| 116 public: | 120 public: |
| 117 // Data collected for origin-based prediction, for a single origin during a | |
| 118 // page load (see PageRequestSummary). | |
| 119 struct OriginRequestSummary { | |
| 120 OriginRequestSummary(); | |
| 121 OriginRequestSummary(const OriginRequestSummary& other); | |
| 122 ~OriginRequestSummary(); | |
| 123 | |
| 124 GURL origin; | |
| 125 bool always_access_network; | |
| 126 bool accessed_network; | |
| 127 int first_occurrence; | |
| 128 }; | |
| 129 | |
| 130 // Stores the data that we need to get from the URLRequest. | |
| 131 struct URLRequestSummary { | |
| 132 URLRequestSummary(); | |
| 133 URLRequestSummary(const URLRequestSummary& other); | |
| 134 ~URLRequestSummary(); | |
| 135 | |
| 136 NavigationID navigation_id; | |
| 137 GURL resource_url; | |
| 138 GURL request_url; // URL after all redirects. | |
| 139 content::ResourceType resource_type; | |
| 140 net::RequestPriority priority; | |
| 141 base::TimeTicks response_time; | |
| 142 bool before_first_contentful_paint; | |
| 143 | |
| 144 // Only for responses. | |
| 145 std::string mime_type; | |
| 146 bool was_cached; | |
| 147 GURL redirect_url; // Empty unless request was redirected to a valid url. | |
| 148 | |
| 149 bool has_validators; | |
| 150 bool always_revalidate; | |
| 151 bool is_no_store; | |
| 152 bool network_accessed; | |
| 153 | |
| 154 // Initializes a |URLRequestSummary| from a |URLRequest| response. | |
| 155 // Returns true for success. Note: NavigationID is NOT initialized | |
| 156 // by this function. | |
| 157 static bool SummarizeResponse(const net::URLRequest& request, | |
| 158 URLRequestSummary* summary); | |
| 159 }; | |
| 160 | |
| 161 // Stores the data learned from a single navigation. | |
| 162 struct PageRequestSummary { | |
| 163 explicit PageRequestSummary(const GURL& main_frame_url); | |
| 164 PageRequestSummary(const PageRequestSummary& other); | |
| 165 ~PageRequestSummary(); | |
| 166 | |
| 167 GURL main_frame_url; | |
| 168 GURL initial_url; | |
| 169 base::TimeTicks first_contentful_paint; | |
| 170 | |
| 171 // Stores all subresource requests within a single navigation, from initial | |
| 172 // main frame request to navigation completion. | |
| 173 std::vector<URLRequestSummary> subresource_requests; | |
| 174 // Map of origin -> OriginRequestSummary. Only one instance of each origin | |
| 175 // is kept per navigation, but the summary is updated several times. | |
| 176 std::map<GURL, OriginRequestSummary> origins; | |
| 177 }; | |
| 178 | |
| 179 // Stores a result of prediction. Essentially, |subresource_urls| is main | 121 // Stores a result of prediction. Essentially, |subresource_urls| is main |
| 180 // result and other fields are used for diagnosis and histograms reporting. | 122 // result and other fields are used for diagnosis and histograms reporting. |
| 181 struct Prediction { | 123 struct Prediction { |
| 182 Prediction(); | 124 Prediction(); |
| 183 Prediction(const Prediction& other); | 125 Prediction(const Prediction& other); |
| 184 ~Prediction(); | 126 ~Prediction(); |
| 185 | 127 |
| 186 bool is_host; | 128 bool is_host; |
| 187 bool is_redirected; | 129 bool is_redirected; |
| 188 std::string main_frame_key; | 130 std::string main_frame_key; |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 215 ResourcePrefetchPredictor(const LoadingPredictorConfig& config, | 157 ResourcePrefetchPredictor(const LoadingPredictorConfig& config, |
| 216 Profile* profile); | 158 Profile* profile); |
| 217 ~ResourcePrefetchPredictor() override; | 159 ~ResourcePrefetchPredictor() override; |
| 218 | 160 |
| 219 // Starts initialization by posting a task to the DB sequence of the | 161 // Starts initialization by posting a task to the DB sequence of the |
| 220 // ResourcePrefetchPredictorTables to read the predictor database. Virtual for | 162 // ResourcePrefetchPredictorTables to read the predictor database. Virtual for |
| 221 // testing. | 163 // testing. |
| 222 virtual void StartInitialization(); | 164 virtual void StartInitialization(); |
| 223 virtual void Shutdown(); | 165 virtual void Shutdown(); |
| 224 | 166 |
| 225 // Determines the resource type from the declared one, falling back to MIME | |
| 226 // type detection when it is not explicit. | |
| 227 static content::ResourceType GetResourceType( | |
| 228 content::ResourceType resource_type, | |
| 229 const std::string& mime_type); | |
| 230 | |
| 231 // Determines the ResourceType from the mime type, defaulting to the | |
| 232 // |fallback| if the ResourceType could not be determined. | |
| 233 static content::ResourceType GetResourceTypeFromMimeType( | |
| 234 const std::string& mime_type, | |
| 235 content::ResourceType fallback); | |
| 236 | |
| 237 // Called when ResourcePrefetcher is finished, i.e. there is nothing pending | 167 // Called when ResourcePrefetcher is finished, i.e. there is nothing pending |
| 238 // in flight. | 168 // in flight. |
| 239 void OnPrefetchingFinished( | 169 void OnPrefetchingFinished( |
| 240 const GURL& main_frame_url, | 170 const GURL& main_frame_url, |
| 241 std::unique_ptr<ResourcePrefetcher::PrefetcherStats> stats); | 171 std::unique_ptr<ResourcePrefetcher::PrefetcherStats> stats); |
| 242 | 172 |
| 243 // Returns true if prefetching data exists for the |main_frame_url|. | 173 // Returns true if prefetching data exists for the |main_frame_url|. |
| 244 virtual bool IsUrlPrefetchable(const GURL& main_frame_url) const; | 174 virtual bool IsUrlPrefetchable(const GURL& main_frame_url) const; |
| 245 | 175 |
| 246 // Returns true iff |resource| has sufficient confidence level and required | 176 // Returns true iff |resource| has sufficient confidence level and required |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 264 // prefetched. |prediction| pointer may be nullptr to get return value only. | 194 // prefetched. |prediction| pointer may be nullptr to get return value only. |
| 265 virtual bool GetPrefetchData(const GURL& main_frame_url, | 195 virtual bool GetPrefetchData(const GURL& main_frame_url, |
| 266 Prediction* prediction) const; | 196 Prediction* prediction) const; |
| 267 | 197 |
| 268 // Returns true iff there is OriginData that can be used for a |url| and fills | 198 // Returns true iff there is OriginData that can be used for a |url| and fills |
| 269 // |prediction| with origins and hosts that need to be preconnected and | 199 // |prediction| with origins and hosts that need to be preconnected and |
| 270 // preresolved respectively. | 200 // preresolved respectively. |
| 271 virtual bool PredictPreconnectOrigins(const GURL& url, | 201 virtual bool PredictPreconnectOrigins(const GURL& url, |
| 272 PreconnectPrediction* prediction) const; | 202 PreconnectPrediction* prediction) const; |
| 273 | 203 |
| 204 // Called by the collector after a page has finished loading resources and | |
| 205 // assembled a PageRequestSummary. | |
| 206 virtual void RecordPageRequestSummary( | |
| 207 std::unique_ptr<PageRequestSummary> summary); | |
| 208 | |
| 274 private: | 209 private: |
| 275 // 'LoadingPredictorObserver' calls the below functions to inform the | |
| 276 // predictor of main frame and resource requests. Should only be called if the | |
| 277 // corresponding Should* functions return true. | |
| 278 void RecordURLRequest(const URLRequestSummary& request); | |
| 279 void RecordURLResponse(const URLRequestSummary& response); | |
| 280 void RecordURLRedirect(const URLRequestSummary& response); | |
| 281 | |
| 282 // Called when the main frame of a page completes loading. | |
| 283 void RecordMainFrameLoadComplete(const NavigationID& navigation_id); | |
| 284 | |
| 285 // Called after the main frame's first contentful paint. | |
| 286 void RecordFirstContentfulPaint( | |
| 287 const NavigationID& navigation_id, | |
| 288 const base::TimeTicks& first_contentful_paint); | |
| 289 | |
| 290 // Starts prefetching for |main_frame_url| from a |prediction|. | 210 // Starts prefetching for |main_frame_url| from a |prediction|. |
| 291 void StartPrefetching(const GURL& main_frame_url, | 211 void StartPrefetching(const GURL& main_frame_url, |
| 292 const Prediction& prediction); | 212 const Prediction& prediction); |
| 293 | 213 |
| 294 // Stops prefetching that may be in progress corresponding to | 214 // Stops prefetching that may be in progress corresponding to |
| 295 // |main_frame_url|. | 215 // |main_frame_url|. |
| 296 void StopPrefetching(const GURL& main_frame_url); | 216 void StopPrefetching(const GURL& main_frame_url); |
| 297 | 217 |
| 298 friend class LoadingPredictor; | 218 friend class LoadingPredictor; |
| 299 friend class ::PredictorsHandler; | 219 friend class ::PredictorsHandler; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 338 TestPrefetchingDurationHistogram); | 258 TestPrefetchingDurationHistogram); |
| 339 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 259 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 340 TestRecordFirstContentfulPaint); | 260 TestRecordFirstContentfulPaint); |
| 341 | 261 |
| 342 enum InitializationState { | 262 enum InitializationState { |
| 343 NOT_INITIALIZED = 0, | 263 NOT_INITIALIZED = 0, |
| 344 INITIALIZING = 1, | 264 INITIALIZING = 1, |
| 345 INITIALIZED = 2 | 265 INITIALIZED = 2 |
| 346 }; | 266 }; |
| 347 // Returns true if the request (should have a response in it) is "no-store". | 267 // Returns true if the request (should have a response in it) is "no-store". |
| 348 static bool IsNoStore(const net::URLRequest& request); | 268 static bool IsNoStore(const net::URLRequest& request); |
|
alexilin
2017/06/30 14:58:49
Implementation of this function was moved into ano
trevordixon
2017/07/11 11:08:08
Done.
| |
| 349 | 269 |
| 350 // Functions called on different network events pertaining to the loading of | |
| 351 // main frame resource or sub resources. | |
| 352 void OnMainFrameRequest(const URLRequestSummary& request); | |
| 353 void OnMainFrameRedirect(const URLRequestSummary& response); | |
| 354 void OnSubresourceResponse(const URLRequestSummary& response); | |
| 355 void OnSubresourceRedirect(const URLRequestSummary& response); | |
| 356 | |
| 357 // Called when onload completes for a navigation. We treat this point as the | |
| 358 // "completion" of the navigation. The resources requested by the page up to | |
| 359 // this point are the only ones considered for prefetching. | |
| 360 void OnNavigationComplete(const NavigationID& nav_id_without_timing_info); | |
| 361 | |
| 362 // Returns true iff one of the following conditions is true | 270 // Returns true iff one of the following conditions is true |
| 363 // * |redirect_data| contains confident redirect endpoint for |entry_point| | 271 // * |redirect_data| contains confident redirect endpoint for |entry_point| |
| 364 // and assigns it to the |redirect_endpoint| | 272 // and assigns it to the |redirect_endpoint| |
| 365 // | 273 // |
| 366 // * |redirect_data| doens't contain an entry for |entry_point| and assigns | 274 // * |redirect_data| doens't contain an entry for |entry_point| and assigns |
| 367 // |entry_point| to the |redirect_endpoint|. | 275 // |entry_point| to the |redirect_endpoint|. |
| 368 bool GetRedirectEndpoint(const std::string& entry_point, | 276 bool GetRedirectEndpoint(const std::string& entry_point, |
| 369 const RedirectDataMap& redirect_data, | 277 const RedirectDataMap& redirect_data, |
| 370 std::string* redirect_endpoint) const; | 278 std::string* redirect_endpoint) const; |
| 371 | 279 |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 388 std::unique_ptr<PrefetchDataMap> host_resource_data, | 296 std::unique_ptr<PrefetchDataMap> host_resource_data, |
| 389 std::unique_ptr<RedirectDataMap> url_redirect_data, | 297 std::unique_ptr<RedirectDataMap> url_redirect_data, |
| 390 std::unique_ptr<RedirectDataMap> host_redirect_data, | 298 std::unique_ptr<RedirectDataMap> host_redirect_data, |
| 391 std::unique_ptr<ManifestDataMap> manifest_data, | 299 std::unique_ptr<ManifestDataMap> manifest_data, |
| 392 std::unique_ptr<OriginDataMap> origin_data); | 300 std::unique_ptr<OriginDataMap> origin_data); |
| 393 | 301 |
| 394 // Called during initialization when history is read and the predictor | 302 // Called during initialization when history is read and the predictor |
| 395 // database has been read. | 303 // database has been read. |
| 396 void OnHistoryAndCacheLoaded(); | 304 void OnHistoryAndCacheLoaded(); |
| 397 | 305 |
| 398 // Cleanup inflight_navigations_ and call a cleanup for stats_collector_. | 306 // Deletes all URLs from the predictor database and caches. |
| 399 void CleanupAbandonedNavigations(const NavigationID& navigation_id); | |
| 400 | |
| 401 // Deletes all URLs from the predictor database, the caches and removes all | |
| 402 // inflight navigations. | |
| 403 void DeleteAllUrls(); | 307 void DeleteAllUrls(); |
| 404 | 308 |
| 405 // Deletes data for the input |urls| and their corresponding hosts from the | 309 // Deletes data for the input |urls| and their corresponding hosts from the |
| 406 // predictor database and caches. | 310 // predictor database and caches. |
| 407 void DeleteUrls(const history::URLRows& urls); | 311 void DeleteUrls(const history::URLRows& urls); |
| 408 | 312 |
| 409 // Callback for GetUrlVisitCountTask. | 313 // Callback for GetUrlVisitCountTask. |
| 410 void OnVisitCountLookup(size_t url_visit_count, | 314 void OnVisitCountLookup(size_t url_visit_count, |
| 411 const PageRequestSummary& summary); | 315 const PageRequestSummary& summary); |
| 412 | 316 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 468 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_; | 372 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_; |
| 469 base::CancelableTaskTracker history_lookup_consumer_; | 373 base::CancelableTaskTracker history_lookup_consumer_; |
| 470 | 374 |
| 471 std::unique_ptr<PrefetchDataMap> url_resource_data_; | 375 std::unique_ptr<PrefetchDataMap> url_resource_data_; |
| 472 std::unique_ptr<PrefetchDataMap> host_resource_data_; | 376 std::unique_ptr<PrefetchDataMap> host_resource_data_; |
| 473 std::unique_ptr<RedirectDataMap> url_redirect_data_; | 377 std::unique_ptr<RedirectDataMap> url_redirect_data_; |
| 474 std::unique_ptr<RedirectDataMap> host_redirect_data_; | 378 std::unique_ptr<RedirectDataMap> host_redirect_data_; |
| 475 std::unique_ptr<ManifestDataMap> manifest_data_; | 379 std::unique_ptr<ManifestDataMap> manifest_data_; |
| 476 std::unique_ptr<OriginDataMap> origin_data_; | 380 std::unique_ptr<OriginDataMap> origin_data_; |
| 477 | 381 |
| 478 NavigationMap inflight_navigations_; | |
| 479 | |
| 480 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> | 382 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> |
| 481 history_service_observer_; | 383 history_service_observer_; |
| 482 | 384 |
| 483 base::WeakPtrFactory<ResourcePrefetchPredictor> weak_factory_; | 385 base::WeakPtrFactory<ResourcePrefetchPredictor> weak_factory_; |
| 484 | 386 |
| 485 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor); | 387 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor); |
| 486 }; | 388 }; |
| 487 | 389 |
| 488 // An interface used to notify that data in the ResourcePrefetchPredictor | 390 // An interface used to notify that data in the ResourcePrefetchPredictor |
| 489 // has changed. All methods are invoked on the UI thread. | 391 // has changed. All methods are invoked on the UI thread. |
| 490 class TestObserver { | 392 class TestObserver { |
| 491 public: | 393 public: |
| 492 // De-registers itself from |predictor_| on destruction. | 394 // De-registers itself from |predictor_| on destruction. |
| 493 virtual ~TestObserver(); | 395 virtual ~TestObserver(); |
| 494 | 396 |
| 495 virtual void OnPredictorInitialized() {} | 397 virtual void OnPredictorInitialized() {} |
| 496 | 398 |
| 497 virtual void OnNavigationLearned( | 399 virtual void OnNavigationLearned(size_t url_visit_count, |
| 498 size_t url_visit_count, | 400 const PageRequestSummary& summary) {} |
| 499 const ResourcePrefetchPredictor::PageRequestSummary& summary) {} | |
| 500 | 401 |
| 501 virtual void OnPrefetchingStarted(const GURL& main_frame_url) {} | 402 virtual void OnPrefetchingStarted(const GURL& main_frame_url) {} |
| 502 | 403 |
| 503 virtual void OnPrefetchingStopped(const GURL& main_frame_url) {} | 404 virtual void OnPrefetchingStopped(const GURL& main_frame_url) {} |
| 504 | 405 |
| 505 virtual void OnPrefetchingFinished(const GURL& main_frame_url) {} | 406 virtual void OnPrefetchingFinished(const GURL& main_frame_url) {} |
| 506 | 407 |
| 507 protected: | 408 protected: |
| 508 // |predictor| must be non-NULL and has to outlive the TestObserver. | 409 // |predictor| must be non-NULL and has to outlive the TestObserver. |
| 509 // Also the predictor must not have a TestObserver set. | 410 // Also the predictor must not have a TestObserver set. |
| 510 explicit TestObserver(ResourcePrefetchPredictor* predictor); | 411 explicit TestObserver(ResourcePrefetchPredictor* predictor); |
| 511 | 412 |
| 512 private: | 413 private: |
| 513 ResourcePrefetchPredictor* predictor_; | 414 ResourcePrefetchPredictor* predictor_; |
| 514 | 415 |
| 515 DISALLOW_COPY_AND_ASSIGN(TestObserver); | 416 DISALLOW_COPY_AND_ASSIGN(TestObserver); |
| 516 }; | 417 }; |
| 517 | 418 |
| 518 } // namespace predictors | 419 } // namespace predictors |
| 519 | 420 |
| 520 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ | 421 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ |
| OLD | NEW |