OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include "chrome/browser/prerender/prerender_manager.h" | 5 #include "chrome/browser/prerender/prerender_manager.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
13 #include "base/time.h" | 13 #include "base/time.h" |
14 #include "base/values.h" | 14 #include "base/values.h" |
15 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
16 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
17 #include "chrome/browser/favicon/favicon_tab_helper.h" | 17 #include "chrome/browser/favicon/favicon_tab_helper.h" |
| 18 #include "chrome/browser/history/top_sites.h" |
18 #include "chrome/browser/prerender/prerender_condition.h" | 19 #include "chrome/browser/prerender/prerender_condition.h" |
19 #include "chrome/browser/prerender/prerender_contents.h" | 20 #include "chrome/browser/prerender/prerender_contents.h" |
20 #include "chrome/browser/prerender/prerender_final_status.h" | 21 #include "chrome/browser/prerender/prerender_final_status.h" |
21 #include "chrome/browser/prerender/prerender_histograms.h" | 22 #include "chrome/browser/prerender/prerender_histograms.h" |
22 #include "chrome/browser/prerender/prerender_history.h" | 23 #include "chrome/browser/prerender/prerender_history.h" |
23 #include "chrome/browser/prerender/prerender_observer.h" | 24 #include "chrome/browser/prerender/prerender_observer.h" |
24 #include "chrome/browser/prerender/prerender_tracker.h" | 25 #include "chrome/browser/prerender/prerender_tracker.h" |
25 #include "chrome/browser/prerender/prerender_util.h" | 26 #include "chrome/browser/prerender/prerender_util.h" |
26 #include "chrome/browser/profiles/profile.h" | 27 #include "chrome/browser/profiles/profile.h" |
27 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 28 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
28 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper_delegate.h" | 29 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper_delegate.h" |
| 30 #include "chrome/common/chrome_notification_types.h" |
29 #include "chrome/common/chrome_switches.h" | 31 #include "chrome/common/chrome_switches.h" |
30 #include "chrome/common/render_messages.h" | 32 #include "chrome/common/render_messages.h" |
31 #include "content/browser/browser_thread.h" | 33 #include "content/browser/browser_thread.h" |
| 34 #include "content/browser/cancelable_request.h" |
32 #include "content/browser/renderer_host/render_process_host.h" | 35 #include "content/browser/renderer_host/render_process_host.h" |
33 #include "content/browser/renderer_host/render_view_host.h" | 36 #include "content/browser/renderer_host/render_view_host.h" |
34 #include "content/browser/renderer_host/resource_dispatcher_host.h" | 37 #include "content/browser/renderer_host/resource_dispatcher_host.h" |
35 #include "content/browser/tab_contents/render_view_host_manager.h" | 38 #include "content/browser/tab_contents/render_view_host_manager.h" |
36 #include "content/browser/tab_contents/tab_contents.h" | 39 #include "content/browser/tab_contents/tab_contents.h" |
37 #include "content/browser/tab_contents/tab_contents_delegate.h" | 40 #include "content/browser/tab_contents/tab_contents_delegate.h" |
| 41 #include "content/common/notification_observer.h" |
| 42 #include "content/common/notification_registrar.h" |
38 #include "content/common/notification_service.h" | 43 #include "content/common/notification_service.h" |
39 | 44 |
40 namespace prerender { | 45 namespace prerender { |
41 | 46 |
42 namespace { | 47 namespace { |
43 | 48 |
44 // Time window for which we will record windowed PLT's from the last | 49 // Time window for which we will record windowed PLT's from the last |
45 // observed link rel=prefetch tag. | 50 // observed link rel=prefetch tag. |
46 const int kWindowDurationSeconds = 30; | 51 const int kWindowDurationSeconds = 30; |
47 | 52 |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 PendingContentsData(const GURL& url, | 163 PendingContentsData(const GURL& url, |
159 const GURL& referrer, | 164 const GURL& referrer, |
160 Origin origin) | 165 Origin origin) |
161 : url_(url), referrer_(referrer), origin_(origin) { } | 166 : url_(url), referrer_(referrer), origin_(origin) { } |
162 ~PendingContentsData() {} | 167 ~PendingContentsData() {} |
163 GURL url_; | 168 GURL url_; |
164 GURL referrer_; | 169 GURL referrer_; |
165 Origin origin_; | 170 Origin origin_; |
166 }; | 171 }; |
167 | 172 |
| 173 class PrerenderManager::MostVisitedSites : public NotificationObserver { |
| 174 public: |
| 175 explicit MostVisitedSites(Profile* profile) : |
| 176 profile_(profile) { |
| 177 history::TopSites* ts = GetTopSites(); |
| 178 if (ts) { |
| 179 registrar_.Add(this, chrome::NOTIFICATION_TOP_SITES_CHANGED, |
| 180 Source<history::TopSites>(ts)); |
| 181 } |
| 182 |
| 183 UpdateMostVisited(); |
| 184 } |
| 185 |
| 186 void UpdateMostVisited() { |
| 187 history::TopSites* ts = GetTopSites(); |
| 188 if (ts) { |
| 189 ts->GetMostVisitedURLs( |
| 190 &topsites_consumer_, |
| 191 NewCallback(this, |
| 192 &prerender::PrerenderManager::MostVisitedSites:: |
| 193 OnMostVisitedURLsAvailable)); |
| 194 } |
| 195 } |
| 196 |
| 197 void OnMostVisitedURLsAvailable(const history::MostVisitedURLList& data) { |
| 198 urls_.clear(); |
| 199 for (int i = 0; i < static_cast<int>(data.size()); i++) |
| 200 urls_.insert(data[i].url); |
| 201 } |
| 202 |
| 203 void Observe(int type, |
| 204 const NotificationSource& source, |
| 205 const NotificationDetails& details) { |
| 206 DCHECK_EQ(type, chrome::NOTIFICATION_TOP_SITES_CHANGED); |
| 207 UpdateMostVisited(); |
| 208 } |
| 209 |
| 210 bool IsTopSite(const GURL& url) const { |
| 211 return (urls_.count(url) > 0); |
| 212 } |
| 213 |
| 214 private: |
| 215 history::TopSites* GetTopSites() const { |
| 216 if (profile_) |
| 217 return profile_->GetTopSites(); |
| 218 return NULL; |
| 219 } |
| 220 CancelableRequestConsumer topsites_consumer_; |
| 221 Profile* profile_; |
| 222 NotificationRegistrar registrar_; |
| 223 std::set<GURL> urls_; |
| 224 }; |
| 225 |
| 226 bool PrerenderManager::IsTopSite(const GURL& url) const { |
| 227 return most_visited_->IsTopSite(url); |
| 228 } |
| 229 |
168 PrerenderManager::PrerenderManager(Profile* profile, | 230 PrerenderManager::PrerenderManager(Profile* profile, |
169 PrerenderTracker* prerender_tracker) | 231 PrerenderTracker* prerender_tracker) |
170 : enabled_(true), | 232 : enabled_(true), |
171 profile_(profile), | 233 profile_(profile), |
172 prerender_tracker_(prerender_tracker), | 234 prerender_tracker_(prerender_tracker), |
173 prerender_contents_factory_(PrerenderContents::CreateFactory()), | 235 prerender_contents_factory_(PrerenderContents::CreateFactory()), |
174 last_prerender_start_time_(GetCurrentTimeTicks() - | 236 last_prerender_start_time_(GetCurrentTimeTicks() - |
175 base::TimeDelta::FromMilliseconds(kMinTimeBetweenPrerendersMs)), | 237 base::TimeDelta::FromMilliseconds(kMinTimeBetweenPrerendersMs)), |
176 runnable_method_factory_(this), | 238 runnable_method_factory_(this), |
177 prerender_history_(new PrerenderHistory(kHistoryLength)), | 239 prerender_history_(new PrerenderHistory(kHistoryLength)), |
178 histograms_(new PrerenderHistograms()) { | 240 histograms_(new PrerenderHistograms()), |
| 241 most_visited_(new MostVisitedSites(profile)) { |
179 // There are some assumptions that the PrerenderManager is on the UI thread. | 242 // There are some assumptions that the PrerenderManager is on the UI thread. |
180 // Any other checks simply make sure that the PrerenderManager is accessed on | 243 // Any other checks simply make sure that the PrerenderManager is accessed on |
181 // the same thread that it was created on. | 244 // the same thread that it was created on. |
182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 245 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
183 } | 246 } |
184 | 247 |
185 PrerenderManager::~PrerenderManager() { | 248 PrerenderManager::~PrerenderManager() { |
186 DestroyAllContents(FINAL_STATUS_MANAGER_SHUTDOWN); | 249 DestroyAllContents(FINAL_STATUS_MANAGER_SHUTDOWN); |
187 STLDeleteElements(&prerender_conditions_); | 250 STLDeleteElements(&prerender_conditions_); |
188 } | 251 } |
(...skipping 763 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
952 DeletePendingDeleteEntries(); | 1015 DeletePendingDeleteEntries(); |
953 } | 1016 } |
954 | 1017 |
955 void PrerenderManager::RecordFinalStatus(Origin origin, | 1018 void PrerenderManager::RecordFinalStatus(Origin origin, |
956 uint8 experiment_id, | 1019 uint8 experiment_id, |
957 FinalStatus final_status) const { | 1020 FinalStatus final_status) const { |
958 histograms_->RecordFinalStatus(origin, experiment_id, final_status); | 1021 histograms_->RecordFinalStatus(origin, experiment_id, final_status); |
959 } | 1022 } |
960 | 1023 |
961 } // namespace prerender | 1024 } // namespace prerender |
OLD | NEW |