| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_tab_helper.h" | 5 #include "chrome/browser/predictors/resource_prefetch_predictor_tab_helper.h" |
| 6 | 6 |
| 7 #include "chrome/browser/predictors/resource_prefetch_predictor.h" | 7 #include <string> |
| 8 #include "chrome/browser/predictors/resource_prefetch_predictor_factory.h" | 8 |
| 9 #include "chrome/browser/predictors/loading_predictor.h" |
| 10 #include "chrome/browser/predictors/loading_predictor_factory.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 10 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
| 11 | 13 |
| 12 DEFINE_WEB_CONTENTS_USER_DATA_KEY( | 14 DEFINE_WEB_CONTENTS_USER_DATA_KEY( |
| 13 predictors::ResourcePrefetchPredictorTabHelper); | 15 predictors::ResourcePrefetchPredictorTabHelper); |
| 14 | 16 |
| 15 using content::BrowserThread; | 17 using content::BrowserThread; |
| 16 | 18 |
| 17 namespace predictors { | 19 namespace predictors { |
| 18 | 20 |
| 19 ResourcePrefetchPredictorTabHelper::ResourcePrefetchPredictorTabHelper( | 21 ResourcePrefetchPredictorTabHelper::ResourcePrefetchPredictorTabHelper( |
| 20 content::WebContents* web_contents) | 22 content::WebContents* web_contents) |
| 21 : content::WebContentsObserver(web_contents) { | 23 : content::WebContentsObserver(web_contents) { |
| 22 } | 24 } |
| 23 | 25 |
| 24 ResourcePrefetchPredictorTabHelper::~ResourcePrefetchPredictorTabHelper() { | 26 ResourcePrefetchPredictorTabHelper::~ResourcePrefetchPredictorTabHelper() { |
| 25 } | 27 } |
| 26 | 28 |
| 27 void ResourcePrefetchPredictorTabHelper::DocumentOnLoadCompletedInMainFrame() { | 29 void ResourcePrefetchPredictorTabHelper::DocumentOnLoadCompletedInMainFrame() { |
| 28 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 30 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 29 | 31 |
| 30 ResourcePrefetchPredictor* predictor = | 32 auto* loading_predictor = LoadingPredictorFactory::GetForProfile( |
| 31 ResourcePrefetchPredictorFactory::GetForProfile( | 33 Profile::FromBrowserContext(web_contents()->GetBrowserContext())); |
| 32 web_contents()->GetBrowserContext()); | 34 if (!loading_predictor) |
| 33 if (!predictor) | |
| 34 return; | 35 return; |
| 35 | 36 |
| 37 auto* resource_prefetch_predictor = |
| 38 loading_predictor->resource_prefetch_predictor(); |
| 36 NavigationID navigation_id(web_contents()); | 39 NavigationID navigation_id(web_contents()); |
| 37 predictor->RecordMainFrameLoadComplete(navigation_id); | 40 resource_prefetch_predictor->RecordMainFrameLoadComplete(navigation_id); |
| 38 } | 41 } |
| 39 | 42 |
| 40 void ResourcePrefetchPredictorTabHelper::DidLoadResourceFromMemoryCache( | 43 void ResourcePrefetchPredictorTabHelper::DidLoadResourceFromMemoryCache( |
| 41 const GURL& url, | 44 const GURL& url, |
| 42 const std::string& mime_type, | 45 const std::string& mime_type, |
| 43 content::ResourceType resource_type) { | 46 content::ResourceType resource_type) { |
| 44 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 47 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 45 | 48 |
| 46 ResourcePrefetchPredictor* predictor = | 49 auto* loading_predictor = LoadingPredictorFactory::GetForProfile( |
| 47 ResourcePrefetchPredictorFactory::GetForProfile( | 50 Profile::FromBrowserContext(web_contents()->GetBrowserContext())); |
| 48 web_contents()->GetBrowserContext()); | 51 if (!loading_predictor) |
| 49 if (!predictor) | |
| 50 return; | 52 return; |
| 51 | 53 |
| 52 ResourcePrefetchPredictor::URLRequestSummary summary; | 54 ResourcePrefetchPredictor::URLRequestSummary summary; |
| 53 summary.navigation_id = NavigationID(web_contents()); | 55 summary.navigation_id = NavigationID(web_contents()); |
| 54 summary.resource_url = url; | 56 summary.resource_url = url; |
| 55 summary.mime_type = mime_type; | 57 summary.mime_type = mime_type; |
| 56 summary.resource_type = | 58 summary.resource_type = |
| 57 ResourcePrefetchPredictor::GetResourceTypeFromMimeType( | 59 ResourcePrefetchPredictor::GetResourceTypeFromMimeType( |
| 58 mime_type, resource_type); | 60 mime_type, resource_type); |
| 59 summary.was_cached = true; | 61 summary.was_cached = true; |
| 60 predictor->RecordURLResponse(summary); | 62 auto* resource_prefetch_predictor = |
| 63 loading_predictor->resource_prefetch_predictor(); |
| 64 resource_prefetch_predictor->RecordURLResponse(summary); |
| 61 } | 65 } |
| 62 | 66 |
| 63 } // namespace predictors | 67 } // namespace predictors |
| OLD | NEW |