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

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: Addressed all remaining comments. 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
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.
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;
SteveT 2012/12/14 18:35:41 nit: I think we prefer curlies around a single lin
Bart N. 2012/12/16 00:09:10 Done.
41
42 if (uma_enabled)
43 headers->SetHeaderIfMissing("X-Chrome-UMA-Enabled", "1");
44
45 // Lazily initialize the header, if not already done, before attempting to
46 // transmit it.
47 InitVariationIDsCacheIfNeeded();
48 base::AutoLock scoped_lock(lock_);
49 if (!variation_ids_header_.empty())
50 headers->SetHeaderIfMissing("X-Chrome-Variations", variation_ids_header_);
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::InitVariationIDsCacheIfNeeded() {
74 base::AutoLock scoped_lock(lock_);
75 if (variation_ids_cache_initialized_)
76 return;
77
78 // Register for additional cache updates. This is done first to avoid a race
79 // that could cause registered FieldTrials to be missed.
80 base::FieldTrialList::AddObserver(this);
81
82 base::FieldTrial::ActiveGroups initial_groups;
83 base::FieldTrialList::GetActiveFieldTrialGroups(&initial_groups);
84 for (base::FieldTrial::ActiveGroups::const_iterator it =
85 initial_groups.begin(); it != initial_groups.end(); ++it) {
86 const chrome_variations::VariationID id =
87 chrome_variations::GetGoogleVariationID(
88 chrome_variations::GOOGLE_WEB_PROPERTIES, it->trial_name,
89 it->group_name);
90 if (id != chrome_variations::kEmptyID)
91 variation_ids_set_.insert(id);
92 }
93 UpdateVariationIDsHeaderValue();
94
95 variation_ids_cache_initialized_ = true;
96 }
97
98 void ChromeMetricsHelper::UpdateVariationIDsHeaderValue() {
99 // The header value is a serialized protobuffer of Variation IDs which is
100 // base64 encoded before transmitting as a string.
101 if (variation_ids_set_.empty())
102 return;
103
104 // This is the bottleneck for the creation of the header, so validate the size
105 // here. Force a hard maximum on the ID count in case the Variations server
106 // returns too many IDs and DOSs receiving servers with large requests.
107 DCHECK_LE(variation_ids_set_.size(), 10U);
108 if (variation_ids_set_.size() > 20) {
109 variation_ids_header_.clear();
110 return;
111 }
112
113 metrics::ChromeVariations proto;
114 for (std::set<chrome_variations::VariationID>::const_iterator it =
115 variation_ids_set_.begin(); it != variation_ids_set_.end(); ++it)
116 proto.add_variation_id(*it);
117
118 std::string serialized;
119 proto.SerializeToString(&serialized);
120
121 std::string hashed;
122 if (base::Base64Encode(serialized, &hashed)) {
123 // If successful, swap the header value with the new one.
124 // Note that the list of IDs and the header could be temporarily out of sync
125 // if IDs are added as the header is recreated. The receiving servers are OK
126 // with such descrepancies.
127 variation_ids_header_ = hashed;
128 } else {
129 NOTREACHED() << "Failed to base64 encode Variation IDs value: "
130 << serialized;
131 }
132 }
OLDNEW
« no previous file with comments | « chrome/browser/chrome_metrics_helper.h ('k') | chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698