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 "base/threading/thread_restrictions.h"
12 #include "chrome/installer/util/google_update_settings.h"
13 #include "chrome/installer/util/install_util.h"
14 #include "components/variations/experiment_labels.h"
15 #include "content/public/browser/browser_thread.h"
17 namespace chrome_variations
{
21 // Delay before performing a registry sync, in seconds.
22 const int kRegistrySyncDelaySeconds
= 5;
24 // Performs the actual synchronization process with the registry, which should
25 // be done on a blocking pool thread.
26 void SyncWithRegistryOnBlockingPool() {
27 base::ThreadRestrictions::AssertIOAllowed();
29 // Note that all registry operations are done here on the UI thread as there
30 // are no threading restrictions on them.
31 base::FilePath chrome_exe
;
32 if (!PathService::Get(base::FILE_EXE
, &chrome_exe
)) {
33 NOTREACHED() << "Failed to get chrome exe path";
36 const bool is_system_install
= !InstallUtil::IsPerUserInstall(chrome_exe
);
38 // Read the current bits from the registry.
39 base::string16 registry_labels
;
40 bool success
= GoogleUpdateSettings::ReadExperimentLabels(is_system_install
,
43 DVLOG(1) << "Error reading Variation labels from the registry.";
47 // Since the non-Variations contents of experiment_labels should not be,
48 // clobbered, separate them from the Variations contents.
49 const base::string16 other_labels
=
50 ExtractNonVariationLabels(registry_labels
);
52 // Compute the new Variations part of the label.
53 base::FieldTrial::ActiveGroups active_groups
;
54 base::FieldTrialList::GetActiveFieldTrialGroups(&active_groups
);
55 const base::string16 variation_labels
=
56 BuildGoogleUpdateExperimentLabel(active_groups
);
58 // Append the old non-Variations labels with the new Variations labels and
59 // write it back to the registry.
60 const base::string16 combined_labels
=
61 CombineExperimentLabels(variation_labels
, other_labels
);
63 if (!GoogleUpdateSettings::SetExperimentLabels(is_system_install
,
65 DVLOG(1) << "Error writing Variation labels to the registry: "
72 VariationsRegistrySyncer::VariationsRegistrySyncer() {
75 VariationsRegistrySyncer::~VariationsRegistrySyncer() {
78 void VariationsRegistrySyncer::RequestRegistrySync() {
79 if (timer_
.IsRunning()) {
84 timer_
.Start(FROM_HERE
,
85 base::TimeDelta::FromSeconds(kRegistrySyncDelaySeconds
),
87 &VariationsRegistrySyncer::StartRegistrySync
);
90 void VariationsRegistrySyncer::StartRegistrySync() {
91 // Do the work on a blocking pool thread, as chrome://profiler has shown
92 // that it can cause jank if done on the UI thrread.
93 content::BrowserThread::GetBlockingPool()->PostTask(
94 FROM_HERE
, base::Bind(&SyncWithRegistryOnBlockingPool
));
97 } // namespace chrome_variations