• 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 
11 FieldTrialParamAssociator::FieldTrialParamAssociator() = default;
12 FieldTrialParamAssociator::~FieldTrialParamAssociator() = default;
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 
ClearParamsForTesting(const std::string & trial_name,const std::string & group_name)75 void FieldTrialParamAssociator::ClearParamsForTesting(
76     const std::string& trial_name,
77     const std::string& group_name) {
78   AutoLock scoped_lock(lock_);
79   const FieldTrialKey key(trial_name, group_name);
80   field_trial_params_.erase(key);
81 }
82 
ClearAllCachedParamsForTesting()83 void FieldTrialParamAssociator::ClearAllCachedParamsForTesting() {
84   AutoLock scoped_lock(lock_);
85   field_trial_params_.clear();
86 }
87 
88 }  // namespace base
89