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