Port experiment_labels.h to be cross-platform.
[chromium-blink-merge.git] / chrome / browser / metrics / variations / variations_registry_syncer_win.cc
blob3f24e419a78fba27c952fa02155cf6e9ab62b2ab
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.
5 #include "chrome/browser/metrics/variations/variations_registry_syncer_win.h"
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"
15 namespace {
17 // Delay before performing a registry sync, in seconds.
18 const int kRegistrySyncDelaySeconds = 5;
20 } // namespace
22 namespace chrome_variations {
24 VariationsRegistrySyncer::VariationsRegistrySyncer() {
27 VariationsRegistrySyncer::~VariationsRegistrySyncer() {
30 void VariationsRegistrySyncer::RequestRegistrySync() {
31 if (timer_.IsRunning()) {
32 timer_.Reset();
33 return;
36 timer_.Start(FROM_HERE,
37 base::TimeDelta::FromSeconds(kRegistrySyncDelaySeconds),
38 this, &VariationsRegistrySyncer::SyncWithRegistry);
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;
49 const bool is_system_install =
50 !InstallUtil::IsPerUserInstall(chrome_exe.value().c_str());
52 // Read the current bits from the registry.
53 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;
61 // Since the non-Variations contents of experiment_labels should not be,
62 // clobbered, separate them from the Variations contents.
63 const string16 other_labels = ExtractNonVariationLabels(registry_labels);
65 // Compute the new Variations part of the label.
66 base::FieldTrial::ActiveGroups active_groups;
67 base::FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
68 const string16 variation_labels =
69 BuildGoogleUpdateExperimentLabel(active_groups);
71 // Append the old non-Variations labels with the new Variations labels and
72 // write it back to the registry.
73 const string16 combined_labels =
74 CombineExperimentLabels(variation_labels, other_labels);
76 if (!GoogleUpdateSettings::SetExperimentLabels(is_system_install,
77 combined_labels)) {
78 DVLOG(1) << "Error writing Variation labels to the registry: "
79 << combined_labels;
83 } // namespace chrome_variations