OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/chrome_metrics_helper.h" | |
6 | |
7 #include "base/base64.h" | |
8 #include "base/memory/singleton.h" | |
9 #include "chrome/browser/google/google_util.h" | |
10 #include "chrome/browser/profiles/profile.h" | |
11 #include "chrome/browser/profiles/profile_io_data.h" | |
12 #include "chrome/common/metrics/proto/chrome_experiments.pb.h" | |
13 #include "chrome/common/metrics/variations/variations_util.h" | |
14 #include "content/public/browser/browser_thread.h" | |
15 #include "net/http/http_request_headers.h" | |
16 | |
17 using content::BrowserThread; | |
18 | |
19 ChromeMetricsHelper* ChromeMetricsHelper::GetInstance() { | |
20 return Singleton<ChromeMetricsHelper>::get(); | |
21 } | |
22 | |
23 void ChromeMetricsHelper::AppendHeaders(const GURL& url, | |
24 bool incognito, | |
25 bool uma_enabled, | |
26 net::HttpRequestHeaders* headers) { | |
27 // Note the criteria for attaching Chrome experiment headers: | |
28 // 1. We only transmit to *.google.<TLD> domains. NOTE that this use of | |
29 // google_util helpers to check this does not guarantee that the URL is | |
30 // Google-owned, only that it is of the form *.google.<TLD>. In the future | |
31 // we may choose to reinforce this check. | |
32 // 2. Only transmit for non-Incognito profiles. | |
Peter Kasting
2012/12/17 19:51:57
Why this bullet? Incognito is to protect your loc
SteveT
2012/12/17 20:30:46
We didn't have very strong reasons for removing th
| |
33 // 3. For the X-Chrome-UMA-Enabled bit, only set it if UMA is in fact enabled | |
34 // for this install of Chrome. | |
35 // 4. For the X-Chrome-Variations, only include non-empty variation IDs. | |
36 if (incognito || | |
37 !google_util::IsGoogleDomainUrl(url.spec(), | |
38 google_util::ALLOW_SUBDOMAIN, | |
39 google_util::ALLOW_NON_STANDARD_PORTS)) { | |
40 return; | |
41 } | |
42 | |
43 if (uma_enabled) | |
44 headers->SetHeaderIfMissing("X-Chrome-UMA-Enabled", "1"); | |
45 | |
46 // Lazily initialize the header, if not already done, before attempting to | |
47 // transmit it. | |
48 InitVariationIDsCacheIfNeeded(); | |
49 base::AutoLock scoped_lock(lock_); | |
50 if (!variation_ids_header_.empty()) | |
51 headers->SetHeaderIfMissing("X-Chrome-Variations", variation_ids_header_); | |
52 } | |
53 | |
54 ChromeMetricsHelper::ChromeMetricsHelper() | |
55 : variation_ids_cache_initialized_(false) { | |
56 } | |
57 | |
58 ChromeMetricsHelper::~ChromeMetricsHelper() { | |
59 } | |
60 | |
61 void ChromeMetricsHelper::OnFieldTrialGroupFinalized( | |
62 const std::string& trial_name, | |
63 const std::string& group_name) { | |
64 chrome_variations::VariationID new_id = | |
65 chrome_variations::GetGoogleVariationID( | |
66 chrome_variations::GOOGLE_WEB_PROPERTIES, trial_name, group_name); | |
67 if (new_id == chrome_variations::kEmptyID) | |
68 return; | |
69 base::AutoLock scoped_lock(lock_); | |
70 variation_ids_set_.insert(new_id); | |
71 UpdateVariationIDsHeaderValue(); | |
72 } | |
73 | |
74 void ChromeMetricsHelper::InitVariationIDsCacheIfNeeded() { | |
75 base::AutoLock scoped_lock(lock_); | |
76 if (variation_ids_cache_initialized_) | |
77 return; | |
78 | |
79 // Register for additional cache updates. This is done first to avoid a race | |
80 // that could cause registered FieldTrials to be missed. | |
81 DCHECK(MessageLoop::current()); | |
82 base::FieldTrialList::AddObserver(this); | |
83 | |
84 base::FieldTrial::ActiveGroups initial_groups; | |
85 base::FieldTrialList::GetActiveFieldTrialGroups(&initial_groups); | |
86 for (base::FieldTrial::ActiveGroups::const_iterator it = | |
87 initial_groups.begin(); it != initial_groups.end(); ++it) { | |
88 const chrome_variations::VariationID id = | |
89 chrome_variations::GetGoogleVariationID( | |
90 chrome_variations::GOOGLE_WEB_PROPERTIES, it->trial_name, | |
91 it->group_name); | |
92 if (id != chrome_variations::kEmptyID) | |
93 variation_ids_set_.insert(id); | |
94 } | |
95 UpdateVariationIDsHeaderValue(); | |
96 | |
97 variation_ids_cache_initialized_ = true; | |
98 } | |
99 | |
100 void ChromeMetricsHelper::UpdateVariationIDsHeaderValue() { | |
101 // The header value is a serialized protobuffer of Variation IDs which is | |
102 // base64 encoded before transmitting as a string. | |
103 if (variation_ids_set_.empty()) | |
104 return; | |
105 | |
106 // This is the bottleneck for the creation of the header, so validate the size | |
107 // here. Force a hard maximum on the ID count in case the Variations server | |
108 // returns too many IDs and DOSs receiving servers with large requests. | |
109 DCHECK_LE(variation_ids_set_.size(), 10U); | |
110 if (variation_ids_set_.size() > 20) { | |
Peter Kasting
2012/12/17 19:51:57
I read Steve's explanation for this, but the style
SteveT
2012/12/17 20:30:46
Again, I believe I mentioned this before, but we (
| |
111 variation_ids_header_.clear(); | |
112 return; | |
113 } | |
114 | |
115 metrics::ChromeVariations proto; | |
116 for (std::set<chrome_variations::VariationID>::const_iterator it = | |
117 variation_ids_set_.begin(); it != variation_ids_set_.end(); ++it) | |
118 proto.add_variation_id(*it); | |
119 | |
120 std::string serialized; | |
121 proto.SerializeToString(&serialized); | |
122 | |
123 std::string hashed; | |
124 if (base::Base64Encode(serialized, &hashed)) { | |
125 // If successful, swap the header value with the new one. | |
126 // Note that the list of IDs and the header could be temporarily out of sync | |
127 // if IDs are added as the header is recreated. The receiving servers are OK | |
128 // with such descrepancies. | |
129 variation_ids_header_ = hashed; | |
130 } else { | |
131 NOTREACHED() << "Failed to base64 encode Variation IDs value: " | |
132 << serialized; | |
133 } | |
134 } | |
OLD | NEW |