OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/metrics/field_trial.h" | 5 #include "base/metrics/field_trial.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/rand_util.h" | 8 #include "base/rand_util.h" |
9 #include "base/sha1.h" | 9 #include "base/sha1.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 return BitsToOpenEndedUnitInterval(*bits); | 182 return BitsToOpenEndedUnitInterval(*bits); |
183 } | 183 } |
184 | 184 |
185 //------------------------------------------------------------------------------ | 185 //------------------------------------------------------------------------------ |
186 // FieldTrialList methods and members. | 186 // FieldTrialList methods and members. |
187 | 187 |
188 // static | 188 // static |
189 FieldTrialList* FieldTrialList::global_ = NULL; | 189 FieldTrialList* FieldTrialList::global_ = NULL; |
190 | 190 |
191 // static | 191 // static |
192 bool FieldTrialList::register_without_global_ = false; | 192 bool FieldTrialList::used_without_global_ = false; |
193 | 193 |
194 FieldTrialList::FieldTrialList(const std::string& client_id) | 194 FieldTrialList::FieldTrialList(const std::string& client_id) |
195 : application_start_time_(TimeTicks::Now()), | 195 : application_start_time_(TimeTicks::Now()), |
196 client_id_(client_id), | 196 client_id_(client_id), |
197 observer_list_(ObserverList<Observer>::NOTIFY_EXISTING_ONLY) { | 197 observer_list_(ObserverList<Observer>::NOTIFY_EXISTING_ONLY) { |
198 DCHECK(!global_); | 198 DCHECK(!global_); |
199 DCHECK(!register_without_global_); | 199 DCHECK(!used_without_global_); |
200 global_ = this; | 200 global_ = this; |
201 | 201 |
202 Time::Exploded exploded; | 202 Time::Exploded exploded; |
203 Time two_years_from_now = | 203 Time two_years_from_now = |
204 Time::NowFromSystemTime() + TimeDelta::FromDays(730); | 204 Time::NowFromSystemTime() + TimeDelta::FromDays(730); |
205 two_years_from_now.LocalExplode(&exploded); | 205 two_years_from_now.LocalExplode(&exploded); |
206 kExpirationYearInFuture = exploded.year; | 206 kExpirationYearInFuture = exploded.year; |
207 } | 207 } |
208 | 208 |
209 FieldTrialList::~FieldTrialList() { | 209 FieldTrialList::~FieldTrialList() { |
210 AutoLock auto_lock(lock_); | 210 AutoLock auto_lock(lock_); |
211 while (!registered_.empty()) { | 211 while (!registered_.empty()) { |
212 RegistrationList::iterator it = registered_.begin(); | 212 RegistrationList::iterator it = registered_.begin(); |
213 it->second->Release(); | 213 it->second->Release(); |
214 registered_.erase(it->first); | 214 registered_.erase(it->first); |
215 } | 215 } |
216 DCHECK_EQ(this, global_); | 216 DCHECK_EQ(this, global_); |
217 global_ = NULL; | 217 global_ = NULL; |
218 } | 218 } |
219 | 219 |
220 // static | 220 // static |
221 void FieldTrialList::Register(FieldTrial* trial) { | 221 void FieldTrialList::Register(FieldTrial* trial) { |
222 if (!global_) { | 222 if (!global_) { |
223 register_without_global_ = true; | 223 used_without_global_ = true; |
224 return; | 224 return; |
225 } | 225 } |
226 AutoLock auto_lock(global_->lock_); | 226 AutoLock auto_lock(global_->lock_); |
227 DCHECK(!global_->PreLockedFind(trial->name())); | 227 DCHECK(!global_->PreLockedFind(trial->name())); |
228 trial->AddRef(); | 228 trial->AddRef(); |
229 global_->registered_[trial->name()] = trial; | 229 global_->registered_[trial->name()] = trial; |
230 } | 230 } |
231 | 231 |
232 // static | 232 // static |
233 FieldTrial* FieldTrialList::Find(const std::string& name) { | 233 FieldTrial* FieldTrialList::Find(const std::string& name) { |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
362 // static | 362 // static |
363 size_t FieldTrialList::GetFieldTrialCount() { | 363 size_t FieldTrialList::GetFieldTrialCount() { |
364 if (!global_) | 364 if (!global_) |
365 return 0; | 365 return 0; |
366 AutoLock auto_lock(global_->lock_); | 366 AutoLock auto_lock(global_->lock_); |
367 return global_->registered_.size(); | 367 return global_->registered_.size(); |
368 } | 368 } |
369 | 369 |
370 // static | 370 // static |
371 bool FieldTrialList::IsOneTimeRandomizationEnabled() { | 371 bool FieldTrialList::IsOneTimeRandomizationEnabled() { |
372 // TODO(joi): Put back a DCHECK(global_) here. First, need to make sure all | 372 if (!global_) { |
373 // unit test executables have exactly one FieldTrialList instance (currently | 373 used_without_global_ = true; |
374 // they have 0 or 1). | |
375 if (!global_) | |
376 return false; | 374 return false; |
| 375 } |
377 | 376 |
378 return !global_->client_id_.empty(); | 377 return !global_->client_id_.empty(); |
379 } | 378 } |
380 | 379 |
381 // static | 380 // static |
382 const std::string& FieldTrialList::client_id() { | 381 const std::string& FieldTrialList::client_id() { |
383 DCHECK(global_); | 382 DCHECK(global_); |
384 if (!global_) | 383 if (!global_) |
385 return EmptyString(); | 384 return EmptyString(); |
386 | 385 |
387 return global_->client_id_; | 386 return global_->client_id_; |
388 } | 387 } |
389 | 388 |
390 FieldTrial* FieldTrialList::PreLockedFind(const std::string& name) { | 389 FieldTrial* FieldTrialList::PreLockedFind(const std::string& name) { |
391 RegistrationList::iterator it = registered_.find(name); | 390 RegistrationList::iterator it = registered_.find(name); |
392 if (registered_.end() == it) | 391 if (registered_.end() == it) |
393 return NULL; | 392 return NULL; |
394 return it->second; | 393 return it->second; |
395 } | 394 } |
396 | 395 |
397 } // namespace base | 396 } // namespace base |
OLD | NEW |