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 #include "net/url_request/url_fetcher.h" | |
17 #include "net/url_request/url_request.h" | |
18 | |
19 using base::AutoLock; | |
20 using content::BrowserThread; | |
Peter Kasting
2012/12/11 21:08:48
Nit: Normally we avoid using directives unless the
Bart N.
2012/12/11 23:18:56
Done for AutoLock, however the existing BrowserThr
| |
21 | |
22 ChromeMetricsHelper* ChromeMetricsHelper::GetInstance() { | |
23 return Singleton<ChromeMetricsHelper>::get(); | |
24 } | |
25 | |
26 ChromeMetricsHelper::ChromeMetricsHelper() | |
Peter Kasting
2012/12/11 21:08:48
Nit: Function definition order must match declarat
Bart N.
2012/12/11 23:18:56
Done.
| |
27 : variation_ids_cache_initialized_(false) { | |
28 } | |
29 | |
30 ChromeMetricsHelper::~ChromeMetricsHelper() { | |
31 } | |
32 | |
33 void ChromeMetricsHelper::AppendHeadersToRequest( | |
Peter Kasting
2012/12/11 21:08:48
Nit: Based on the implementations of these two fun
Bart N.
2012/12/11 23:18:56
I think it's a good suggestion. I made this class
SteveT
2012/12/13 16:24:08
+1 to this class not knowing about Fetches vs Requ
Bart N.
2012/12/14 18:21:42
Done.
| |
34 net::URLRequest* request, | |
35 content::ResourceContext* resource_context) { | |
36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
Peter Kasting
2012/12/11 21:08:48
You may need to add a call to IsWellKnownThread()
Bart N.
2012/12/11 23:18:56
Good to know. Anyway, this the old code.
Done.
| |
37 | |
38 // Don't attempt to append headers to requests that have already started. | |
39 // TODO(stevet): Remove this once the request ordering issues are resolved | |
40 // in crbug.com/128048. | |
41 if (request->is_pending()) | |
42 return; | |
43 | |
44 net::HttpRequestHeaders headers; | |
45 headers.CopyFrom(request->extra_request_headers()); | |
46 ProfileIOData* io_data = ProfileIOData::FromResourceContext(resource_context); | |
47 AppendHeaders(request->url(), io_data->is_incognito(), | |
48 io_data->GetMetricsEnabledStateOnIOThread(), &headers); | |
49 request->SetExtraRequestHeaders(headers); | |
50 } | |
51 | |
52 void ChromeMetricsHelper::AppendHeadersToFetcher(net::URLFetcher* fetcher, | |
53 const Profile* profile) { | |
54 net::HttpRequestHeaders headers; | |
55 AppendHeaders(fetcher->GetOriginalURL(), profile->IsOffTheRecord(), false, | |
56 &headers); | |
57 fetcher->SetExtraRequestHeaders(headers.ToString()); | |
58 } | |
59 | |
60 void ChromeMetricsHelper::AppendHeaders( | |
61 const GURL& url, | |
62 bool incognito, | |
63 bool uma_enabled, | |
64 net::HttpRequestHeaders* headers) { | |
65 | |
66 // Note the criteria for attaching Chrome experiment headers: | |
67 // 1. We only transmit to *.google.<TLD> domains. NOTE that this use of | |
68 // google_util helpers to check this does not guarantee that the URL is | |
69 // Google-owned, only that it is of the form *.google.<TLD>. In the future | |
70 // we may choose to reinforce this check. | |
71 // 2. Only transmit for non-Incognito profiles. | |
72 // 3. For the X-Chrome-UMA-Enabled bit, only set it if UMA is in fact enabled | |
73 // for this install of Chrome. | |
74 // 4. For the X-Chrome-Variations, only include non-empty variation IDs. | |
75 if (incognito || | |
76 !google_util::IsGoogleDomainUrl(url.spec(), | |
77 google_util::ALLOW_SUBDOMAIN, | |
78 google_util::ALLOW_NON_STANDARD_PORTS)) | |
79 return; | |
80 | |
81 if (uma_enabled) | |
82 headers->SetHeaderIfMissing("X-Chrome-UMA-Enabled", "1"); | |
83 | |
84 // Lazily initialize the header, if not already done, before attempting to | |
85 // transmit it. | |
86 InitVariationIDsCacheIfNeeded(); | |
87 AutoLock scoped_lock(lock_); | |
88 if (!variation_ids_header_.empty()) | |
89 headers->SetHeaderIfMissing("X-Chrome-Variations", variation_ids_header_); | |
90 } | |
91 | |
92 void ChromeMetricsHelper::OnFieldTrialGroupFinalized( | |
Peter Kasting
2012/12/11 21:08:48
Is this supposed to be called exclusively on one p
Bart N.
2012/12/11 23:18:56
I think it can be called only from the IO thread (
SteveT
2012/12/13 16:24:08
So this method can be called on this class after t
Bart N.
2012/12/14 18:21:42
I think so. However, I've realized that the existi
SteveT
2012/12/14 18:35:41
This seems like a general issue with the pattern,
Bart N.
2012/12/16 00:09:09
No idea. In our case, both IO/UI have it.
Done.
| |
93 const std::string& trial_name, | |
94 const std::string& group_name) { | |
95 chrome_variations::VariationID new_id = | |
96 chrome_variations::GetGoogleVariationID( | |
97 chrome_variations::GOOGLE_WEB_PROPERTIES, trial_name, group_name); | |
98 if (new_id == chrome_variations::kEmptyID) | |
99 return; | |
100 AutoLock scoped_lock(lock_); | |
101 variation_ids_set_.insert(new_id); | |
102 UpdateVariationIDsHeaderValue(); | |
103 } | |
104 | |
105 void ChromeMetricsHelper::InitVariationIDsCacheIfNeeded() { | |
106 AutoLock scoped_lock(lock_); | |
107 if (variation_ids_cache_initialized_) | |
108 return; | |
109 | |
110 // Register for additional cache updates. This is done first to avoid a race | |
111 // that could cause registered FieldTrials to be missed. | |
112 base::FieldTrialList::AddObserver(this); | |
113 | |
114 base::FieldTrial::ActiveGroups initial_groups; | |
115 base::FieldTrialList::GetActiveFieldTrialGroups(&initial_groups); | |
116 for (base::FieldTrial::ActiveGroups::const_iterator it = | |
117 initial_groups.begin(); it != initial_groups.end(); ++it) { | |
118 const chrome_variations::VariationID id = | |
119 chrome_variations::GetGoogleVariationID( | |
120 chrome_variations::GOOGLE_WEB_PROPERTIES, it->trial_name, | |
121 it->group_name); | |
122 if (id != chrome_variations::kEmptyID) | |
123 variation_ids_set_.insert(id); | |
124 } | |
125 UpdateVariationIDsHeaderValue(); | |
126 | |
127 variation_ids_cache_initialized_ = true; | |
128 } | |
129 | |
130 void ChromeMetricsHelper::UpdateVariationIDsHeaderValue() { | |
Peter Kasting
2012/12/11 21:08:48
Nit: While it's technically not necessary since bo
Bart N.
2012/12/11 23:18:56
Doesn't it incur extra CPU cost? This method is pr
Peter Kasting
2012/12/12 00:01:06
I don't know about the cost, but I can't imagine i
Bart N.
2012/12/12 02:06:17
OK. Two things:
(1) The cost of acquiring a lock i
| |
131 // The header value is a serialized protobuffer of Variation IDs which is | |
132 // base64 encoded before transmitting as a string. | |
133 if (variation_ids_set_.empty()) | |
134 return; | |
135 | |
136 // This is the bottleneck for the creation of the header, so validate the size | |
137 // here. Force a hard maximum on the ID count in case the Variations server | |
138 // returns too many IDs and DOSs receiving servers with large requests. | |
139 DCHECK_LE(variation_ids_set_.size(), 10U); | |
140 if (variation_ids_set_.size() > 20) { | |
Peter Kasting
2012/12/11 21:08:48
You cannot handle DCHECK failure. A DCHECK means
Bart N.
2012/12/11 23:18:56
I'll leave it up to Steve to answer this question;
SteveT
2012/12/13 16:24:08
Yeah so this was added as a double safety:
* The
| |
141 variation_ids_header_.clear(); | |
142 return; | |
143 } | |
144 | |
145 metrics::ChromeVariations proto; | |
146 for (std::set<chrome_variations::VariationID>::const_iterator it = | |
147 variation_ids_set_.begin(); it != variation_ids_set_.end(); ++it) | |
148 proto.add_variation_id(*it); | |
149 | |
150 std::string serialized; | |
151 proto.SerializeToString(&serialized); | |
152 | |
153 std::string hashed; | |
154 if (base::Base64Encode(serialized, &hashed)) { | |
155 // If successful, swap the header value with the new one. | |
156 // Note that the list of IDs and the header could be temporarily out of sync | |
157 // if IDs are added as the header is recreated. The receiving servers are OK | |
158 // with such descrepancies. | |
159 variation_ids_header_ = hashed; | |
160 } else { | |
161 DVLOG(1) << "Failed to base64 encode Variation IDs value: " << serialized; | |
Peter Kasting
2012/12/11 21:08:48
Is logging really what you want here? Note that i
Bart N.
2012/12/11 23:18:56
Steve?
SteveT
2012/12/13 16:24:08
Hmm, yeah, I believe we were just trying to follow
| |
162 } | |
163 } | |
OLD | NEW |