OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/variations/variations_http_header_provider.h" | 5 #include "components/variations/variations_http_header_provider.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 18 matching lines...) Expand all Loading... |
29 InitVariationIDsCacheIfNeeded(); | 29 InitVariationIDsCacheIfNeeded(); |
30 | 30 |
31 std::string variation_ids_header_copy; | 31 std::string variation_ids_header_copy; |
32 { | 32 { |
33 base::AutoLock scoped_lock(lock_); | 33 base::AutoLock scoped_lock(lock_); |
34 variation_ids_header_copy = variation_ids_header_; | 34 variation_ids_header_copy = variation_ids_header_; |
35 } | 35 } |
36 return variation_ids_header_copy; | 36 return variation_ids_header_copy; |
37 } | 37 } |
38 | 38 |
| 39 std::string VariationsHttpHeaderProvider::GetVariationsString() { |
| 40 InitVariationIDsCacheIfNeeded(); |
| 41 |
| 42 // Construct a space-separated string with leading and trailing spaces from |
| 43 // the variations set. Note: The ids in it will be in sorted order per the |
| 44 // std::set contract. |
| 45 std::string ids_string = " "; |
| 46 { |
| 47 base::AutoLock scoped_lock(lock_); |
| 48 for (VariationID id : GetAllVariationIds()) { |
| 49 ids_string.append(base::IntToString(id)); |
| 50 ids_string.push_back(' '); |
| 51 } |
| 52 } |
| 53 return ids_string; |
| 54 } |
| 55 |
39 bool VariationsHttpHeaderProvider::SetDefaultVariationIds( | 56 bool VariationsHttpHeaderProvider::SetDefaultVariationIds( |
40 const std::string& variation_ids) { | 57 const std::string& variation_ids) { |
41 default_variation_ids_set_.clear(); | 58 default_variation_ids_set_.clear(); |
42 default_trigger_id_set_.clear(); | 59 default_trigger_id_set_.clear(); |
43 for (const base::StringPiece& entry : base::SplitStringPiece( | 60 for (const base::StringPiece& entry : base::SplitStringPiece( |
44 variation_ids, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) { | 61 variation_ids, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) { |
45 if (entry.empty()) { | 62 if (entry.empty()) { |
46 default_variation_ids_set_.clear(); | 63 default_variation_ids_set_.clear(); |
47 default_trigger_id_set_.clear(); | 64 default_trigger_id_set_.clear(); |
48 return false; | 65 return false; |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 | 137 |
121 // Register for additional cache updates. This is done first to avoid a race | 138 // Register for additional cache updates. This is done first to avoid a race |
122 // that could cause registered FieldTrials to be missed. | 139 // that could cause registered FieldTrials to be missed. |
123 DCHECK(base::MessageLoop::current()); | 140 DCHECK(base::MessageLoop::current()); |
124 base::FieldTrialList::AddObserver(this); | 141 base::FieldTrialList::AddObserver(this); |
125 | 142 |
126 base::TimeTicks before_time = base::TimeTicks::Now(); | 143 base::TimeTicks before_time = base::TimeTicks::Now(); |
127 | 144 |
128 base::FieldTrial::ActiveGroups initial_groups; | 145 base::FieldTrial::ActiveGroups initial_groups; |
129 base::FieldTrialList::GetActiveFieldTrialGroups(&initial_groups); | 146 base::FieldTrialList::GetActiveFieldTrialGroups(&initial_groups); |
130 for (base::FieldTrial::ActiveGroups::const_iterator it = | 147 |
131 initial_groups.begin(); | 148 for (const auto& entry : initial_groups) { |
132 it != initial_groups.end(); ++it) { | 149 const VariationID id = |
133 const VariationID id = GetGoogleVariationID(GOOGLE_WEB_PROPERTIES, | 150 GetGoogleVariationID(GOOGLE_WEB_PROPERTIES, entry.trial_name, |
134 it->trial_name, it->group_name); | 151 entry.group_name); |
135 if (id != EMPTY_ID) | 152 if (id != EMPTY_ID) |
136 variation_ids_set_.insert(id); | 153 variation_ids_set_.insert(id); |
137 | 154 |
138 const VariationID trigger_id = GetGoogleVariationID( | 155 const VariationID trigger_id = |
139 GOOGLE_WEB_PROPERTIES_TRIGGER, it->trial_name, it->group_name); | 156 GetGoogleVariationID(GOOGLE_WEB_PROPERTIES_TRIGGER, entry.trial_name, |
| 157 entry.group_name); |
| 158 |
140 if (trigger_id != EMPTY_ID) | 159 if (trigger_id != EMPTY_ID) |
141 variation_trigger_ids_set_.insert(trigger_id); | 160 variation_trigger_ids_set_.insert(trigger_id); |
142 } | 161 } |
143 UpdateVariationIDsHeaderValue(); | 162 UpdateVariationIDsHeaderValue(); |
144 | 163 |
145 UMA_HISTOGRAM_CUSTOM_COUNTS( | 164 UMA_HISTOGRAM_CUSTOM_COUNTS( |
146 "Variations.HeaderConstructionTime", | 165 "Variations.HeaderConstructionTime", |
147 (base::TimeTicks::Now() - before_time).InMicroseconds(), 0, | 166 (base::TimeTicks::Now() - before_time).InMicroseconds(), 0, |
148 base::TimeDelta::FromSeconds(1).InMicroseconds(), 50); | 167 base::TimeDelta::FromSeconds(1).InMicroseconds(), 50); |
149 | 168 |
(...skipping 17 matching lines...) Expand all Loading... |
167 // here. Force a hard maximum on the ID count in case the Variations server | 186 // here. Force a hard maximum on the ID count in case the Variations server |
168 // returns too many IDs and DOSs receiving servers with large requests. | 187 // returns too many IDs and DOSs receiving servers with large requests. |
169 const size_t total_id_count = | 188 const size_t total_id_count = |
170 variation_ids_set_.size() + variation_trigger_ids_set_.size(); | 189 variation_ids_set_.size() + variation_trigger_ids_set_.size(); |
171 DCHECK_LE(total_id_count, 10U); | 190 DCHECK_LE(total_id_count, 10U); |
172 UMA_HISTOGRAM_COUNTS_100("Variations.Headers.ExperimentCount", | 191 UMA_HISTOGRAM_COUNTS_100("Variations.Headers.ExperimentCount", |
173 total_id_count); | 192 total_id_count); |
174 if (total_id_count > 20) | 193 if (total_id_count > 20) |
175 return; | 194 return; |
176 | 195 |
177 // Merge the two sets of experiment ids. | 196 std::set<VariationID> all_variation_ids_set = GetAllVariationIds(); |
178 std::set<VariationID> all_variation_ids_set = default_variation_ids_set_; | |
179 for (VariationID id : variation_ids_set_) | |
180 all_variation_ids_set.insert(id); | |
181 for (VariationID id : synthetic_variation_ids_set_) | |
182 all_variation_ids_set.insert(id); | |
183 | |
184 std::set<VariationID> all_trigger_ids_set = default_trigger_id_set_; | 197 std::set<VariationID> all_trigger_ids_set = default_trigger_id_set_; |
185 for (VariationID id : variation_trigger_ids_set_) | 198 for (VariationID id : variation_trigger_ids_set_) |
186 all_trigger_ids_set.insert(id); | 199 all_trigger_ids_set.insert(id); |
187 | 200 |
188 ClientVariations proto; | 201 ClientVariations proto; |
189 for (VariationID id : all_variation_ids_set) | 202 for (VariationID id : all_variation_ids_set) |
190 proto.add_variation_id(id); | 203 proto.add_variation_id(id); |
191 for (VariationID id : all_trigger_ids_set) | 204 for (VariationID id : all_trigger_ids_set) |
192 proto.add_trigger_variation_id(id); | 205 proto.add_trigger_variation_id(id); |
193 | 206 |
194 std::string serialized; | 207 std::string serialized; |
195 proto.SerializeToString(&serialized); | 208 proto.SerializeToString(&serialized); |
196 | 209 |
197 std::string hashed; | 210 std::string hashed; |
198 base::Base64Encode(serialized, &hashed); | 211 base::Base64Encode(serialized, &hashed); |
199 // If successful, swap the header value with the new one. | 212 // If successful, swap the header value with the new one. |
200 // Note that the list of IDs and the header could be temporarily out of sync | 213 // Note that the list of IDs and the header could be temporarily out of sync |
201 // if IDs are added as the header is recreated. The receiving servers are OK | 214 // if IDs are added as the header is recreated. The receiving servers are OK |
202 // with such discrepancies. | 215 // with such discrepancies. |
203 variation_ids_header_ = hashed; | 216 variation_ids_header_ = hashed; |
204 } | 217 } |
205 | 218 |
| 219 std::set<VariationID> VariationsHttpHeaderProvider::GetAllVariationIds() { |
| 220 lock_.AssertAcquired(); |
| 221 |
| 222 std::set<VariationID> all_variation_ids_set = default_variation_ids_set_; |
| 223 for (VariationID id : variation_ids_set_) |
| 224 all_variation_ids_set.insert(id); |
| 225 for (VariationID id : synthetic_variation_ids_set_) |
| 226 all_variation_ids_set.insert(id); |
| 227 return all_variation_ids_set; |
| 228 } |
| 229 |
206 } // namespace variations | 230 } // namespace variations |
OLD | NEW |