Home | History | Annotate | Download | only in variations
      1 // Copyright (c) 2013 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/browser/metrics/variations/variations_registry_syncer_win.h"
      6 
      7 #include "base/files/file_path.h"
      8 #include "base/metrics/field_trial.h"
      9 #include "base/path_service.h"
     10 #include "base/strings/string16.h"
     11 #include "chrome/common/metrics/variations/experiment_labels.h"
     12 #include "chrome/installer/util/google_update_settings.h"
     13 #include "chrome/installer/util/install_util.h"
     14 
     15 namespace {
     16 
     17 // Delay before performing a registry sync, in seconds.
     18 const int kRegistrySyncDelaySeconds = 5;
     19 
     20 }  // namespace
     21 
     22 namespace chrome_variations {
     23 
     24 VariationsRegistrySyncer::VariationsRegistrySyncer() {
     25 }
     26 
     27 VariationsRegistrySyncer::~VariationsRegistrySyncer() {
     28 }
     29 
     30 void VariationsRegistrySyncer::RequestRegistrySync() {
     31   if (timer_.IsRunning()) {
     32     timer_.Reset();
     33     return;
     34   }
     35 
     36   timer_.Start(FROM_HERE,
     37                base::TimeDelta::FromSeconds(kRegistrySyncDelaySeconds),
     38                this, &VariationsRegistrySyncer::SyncWithRegistry);
     39 }
     40 
     41 void VariationsRegistrySyncer::SyncWithRegistry() {
     42   // Note that all registry operations are done here on the UI thread as there
     43   // are no threading restrictions on them.
     44   base::FilePath chrome_exe;
     45   if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
     46     NOTREACHED() << "Failed to get chrome exe path";
     47     return;
     48   }
     49   const bool is_system_install =
     50       !InstallUtil::IsPerUserInstall(chrome_exe.value().c_str());
     51 
     52   // Read the current bits from the registry.
     53   base::string16 registry_labels;
     54   bool success = GoogleUpdateSettings::ReadExperimentLabels(is_system_install,
     55                                                             &registry_labels);
     56   if (!success) {
     57     DVLOG(1) << "Error reading Variation labels from the registry.";
     58     return;
     59   }
     60 
     61   // Since the non-Variations contents of experiment_labels should not be,
     62   // clobbered, separate them from the Variations contents.
     63   const base::string16 other_labels =
     64       ExtractNonVariationLabels(registry_labels);
     65 
     66   // Compute the new Variations part of the label.
     67   base::FieldTrial::ActiveGroups active_groups;
     68   base::FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
     69   const base::string16 variation_labels =
     70       BuildGoogleUpdateExperimentLabel(active_groups);
     71 
     72   // Append the old non-Variations labels with the new Variations labels and
     73   // write it back to the registry.
     74   const base::string16 combined_labels =
     75       CombineExperimentLabels(variation_labels, other_labels);
     76 
     77   if (!GoogleUpdateSettings::SetExperimentLabels(is_system_install,
     78                                                  combined_labels)) {
     79     DVLOG(1) << "Error writing Variation labels to the registry: "
     80              << combined_labels;
     81   }
     82 }
     83 
     84 }  // namespace chrome_variations
     85