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

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: Addressing Will's comments and adding a unittest. Created 8 years, 6 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/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/time.h"
16 #include "chrome/browser/history/history_types.h"
17 #include "chrome/browser/predictors/resource_prefetch_common.h"
18 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h"
19 #include "chrome/browser/profiles/profile_keyed_service.h"
20 #include "content/public/browser/notification_observer.h"
21 #include "content/public/browser/notification_registrar.h"
22 #include "googleurl/src/gurl.h"
23 #include "webkit/glue/resource_type.h"
24
25 class PredictorsHandler;
26 class Profile;
27
28 namespace content {
29 class WebContents;
30 }
31
32 namespace net {
33 class URLRequest;
34 }
35
36 namespace predictors {
37
38 // Contains logic for learning what can be prefetched and for kicking off
39 // speculative prefetching.
40 // - The class is a profile keyed service owned by the profile.
41 // - All the non-static methods of this class need to be called on the UI
42 // thread.
43 //
44 // The overall flow of the resource prefetching algorithm is as follows:
45 //
46 // * ResourcePrefetchPredictorObserver - Listens for URL requests, responses and
47 // redirects on the IO thread(via RDHostDelegate) and post tasks to the
48 // ResourcePrefetchPredictor on the UI thread. This is owned by the
49 // ProfileIOData for the profile.
50 // * ResourcePrefetchPredictorTables - Persists ResourcePrefetchPredictor data
51 // to a sql database. Runs entirely on the DB thread. Owned by the
52 // PredictorDatabase.
53 // * ResourcePrefetchPredictor - Learns about resource requirements per URL in
54 // the UI thread through the ResourcePrefetchPredictorObserver and perisists
55 // it to disk in the DB thread through the ResourcePrefetchPredictorTables.
56 // Owned by profile.
57 //
58 // TODO(shishir): Implement the prefetching of resources.
59 class ResourcePrefetchPredictor
60 : public ProfileKeyedService,
61 public content::NotificationObserver,
62 public base::SupportsWeakPtr<ResourcePrefetchPredictor> {
63 public:
64 explicit ResourcePrefetchPredictor(Profile* profile);
65 virtual ~ResourcePrefetchPredictor();
66
67 // Stores the data that we need to get from the URLRequest.
68 struct URLRequestSummary {
69 URLRequestSummary();
70 URLRequestSummary(const URLRequestSummary& other);
71 ~URLRequestSummary();
72
73 NavigationID navigation_id;
74 GURL resource_url;
75 ResourceType::Type resource_type;
76
77 // Only for responses.
78 std::string mime_type;
79 bool was_cached;
80 };
81
82 // Thread safe.
83 static bool IsEnabled();
84 static bool ShouldRecordRequest(net::URLRequest* request,
85 ResourceType::Type resource_type);
86 static bool ShouldRecordResponse(net::URLRequest* response);
87 static bool ShouldRecordRedirect(net::URLRequest* response);
88
89 static ResourceType::Type GetResourceTypeFromMimeType(
90 const std::string& mime_type,
91 ResourceType::Type fallback);
92
93 void RecordURLRequest(const URLRequestSummary& request);
94 void RecordUrlResponse(const URLRequestSummary& response);
95 void RecordUrlRedirect(const URLRequestSummary& response);
96
97 private:
98 friend class ::PredictorsHandler;
99
100 // TODO(shishir): Maybe use pointers to make the sort cheaper.
101 typedef ResourcePrefetchPredictorTables::UrlTableRow UrlTableRow;
102 typedef std::vector<UrlTableRow> UrlTableRowVector;
103
104 enum InitializationState {
105 NOT_INITIALIZED = 0,
106 INITIALIZING = 1,
107 INITIALIZED = 2
108 };
109
110 struct UrlTableCacheValue {
111 UrlTableRowVector rows;
112 base::Time last_visit;
113 };
114
115 typedef std::map<NavigationID, std::vector<URLRequestSummary> > NavigationMap;
116 typedef std::map<GURL, UrlTableCacheValue> UrlTableCacheMap;
117
118 // content::NotificationObserver methods OVERRIDE.
119 virtual void Observe(int type,
120 const content::NotificationSource& source,
121 const content::NotificationDetails& details) OVERRIDE;
122
123 static bool IsHandledMainPage(net::URLRequest* request);
124 static bool IsHandledSubresource(net::URLRequest* response);
125 static bool IsCacheable(const net::URLRequest* response);
126
127 // Deal with different kinds of requests.
128 void OnMainFrameRequest(const URLRequestSummary& request);
129 void OnMainFrameResponse(const URLRequestSummary& response);
130 void OnMainFrameRedirect(const URLRequestSummary& response);
131 void OnSubresourceResponse(const URLRequestSummary& response);
132 void OnSubresourceLoadedFromMemory(const NavigationID& navigation_id,
133 const GURL& resource_url,
134 const std::string& mime_type,
135 ResourceType::Type resource_type);
136
137 void LazilyInitialize();
138 void OnHistoryAndCacheLoaded();
139 void CreateCaches(std::vector<UrlTableRow>* url_rows);
140 bool ShouldTrackUrl(const GURL& url);
141 void CleanupAbandonedNavigations(const NavigationID& navigation_id);
142 void OnNavigationComplete(const NavigationID& navigation_id);
143 void LearnUrlNavigation(const GURL& main_frame_url,
144 const std::vector<URLRequestSummary>& new_value);
145 void RemoveAnEntryFromUrlDB();
146 void MaybeReportAccuracyStats(const NavigationID& navigation_id);
147
148 void DeleteAllUrls();
149 void DeleteUrls(const history::URLRows& urls);
150
151 Profile* const profile_;
152 InitializationState initialization_state_;
153 scoped_refptr<ResourcePrefetchPredictorTables> tables_;
154 content::NotificationRegistrar notification_registrar_;
155
156 NavigationMap inflight_navigations_;
157 UrlTableCacheMap url_table_cache_;
158
159 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor);
160 };
161
162 } // namespace predictors
163
164 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698