| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/precache/precache_util.h" | 5 #include "chrome/browser/precache/precache_util.h" |
| 6 | 6 |
| 7 #include "base/time/time.h" | 7 #include "base/time/time.h" |
| 8 #include "chrome/browser/browser_process.h" | 8 #include "chrome/browser/browser_process.h" |
| 9 #include "chrome/browser/precache/precache_manager_factory.h" | 9 #include "chrome/browser/precache/precache_manager_factory.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/profiles/profile_manager.h" | 11 #include "chrome/browser/profiles/profile_manager.h" |
| 12 #include "components/data_use_measurement/content/data_use_measurement.h" | 12 #include "components/data_use_measurement/content/data_use_measurement.h" |
| 13 #include "components/precache/content/precache_manager.h" | 13 #include "components/precache/content/precache_manager.h" |
| 14 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 15 #include "net/url_request/url_request.h" | 15 #include "net/url_request/url_request.h" |
| 16 #include "url/gurl.h" | 16 #include "url/gurl.h" |
| 17 | 17 |
| 18 namespace net { |
| 19 class HttpResponseInfo; |
| 20 } |
| 21 |
| 18 namespace { | 22 namespace { |
| 19 | 23 |
| 20 void UpdatePrecacheMetricsAndStateOnUIThread(const GURL& url, | 24 void UpdatePrecacheMetricsAndStateOnUIThread(const GURL& url, |
| 21 const GURL& referrer, | 25 const GURL& referrer, |
| 22 base::TimeDelta latency, | 26 base::TimeDelta latency, |
| 23 const base::Time& fetch_time, | 27 const base::Time& fetch_time, |
| 28 const net::HttpResponseInfo& info, |
| 24 int64_t size, | 29 int64_t size, |
| 25 bool was_cached, | |
| 26 bool is_user_traffic, | 30 bool is_user_traffic, |
| 27 void* profile_id) { | 31 void* profile_id) { |
| 28 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 32 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 29 | 33 |
| 30 if (!g_browser_process->profile_manager()->IsValidProfile(profile_id)) | 34 if (!g_browser_process->profile_manager()->IsValidProfile(profile_id)) |
| 31 return; | 35 return; |
| 32 Profile* profile = reinterpret_cast<Profile*>(profile_id); | 36 Profile* profile = reinterpret_cast<Profile*>(profile_id); |
| 33 | 37 |
| 34 precache::PrecacheManager* precache_manager = | 38 precache::PrecacheManager* precache_manager = |
| 35 precache::PrecacheManagerFactory::GetForBrowserContext(profile); | 39 precache::PrecacheManagerFactory::GetForBrowserContext(profile); |
| 36 // |precache_manager| could be NULL if the profile is off the record. | 40 // |precache_manager| could be NULL if the profile is off the record. |
| 37 if (!precache_manager || !precache_manager->IsPrecachingAllowed()) | 41 if (!precache_manager || !precache_manager->IsPrecachingAllowed()) |
| 38 return; | 42 return; |
| 39 | 43 |
| 40 precache_manager->UpdatePrecacheMetricsAndState( | 44 precache_manager->UpdatePrecacheMetricsAndState( |
| 41 url, referrer, latency, fetch_time, size, was_cached, is_user_traffic); | 45 url, referrer, latency, fetch_time, info, size, is_user_traffic); |
| 42 } | 46 } |
| 43 | 47 |
| 44 } // namespace | 48 } // namespace |
| 45 | 49 |
| 46 namespace precache { | 50 namespace precache { |
| 47 | 51 |
| 48 // TODO(rajendrant): Add unittests for this function. | 52 // TODO(rajendrant): Add unittests for this function. |
| 49 void UpdatePrecacheMetricsAndState(const net::URLRequest* request, | 53 void UpdatePrecacheMetricsAndState(const net::URLRequest* request, |
| 50 void* profile_id) { | 54 void* profile_id) { |
| 51 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 55 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 52 | 56 |
| 53 // For better accuracy, we use the actual bytes read instead of the length | 57 // For better accuracy, we use the actual bytes read instead of the length |
| 54 // specified with the Content-Length header, which may be inaccurate, | 58 // specified with the Content-Length header, which may be inaccurate, |
| 55 // or missing, as is the case with chunked encoding. | 59 // or missing, as is the case with chunked encoding. |
| 56 int64_t received_content_length = request->received_response_content_length(); | 60 int64_t received_content_length = request->received_response_content_length(); |
| 57 base::TimeDelta latency = base::TimeTicks::Now() - request->creation_time(); | 61 base::TimeDelta latency = base::TimeTicks::Now() - request->creation_time(); |
| 58 | 62 |
| 59 // Record precache metrics when a fetch is completed successfully, if | 63 // Record precache metrics when a fetch is completed successfully, if |
| 60 // precaching is allowed. | 64 // precaching is allowed. |
| 61 content::BrowserThread::PostTask( | 65 content::BrowserThread::PostTask( |
| 62 content::BrowserThread::UI, FROM_HERE, | 66 content::BrowserThread::UI, FROM_HERE, |
| 63 base::Bind( | 67 base::Bind( |
| 64 &UpdatePrecacheMetricsAndStateOnUIThread, request->url(), | 68 &UpdatePrecacheMetricsAndStateOnUIThread, request->url(), |
| 65 GURL(request->referrer()), latency, base::Time::Now(), | 69 GURL(request->referrer()), latency, base::Time::Now(), |
| 66 received_content_length, request->was_cached(), | 70 request->response_info(), received_content_length, |
| 67 data_use_measurement::DataUseMeasurement::IsUserInitiatedRequest( | 71 data_use_measurement::DataUseMeasurement::IsUserInitiatedRequest( |
| 68 request), | 72 request), |
| 69 profile_id)); | 73 profile_id)); |
| 70 } | 74 } |
| 71 | 75 |
| 72 } // namespace precache | 76 } // namespace precache |
| OLD | NEW |