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 void UpdateOrAddToOrigins(const URLRequestSummary& request_summary); | |
| 78 ~PageRequestSummary(); | |
| 79 | |
| 80 GURL main_frame_url; | |
| 81 GURL initial_url; | |
| 82 base::TimeTicks first_contentful_paint; | |
| 83 | |
| 84 // Stores all subresource requests within a single navigation, from initial | |
| 85 // main frame request to navigation completion. | |
| 86 std::vector<URLRequestSummary> subresource_requests; | |
| 87 // Map of origin -> OriginRequestSummary. Only one instance of each origin | |
| 88 // is kept per navigation, but the summary is updated several times. | |
| 89 std::map<GURL, OriginRequestSummary> origins; | |
| 90 }; | |
| 91 | |
| 19 // Records navigation events as reported by various observers to the database | 92 // 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 | 93 // and stats collection classes. All the non-static methods of this class need |
| 21 // to be called on the UI thread. | 94 // to be called on the UI thread. |
| 22 class LoadingDataCollector | 95 class LoadingDataCollector |
| 23 : public base::SupportsWeakPtr<LoadingDataCollector> { | 96 : public base::SupportsWeakPtr<LoadingDataCollector> { |
| 24 public: | 97 public: |
| 25 explicit LoadingDataCollector( | 98 explicit LoadingDataCollector( |
| 26 predictors::ResourcePrefetchPredictor* predictor); | 99 predictors::ResourcePrefetchPredictor* predictor, |
| 100 predictors::LoadingStatsCollector* stats_collector, | |
| 101 const LoadingPredictorConfig& config); | |
| 27 ~LoadingDataCollector(); | 102 ~LoadingDataCollector(); |
| 28 | 103 |
| 104 // Determines the resource type from the declared one, falling back to MIME | |
| 105 // type detection when it is not explicit. | |
| 106 static content::ResourceType GetResourceType( | |
| 107 content::ResourceType resource_type, | |
| 108 const std::string& mime_type); | |
| 109 | |
| 110 // Determines the ResourceType from the mime type, defaulting to the | |
| 111 // |fallback| if the ResourceType could not be determined. | |
| 112 static content::ResourceType GetResourceTypeFromMimeType( | |
| 113 const std::string& mime_type, | |
| 114 content::ResourceType fallback); | |
| 115 | |
| 29 // Thread safe. | 116 // Thread safe. |
| 30 static bool ShouldRecordRequest(net::URLRequest* request, | 117 static bool ShouldRecordRequest(net::URLRequest* request, |
| 31 content::ResourceType resource_type); | 118 content::ResourceType resource_type); |
| 32 static bool ShouldRecordResponse(net::URLRequest* response); | 119 static bool ShouldRecordResponse(net::URLRequest* response); |
| 33 static bool ShouldRecordRedirect(net::URLRequest* response); | 120 static bool ShouldRecordRedirect(net::URLRequest* response); |
| 34 | 121 |
| 35 // 'LoadingPredictorObserver' and 'ResourcePrefetchPredictorTabHelper' call | 122 // 'LoadingPredictorObserver' and 'ResourcePrefetchPredictorTabHelper' call |
| 36 // the below functions to inform the predictor of main frame and resource | 123 // the below functions to inform the collector of main frame and resource |
| 37 // requests. Should only be called if the corresponding Should* functions | 124 // requests. Should only be called if the corresponding Should* functions |
| 38 // return true. | 125 // return true. |
| 39 void RecordURLRequest( | 126 void RecordURLRequest(const URLRequestSummary& request); |
| 40 const predictors::ResourcePrefetchPredictor::URLRequestSummary& request); | 127 void RecordURLResponse(const URLRequestSummary& response); |
| 41 void RecordURLResponse( | 128 void RecordURLRedirect(const URLRequestSummary& response); |
| 42 const predictors::ResourcePrefetchPredictor::URLRequestSummary& response); | |
| 43 void RecordURLRedirect( | |
| 44 const predictors::ResourcePrefetchPredictor::URLRequestSummary& response); | |
| 45 | 129 |
| 46 // Called when the main frame of a page completes loading. | 130 // Called when the main frame of a page completes loading. We treat this point |
| 131 // as the "completion" of the navigation. The resources requested by the page | |
| 132 // up to this point are the only ones considered. | |
| 47 void RecordMainFrameLoadComplete(const NavigationID& navigation_id); | 133 void RecordMainFrameLoadComplete(const NavigationID& navigation_id); |
| 48 | 134 |
| 49 // Called after the main frame's first contentful paint. | 135 // Called after the main frame's first contentful paint. |
| 50 void RecordFirstContentfulPaint( | 136 virtual void RecordFirstContentfulPaint( |
|
alexilin
2017/06/23 16:55:05
nit:
You aren't mocking this class so there is no
trevordixon
2017/06/27 21:28:55
Done.
| |
| 51 const NavigationID& navigation_id, | 137 const NavigationID& navigation_id, |
| 52 const base::TimeTicks& first_contentful_paint); | 138 const base::TimeTicks& first_contentful_paint); |
| 53 | 139 |
| 54 private: | 140 private: |
| 141 using NavigationMap = | |
| 142 std::map<NavigationID, std::unique_ptr<PageRequestSummary>>; | |
| 143 | |
| 144 friend class LoadingDataCollectorTest; | |
| 55 friend class ResourcePrefetchPredictorBrowserTest; | 145 friend class ResourcePrefetchPredictorBrowserTest; |
| 56 | 146 |
| 57 FRIEND_TEST_ALL_PREFIXES(LoadingDataCollectorTest, HandledResourceTypes); | 147 FRIEND_TEST_ALL_PREFIXES(LoadingDataCollectorTest, HandledResourceTypes); |
| 148 FRIEND_TEST_ALL_PREFIXES(LoadingDataCollectorTest, SimpleNavigation); | |
| 149 FRIEND_TEST_ALL_PREFIXES(LoadingDataCollectorTest, SimpleRedirect); | |
| 150 FRIEND_TEST_ALL_PREFIXES(LoadingDataCollectorTest, OnMainFrameRequest); | |
| 151 FRIEND_TEST_ALL_PREFIXES(LoadingDataCollectorTest, OnMainFrameRedirect); | |
| 152 FRIEND_TEST_ALL_PREFIXES(LoadingDataCollectorTest, OnSubresourceResponse); | |
| 153 FRIEND_TEST_ALL_PREFIXES(LoadingDataCollectorTest, | |
| 154 TestRecordFirstContentfulPaint); | |
| 58 | 155 |
| 59 // Returns true if the main page request is supported for prediction. | 156 // Returns true if the main page request is supported for prediction. |
| 60 static bool IsHandledMainPage(net::URLRequest* request); | 157 static bool IsHandledMainPage(net::URLRequest* request); |
| 61 | 158 |
| 62 // Returns true if the subresource request is supported for prediction. | 159 // Returns true if the subresource request is supported for prediction. |
| 63 static bool IsHandledSubresource(net::URLRequest* request, | 160 static bool IsHandledSubresource(net::URLRequest* request, |
| 64 content::ResourceType resource_type); | 161 content::ResourceType resource_type); |
| 65 | 162 |
| 66 // Returns true if the subresource has a supported type. | 163 // Returns true if the subresource has a supported type. |
| 67 static bool IsHandledResourceType(content::ResourceType resource_type, | 164 static bool IsHandledResourceType(content::ResourceType resource_type, |
| 68 const std::string& mime_type); | 165 const std::string& mime_type); |
| 69 | 166 |
| 70 static void SetAllowPortInUrlsForTesting(bool state); | 167 static void SetAllowPortInUrlsForTesting(bool state); |
| 71 | 168 |
| 72 predictors::ResourcePrefetchPredictor* const predictor_; | 169 // Functions called on different network events pertaining to the loading of |
| 170 // main frame resource or sub resources. | |
| 171 void OnMainFrameRedirect(const URLRequestSummary& response); | |
| 172 void OnSubresourceRedirect(const URLRequestSummary& response); | |
| 173 | |
| 174 // Cleanup inflight_navigations_ and call a cleanup for stats_collector_. | |
| 175 void CleanupAbandonedNavigations(const NavigationID& navigation_id); | |
| 176 | |
| 177 ResourcePrefetchPredictor* const predictor_; | |
| 178 LoadingStatsCollector* const stats_collector_; | |
| 179 const LoadingPredictorConfig config_; | |
| 180 | |
| 181 NavigationMap inflight_navigations_; | |
| 73 }; | 182 }; |
| 74 | 183 |
| 75 } // namespace predictors | 184 } // namespace predictors |
| 76 | 185 |
| 77 #endif // CHROME_BROWSER_PREDICTORS_LOADING_DATA_COLLECTOR_H_ | 186 #endif // CHROME_BROWSER_PREDICTORS_LOADING_DATA_COLLECTOR_H_ |
| OLD | NEW |