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

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

Issue 10416002: Seculative resource prefetching for URLs CL. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Resolving conflicts. Created 8 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
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_
6 #define CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_
7 #pragma once
8
9 #include <map>
10 #include <string>
11 #include <vector>
12
13 #include "base/gtest_prod_util.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/time.h"
17 #include "chrome/browser/history/history_types.h"
18 #include "chrome/browser/predictors/resource_prefetch_common.h"
19 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h"
20 #include "chrome/browser/profiles/profile_keyed_service.h"
21 #include "content/public/browser/notification_observer.h"
22 #include "content/public/browser/notification_registrar.h"
23 #include "googleurl/src/gurl.h"
24 #include "webkit/glue/resource_type.h"
25
26 class PredictorsHandler;
27 class Profile;
28
29 namespace content {
30 class WebContents;
31 }
32
33 namespace net {
34 class URLRequest;
35 }
36
37 namespace predictors {
38
39 // Contains logic for learning what can be prefetched and for kicking off
40 // speculative prefetching.
41 // - The class is a profile keyed service owned by the profile.
42 // - All the non-static methods of this class need to be called on the UI
43 // thread.
44 //
45 // The overall flow of the resource prefetching algorithm is as follows:
46 //
47 // * ResourcePrefetchPredictorObserver - Listens for URL requests, responses and
48 // redirects on the IO thread(via RDHostDelegate) and post tasks to the
49 // ResourcePrefetchPredictor on the UI thread. This is owned by the
50 // ProfileIOData for the profile.
51 // * ResourcePrefetchPredictorTables - Persists ResourcePrefetchPredictor data
52 // to a sql database. Runs entirely on the DB thread. Owned by the
53 // PredictorDatabase.
54 // * ResourcePrefetchPredictor - Learns about resource requirements per URL in
55 // the UI thread through the ResourcePrefetchPredictorObserver and perisists
56 // it to disk in the DB thread through the ResourcePrefetchPredictorTables.
57 // Owned by profile.
58 //
59 // TODO(shishir): Implement the prefetching of resources.
60 // TODO(shishir): Do speculative prefetching for https resources and/or https
61 // main_frame urls.
62 class ResourcePrefetchPredictor
63 : public ProfileKeyedService,
64 public content::NotificationObserver,
65 public base::SupportsWeakPtr<ResourcePrefetchPredictor> {
66 public:
67 // The following config allows us to change the predictor constants and run
68 // field trials with different constants.
69 struct Config {
70 // Initializes the config with default values.
71 Config();
72
73 // If a navigation hasn't seen a load complete event in this much time, it
74 // is considered abandoned.
75 int max_navigation_lifetime_seconds; // Default 60
76 // Size of LRU caches for the Url data.
77 size_t max_urls_to_track; // Default 500
78 // The number of times, we should have seen a visit to this Url in history
79 // to start tracking it. This is to ensure we dont bother with oneoff
80 // entries.
81 int min_url_visit_count; // Default 3
82 // The maximum number of resources to store per entry.
83 int max_resources_per_entry; // Default 50
84 // The number of consecutive misses after we stop tracking a resource Url.
85 int max_consecutive_misses; // Default 3
86 // The number of resources we should report accuracy stats on.
87 int num_resources_assumed_prefetched; // Default 25
88 };
89
90 // Stores the data that we need to get from the URLRequest.
91 struct URLRequestSummary {
92 URLRequestSummary();
93 URLRequestSummary(const URLRequestSummary& other);
94 ~URLRequestSummary();
95
96 NavigationID navigation_id;
97 GURL resource_url;
98 ResourceType::Type resource_type;
99
100 // Only for responses.
101 std::string mime_type;
102 bool was_cached;
103 };
104
105 ResourcePrefetchPredictor(const Config& config, Profile* profile);
106 virtual ~ResourcePrefetchPredictor();
107
108 // Thread safe.
109 static bool IsEnabled();
110 static bool ShouldRecordRequest(net::URLRequest* request,
111 ResourceType::Type resource_type);
112 static bool ShouldRecordResponse(net::URLRequest* response);
113 static bool ShouldRecordRedirect(net::URLRequest* response);
114
115 static ResourceType::Type GetResourceTypeFromMimeType(
116 const std::string& mime_type,
117 ResourceType::Type fallback);
118
119 void RecordURLRequest(const URLRequestSummary& request);
120 void RecordUrlResponse(const URLRequestSummary& response);
121 void RecordUrlRedirect(const URLRequestSummary& response);
122
123 private:
124 friend class ::PredictorsHandler;
125 friend class ResourcePrefetchPredictorTest;
126
127 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, DeleteUrls);
128 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest,
129 LazilyInitializeEmpty);
130 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest,
131 LazilyInitializeWithData);
132 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest,
133 NavigationNotRecorded);
134 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, NavigationUrlInDB);
135 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, NavigationUrlNotInDB);
136 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest,
137 NavigationUrlNotInDBAndDBFull);
138 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, OnMainFrameRequest);
139 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, OnMainFrameRedirect);
140 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest,
141 OnSubresourceResponse);
142
143 // TODO(shishir): Maybe use pointers to make the sort cheaper.
144 typedef ResourcePrefetchPredictorTables::UrlTableRow UrlTableRow;
145 typedef std::vector<UrlTableRow> UrlTableRowVector;
146
147 enum InitializationState {
148 NOT_INITIALIZED = 0,
149 INITIALIZING = 1,
150 INITIALIZED = 2
151 };
152
153 struct UrlTableCacheValue {
154 UrlTableCacheValue();
155 ~UrlTableCacheValue();
156
157 UrlTableRowVector rows;
158 base::Time last_visit;
159 };
160
161 typedef std::map<NavigationID, std::vector<URLRequestSummary> > NavigationMap;
162 typedef std::map<GURL, UrlTableCacheValue> UrlTableCacheMap;
163
164 // content::NotificationObserver methods OVERRIDE.
165 virtual void Observe(int type,
166 const content::NotificationSource& source,
167 const content::NotificationDetails& details) OVERRIDE;
168
169 static bool IsHandledMainPage(net::URLRequest* request);
170 static bool IsHandledSubresource(net::URLRequest* response);
171 static bool IsCacheable(const net::URLRequest* response);
172
173 // Deal with different kinds of requests.
174 void OnMainFrameRequest(const URLRequestSummary& request);
175 void OnMainFrameResponse(const URLRequestSummary& response);
176 void OnMainFrameRedirect(const URLRequestSummary& response);
177 void OnSubresourceResponse(const URLRequestSummary& response);
178 void OnSubresourceLoadedFromMemory(const NavigationID& navigation_id,
179 const GURL& resource_url,
180 const std::string& mime_type,
181 ResourceType::Type resource_type);
182
183 // Initialization code.
184 void LazilyInitialize();
185 void OnHistoryAndCacheLoaded();
186 void CreateCaches(std::vector<UrlTableRow>* url_rows);
187
188 // Database and cache cleanup code.
189 void RemoveAnEntryFromUrlDB();
190 void DeleteAllUrls();
191 void DeleteUrls(const history::URLRows& urls);
192
193 bool ShouldTrackUrl(const GURL& url);
194 void CleanupAbandonedNavigations(const NavigationID& navigation_id);
195 void OnNavigationComplete(const NavigationID& navigation_id);
196 void LearnUrlNavigation(const GURL& main_frame_url,
197 const std::vector<URLRequestSummary>& new_value);
198 void MaybeReportAccuracyStats(const NavigationID& navigation_id) const;
199
200 void SetTablesForTesting(
201 scoped_refptr<ResourcePrefetchPredictorTables> tables);
202
203 Profile* const profile_;
204 Config config_;
205 InitializationState initialization_state_;
206 scoped_refptr<ResourcePrefetchPredictorTables> tables_;
207 content::NotificationRegistrar notification_registrar_;
208
209 NavigationMap inflight_navigations_;
210 UrlTableCacheMap url_table_cache_;
211
212 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor);
213 };
214
215 } // namespace predictors
216
217 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_
OLDNEW
« no previous file with comments | « chrome/browser/predictors/resource_prefetch_common.cc ('k') | chrome/browser/predictors/resource_prefetch_predictor.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698