- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / metrics / variations / variations_registry_syncer_win.cc
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/variations_util.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   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 string16 other_labels = ExtractNonVariationLabels(registry_labels);
64
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);
70
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);
75
76   if (!GoogleUpdateSettings::SetExperimentLabels(is_system_install,
77                                                  combined_labels)) {
78     DVLOG(1) << "Error writing Variation labels to the registry: "
79              << combined_labels;
80   }
81 }
82
83 }  // namespace chrome_variations