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 <string> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/metrics/field_trial.h" |
7 #include "base/time/time.h" | 11 #include "base/time/time.h" |
8 #include "chrome/browser/browser_process.h" | 12 #include "chrome/browser/browser_process.h" |
| 13 #include "chrome/browser/metrics/chrome_metrics_service_accessor.h" |
9 #include "chrome/browser/precache/precache_manager_factory.h" | 14 #include "chrome/browser/precache/precache_manager_factory.h" |
10 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
11 #include "chrome/browser/profiles/profile_manager.h" | 16 #include "chrome/browser/profiles/profile_manager.h" |
12 #include "components/data_use_measurement/content/content_url_request_classifier
.h" | 17 #include "components/data_use_measurement/content/content_url_request_classifier
.h" |
13 #include "components/precache/content/precache_manager.h" | 18 #include "components/precache/content/precache_manager.h" |
14 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
15 #include "net/url_request/url_request.h" | 20 #include "net/url_request/url_request.h" |
16 #include "url/gurl.h" | 21 #include "url/gurl.h" |
17 | 22 |
18 namespace net { | 23 namespace net { |
19 class HttpResponseInfo; | 24 class HttpResponseInfo; |
20 } | 25 } |
21 | 26 |
22 namespace { | 27 namespace { |
23 | 28 |
| 29 const char kPrecacheSynthetic15D[] = "PrecacheSynthetic15D"; |
| 30 const char kPrecacheSynthetic1D[] = "PrecacheSynthetic1D"; |
| 31 |
24 void UpdatePrecacheMetricsAndStateOnUIThread(const GURL& url, | 32 void UpdatePrecacheMetricsAndStateOnUIThread(const GURL& url, |
25 const GURL& referrer, | 33 const GURL& referrer, |
26 base::TimeDelta latency, | 34 base::TimeDelta latency, |
27 const base::Time& fetch_time, | 35 const base::Time& fetch_time, |
28 const net::HttpResponseInfo& info, | 36 const net::HttpResponseInfo& info, |
29 int64_t size, | 37 int64_t size, |
30 bool is_user_traffic, | 38 bool is_user_traffic, |
31 void* profile_id) { | 39 void* profile_id) { |
32 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 40 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
33 | 41 |
34 if (!g_browser_process->profile_manager()->IsValidProfile(profile_id)) | 42 if (!g_browser_process->profile_manager()->IsValidProfile(profile_id)) |
35 return; | 43 return; |
36 Profile* profile = reinterpret_cast<Profile*>(profile_id); | 44 Profile* profile = reinterpret_cast<Profile*>(profile_id); |
37 | 45 |
38 precache::PrecacheManager* precache_manager = | 46 precache::PrecacheManager* precache_manager = |
39 precache::PrecacheManagerFactory::GetForBrowserContext(profile); | 47 precache::PrecacheManagerFactory::GetForBrowserContext(profile); |
40 // |precache_manager| could be NULL if the profile is off the record. | 48 // |precache_manager| could be NULL if the profile is off the record. |
41 if (!precache_manager || !precache_manager->IsPrecachingAllowed()) | 49 if (!precache_manager || !precache_manager->IsPrecachingAllowed()) |
42 return; | 50 return; |
43 | 51 |
44 precache_manager->UpdatePrecacheMetricsAndState( | 52 precache_manager->UpdatePrecacheMetricsAndState( |
45 url, referrer, latency, fetch_time, info, size, is_user_traffic); | 53 url, referrer, latency, fetch_time, info, size, is_user_traffic, |
| 54 base::Bind(&precache::RegisterPrecacheSyntheticFieldTrial)); |
46 } | 55 } |
47 | 56 |
48 } // namespace | 57 } // namespace |
49 | 58 |
50 namespace precache { | 59 namespace precache { |
51 | 60 |
52 // TODO(rajendrant): Add unittests for this function. | 61 // TODO(rajendrant): Add unittests for this function. |
53 void UpdatePrecacheMetricsAndState(const net::URLRequest* request, | 62 void UpdatePrecacheMetricsAndState(const net::URLRequest* request, |
54 void* profile_id) { | 63 void* profile_id) { |
55 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 64 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
56 | 65 |
57 // For better accuracy, we use the actual bytes read instead of the length | 66 // For better accuracy, we use the actual bytes read instead of the length |
58 // specified with the Content-Length header, which may be inaccurate, | 67 // specified with the Content-Length header, which may be inaccurate, |
59 // or missing, as is the case with chunked encoding. | 68 // or missing, as is the case with chunked encoding. |
60 int64_t received_content_length = request->received_response_content_length(); | 69 int64_t received_content_length = request->received_response_content_length(); |
61 base::TimeDelta latency = base::TimeTicks::Now() - request->creation_time(); | 70 base::TimeDelta latency = base::TimeTicks::Now() - request->creation_time(); |
62 | 71 |
63 // Record precache metrics when a fetch is completed successfully, if | 72 // Record precache metrics when a fetch is completed successfully, if |
64 // precaching is allowed. | 73 // precaching is allowed. |
65 content::BrowserThread::PostTask( | 74 content::BrowserThread::PostTask( |
66 content::BrowserThread::UI, FROM_HERE, | 75 content::BrowserThread::UI, FROM_HERE, |
67 base::Bind(&UpdatePrecacheMetricsAndStateOnUIThread, request->url(), | 76 base::Bind(&UpdatePrecacheMetricsAndStateOnUIThread, request->url(), |
68 GURL(request->referrer()), latency, base::Time::Now(), | 77 GURL(request->referrer()), latency, base::Time::Now(), |
69 request->response_info(), received_content_length, | 78 request->response_info(), received_content_length, |
70 data_use_measurement::IsUserRequest(*request), profile_id)); | 79 data_use_measurement::IsUserRequest(*request), profile_id)); |
71 } | 80 } |
72 | 81 |
| 82 // |last_precache_time| is the last time precache task was run. |
| 83 void RegisterPrecacheSyntheticFieldTrial(base::Time last_precache_time) { |
| 84 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 85 |
| 86 std::vector<uint32_t> groups; |
| 87 base::TimeDelta time_ago = base::Time::Now() - last_precache_time; |
| 88 // Look up the current group name (e.g. Control or Enabled). |
| 89 std::string group_name = |
| 90 base::FieldTrialList::FindFullName(kPrecacheFieldTrialName); |
| 91 // group_name should only be empty if the Precache trial does not exist. |
| 92 if (!group_name.empty()) { |
| 93 // Register matching synthetic trials for 15-day and 1-day candidates. |
| 94 if (time_ago <= base::TimeDelta::FromDays(15)) |
| 95 ChromeMetricsServiceAccessor::RegisterSyntheticFieldTrial( |
| 96 kPrecacheSynthetic15D, group_name); |
| 97 if (time_ago <= base::TimeDelta::FromDays(1)) |
| 98 ChromeMetricsServiceAccessor::RegisterSyntheticFieldTrial( |
| 99 kPrecacheSynthetic1D, group_name); |
| 100 } |
| 101 } |
| 102 |
73 } // namespace precache | 103 } // namespace precache |
OLD | NEW |