summaryrefslogtreecommitdiffstats
path: root/base/field_trial.cc
blob: c88ab65e3d0e0c65b7000a2e9ba696af1bdc8fac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.


#include "base/field_trial.h"
#include "base/logging.h"
#include "base/rand_util.h"

using base::Time;

//------------------------------------------------------------------------------
// FieldTrialList methods and members.

// static
FieldTrialList* FieldTrialList::global_ = NULL;

FieldTrialList::FieldTrialList()
  : application_start_time_(Time::Now()) {
  DCHECK(!global_);
  global_ = this;
}

FieldTrialList::~FieldTrialList() {
  while (!registered_.empty()) {
    RegistrationList::iterator it = registered_.begin();
    it->second->Release();
    registered_.erase(it->first);
  }
  DCHECK(this == global_);
  global_ = NULL;
}

// static
void FieldTrialList::Register(FieldTrial* trial) {
  DCHECK(global_->CalledOnValidThread());
  DCHECK(!Find(trial->name()));
  trial->AddRef();
  global_->registered_[trial->name()] = trial;
}

// static
FieldTrial* FieldTrialList::Find(const std::wstring& name) {
  DCHECK(global_->CalledOnValidThread());
  RegistrationList::iterator it = global_->registered_.find(name);
  if (global_->registered_.end() == it)
    return NULL;
  return it->second;
}

//------------------------------------------------------------------------------
// FieldTrial methods and members.

FieldTrial::FieldTrial(const std::wstring& name, double probability)
  : name_(name) {
  double rand = base::RandDouble();
  DCHECK(rand >= 0.0  && rand < 1.0);
  boolean_value_ = rand < probability;
  FieldTrialList::Register(this);
}