| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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 "components/startup_metric_utils/browser/pre_read_field_trial_utils_win
.h" | |
| 6 | |
| 7 #include "base/callback.h" | |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "base/metrics/field_trial.h" | |
| 10 #include "base/strings/string_util.h" | |
| 11 #include "base/strings/utf_string_conversions.h" | |
| 12 #include "base/win/registry.h" | |
| 13 #include "components/variations/variations_associated_data.h" | |
| 14 | |
| 15 namespace startup_metric_utils { | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 // Name of the pre-read field trial. This name is used to get the pre-read group | |
| 20 // to use for the next startup from base::FieldTrialList. | |
| 21 const char kPreReadFieldTrialName[] = "PreRead"; | |
| 22 | |
| 23 // Name of the synthetic pre-read field trial. A synthetic field trial is | |
| 24 // registered so that UMA metrics recorded during the current startup are | |
| 25 // annotated with the pre-read group that is actually used during this startup. | |
| 26 const char kPreReadSyntheticFieldTrialName[] = "SyntheticPreRead"; | |
| 27 | |
| 28 // Variation names for the PreRead field trial. | |
| 29 const base::char16 kNoPreReadVariationName[] = L"NoPreRead"; | |
| 30 const base::char16 kHighPriorityVariationName[] = L"HighPriority"; | |
| 31 const base::char16 kOnlyIfColdVariationName[] = L"OnlyIfCold"; | |
| 32 const base::char16 kPrefetchVirtualMemoryVariationName[] = | |
| 33 L"PrefetchVirtualMemory"; | |
| 34 | |
| 35 // Registry key in which the PreRead field trial group is stored. | |
| 36 const base::char16 kPreReadFieldTrialRegistryKey[] = L"\\PreReadFieldTrial"; | |
| 37 | |
| 38 // Returns the registry path in which the PreRead group is stored. | |
| 39 base::string16 GetPreReadRegistryPath( | |
| 40 const base::string16& product_registry_path) { | |
| 41 return product_registry_path + kPreReadFieldTrialRegistryKey; | |
| 42 } | |
| 43 | |
| 44 } // namespace | |
| 45 | |
| 46 void GetPreReadOptions(const base::string16& product_registry_path, | |
| 47 bool* no_pre_read, | |
| 48 bool* high_priority, | |
| 49 bool* only_if_cold, | |
| 50 bool* prefetch_virtual_memory) { | |
| 51 DCHECK(!product_registry_path.empty()); | |
| 52 DCHECK(no_pre_read); | |
| 53 DCHECK(high_priority); | |
| 54 DCHECK(only_if_cold); | |
| 55 DCHECK(prefetch_virtual_memory); | |
| 56 | |
| 57 // Open the PreRead field trial's registry key. | |
| 58 const base::string16 registry_path = | |
| 59 GetPreReadRegistryPath(product_registry_path); | |
| 60 const base::win::RegKey key(HKEY_CURRENT_USER, registry_path.c_str(), | |
| 61 KEY_QUERY_VALUE); | |
| 62 | |
| 63 // Set the PreRead field trial's options. | |
| 64 struct VariationMapping { | |
| 65 const base::char16* name; | |
| 66 bool* variable; | |
| 67 } const variations_mappings[] = { | |
| 68 {kNoPreReadVariationName, no_pre_read}, | |
| 69 {kHighPriorityVariationName, high_priority}, | |
| 70 {kOnlyIfColdVariationName, only_if_cold}, | |
| 71 {kPrefetchVirtualMemoryVariationName, prefetch_virtual_memory}, | |
| 72 }; | |
| 73 | |
| 74 for (const auto& mapping : variations_mappings) { | |
| 75 // Set the option variable to true if the corresponding value is found in | |
| 76 // the registry. Set to false otherwise (default behavior). | |
| 77 DWORD value = 0; | |
| 78 *mapping.variable = key.ReadValueDW(mapping.name, &value) == ERROR_SUCCESS; | |
| 79 DCHECK(!*mapping.variable || value == 1); | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 void UpdatePreReadOptions(const base::string16& product_registry_path) { | |
| 84 DCHECK(!product_registry_path.empty()); | |
| 85 | |
| 86 const base::string16 registry_path = | |
| 87 GetPreReadRegistryPath(product_registry_path); | |
| 88 | |
| 89 // Clear the experiment key. That way, when the trial is shut down, the | |
| 90 // registry will be cleaned automatically. Also, this prevents variation | |
| 91 // params from the previous group to stay in the registry when the group is | |
| 92 // updated. | |
| 93 base::win::RegKey(HKEY_CURRENT_USER, registry_path.c_str(), KEY_SET_VALUE) | |
| 94 .DeleteKey(L""); | |
| 95 | |
| 96 // Get the new group name. | |
| 97 const base::string16 group = base::UTF8ToUTF16( | |
| 98 base::FieldTrialList::FindFullName(kPreReadFieldTrialName)); | |
| 99 if (group.empty()) | |
| 100 return; | |
| 101 | |
| 102 // Get variation params for the new group. | |
| 103 std::map<std::string, std::string> variation_params; | |
| 104 variations::GetVariationParams(kPreReadFieldTrialName, &variation_params); | |
| 105 | |
| 106 // Open the registry key. | |
| 107 base::win::RegKey key(HKEY_CURRENT_USER, registry_path.c_str(), | |
| 108 KEY_SET_VALUE); | |
| 109 | |
| 110 // Write variation params in the registry. | |
| 111 for (const auto& variation_param : variation_params) { | |
| 112 if (!variation_param.second.empty()) { | |
| 113 if (key.WriteValue(base::UTF8ToUTF16(variation_param.first).c_str(), 1) != | |
| 114 ERROR_SUCCESS) { | |
| 115 // Abort on failure to prevent the group name from being written in the | |
| 116 // registry if not all variation params have been written. No synthetic | |
| 117 // field trial is registered when there is no group name in the | |
| 118 // registry. | |
| 119 return; | |
| 120 } | |
| 121 } | |
| 122 } | |
| 123 | |
| 124 // Write the new group name in the registry. | |
| 125 key.WriteValue(L"", group.c_str()); | |
| 126 } | |
| 127 | |
| 128 void RegisterPreReadSyntheticFieldTrial( | |
| 129 const base::string16& product_registry_path, | |
| 130 const RegisterPreReadSyntheticFieldTrialCallback& | |
| 131 register_synthetic_field_trial) { | |
| 132 DCHECK(!product_registry_path.empty()); | |
| 133 | |
| 134 const base::win::RegKey key( | |
| 135 HKEY_CURRENT_USER, GetPreReadRegistryPath(product_registry_path).c_str(), | |
| 136 KEY_QUERY_VALUE); | |
| 137 base::string16 group; | |
| 138 key.ReadValue(L"", &group); | |
| 139 | |
| 140 if (!group.empty()) { | |
| 141 register_synthetic_field_trial.Run(kPreReadSyntheticFieldTrialName, | |
| 142 base::UTF16ToUTF8(group)); | |
| 143 } | |
| 144 } | |
| 145 | |
| 146 } // namespace startup_metric_utils | |
| OLD | NEW |