Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(353)

Side by Side Diff: chrome/browser/chrome_metrics_helper.cc

Issue 11522009: X-Chrome-Variations logic refactoring (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Renamed SuggestFieldTrial and updated exp IDs range. Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 content::BrowserThread;
20
21 ChromeMetricsHelper* ChromeMetricsHelper::GetInstance() {
22 return Singleton<ChromeMetricsHelper>::get();
23 }
24
25 void ChromeMetricsHelper::AppendHeadersToRequest(
26 net::URLRequest* request,
27 content::ResourceContext* resource_context) {
28 DCHECK(!BrowserThread::IsWellKnownThread(BrowserThread::IO) ||
29 BrowserThread::CurrentlyOn(BrowserThread::IO));
30
31 // Don't attempt to append headers to requests that have already started.
32 // TODO(stevet): Remove this once the request ordering issues are resolved
33 // in crbug.com/128048.
34 if (request->is_pending())
35 return;
36
37 net::HttpRequestHeaders headers;
38 headers.CopyFrom(request->extra_request_headers());
39 ProfileIOData* io_data = ProfileIOData::FromResourceContext(resource_context);
40 AppendHeaders(request->url(), io_data->is_incognito(),
41 io_data->GetMetricsEnabledStateOnIOThread(), &headers);
42 request->SetExtraRequestHeaders(headers);
43 }
44
45 void ChromeMetricsHelper::AppendHeadersToFetcher(net::URLFetcher* fetcher,
46 const Profile* profile) {
47 net::HttpRequestHeaders headers;
48 AppendHeaders(fetcher->GetOriginalURL(), profile->IsOffTheRecord(), false,
49 &headers);
50 fetcher->SetExtraRequestHeaders(headers.ToString());
51 }
52
53 ChromeMetricsHelper::ChromeMetricsHelper()
54 : variation_ids_cache_initialized_(false) {
55 }
56
57 ChromeMetricsHelper::~ChromeMetricsHelper() {
58 }
59
60 void ChromeMetricsHelper::OnFieldTrialGroupFinalized(
61 const std::string& trial_name,
62 const std::string& group_name) {
63 chrome_variations::VariationID new_id =
64 chrome_variations::GetGoogleVariationID(
65 chrome_variations::GOOGLE_WEB_PROPERTIES, trial_name, group_name);
66 if (new_id == chrome_variations::kEmptyID)
67 return;
68 base::AutoLock scoped_lock(lock_);
69 variation_ids_set_.insert(new_id);
70 UpdateVariationIDsHeaderValue();
71 }
72
73 void ChromeMetricsHelper::AppendHeaders(
74 const GURL& url,
75 bool incognito,
76 bool uma_enabled,
77 net::HttpRequestHeaders* headers) {
78
79 // Note the criteria for attaching Chrome experiment headers:
80 // 1. We only transmit to *.google.<TLD> domains. NOTE that this use of
81 // google_util helpers to check this does not guarantee that the URL is
82 // Google-owned, only that it is of the form *.google.<TLD>. In the future
83 // we may choose to reinforce this check.
84 // 2. Only transmit for non-Incognito profiles.
85 // 3. For the X-Chrome-UMA-Enabled bit, only set it if UMA is in fact enabled
86 // for this install of Chrome.
87 // 4. For the X-Chrome-Variations, only include non-empty variation IDs.
88 if (incognito ||
89 !google_util::IsGoogleDomainUrl(url.spec(),
90 google_util::ALLOW_SUBDOMAIN,
91 google_util::ALLOW_NON_STANDARD_PORTS))
92 return;
93
94 if (uma_enabled)
95 headers->SetHeaderIfMissing("X-Chrome-UMA-Enabled", "1");
96
97 // Lazily initialize the header, if not already done, before attempting to
98 // transmit it.
99 InitVariationIDsCacheIfNeeded();
100 base::AutoLock scoped_lock(lock_);
101 if (!variation_ids_header_.empty())
102 headers->SetHeaderIfMissing("X-Chrome-Variations", variation_ids_header_);
103 }
104
105 void ChromeMetricsHelper::InitVariationIDsCacheIfNeeded() {
106 base::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() {
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) {
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;
162 }
163 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698