• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 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 "base/metrics/field_trial_param_associator.h"
6 
7 #include "base/metrics/field_trial.h"
8 
9 namespace base {
10 
FieldTrialParamAssociator()11 FieldTrialParamAssociator::FieldTrialParamAssociator() {}
~FieldTrialParamAssociator()12 FieldTrialParamAssociator::~FieldTrialParamAssociator() {}
13 
14 // static
GetInstance()15 FieldTrialParamAssociator* FieldTrialParamAssociator::GetInstance() {
16   return Singleton<FieldTrialParamAssociator,
17                    LeakySingletonTraits<FieldTrialParamAssociator>>::get();
18 }
19 
AssociateFieldTrialParams(const std::string & trial_name,const std::string & group_name,const FieldTrialParams & params)20 bool FieldTrialParamAssociator::AssociateFieldTrialParams(
21     const std::string& trial_name,
22     const std::string& group_name,
23     const FieldTrialParams& params) {
24   if (FieldTrialList::IsTrialActive(trial_name))
25     return false;
26 
27   AutoLock scoped_lock(lock_);
28   const FieldTrialKey key(trial_name, group_name);
29   if (ContainsKey(field_trial_params_, key))
30     return false;
31 
32   field_trial_params_[key] = params;
33   return true;
34 }
35 
GetFieldTrialParams(const std::string & trial_name,FieldTrialParams * params)36 bool FieldTrialParamAssociator::GetFieldTrialParams(
37     const std::string& trial_name,
38     FieldTrialParams* params) {
39   FieldTrial* field_trial = FieldTrialList::Find(trial_name);
40   if (!field_trial)
41     return false;
42 
43   // First try the local map, falling back to getting it from shared memory.
44   if (GetFieldTrialParamsWithoutFallback(trial_name, field_trial->group_name(),
45                                          params)) {
46     return true;
47   }
48 
49   // TODO(lawrencewu): add the params to field_trial_params_ for next time.
50   return FieldTrialList::GetParamsFromSharedMemory(field_trial, params);
51 }
52 
GetFieldTrialParamsWithoutFallback(const std::string & trial_name,const std::string & group_name,FieldTrialParams * params)53 bool FieldTrialParamAssociator::GetFieldTrialParamsWithoutFallback(
54     const std::string& trial_name,
55     const std::string& group_name,
56     FieldTrialParams* params) {
57   AutoLock scoped_lock(lock_);
58 
59   const FieldTrialKey key(trial_name, group_name);
60   if (!ContainsKey(field_trial_params_, key))
61     return false;
62 
63   *params = field_trial_params_[key];
64   return true;
65 }
66 
ClearAllParamsForTesting()67 void FieldTrialParamAssociator::ClearAllParamsForTesting() {
68   {
69     AutoLock scoped_lock(lock_);
70     field_trial_params_.clear();
71   }
72   FieldTrialList::ClearParamsFromSharedMemoryForTesting();
73 }
74 
ClearAllCachedParamsForTesting()75 void FieldTrialParamAssociator::ClearAllCachedParamsForTesting() {
76   AutoLock scoped_lock(lock_);
77   field_trial_params_.clear();
78 }
79 
80 }  // namespace base
81