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

Side by Side Diff: chrome/browser/metrics/variations/variations_http_header_provider.cc

Issue 11821019: Move the Chrome Variations HTTP header helper file into c/b/metrics/variations, and fix up lock beh… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove TODO Created 7 years, 11 months 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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/chrome_metrics_helper.h" 5 #include "chrome/browser/metrics/variations/variations_http_header_provider.h"
6 6
7 #include "base/base64.h" 7 #include "base/base64.h"
8 #include "base/memory/singleton.h" 8 #include "base/memory/singleton.h"
9 #include "chrome/browser/google/google_util.h" 9 #include "chrome/browser/google/google_util.h"
10 #include "chrome/browser/profiles/profile.h" 10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/profiles/profile_io_data.h" 11 #include "chrome/browser/profiles/profile_io_data.h"
12 #include "chrome/common/metrics/proto/chrome_experiments.pb.h" 12 #include "chrome/common/metrics/proto/chrome_experiments.pb.h"
13 #include "chrome/common/metrics/variations/variations_util.h" 13 #include "chrome/common/metrics/variations/variations_util.h"
14 #include "content/public/browser/browser_thread.h" 14 #include "content/public/browser/browser_thread.h"
15 #include "net/http/http_request_headers.h" 15 #include "net/http/http_request_headers.h"
16 16
17 using content::BrowserThread; 17 using content::BrowserThread;
18 18
19 ChromeMetricsHelper* ChromeMetricsHelper::GetInstance() { 19 namespace chrome_variations {
20 return Singleton<ChromeMetricsHelper>::get(); 20
21 VariationsHttpHeaderProvider* VariationsHttpHeaderProvider::GetInstance() {
22 return Singleton<VariationsHttpHeaderProvider>::get();
21 } 23 }
22 24
23 void ChromeMetricsHelper::AppendHeaders(const GURL& url, 25 void VariationsHttpHeaderProvider::AppendHeaders(
24 bool incognito, 26 const GURL& url,
25 bool uma_enabled, 27 bool incognito,
26 net::HttpRequestHeaders* headers) { 28 bool uma_enabled,
29 net::HttpRequestHeaders* headers) {
27 // Note the criteria for attaching Chrome experiment headers: 30 // Note the criteria for attaching Chrome experiment headers:
28 // 1. We only transmit to *.google.<TLD> domains. NOTE that this use of 31 // 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 32 // 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 33 // Google-owned, only that it is of the form *.google.<TLD>. In the future
31 // we may choose to reinforce this check. 34 // we may choose to reinforce this check.
32 // 2. Only transmit for non-Incognito profiles. 35 // 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 36 // 3. For the X-Chrome-UMA-Enabled bit, only set it if UMA is in fact enabled
34 // for this install of Chrome. 37 // for this install of Chrome.
35 // 4. For the X-Chrome-Variations, only include non-empty variation IDs. 38 // 4. For the X-Chrome-Variations, only include non-empty variation IDs.
36 if (incognito || 39 if (incognito ||
37 !google_util::IsGoogleDomainUrl(url.spec(), 40 !google_util::IsGoogleDomainUrl(url.spec(),
38 google_util::ALLOW_SUBDOMAIN, 41 google_util::ALLOW_SUBDOMAIN,
39 google_util::ALLOW_NON_STANDARD_PORTS)) { 42 google_util::ALLOW_NON_STANDARD_PORTS)) {
40 return; 43 return;
41 } 44 }
42 45
43 if (uma_enabled) 46 if (uma_enabled)
44 headers->SetHeaderIfMissing("X-Chrome-UMA-Enabled", "1"); 47 headers->SetHeaderIfMissing("X-Chrome-UMA-Enabled", "1");
45 48
46 // Lazily initialize the header, if not already done, before attempting to 49 // Lazily initialize the header, if not already done, before attempting to
47 // transmit it. 50 // transmit it.
48 InitVariationIDsCacheIfNeeded(); 51 InitVariationIDsCacheIfNeeded();
49 base::AutoLock scoped_lock(lock_); 52
50 if (!variation_ids_header_.empty()) 53 std::string variation_ids_header_copy;
51 headers->SetHeaderIfMissing("X-Chrome-Variations", variation_ids_header_); 54 {
55 base::AutoLock scoped_lock(lock_);
56 variation_ids_header_copy = variation_ids_header_;
57 }
58
59 if (!variation_ids_header_copy.empty()) {
60 headers->SetHeaderIfMissing("X-Chrome-Variations",
61 variation_ids_header_copy);
62 }
52 } 63 }
53 64
54 ChromeMetricsHelper::ChromeMetricsHelper() 65 VariationsHttpHeaderProvider::VariationsHttpHeaderProvider()
55 : variation_ids_cache_initialized_(false) { 66 : variation_ids_cache_initialized_(false) {
56 } 67 }
57 68
58 ChromeMetricsHelper::~ChromeMetricsHelper() { 69 VariationsHttpHeaderProvider::~VariationsHttpHeaderProvider() {
59 } 70 }
60 71
61 void ChromeMetricsHelper::OnFieldTrialGroupFinalized( 72 void VariationsHttpHeaderProvider::OnFieldTrialGroupFinalized(
62 const std::string& trial_name, 73 const std::string& trial_name,
63 const std::string& group_name) { 74 const std::string& group_name) {
64 chrome_variations::VariationID new_id = 75 VariationID new_id =
65 chrome_variations::GetGoogleVariationID( 76 GetGoogleVariationID(GOOGLE_WEB_PROPERTIES, trial_name, group_name);
66 chrome_variations::GOOGLE_WEB_PROPERTIES, trial_name, group_name); 77 if (new_id == kEmptyID)
67 if (new_id == chrome_variations::kEmptyID)
68 return; 78 return;
79
69 base::AutoLock scoped_lock(lock_); 80 base::AutoLock scoped_lock(lock_);
70 variation_ids_set_.insert(new_id); 81 variation_ids_set_.insert(new_id);
71 UpdateVariationIDsHeaderValue(); 82 UpdateVariationIDsHeaderValue();
72 } 83 }
73 84
74 void ChromeMetricsHelper::InitVariationIDsCacheIfNeeded() { 85 void VariationsHttpHeaderProvider::InitVariationIDsCacheIfNeeded() {
75 base::AutoLock scoped_lock(lock_); 86 base::AutoLock scoped_lock(lock_);
76 if (variation_ids_cache_initialized_) 87 if (variation_ids_cache_initialized_)
77 return; 88 return;
78 89
79 // Register for additional cache updates. This is done first to avoid a race 90 // Register for additional cache updates. This is done first to avoid a race
80 // that could cause registered FieldTrials to be missed. 91 // that could cause registered FieldTrials to be missed.
81 DCHECK(MessageLoop::current()); 92 DCHECK(MessageLoop::current());
82 base::FieldTrialList::AddObserver(this); 93 base::FieldTrialList::AddObserver(this);
83 94
84 base::FieldTrial::ActiveGroups initial_groups; 95 base::FieldTrial::ActiveGroups initial_groups;
85 base::FieldTrialList::GetActiveFieldTrialGroups(&initial_groups); 96 base::FieldTrialList::GetActiveFieldTrialGroups(&initial_groups);
86 for (base::FieldTrial::ActiveGroups::const_iterator it = 97 for (base::FieldTrial::ActiveGroups::const_iterator it =
87 initial_groups.begin(); it != initial_groups.end(); ++it) { 98 initial_groups.begin();
88 const chrome_variations::VariationID id = 99 it != initial_groups.end(); ++it) {
89 chrome_variations::GetGoogleVariationID( 100 const VariationID id =
90 chrome_variations::GOOGLE_WEB_PROPERTIES, it->trial_name, 101 GetGoogleVariationID(GOOGLE_WEB_PROPERTIES, it->trial_name,
91 it->group_name); 102 it->group_name);
92 if (id != chrome_variations::kEmptyID) 103 if (id != kEmptyID)
93 variation_ids_set_.insert(id); 104 variation_ids_set_.insert(id);
94 } 105 }
95 UpdateVariationIDsHeaderValue(); 106 UpdateVariationIDsHeaderValue();
96 107
97 variation_ids_cache_initialized_ = true; 108 variation_ids_cache_initialized_ = true;
98 } 109 }
99 110
100 void ChromeMetricsHelper::UpdateVariationIDsHeaderValue() { 111 void VariationsHttpHeaderProvider::UpdateVariationIDsHeaderValue() {
112 lock_.AssertAcquired();
113
101 // The header value is a serialized protobuffer of Variation IDs which is 114 // The header value is a serialized protobuffer of Variation IDs which is
102 // base64 encoded before transmitting as a string. 115 // base64 encoded before transmitting as a string.
103 if (variation_ids_set_.empty()) 116 if (variation_ids_set_.empty())
104 return; 117 return;
105 118
106 // This is the bottleneck for the creation of the header, so validate the size 119 // 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 120 // 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. 121 // returns too many IDs and DOSs receiving servers with large requests.
109 DCHECK_LE(variation_ids_set_.size(), 10U); 122 DCHECK_LE(variation_ids_set_.size(), 10U);
110 if (variation_ids_set_.size() > 20) { 123 if (variation_ids_set_.size() > 20) {
111 variation_ids_header_.clear(); 124 variation_ids_header_.clear();
112 return; 125 return;
113 } 126 }
114 127
115 metrics::ChromeVariations proto; 128 metrics::ChromeVariations proto;
116 for (std::set<chrome_variations::VariationID>::const_iterator it = 129 for (std::set<VariationID>::const_iterator it = variation_ids_set_.begin();
117 variation_ids_set_.begin(); it != variation_ids_set_.end(); ++it) 130 it != variation_ids_set_.end(); ++it) {
118 proto.add_variation_id(*it); 131 proto.add_variation_id(*it);
132 }
119 133
120 std::string serialized; 134 std::string serialized;
121 proto.SerializeToString(&serialized); 135 proto.SerializeToString(&serialized);
122 136
123 std::string hashed; 137 std::string hashed;
124 if (base::Base64Encode(serialized, &hashed)) { 138 if (base::Base64Encode(serialized, &hashed)) {
125 // If successful, swap the header value with the new one. 139 // 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 140 // 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 141 // if IDs are added as the header is recreated. The receiving servers are OK
128 // with such descrepancies. 142 // with such descrepancies.
129 variation_ids_header_ = hashed; 143 variation_ids_header_ = hashed;
130 } else { 144 } else {
131 NOTREACHED() << "Failed to base64 encode Variation IDs value: " 145 NOTREACHED() << "Failed to base64 encode Variation IDs value: "
132 << serialized; 146 << serialized;
133 } 147 }
134 } 148 }
149
150 } // namespace chrome_variations
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698