OLD | NEW |
---|---|
(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/time.h" | |
15 #include "chrome/browser/predictors/resource_prefetch_common.h" | |
16 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" | |
17 #include "chrome/browser/profiles/refcounted_profile_keyed_service.h" | |
18 #include "content/public/browser/notification_observer.h" | |
19 #include "content/public/browser/notification_registrar.h" | |
20 #include "googleurl/src/gurl.h" | |
21 #include "webkit/glue/resource_type.h" | |
22 | |
23 class PredictorsHandler; | |
24 class Profile; | |
25 | |
26 namespace content { | |
27 class WebContents; | |
28 } | |
29 | |
30 namespace history { | |
31 class URLrowsDatabase; | |
32 } | |
33 | |
34 namespace net { | |
35 class URLRequest; | |
36 } | |
37 | |
38 namespace predictors { | |
39 | |
40 // Contains logic for learning what can be prefetched and for kicking off | |
41 // speculative prefetching. | |
42 // - The class needs to be refcounted as the ownership is shared between the | |
43 // Profile and the ResourcePrefetchPredictorObserver class on the UI and the | |
44 // IO threads respectively. During initialization, the DB thread also needs to | |
45 // call this class as a part of the PostTaskAndReply call. | |
46 // - Most of the methods in the class (including the constructor) have to be | |
47 // called on the UI thread except the static functions that are thread safe. | |
48 // | |
49 // The overall flow of the resource prefetching algorithm is as follows: | |
50 // | |
51 // * ResourcePrefetchPredictorObserver - Listens for URL requests, responses and | |
dominich
2012/05/24 16:07:53
who owns this?
Shishir
2012/05/30 01:07:51
Done.
| |
52 // redirects on the IO thread(via NetworkDelegate) and post tasks to the | |
53 // ResourcePrefetchPredictor on the UI thread. | |
54 // * ResourcePrefetchPredictorTables - Persists ResourcePrefetchPredictor data | |
55 // to a sql database. Runs entirely on the DB thread. | |
56 // * ResourcePrefetchPredictor - Learns about resource requirements per URL in | |
dominich
2012/05/24 16:07:53
document who owns this
Shishir
2012/05/30 01:07:51
Done.
| |
57 // the UI thread through the ResourcePrefetchPredictorObserver and perisists | |
58 // it to disk in the DB thread through the ResourcePrefetchPredictorTables | |
59 // | |
60 // NOTE: The acutal prefetching is not yet implemented. | |
61 class ResourcePrefetchPredictor : public content::NotificationObserver, | |
62 public RefcountedProfileKeyedService { | |
63 public: | |
64 explicit ResourcePrefetchPredictor(Profile* profile); | |
65 virtual ~ResourcePrefetchPredictor(); | |
willchan no longer on Chromium
2012/05/24 22:28:52
If this is refcounted...should this be private?
Shishir
2012/05/30 01:07:51
No longer refcounted.
| |
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 bool InitFromURLRequest(net::URLRequest* request, bool is_response); | |
73 | |
74 NavigationID navigation_id_; | |
75 GURL resource_url_; | |
76 ResourceType::Type resource_type_; | |
77 | |
78 // Only for responses. | |
79 std::string mime_type_; | |
80 bool was_cached_; | |
81 }; | |
82 | |
83 // Thread safe. | |
84 static bool IsEnabled(); | |
85 static bool ShouldRecordRequest(net::URLRequest* request); | |
86 static bool ShouldRecordResponse(net::URLRequest* response); | |
87 static bool ShouldRecordRedirect(net::URLRequest* response); | |
88 | |
89 // UI thread. | |
90 void RecordURLRequest(const URLRequestSummary& request); | |
91 void RecordUrlResponse(const URLRequestSummary& response); | |
92 void RecordUrlRedirect(const URLRequestSummary& response); | |
93 | |
94 private: | |
95 friend class ::PredictorsHandler; | |
96 | |
97 // TODO(shishir): Maybe use pointers to make the sort cheaper. | |
98 typedef ResourcePrefetchPredictorTables::UrlTableRow UrlTableRow; | |
99 typedef std::vector<UrlTableRow> UrlTableRowVector; | |
100 | |
101 struct UrlTableCacheValue { | |
102 UrlTableRowVector rows_; | |
103 base::Time last_visit_; | |
104 }; | |
105 | |
106 typedef std::map<NavigationID, std::vector<URLRequestSummary> > NavigationMap; | |
107 typedef std::map<GURL, UrlTableCacheValue> UrlTableCacheMap; | |
108 | |
109 // RefcountedProfileKeyedService methods OVERRIDE. | |
110 virtual void ShutdownOnUIThread() OVERRIDE; | |
111 | |
112 // content::NotificationObserver methods OVERRIDE. | |
113 virtual void Observe(int type, | |
114 const content::NotificationSource& source, | |
115 const content::NotificationDetails& details) OVERRIDE; | |
116 | |
117 static bool IsHandledMainPage(net::URLRequest* request); | |
118 static bool IsHandledSubresource(net::URLRequest* response); | |
119 static bool IsCacheable(const net::URLRequest* response); | |
120 | |
121 // Deal with different kinds of requests. | |
122 void OnMainFrameRequest(const URLRequestSummary& request); | |
123 void OnMainFrameResponse(const URLRequestSummary& response); | |
124 void OnMainFrameRedirect(const URLRequestSummary& response); | |
125 void OnSubresourceResponse(const URLRequestSummary& response); | |
126 void OnSubresourceLoadedFromMemory(const NavigationID& navigation_id, | |
127 const GURL& resource_url); | |
128 | |
129 void OnHistoryAndCacheLoaded(); | |
130 void CreateCaches(std::vector<UrlTableRow>* url_rows); | |
131 bool ShouldTrackUrl(const GURL& url); | |
132 void CleanupAbandonedNavigations(const NavigationID& navigation_id); | |
133 void OnNavigationComplete(const NavigationID& navigation_id); | |
134 void LearnUrlNavigation(const GURL& main_frame_url, | |
135 const std::vector<URLRequestSummary>& new_value); | |
136 void RemoveAnEntryFromUrlDB(); | |
137 void MaybeReportAccuracyStats(const NavigationID& navigation_id); | |
138 | |
139 Profile* profile_; | |
140 bool initialized_; | |
141 scoped_refptr<ResourcePrefetchPredictorTables> tables_; | |
142 scoped_ptr<content::NotificationRegistrar> notification_registrar_; | |
143 | |
144 NavigationMap inflight_navigations_; | |
145 UrlTableCacheMap url_table_cache_; | |
146 | |
147 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor); | |
148 }; | |
149 | |
150 } // namespace predictors | |
151 | |
152 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ | |
OLD | NEW |