| 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/common/pre_read_field_trial_utils_win.
h" | |
| 6 | |
| 7 #include "base/callback.h" | |
| 8 #include "base/logging.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. All variations change the | |
| 29 // default behavior, i.e. the default is the inverse of the variation. Thus, | |
| 30 // variations that cancel something that is done by default have a negative | |
| 31 // name. | |
| 32 const base::char16 kNoPreReadVariationName[] = L"NoPreRead"; | |
| 33 const base::char16 kHighPriorityVariationName[] = L"HighPriority"; | |
| 34 const base::char16 kPrefetchVirtualMemoryVariationName[] = | |
| 35 L"PrefetchVirtualMemory"; | |
| 36 | |
| 37 // Registry key in which the PreRead field trial group is stored. | |
| 38 const base::char16 kPreReadFieldTrialRegistryKey[] = L"\\PreReadFieldTrial"; | |
| 39 | |
| 40 // Pre-read options to use for the current process. This is initialized by | |
| 41 // InitializePreReadOptions(). | |
| 42 PreReadOptions g_pre_read_options = {}; | |
| 43 | |
| 44 // Returns the registry path in which the PreRead group is stored. | |
| 45 base::string16 GetPreReadRegistryPath( | |
| 46 const base::string16& product_registry_path) { | |
| 47 return product_registry_path + kPreReadFieldTrialRegistryKey; | |
| 48 } | |
| 49 | |
| 50 // Returns true if |key| has a value named |name| which is not zero. | |
| 51 bool ReadBool(const base::win::RegKey& key, const base::char16* name) { | |
| 52 DWORD value = 0; | |
| 53 return key.ReadValueDW(name, &value) == ERROR_SUCCESS && value != 0; | |
| 54 } | |
| 55 | |
| 56 } // namespace | |
| 57 | |
| 58 void InitializePreReadOptions(const base::string16& product_registry_path) { | |
| 59 DCHECK(!product_registry_path.empty()); | |
| 60 | |
| 61 #if DCHECK_IS_ON() | |
| 62 static bool initialized = false; | |
| 63 DCHECK(!initialized); | |
| 64 initialized = true; | |
| 65 #endif // DCHECK_IS_ON() | |
| 66 | |
| 67 // Open the PreRead field trial's registry key. | |
| 68 const base::string16 registry_path = | |
| 69 GetPreReadRegistryPath(product_registry_path); | |
| 70 const base::win::RegKey key(HKEY_CURRENT_USER, registry_path.c_str(), | |
| 71 KEY_QUERY_VALUE); | |
| 72 | |
| 73 // Set the PreRead field trial's options. | |
| 74 g_pre_read_options.pre_read = !ReadBool(key, kNoPreReadVariationName); | |
| 75 g_pre_read_options.high_priority = ReadBool(key, kHighPriorityVariationName); | |
| 76 g_pre_read_options.prefetch_virtual_memory = | |
| 77 ReadBool(key, kPrefetchVirtualMemoryVariationName); | |
| 78 } | |
| 79 | |
| 80 PreReadOptions GetPreReadOptions() { | |
| 81 return g_pre_read_options; | |
| 82 } | |
| 83 | |
| 84 void UpdatePreReadOptions(const base::string16& product_registry_path) { | |
| 85 DCHECK(!product_registry_path.empty()); | |
| 86 | |
| 87 const base::string16 registry_path = | |
| 88 GetPreReadRegistryPath(product_registry_path); | |
| 89 | |
| 90 // Clear the experiment key. That way, when the trial is shut down, the | |
| 91 // registry will be cleaned automatically. Also, this prevents variation | |
| 92 // params from the previous group to stay in the registry when the group is | |
| 93 // updated. | |
| 94 base::win::RegKey(HKEY_CURRENT_USER, registry_path.c_str(), KEY_SET_VALUE) | |
| 95 .DeleteKey(L""); | |
| 96 | |
| 97 // Get the new group name. | |
| 98 const base::string16 group = base::UTF8ToUTF16( | |
| 99 base::FieldTrialList::FindFullName(kPreReadFieldTrialName)); | |
| 100 if (group.empty()) | |
| 101 return; | |
| 102 | |
| 103 // Get variation params for the new group. | |
| 104 std::map<std::string, std::string> variation_params; | |
| 105 variations::GetVariationParams(kPreReadFieldTrialName, &variation_params); | |
| 106 | |
| 107 // Open the registry key. | |
| 108 base::win::RegKey key(HKEY_CURRENT_USER, registry_path.c_str(), | |
| 109 KEY_SET_VALUE); | |
| 110 | |
| 111 // Write variation params in the registry. | |
| 112 for (const auto& variation_param : variation_params) { | |
| 113 if (!variation_param.second.empty()) { | |
| 114 if (key.WriteValue(base::UTF8ToUTF16(variation_param.first).c_str(), 1) != | |
| 115 ERROR_SUCCESS) { | |
| 116 // Abort on failure to prevent the group name from being written in the | |
| 117 // registry if not all variation params have been written. No synthetic | |
| 118 // field trial is registered when there is no group name in the | |
| 119 // registry. | |
| 120 return; | |
| 121 } | |
| 122 } | |
| 123 } | |
| 124 | |
| 125 // Write the new group name in the registry. | |
| 126 key.WriteValue(L"", group.c_str()); | |
| 127 } | |
| 128 | |
| 129 void RegisterPreReadSyntheticFieldTrial( | |
| 130 const base::string16& product_registry_path, | |
| 131 const RegisterPreReadSyntheticFieldTrialCallback& | |
| 132 register_synthetic_field_trial) { | |
| 133 DCHECK(!product_registry_path.empty()); | |
| 134 | |
| 135 const base::win::RegKey key( | |
| 136 HKEY_CURRENT_USER, GetPreReadRegistryPath(product_registry_path).c_str(), | |
| 137 KEY_QUERY_VALUE); | |
| 138 base::string16 group; | |
| 139 key.ReadValue(L"", &group); | |
| 140 | |
| 141 if (!group.empty()) { | |
| 142 register_synthetic_field_trial.Run(kPreReadSyntheticFieldTrialName, | |
| 143 base::UTF16ToUTF8(group)); | |
| 144 } | |
| 145 } | |
| 146 | |
| 147 } // namespace startup_metric_utils | |
| OLD | NEW |