Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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_LOADING_DATA_COLLECTOR_H_ | 5 #ifndef CHROME_BROWSER_PREDICTORS_LOADING_DATA_COLLECTOR_H_ |
| 6 #define CHROME_BROWSER_PREDICTORS_LOADING_DATA_COLLECTOR_H_ | 6 #define CHROME_BROWSER_PREDICTORS_LOADING_DATA_COLLECTOR_H_ |
| 7 | 7 |
| 8 #include <map> | |
| 9 #include <memory> | |
| 8 #include <string> | 10 #include <string> |
| 11 #include <vector> | |
| 9 | 12 |
| 10 #include "chrome/browser/predictors/resource_prefetch_predictor.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "chrome/browser/predictors/loading_predictor_config.h" | |
| 15 #include "chrome/browser/predictors/resource_prefetch_common.h" | |
| 11 #include "content/public/common/resource_type.h" | 16 #include "content/public/common/resource_type.h" |
| 17 #include "net/base/request_priority.h" | |
| 18 #include "url/gurl.h" | |
| 12 | 19 |
| 13 namespace net { | 20 namespace net { |
| 14 class URLRequest; | 21 class URLRequest; |
| 15 } | 22 } |
| 16 | 23 |
| 17 namespace predictors { | 24 namespace predictors { |
| 18 | 25 |
| 26 class LoadingStatsCollector; | |
| 27 class ResourcePrefetchPredictor; | |
| 28 | |
| 29 // Data collected for origin-based prediction, for a single origin during a | |
| 30 // page load (see PageRequestSummary). | |
| 31 struct OriginRequestSummary { | |
| 32 OriginRequestSummary(); | |
| 33 OriginRequestSummary(const OriginRequestSummary& other); | |
| 34 ~OriginRequestSummary(); | |
| 35 | |
| 36 GURL origin; | |
| 37 bool always_access_network; | |
| 38 bool accessed_network; | |
| 39 int first_occurrence; | |
| 40 }; | |
| 41 | |
| 42 // Stores the data that we need to get from the URLRequest. | |
| 43 struct URLRequestSummary { | |
| 44 URLRequestSummary(); | |
| 45 URLRequestSummary(const URLRequestSummary& other); | |
| 46 ~URLRequestSummary(); | |
| 47 | |
| 48 NavigationID navigation_id; | |
| 49 GURL resource_url; | |
| 50 GURL request_url; // URL after all redirects. | |
| 51 content::ResourceType resource_type; | |
| 52 net::RequestPriority priority; | |
| 53 base::TimeTicks response_time; | |
| 54 bool before_first_contentful_paint; | |
| 55 | |
| 56 // Only for responses. | |
| 57 std::string mime_type; | |
| 58 bool was_cached; | |
| 59 GURL redirect_url; // Empty unless request was redirected to a valid url. | |
| 60 | |
| 61 bool has_validators; | |
| 62 bool always_revalidate; | |
| 63 bool is_no_store; | |
| 64 bool network_accessed; | |
| 65 | |
| 66 // Initializes a |URLRequestSummary| from a |URLRequest| response. | |
| 67 // Returns true for success. Note: NavigationID is NOT initialized | |
| 68 // by this function. | |
| 69 static bool SummarizeResponse(const net::URLRequest& request, | |
| 70 URLRequestSummary* summary); | |
| 71 }; | |
| 72 | |
| 73 // Stores the data learned from a single navigation. | |
| 74 struct PageRequestSummary { | |
| 75 explicit PageRequestSummary(const GURL& main_frame_url); | |
| 76 PageRequestSummary(const PageRequestSummary& other); | |
| 77 ~PageRequestSummary(); | |
| 78 | |
| 79 GURL main_frame_url; | |
| 80 GURL initial_url; | |
| 81 base::TimeTicks first_contentful_paint; | |
| 82 | |
| 83 // Stores all subresource requests within a single navigation, from initial | |
| 84 // main frame request to navigation completion. | |
| 85 std::vector<URLRequestSummary> subresource_requests; | |
| 86 // Map of origin -> OriginRequestSummary. Only one instance of each origin | |
| 87 // is kept per navigation, but the summary is updated several times. | |
| 88 std::map<GURL, OriginRequestSummary> origins; | |
| 89 }; | |
| 90 | |
| 19 // Records navigation events as reported by various observers to the database | 91 // Records navigation events as reported by various observers to the database |
| 20 // and stats collection classes. All the non-static methods of this class need | 92 // and stats collection classes. All the non-static methods of this class need |
| 21 // to be called on the UI thread. | 93 // to be called on the UI thread. |
| 22 class LoadingDataCollector | 94 class LoadingDataCollector |
| 23 : public base::SupportsWeakPtr<LoadingDataCollector> { | 95 : public base::SupportsWeakPtr<LoadingDataCollector> { |
| 24 public: | 96 public: |
| 25 explicit LoadingDataCollector( | 97 explicit LoadingDataCollector( |
| 26 predictors::ResourcePrefetchPredictor* predictor); | 98 predictors::ResourcePrefetchPredictor* predictor, |
| 99 predictors::LoadingStatsCollector* stats_collector, | |
| 100 const LoadingPredictorConfig& config); | |
| 27 ~LoadingDataCollector(); | 101 ~LoadingDataCollector(); |
| 28 | 102 |
| 103 // Determines the resource type from the declared one, falling back to MIME | |
| 104 // type detection when it is not explicit. | |
| 105 static content::ResourceType GetResourceType( | |
| 106 content::ResourceType resource_type, | |
| 107 const std::string& mime_type); | |
| 108 | |
| 109 // Determines the ResourceType from the mime type, defaulting to the | |
| 110 // |fallback| if the ResourceType could not be determined. | |
| 111 static content::ResourceType GetResourceTypeFromMimeType( | |
| 112 const std::string& mime_type, | |
| 113 content::ResourceType fallback); | |
| 114 | |
| 29 // Thread safe. | 115 // Thread safe. |
| 30 static bool ShouldRecordRequest(net::URLRequest* request, | 116 static bool ShouldRecordRequest(net::URLRequest* request, |
| 31 content::ResourceType resource_type); | 117 content::ResourceType resource_type); |
| 32 static bool ShouldRecordResponse(net::URLRequest* response); | 118 static bool ShouldRecordResponse(net::URLRequest* response); |
| 33 static bool ShouldRecordRedirect(net::URLRequest* response); | 119 static bool ShouldRecordRedirect(net::URLRequest* response); |
| 34 | 120 |
| 35 // 'LoadingPredictorObserver' and 'ResourcePrefetchPredictorTabHelper' call | 121 // 'LoadingPredictorObserver' and 'ResourcePrefetchPredictorTabHelper' call |
| 36 // the below functions to inform the predictor of main frame and resource | 122 // the below functions to inform the collector of main frame and resource |
| 37 // requests. Should only be called if the corresponding Should* functions | 123 // requests. Should only be called if the corresponding Should* functions |
| 38 // return true. | 124 // return true. |
| 39 void RecordURLRequest( | 125 void RecordURLRequest(const URLRequestSummary& request); |
| 40 const predictors::ResourcePrefetchPredictor::URLRequestSummary& request); | 126 void RecordURLResponse(const URLRequestSummary& response); |
| 41 void RecordURLResponse( | 127 void RecordURLRedirect(const URLRequestSummary& response); |
| 42 const predictors::ResourcePrefetchPredictor::URLRequestSummary& response); | |
| 43 void RecordURLRedirect( | |
| 44 const predictors::ResourcePrefetchPredictor::URLRequestSummary& response); | |
| 45 | 128 |
| 46 // Called when the main frame of a page completes loading. | 129 // Called when the main frame of a page completes loading. We treat this point |
| 130 // as the "completion" of the navigation. The resources requested by the page | |
| 131 // up to this point are the only ones considered. | |
| 47 void RecordMainFrameLoadComplete(const NavigationID& navigation_id); | 132 void RecordMainFrameLoadComplete(const NavigationID& navigation_id); |
| 48 | 133 |
| 49 // Called after the main frame's first contentful paint. | 134 // Called after the main frame's first contentful paint. |
| 50 void RecordFirstContentfulPaint( | 135 virtual void RecordFirstContentfulPaint( |
| 51 const NavigationID& navigation_id, | 136 const NavigationID& navigation_id, |
| 52 const base::TimeTicks& first_contentful_paint); | 137 const base::TimeTicks& first_contentful_paint); |
| 53 | 138 |
| 54 private: | 139 private: |
| 55 friend class ResourcePrefetchPredictorBrowserTest; | 140 friend class ResourcePrefetchPredictorBrowserTest; |
| 56 | 141 |
| 57 FRIEND_TEST_ALL_PREFIXES(LoadingDataCollectorTest, HandledResourceTypes); | 142 FRIEND_TEST_ALL_PREFIXES(LoadingDataCollectorTest, HandledResourceTypes); |
| 58 | 143 |
| 59 // Returns true if the main page request is supported for prediction. | 144 // Returns true if the main page request is supported for prediction. |
| 60 static bool IsHandledMainPage(net::URLRequest* request); | 145 static bool IsHandledMainPage(net::URLRequest* request); |
| 61 | 146 |
| 62 // Returns true if the subresource request is supported for prediction. | 147 // Returns true if the subresource request is supported for prediction. |
| 63 static bool IsHandledSubresource(net::URLRequest* request, | 148 static bool IsHandledSubresource(net::URLRequest* request, |
| 64 content::ResourceType resource_type); | 149 content::ResourceType resource_type); |
| 65 | 150 |
| 66 // Returns true if the subresource has a supported type. | 151 // Returns true if the subresource has a supported type. |
| 67 static bool IsHandledResourceType(content::ResourceType resource_type, | 152 static bool IsHandledResourceType(content::ResourceType resource_type, |
| 68 const std::string& mime_type); | 153 const std::string& mime_type); |
| 69 | 154 |
| 70 static void SetAllowPortInUrlsForTesting(bool state); | 155 static void SetAllowPortInUrlsForTesting(bool state); |
| 71 | 156 |
| 72 predictors::ResourcePrefetchPredictor* const predictor_; | 157 // Functions called on different network events pertaining to the loading of |
| 158 // main frame resource or sub resources. | |
| 159 void OnMainFrameRequest(const URLRequestSummary& request); | |
| 160 void OnMainFrameRedirect(const URLRequestSummary& response); | |
| 161 void OnSubresourceResponse(const URLRequestSummary& response); | |
| 162 void OnSubresourceRedirect(const URLRequestSummary& response); | |
| 163 | |
| 164 // Cleanup inflight_navigations_ and call a cleanup for stats_collector_. | |
| 165 void CleanupAbandonedNavigations(const NavigationID& navigation_id); | |
| 166 | |
| 167 typedef std::map<NavigationID, std::unique_ptr<PageRequestSummary>> | |
|
alexilin
2017/06/16 09:31:57
nit:
using NavigationMap = std::map<NavigationID,
trevordixon
2017/06/16 10:53:19
Done.
| |
| 168 NavigationMap; | |
| 169 | |
| 170 ResourcePrefetchPredictor* const predictor_; | |
| 171 LoadingStatsCollector* const stats_collector_; | |
| 172 const LoadingPredictorConfig config_; | |
| 173 | |
| 174 NavigationMap inflight_navigations_; | |
| 73 }; | 175 }; |
| 74 | 176 |
| 75 } // namespace predictors | 177 } // namespace predictors |
| 76 | 178 |
| 77 #endif // CHROME_BROWSER_PREDICTORS_LOADING_DATA_COLLECTOR_H_ | 179 #endif // CHROME_BROWSER_PREDICTORS_LOADING_DATA_COLLECTOR_H_ |
| OLD | NEW |