Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / metrics / variations / variations_registry_syncer_win.cc
blob6f092e60178c80ac4bfff3b9ce9ee4c1fe0fb3cc
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 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;
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);
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);
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);
77 if (!GoogleUpdateSettings::SetExperimentLabels(is_system_install,
78 combined_labels)) {
79 DVLOG(1) << "Error writing Variation labels to the registry: "
80 << combined_labels;
84 } // namespace chrome_variations