OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/predictors/resource_prefetch_predictor.h" | 5 #include "chrome/browser/predictors/resource_prefetch_predictor.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <set> | 8 #include <set> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 | 170 |
171 notification_registrar_.Add(this, | 171 notification_registrar_.Add(this, |
172 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, | 172 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, |
173 content::NotificationService::AllSources()); | 173 content::NotificationService::AllSources()); |
174 } | 174 } |
175 | 175 |
176 ResourcePrefetchPredictor::~ResourcePrefetchPredictor() { | 176 ResourcePrefetchPredictor::~ResourcePrefetchPredictor() { |
177 } | 177 } |
178 | 178 |
179 void ResourcePrefetchPredictor::Shutdown() { | 179 void ResourcePrefetchPredictor::Shutdown() { |
180 if (prefetch_manager_) { | 180 if (prefetch_manager_.get()) { |
181 prefetch_manager_->ShutdownOnUIThread(); | 181 prefetch_manager_->ShutdownOnUIThread(); |
182 prefetch_manager_ = NULL; | 182 prefetch_manager_ = NULL; |
183 } | 183 } |
184 } | 184 } |
185 | 185 |
186 void ResourcePrefetchPredictor::LazilyInitialize() { | 186 void ResourcePrefetchPredictor::LazilyInitialize() { |
187 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 187 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
188 | 188 |
189 DCHECK_EQ(initialization_state_, NOT_INITIALIZED); | 189 DCHECK_EQ(initialization_state_, NOT_INITIALIZED); |
190 initialization_state_ = INITIALIZING; | 190 initialization_state_ = INITIALIZING; |
(...skipping 30 matching lines...) Expand all Loading... |
221 // pointers. | 221 // pointers. |
222 for (UrlTableCacheMap::iterator it = url_table_cache_.begin(); | 222 for (UrlTableCacheMap::iterator it = url_table_cache_.begin(); |
223 it != url_table_cache_.end(); ++it) { | 223 it != url_table_cache_.end(); ++it) { |
224 std::sort(it->second.resources.begin(), | 224 std::sort(it->second.resources.begin(), |
225 it->second.resources.end(), | 225 it->second.resources.end(), |
226 ResourcePrefetchPredictorTables::UrlResourceRowSorter()); | 226 ResourcePrefetchPredictorTables::UrlResourceRowSorter()); |
227 } | 227 } |
228 | 228 |
229 // Add notifications for history loading if it is not ready. | 229 // Add notifications for history loading if it is not ready. |
230 HistoryService* history_service = HistoryServiceFactory::GetForProfile( | 230 HistoryService* history_service = HistoryServiceFactory::GetForProfile( |
231 profile_, Profile::EXPLICIT_ACCESS); | 231 profile_, Profile::EXPLICIT_ACCESS).get(); |
232 if (!history_service) { | 232 if (!history_service) { |
233 notification_registrar_.Add(this, chrome::NOTIFICATION_HISTORY_LOADED, | 233 notification_registrar_.Add(this, chrome::NOTIFICATION_HISTORY_LOADED, |
234 content::Source<Profile>(profile_)); | 234 content::Source<Profile>(profile_)); |
235 } else { | 235 } else { |
236 OnHistoryAndCacheLoaded(); | 236 OnHistoryAndCacheLoaded(); |
237 } | 237 } |
238 } | 238 } |
239 | 239 |
240 // static | 240 // static |
241 bool ResourcePrefetchPredictor::ShouldRecordRequest( | 241 bool ResourcePrefetchPredictor::ShouldRecordRequest( |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 } | 294 } |
295 | 295 |
296 if (response->method() != "GET") | 296 if (response->method() != "GET") |
297 resource_status |= RESOURCE_STATUS_NOT_GET; | 297 resource_status |= RESOURCE_STATUS_NOT_GET; |
298 | 298 |
299 if (response->original_url().spec().length() > | 299 if (response->original_url().spec().length() > |
300 kMaxSubresourceUrlLengthBytes) { | 300 kMaxSubresourceUrlLengthBytes) { |
301 resource_status |= RESOURCE_STATUS_URL_TOO_LONG; | 301 resource_status |= RESOURCE_STATUS_URL_TOO_LONG; |
302 } | 302 } |
303 | 303 |
304 if (!response->response_info().headers) | 304 if (!response->response_info().headers.get()) |
305 resource_status |= RESOURCE_STATUS_HEADERS_MISSING; | 305 resource_status |= RESOURCE_STATUS_HEADERS_MISSING; |
306 | 306 |
307 if (!IsCacheable(response)) | 307 if (!IsCacheable(response)) |
308 resource_status |= RESOURCE_STATUS_NOT_CACHEABLE; | 308 resource_status |= RESOURCE_STATUS_NOT_CACHEABLE; |
309 | 309 |
310 UMA_HISTOGRAM_ENUMERATION("ResourcePrefetchPredictor.ResourceStatus", | 310 UMA_HISTOGRAM_ENUMERATION("ResourcePrefetchPredictor.ResourceStatus", |
311 resource_status, | 311 resource_status, |
312 RESOURCE_STATUS_MAX); | 312 RESOURCE_STATUS_MAX); |
313 | 313 |
314 return resource_status == 0; | 314 return resource_status == 0; |
315 } | 315 } |
316 | 316 |
317 // static | 317 // static |
318 bool ResourcePrefetchPredictor::IsCacheable(const net::URLRequest* response) { | 318 bool ResourcePrefetchPredictor::IsCacheable(const net::URLRequest* response) { |
319 if (response->was_cached()) | 319 if (response->was_cached()) |
320 return true; | 320 return true; |
321 | 321 |
322 // For non cached responses, we will ensure that the freshness lifetime is | 322 // For non cached responses, we will ensure that the freshness lifetime is |
323 // some sane value. | 323 // some sane value. |
324 const net::HttpResponseInfo& response_info = response->response_info(); | 324 const net::HttpResponseInfo& response_info = response->response_info(); |
325 if (!response_info.headers) | 325 if (!response_info.headers.get()) |
326 return false; | 326 return false; |
327 base::Time response_time(response_info.response_time); | 327 base::Time response_time(response_info.response_time); |
328 response_time += base::TimeDelta::FromSeconds(1); | 328 response_time += base::TimeDelta::FromSeconds(1); |
329 base::TimeDelta freshness = response_info.headers->GetFreshnessLifetime( | 329 base::TimeDelta freshness = response_info.headers->GetFreshnessLifetime( |
330 response_time); | 330 response_time); |
331 return freshness > base::TimeDelta(); | 331 return freshness > base::TimeDelta(); |
332 } | 332 } |
333 | 333 |
334 // static | 334 // static |
335 ResourceType::Type ResourcePrefetchPredictor::GetResourceTypeFromMimeType( | 335 ResourceType::Type ResourcePrefetchPredictor::GetResourceTypeFromMimeType( |
(...skipping 327 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
663 MaybeReportSimulatedAccuracyStats(navigation_id); | 663 MaybeReportSimulatedAccuracyStats(navigation_id); |
664 } | 664 } |
665 | 665 |
666 // Remove the navigation from the inflight navigations. | 666 // Remove the navigation from the inflight navigations. |
667 std::vector<URLRequestSummary>* requests = | 667 std::vector<URLRequestSummary>* requests = |
668 inflight_navigations_[navigation_id].release(); | 668 inflight_navigations_[navigation_id].release(); |
669 inflight_navigations_.erase(navigation_id); | 669 inflight_navigations_.erase(navigation_id); |
670 | 670 |
671 // Kick off history lookup to determine if we should record the URL. | 671 // Kick off history lookup to determine if we should record the URL. |
672 HistoryService* history_service = HistoryServiceFactory::GetForProfile( | 672 HistoryService* history_service = HistoryServiceFactory::GetForProfile( |
673 profile_, Profile::EXPLICIT_ACCESS); | 673 profile_, Profile::EXPLICIT_ACCESS).get(); |
674 DCHECK(history_service); | 674 DCHECK(history_service); |
675 history_service->ScheduleDBTask( | 675 history_service->ScheduleDBTask( |
676 new GetUrlVisitCountTask( | 676 new GetUrlVisitCountTask( |
677 navigation_id, | 677 navigation_id, |
678 requests, | 678 requests, |
679 base::Bind(&ResourcePrefetchPredictor::OnVisitCountLookup, | 679 base::Bind(&ResourcePrefetchPredictor::OnVisitCountLookup, |
680 AsWeakPtr())), | 680 AsWeakPtr())), |
681 &history_lookup_consumer_); | 681 &history_lookup_consumer_); |
682 } | 682 } |
683 | 683 |
(...skipping 435 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1119 tables_, | 1119 tables_, |
1120 urls_to_delete)); | 1120 urls_to_delete)); |
1121 } | 1121 } |
1122 | 1122 |
1123 void ResourcePrefetchPredictor::SetTablesForTesting( | 1123 void ResourcePrefetchPredictor::SetTablesForTesting( |
1124 scoped_refptr<ResourcePrefetchPredictorTables> tables) { | 1124 scoped_refptr<ResourcePrefetchPredictorTables> tables) { |
1125 tables_ = tables; | 1125 tables_ = tables; |
1126 } | 1126 } |
1127 | 1127 |
1128 } // namespace predictors | 1128 } // namespace predictors |
OLD | NEW |