Home | History | Annotate | Download | only in variations
      1 // Copyright (c) 2012 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 "chrome/common/metrics/variations/variations_util.h"
      6 
      7 #include <vector>
      8 
      9 #include "base/strings/stringprintf.h"
     10 #include "base/strings/utf_string_conversions.h"
     11 #include "chrome/common/child_process_logging.h"
     12 #include "chrome/common/crash_keys.h"
     13 
     14 namespace chrome_variations {
     15 
     16 namespace {
     17 
     18 // Populates |name_group_ids| based on |active_groups|.
     19 void GetFieldTrialActiveGroupIdsForActiveGroups(
     20     const base::FieldTrial::ActiveGroups& active_groups,
     21     std::vector<ActiveGroupId>* name_group_ids) {
     22   DCHECK(name_group_ids->empty());
     23   for (base::FieldTrial::ActiveGroups::const_iterator it =
     24        active_groups.begin(); it != active_groups.end(); ++it) {
     25     name_group_ids->push_back(MakeActiveGroupId(it->trial_name,
     26                                                 it->group_name));
     27   }
     28 }
     29 
     30 }  // namespace
     31 
     32 void GetFieldTrialActiveGroupIds(
     33     std::vector<ActiveGroupId>* name_group_ids) {
     34   DCHECK(name_group_ids->empty());
     35   // A note on thread safety: Since GetActiveFieldTrialGroups() is thread
     36   // safe, and we operate on a separate list of that data, this function is
     37   // technically thread safe as well, with respect to the FieldTrialList data.
     38   base::FieldTrial::ActiveGroups active_groups;
     39   base::FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
     40   GetFieldTrialActiveGroupIdsForActiveGroups(active_groups,
     41                                              name_group_ids);
     42 }
     43 
     44 void GetFieldTrialActiveGroupIdsAsStrings(
     45     std::vector<std::string>* output) {
     46   DCHECK(output->empty());
     47   std::vector<ActiveGroupId> name_group_ids;
     48   GetFieldTrialActiveGroupIds(&name_group_ids);
     49   for (size_t i = 0; i < name_group_ids.size(); ++i) {
     50     output->push_back(base::StringPrintf(
     51         "%x-%x", name_group_ids[i].name, name_group_ids[i].group));
     52   }
     53 }
     54 
     55 void SetChildProcessLoggingVariationList() {
     56   std::vector<std::string> experiment_strings;
     57   GetFieldTrialActiveGroupIdsAsStrings(&experiment_strings);
     58   crash_keys::SetVariationsList(experiment_strings);
     59 }
     60 
     61 // Functions below are exposed for testing explicitly behind this namespace.
     62 // They simply wrap existing functions in this file.
     63 namespace testing {
     64 
     65 void TestGetFieldTrialActiveGroupIds(
     66     const base::FieldTrial::ActiveGroups& active_groups,
     67     std::vector<ActiveGroupId>* name_group_ids) {
     68   GetFieldTrialActiveGroupIdsForActiveGroups(active_groups,
     69                                              name_group_ids);
     70 }
     71 
     72 }  // namespace testing
     73 
     74 }  // namespace chrome_variations
     75