Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(841)

Side by Side Diff: chrome/browser/predictors/loading_data_collector.h

Issue 2937623007: predictors: Move more methods from ResourcePrefetchPredictor into LoadingDataCollector. (Closed)
Patch Set: Fix browser test Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
19 // Records to the database and stats collection classes navigation events as 26 class LoadingStatsCollector;
20 // reported by various observers. All the non-static methods of this class need 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
92 // Records navigation events as reported by various observers to the database
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: 96 public:
24 explicit LoadingDataCollector(ResourcePrefetchPredictor* predictor); 97 explicit LoadingDataCollector(
98 predictors::ResourcePrefetchPredictor* predictor,
99 predictors::LoadingStatsCollector* stats_collector,
100 const LoadingPredictorConfig& config);
25 ~LoadingDataCollector(); 101 ~LoadingDataCollector();
26 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
27 // Thread safe. 115 // Thread safe.
28 static bool ShouldRecordRequest(net::URLRequest* request, 116 static bool ShouldRecordRequest(net::URLRequest* request,
29 content::ResourceType resource_type); 117 content::ResourceType resource_type);
30 static bool ShouldRecordResponse(net::URLRequest* response); 118 static bool ShouldRecordResponse(net::URLRequest* response);
31 static bool ShouldRecordRedirect(net::URLRequest* response); 119 static bool ShouldRecordRedirect(net::URLRequest* response);
32 120
33 // 'LoadingPredictorObserver' and 'ResourcePrefetchPredictorTabHelper' call 121 // 'LoadingPredictorObserver' and 'ResourcePrefetchPredictorTabHelper' call
34 // 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
35 // requests. Should only be called if the corresponding Should* functions 123 // requests. Should only be called if the corresponding Should* functions
36 // return true. 124 // return true.
37 void RecordURLRequest( 125 void RecordURLRequest(const URLRequestSummary& request);
38 const ResourcePrefetchPredictor::URLRequestSummary& request); 126 void RecordURLResponse(const URLRequestSummary& response);
39 void RecordURLResponse( 127 void RecordURLRedirect(const URLRequestSummary& response);
40 const ResourcePrefetchPredictor::URLRequestSummary& response);
41 void RecordURLRedirect(
42 const ResourcePrefetchPredictor::URLRequestSummary& response);
43 128
44 // 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.
45 void RecordMainFrameLoadComplete(const NavigationID& navigation_id); 132 void RecordMainFrameLoadComplete(const NavigationID& navigation_id);
46 133
47 // Called after the main frame's first contentful paint. 134 // Called after the main frame's first contentful paint.
48 void RecordFirstContentfulPaint( 135 void RecordFirstContentfulPaint(
49 const NavigationID& navigation_id, 136 const NavigationID& navigation_id,
50 const base::TimeTicks& first_contentful_paint); 137 const base::TimeTicks& first_contentful_paint);
51 138
52 private: 139 private:
140 using NavigationMap =
141 std::map<NavigationID, std::unique_ptr<PageRequestSummary>>;
142
143 friend class LoadingDataCollectorTest;
53 friend class ResourcePrefetchPredictorBrowserTest; 144 friend class ResourcePrefetchPredictorBrowserTest;
54 145
55 FRIEND_TEST_ALL_PREFIXES(LoadingDataCollectorTest, HandledResourceTypes); 146 FRIEND_TEST_ALL_PREFIXES(LoadingDataCollectorTest, HandledResourceTypes);
147 FRIEND_TEST_ALL_PREFIXES(LoadingDataCollectorTest, SimpleNavigation);
148 FRIEND_TEST_ALL_PREFIXES(LoadingDataCollectorTest, SimpleRedirect);
149 FRIEND_TEST_ALL_PREFIXES(LoadingDataCollectorTest, OnMainFrameRequest);
150 FRIEND_TEST_ALL_PREFIXES(LoadingDataCollectorTest, OnMainFrameRedirect);
151 FRIEND_TEST_ALL_PREFIXES(LoadingDataCollectorTest, OnSubresourceResponse);
152 FRIEND_TEST_ALL_PREFIXES(LoadingDataCollectorTest,
153 TestRecordFirstContentfulPaint);
56 154
57 // Returns true if the main page request is supported for prediction. 155 // Returns true if the main page request is supported for prediction.
58 static bool IsHandledMainPage(net::URLRequest* request); 156 static bool IsHandledMainPage(net::URLRequest* request);
59 157
60 // Returns true if the subresource request is supported for prediction. 158 // Returns true if the subresource request is supported for prediction.
61 static bool IsHandledSubresource(net::URLRequest* request, 159 static bool IsHandledSubresource(net::URLRequest* request,
62 content::ResourceType resource_type); 160 content::ResourceType resource_type);
63 161
64 // Returns true if the subresource has a supported type. 162 // Returns true if the subresource has a supported type.
65 static bool IsHandledResourceType(content::ResourceType resource_type, 163 static bool IsHandledResourceType(content::ResourceType resource_type,
66 const std::string& mime_type); 164 const std::string& mime_type);
67 165
68 static void SetAllowPortInUrlsForTesting(bool state); 166 static void SetAllowPortInUrlsForTesting(bool state);
69 167
168 // Functions called on different network events pertaining to the loading of
169 // main frame resource or sub resources.
170 void OnMainFrameRedirect(const URLRequestSummary& response);
171 void OnSubresourceRedirect(const URLRequestSummary& response);
172
173 // Cleanup inflight_navigations_ and call a cleanup for stats_collector_.
174 void CleanupAbandonedNavigations(const NavigationID& navigation_id);
175
70 ResourcePrefetchPredictor* const predictor_; 176 ResourcePrefetchPredictor* const predictor_;
177 LoadingStatsCollector* const stats_collector_;
178 const LoadingPredictorConfig config_;
179
180 NavigationMap inflight_navigations_;
71 }; 181 };
72 182
73 } // namespace predictors 183 } // namespace predictors
74 184
75 #endif // CHROME_BROWSER_PREDICTORS_LOADING_DATA_COLLECTOR_H_ 185 #endif // CHROME_BROWSER_PREDICTORS_LOADING_DATA_COLLECTOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698