| OLD | NEW |
| (Empty) |
| 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 "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" | |
| 16 | |
| 17 namespace chrome_variations { | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 // Delay before performing a registry sync, in seconds. | |
| 22 const int kRegistrySyncDelaySeconds = 5; | |
| 23 | |
| 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(); | |
| 28 | |
| 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"; | |
| 34 return; | |
| 35 } | |
| 36 const bool is_system_install = !InstallUtil::IsPerUserInstall(chrome_exe); | |
| 37 | |
| 38 // Read the current bits from the registry. | |
| 39 base::string16 registry_labels; | |
| 40 bool success = GoogleUpdateSettings::ReadExperimentLabels(is_system_install, | |
| 41 ®istry_labels); | |
| 42 if (!success) { | |
| 43 DVLOG(1) << "Error reading Variation labels from the registry."; | |
| 44 return; | |
| 45 } | |
| 46 | |
| 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 variations::ExtractNonVariationLabels(registry_labels); | |
| 51 | |
| 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 variations::BuildGoogleUpdateExperimentLabel(active_groups); | |
| 57 | |
| 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 variations::CombineExperimentLabels(variation_labels, other_labels); | |
| 62 | |
| 63 if (!GoogleUpdateSettings::SetExperimentLabels(is_system_install, | |
| 64 combined_labels)) { | |
| 65 DVLOG(1) << "Error writing Variation labels to the registry: " | |
| 66 << combined_labels; | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 } // namespace | |
| 71 | |
| 72 VariationsRegistrySyncer::VariationsRegistrySyncer() { | |
| 73 } | |
| 74 | |
| 75 VariationsRegistrySyncer::~VariationsRegistrySyncer() { | |
| 76 } | |
| 77 | |
| 78 void VariationsRegistrySyncer::RequestRegistrySync() { | |
| 79 if (timer_.IsRunning()) { | |
| 80 timer_.Reset(); | |
| 81 return; | |
| 82 } | |
| 83 | |
| 84 timer_.Start(FROM_HERE, | |
| 85 base::TimeDelta::FromSeconds(kRegistrySyncDelaySeconds), | |
| 86 this, | |
| 87 &VariationsRegistrySyncer::StartRegistrySync); | |
| 88 } | |
| 89 | |
| 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)); | |
| 95 } | |
| 96 | |
| 97 } // namespace chrome_variations | |
| OLD | NEW |