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

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 Dominich's comments. 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/predictors/resource_prefetch_common.h"
17 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h"
18 #include "chrome/browser/profiles/profile_keyed_service.h"
19 #include "content/public/browser/notification_observer.h"
20 #include "content/public/browser/notification_registrar.h"
21 #include "googleurl/src/gurl.h"
22 #include "webkit/glue/resource_type.h"
23
24 class PredictorsHandler;
25 class Profile;
26
27 namespace content {
28 class WebContents;
29 }
30
31 namespace history {
32 class URLrowsDatabase;
33 }
34
35 namespace net {
36 class URLRequest;
37 }
38
39 namespace predictors {
40
41 // Contains logic for learning what can be prefetched and for kicking off
42 // speculative prefetching.
43 // - The class is a profile keyed service owned by the profile.
44 // - All the non-static methods of this class need to be called on the UI
45 // thread.
46 //
47 // The overall flow of the resource prefetching algorithm is as follows:
48 //
49 // * ResourcePrefetchPredictorObserver - Listens for URL requests, responses and
50 // redirects on the IO thread(via RDHostDelegate) and post tasks to the
51 // ResourcePrefetchPredictor on the UI thread. This is owned by the
52 // ProfileIOData for the profile.
53 // * ResourcePrefetchPredictorTables - Persists ResourcePrefetchPredictor data
54 // to a sql database. Runs entirely on the DB thread. Owned by the
55 // PredictorDatabase.
56 // * ResourcePrefetchPredictor - Learns about resource requirements per URL in
57 // the UI thread through the ResourcePrefetchPredictorObserver and perisists
58 // it to disk in the DB thread through the ResourcePrefetchPredictorTables.
59 // Owned by profile.
60 //
61 // TODO(shishir): Implement the prefetching of resources.
62 class ResourcePrefetchPredictor
63 : public ProfileKeyedService,
64 public content::NotificationObserver,
65 public base::SupportsWeakPtr<ResourcePrefetchPredictor> {
66 public:
67 explicit ResourcePrefetchPredictor(Profile* profile);
68 virtual ~ResourcePrefetchPredictor();
69
70 // Stores the data that we need to get from the URLRequest.
71 struct URLRequestSummary {
72 URLRequestSummary();
73 URLRequestSummary(const URLRequestSummary& other);
74 ~URLRequestSummary();
75 bool InitFromURLRequest(net::URLRequest* request, bool is_response);
willchan no longer on Chromium 2012/05/31 02:08:58 Why is this defined here? It seems to only be used
Shishir 2012/06/02 01:19:29 Done.
76
77 NavigationID navigation_id_;
willchan no longer on Chromium 2012/05/31 02:08:58 Remove the trailing underscores for structs.
Shishir 2012/06/02 01:19:29 Done.
78 GURL resource_url_;
79 ResourceType::Type resource_type_;
80
81 // Only for responses.
82 std::string mime_type_;
83 bool was_cached_;
84 };
85
86 // Thread safe.
87 static bool IsEnabled();
88 static bool ShouldRecordRequest(net::URLRequest* request,
89 ResourceType::Type resource_type);
90 static bool ShouldRecordResponse(net::URLRequest* response);
91 static bool ShouldRecordRedirect(net::URLRequest* response);
92
93 // UI thread.
willchan no longer on Chromium 2012/05/31 02:08:58 Everything's on the IO thread, right? Other than t
Shishir 2012/06/02 01:19:29 Done.
94 void RecordURLRequest(const URLRequestSummary& request);
95 void RecordUrlResponse(const URLRequestSummary& response);
96 void RecordUrlRedirect(const URLRequestSummary& response);
97
98 private:
99 friend class ::PredictorsHandler;
willchan no longer on Chromium 2012/05/31 02:08:58 Hm...do you really need this? AFAICT, this is only
Shishir 2012/06/02 01:19:29 In the following CLs, we will be exposing much mor
100
101 // TODO(shishir): Maybe use pointers to make the sort cheaper.
102 typedef ResourcePrefetchPredictorTables::UrlTableRow UrlTableRow;
103 typedef std::vector<UrlTableRow> UrlTableRowVector;
104
105 struct UrlTableCacheValue {
106 UrlTableRowVector rows_;
107 base::Time last_visit_;
108 };
109
110 typedef std::map<NavigationID, std::vector<URLRequestSummary> > NavigationMap;
111 typedef std::map<GURL, UrlTableCacheValue> UrlTableCacheMap;
112
113 // content::NotificationObserver methods OVERRIDE.
114 virtual void Observe(int type,
115 const content::NotificationSource& source,
116 const content::NotificationDetails& details) OVERRIDE;
117
118 static bool IsHandledMainPage(net::URLRequest* request);
119 static bool IsHandledSubresource(net::URLRequest* response);
120 static bool IsCacheable(const net::URLRequest* response);
121
122 // Deal with different kinds of requests.
123 void OnMainFrameRequest(const URLRequestSummary& request);
124 void OnMainFrameResponse(const URLRequestSummary& response);
125 void OnMainFrameRedirect(const URLRequestSummary& response);
126 void OnSubresourceResponse(const URLRequestSummary& response);
127 void OnSubresourceLoadedFromMemory(const NavigationID& navigation_id,
128 const GURL& resource_url);
129
130 void LazilyInitialize();
131 void OnHistoryAndCacheLoaded();
132 void CreateCaches(std::vector<UrlTableRow>* url_rows);
133 bool ShouldTrackUrl(const GURL& url);
134 void CleanupAbandonedNavigations(const NavigationID& navigation_id);
135 void OnNavigationComplete(const NavigationID& navigation_id);
136 void LearnUrlNavigation(const GURL& main_frame_url,
137 const std::vector<URLRequestSummary>& new_value);
138 void RemoveAnEntryFromUrlDB();
139 void MaybeReportAccuracyStats(const NavigationID& navigation_id);
140
141 enum InitializationState {
willchan no longer on Chromium 2012/05/31 02:08:58 types go in the beginning of the access section, p
Shishir 2012/06/02 01:19:29 Done.
142 NOT_INITIALIZED = 0,
143 INITIALIZING = 1,
144 INITIALIZED = 2
145 };
146
147 Profile* profile_;
willchan no longer on Chromium 2012/05/31 02:08:58 Profile* const? Should never change, right?
Shishir 2012/06/02 01:19:29 Done.
148 InitializationState initialization_state_;
149 scoped_refptr<ResourcePrefetchPredictorTables> tables_;
willchan no longer on Chromium 2012/05/31 02:08:58 Doesn't this class own the ResourcePrefetchPredict
Shishir 2012/06/02 01:19:29 I would like to defer this to another CL since thi
150 scoped_ptr<content::NotificationRegistrar> notification_registrar_;
151
152 NavigationMap inflight_navigations_;
153 UrlTableCacheMap url_table_cache_;
154
155 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor);
156 };
157
158 } // namespace predictors
159
160 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698