Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/component_updater/sw_reporter_installer_win.h" | 5 #include "chrome/browser/component_updater/sw_reporter_installer_win.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <algorithm> | |
| 9 #include <map> | 10 #include <map> |
| 10 #include <memory> | 11 #include <memory> |
| 12 #include <queue> | |
| 11 #include <string> | 13 #include <string> |
| 12 #include <utility> | 14 #include <utility> |
| 13 #include <vector> | 15 #include <vector> |
| 14 | 16 |
| 15 #include "base/base_paths.h" | 17 #include "base/base_paths.h" |
| 18 #include "base/command_line.h" | |
| 19 #include "base/feature_list.h" | |
| 16 #include "base/files/file_path.h" | 20 #include "base/files/file_path.h" |
| 17 #include "base/files/file_util.h" | 21 #include "base/files/file_util.h" |
| 18 #include "base/logging.h" | 22 #include "base/logging.h" |
| 19 #include "base/metrics/histogram.h" | 23 #include "base/metrics/histogram.h" |
| 20 #include "base/metrics/sparse_histogram.h" | 24 #include "base/metrics/sparse_histogram.h" |
| 21 #include "base/path_service.h" | 25 #include "base/path_service.h" |
| 22 #include "base/strings/string_tokenizer.h" | 26 #include "base/strings/string_tokenizer.h" |
| 27 #include "base/strings/stringprintf.h" | |
| 28 #include "base/strings/utf_string_conversions.h" | |
| 29 #include "base/sys_info.h" | |
| 23 #include "base/threading/thread_task_runner_handle.h" | 30 #include "base/threading/thread_task_runner_handle.h" |
| 24 #include "base/threading/worker_pool.h" | 31 #include "base/threading/worker_pool.h" |
| 25 #include "base/time/time.h" | 32 #include "base/time/time.h" |
| 26 #include "base/win/registry.h" | 33 #include "base/win/registry.h" |
| 27 #include "chrome/browser/browser_process.h" | 34 #include "chrome/browser/browser_process.h" |
| 28 #include "chrome/browser/safe_browsing/srt_fetcher_win.h" | 35 #include "chrome/browser/safe_browsing/srt_fetcher_win.h" |
| 29 #include "chrome/browser/safe_browsing/srt_field_trial_win.h" | 36 #include "chrome/browser/safe_browsing/srt_field_trial_win.h" |
| 30 #include "components/component_updater/component_updater_paths.h" | 37 #include "components/component_updater/component_updater_paths.h" |
| 31 #include "components/component_updater/component_updater_service.h" | 38 #include "components/component_updater/component_updater_service.h" |
| 32 #include "components/component_updater/default_component_installer.h" | 39 #include "components/component_updater/default_component_installer.h" |
| 33 #include "components/component_updater/pref_names.h" | 40 #include "components/component_updater/pref_names.h" |
| 34 #include "components/pref_registry/pref_registry_syncable.h" | 41 #include "components/pref_registry/pref_registry_syncable.h" |
| 35 #include "components/prefs/pref_registry_simple.h" | 42 #include "components/prefs/pref_registry_simple.h" |
| 36 #include "components/update_client/update_client.h" | 43 #include "components/update_client/update_client.h" |
| 37 #include "components/update_client/utils.h" | 44 #include "components/update_client/utils.h" |
| 45 #include "components/variations/variations_associated_data.h" | |
| 38 #include "content/public/browser/browser_thread.h" | 46 #include "content/public/browser/browser_thread.h" |
| 47 #include "third_party/re2/src/re2/re2.h" | |
| 39 | 48 |
| 40 namespace component_updater { | 49 namespace component_updater { |
| 41 | 50 |
| 42 namespace { | 51 namespace { |
| 43 | 52 |
| 44 // These two sets of values are used to send UMA information and are replicated | 53 // These two sets of values are used to send UMA information and are replicated |
| 45 // in the histograms.xml file, so the order MUST NOT CHANGE. | 54 // in the histograms.xml file, so the order MUST NOT CHANGE. |
| 46 enum SRTCompleted { | 55 enum SRTCompleted { |
| 47 SRT_COMPLETED_NOT_YET = 0, | 56 SRT_COMPLETED_NOT_YET = 0, |
| 48 SRT_COMPLETED_YES = 1, | 57 SRT_COMPLETED_YES = 1, |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 60 | 69 |
| 61 const base::FilePath::CharType kSwReporterExeName[] = | 70 const base::FilePath::CharType kSwReporterExeName[] = |
| 62 FILE_PATH_LITERAL("software_reporter_tool.exe"); | 71 FILE_PATH_LITERAL("software_reporter_tool.exe"); |
| 63 | 72 |
| 64 // SRT registry keys and value names. | 73 // SRT registry keys and value names. |
| 65 const wchar_t kCleanerSuffixRegistryKey[] = L"Cleaner"; | 74 const wchar_t kCleanerSuffixRegistryKey[] = L"Cleaner"; |
| 66 const wchar_t kExitCodeValueName[] = L"ExitCode"; | 75 const wchar_t kExitCodeValueName[] = L"ExitCode"; |
| 67 const wchar_t kUploadResultsValueName[] = L"UploadResults"; | 76 const wchar_t kUploadResultsValueName[] = L"UploadResults"; |
| 68 const wchar_t kVersionValueName[] = L"Version"; | 77 const wchar_t kVersionValueName[] = L"Version"; |
| 69 | 78 |
| 79 const base::Feature kExperimentalEngineFeature{ | |
| 80 "ExperimentalSwReporterEngine", base::FEATURE_DISABLED_BY_DEFAULT}; | |
| 81 | |
| 70 void SRTHasCompleted(SRTCompleted value) { | 82 void SRTHasCompleted(SRTCompleted value) { |
| 71 UMA_HISTOGRAM_ENUMERATION("SoftwareReporter.Cleaner.HasCompleted", value, | 83 UMA_HISTOGRAM_ENUMERATION("SoftwareReporter.Cleaner.HasCompleted", value, |
| 72 SRT_COMPLETED_MAX); | 84 SRT_COMPLETED_MAX); |
| 73 } | 85 } |
| 74 | 86 |
| 75 void ReportUploadsWithUma(const base::string16& upload_results) { | 87 void ReportUploadsWithUma(const base::string16& upload_results) { |
| 76 base::WStringTokenizer tokenizer(upload_results, L";"); | 88 base::WStringTokenizer tokenizer(upload_results, L";"); |
| 77 int failure_count = 0; | 89 int failure_count = 0; |
| 78 int success_count = 0; | 90 int success_count = 0; |
| 79 int longest_failure_run = 0; | 91 int longest_failure_run = 0; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 96 | 108 |
| 97 UMA_HISTOGRAM_COUNTS_100("SoftwareReporter.UploadFailureCount", | 109 UMA_HISTOGRAM_COUNTS_100("SoftwareReporter.UploadFailureCount", |
| 98 failure_count); | 110 failure_count); |
| 99 UMA_HISTOGRAM_COUNTS_100("SoftwareReporter.UploadSuccessCount", | 111 UMA_HISTOGRAM_COUNTS_100("SoftwareReporter.UploadSuccessCount", |
| 100 success_count); | 112 success_count); |
| 101 UMA_HISTOGRAM_COUNTS_100("SoftwareReporter.UploadLongestFailureRun", | 113 UMA_HISTOGRAM_COUNTS_100("SoftwareReporter.UploadLongestFailureRun", |
| 102 longest_failure_run); | 114 longest_failure_run); |
| 103 UMA_HISTOGRAM_BOOLEAN("SoftwareReporter.LastUploadResult", last_result); | 115 UMA_HISTOGRAM_BOOLEAN("SoftwareReporter.LastUploadResult", last_result); |
| 104 } | 116 } |
| 105 | 117 |
| 118 bool IsExperimentalEngineEnabled() { | |
| 119 // The experiment is only enabled on x86. There's no way to check this in the | |
| 120 // variations config so we'll hard-code it. | |
| 121 if (base::SysInfo::OperatingSystemArchitecture() != "x86") | |
| 122 return false; | |
| 123 | |
| 124 return base::FeatureList::IsEnabled(kExperimentalEngineFeature); | |
| 125 } | |
| 126 | |
| 106 class SwReporterInstallerTraits : public ComponentInstallerTraits { | 127 class SwReporterInstallerTraits : public ComponentInstallerTraits { |
| 107 public: | 128 public: |
| 108 SwReporterInstallerTraits() {} | 129 SwReporterInstallerTraits() {} |
| 109 | 130 |
| 110 ~SwReporterInstallerTraits() override {} | 131 ~SwReporterInstallerTraits() override {} |
| 111 | 132 |
| 112 bool VerifyInstallation(const base::DictionaryValue& manifest, | 133 bool VerifyInstallation(const base::DictionaryValue& manifest, |
| 113 const base::FilePath& dir) const override { | 134 const base::FilePath& dir) const override { |
| 114 return base::PathExists(dir.Append(kSwReporterExeName)); | 135 return base::PathExists(dir.Append(kSwReporterExeName)); |
| 115 } | 136 } |
| 116 | 137 |
| 117 bool SupportsGroupPolicyEnabledComponentUpdates() const override { | 138 bool SupportsGroupPolicyEnabledComponentUpdates() const override { |
| 118 return true; | 139 return true; |
| 119 } | 140 } |
| 120 | 141 |
| 121 bool RequiresNetworkEncryption() const override { return false; } | 142 bool RequiresNetworkEncryption() const override { return false; } |
| 122 | 143 |
| 123 bool OnCustomInstall(const base::DictionaryValue& manifest, | 144 bool OnCustomInstall(const base::DictionaryValue& manifest, |
| 124 const base::FilePath& install_dir) override { | 145 const base::FilePath& install_dir) override { |
| 125 return true; | 146 return true; |
| 126 } | 147 } |
| 127 | 148 |
| 128 void ComponentReady( | 149 void ComponentReady( |
| 129 const base::Version& version, | 150 const base::Version& version, |
| 130 const base::FilePath& install_dir, | 151 const base::FilePath& install_dir, |
| 131 std::unique_ptr<base::DictionaryValue> manifest) override { | 152 std::unique_ptr<base::DictionaryValue> manifest) override { |
| 132 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 153 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 133 base::FilePath exe_path(install_dir.Append(kSwReporterExeName)); | 154 base::FilePath exe_path(install_dir.Append(kSwReporterExeName)); |
| 155 if (IsExperimentalEngineEnabled()) { | |
| 156 RunExperimentalSwReporter(exe_path, version); | |
| 157 return; | |
| 158 } | |
| 134 safe_browsing::RunSwReporter(safe_browsing::SwReporterInvocation(exe_path), | 159 safe_browsing::RunSwReporter(safe_browsing::SwReporterInvocation(exe_path), |
| 135 version, base::ThreadTaskRunnerHandle::Get(), | 160 version, base::ThreadTaskRunnerHandle::Get(), |
| 136 base::WorkerPool::GetTaskRunner(true)); | 161 base::WorkerPool::GetTaskRunner(true)); |
| 137 } | 162 } |
| 138 | 163 |
| 139 base::FilePath GetRelativeInstallDir() const override { | 164 base::FilePath GetRelativeInstallDir() const override { |
| 140 return base::FilePath(FILE_PATH_LITERAL("SwReporter")); | 165 return base::FilePath(FILE_PATH_LITERAL("SwReporter")); |
| 141 } | 166 } |
| 142 | 167 |
| 143 void GetHash(std::vector<uint8_t>* hash) const override { GetPkHash(hash); } | 168 void GetHash(std::vector<uint8_t>* hash) const override { GetPkHash(hash); } |
| 144 | 169 |
| 145 std::string GetName() const override { return "Software Reporter Tool"; } | 170 std::string GetName() const override { return "Software Reporter Tool"; } |
| 146 | 171 |
| 147 update_client::InstallerAttributes GetInstallerAttributes() const override { | 172 update_client::InstallerAttributes GetInstallerAttributes() const override { |
| 148 return update_client::InstallerAttributes(); | 173 update_client::InstallerAttributes attributes; |
| 174 if (IsExperimentalEngineEnabled()) { | |
| 175 // Pass the "tag" parameter to the installer; it will be used to choose | |
| 176 // which binary is downloaded. | |
| 177 constexpr char kTagParam[] = "tag"; | |
| 178 std::string tag = variations::GetVariationParamValueByFeature( | |
| 179 kExperimentalEngineFeature, kTagParam); | |
| 180 | |
| 181 // Validate that the tag is valid (regexp taken from | |
| 182 // ComponentInstallerTraits::InstallerAttributes). If not set it to a | |
| 183 // valid but unrecognized value so that nothing will be downloaded. | |
| 184 if (tag.empty() || | |
| 185 !re2::RE2::FullMatch(tag, "^[-.,;+_=a-zA-Z0-9]{0,256}$")) { | |
| 186 ReportExperimentError(kBadTagParam); | |
| 187 attributes[kTagParam] = "missing_tag"; | |
| 188 } else { | |
| 189 attributes[kTagParam] = tag; | |
| 190 } | |
| 191 } | |
| 192 return attributes; | |
| 149 } | 193 } |
| 150 std::vector<std::string> GetMimeTypes() const override { | 194 std::vector<std::string> GetMimeTypes() const override { |
| 151 return std::vector<std::string>(); | 195 return std::vector<std::string>(); |
| 152 } | 196 } |
| 153 | 197 |
| 154 static std::string ID() { | 198 static std::string ID() { |
| 155 update_client::CrxComponent component; | 199 update_client::CrxComponent component; |
| 156 component.version = Version("0.0.0.0"); | 200 component.version = Version("0.0.0.0"); |
| 157 GetPkHash(&component.pk_hash); | 201 GetPkHash(&component.pk_hash); |
| 158 return update_client::GetCrxComponentID(component); | 202 return update_client::GetCrxComponentID(component); |
| 159 } | 203 } |
| 160 | 204 |
| 161 private: | 205 private: |
| 162 static void GetPkHash(std::vector<uint8_t>* hash) { | 206 static void GetPkHash(std::vector<uint8_t>* hash) { |
| 163 DCHECK(hash); | 207 DCHECK(hash); |
| 164 hash->assign(kSha256Hash, kSha256Hash + sizeof(kSha256Hash)); | 208 hash->assign(kSha256Hash, kSha256Hash + sizeof(kSha256Hash)); |
| 165 } | 209 } |
| 210 | |
| 211 enum ExperimentError { | |
| 212 kBadTagParam, | |
|
grt (UTC plus 2)
2016/08/15 19:39:40
enums use SHOUTY_STYLE.
since this goes straight i
macourteau
2016/09/12 19:53:14
Done.
| |
| 213 kBadVariationParams, | |
| 214 kNumErrorTypes | |
| 215 }; | |
| 216 | |
| 217 void ReportExperimentError(ExperimentError error) const { | |
| 218 UMA_HISTOGRAM_ENUMERATION("SoftwareReporter.ExperimentErrors", error, | |
| 219 kNumErrorTypes); | |
| 220 } | |
| 221 | |
| 222 // Read the command-line params and an UMA histogram suffix from the | |
| 223 // experiment parameters, and launch the SwReporter one or more times with | |
| 224 // those parameters. If anything goes wrong the SwReporter should not be | |
| 225 // run at all - do not fall back to the default. | |
| 226 void RunExperimentalSwReporter(const base::FilePath& exe_path, | |
| 227 const base::Version& version) { | |
| 228 DCHECK(IsExperimentalEngineEnabled()); | |
| 229 | |
| 230 // Command-line arguments are passed as args0, args1, ..., argsN. | |
| 231 // Suffixes are passed as suffix0, suffix1, ..., suffixN. | |
| 232 // | |
| 233 // Read all the parameters whose names match these patterns into maps, | |
| 234 // then make sure that all indices 0 through N are stored in both maps. | |
| 235 // If any are missing, the experiment config is badly formatted. If it's | |
| 236 // ok, add each pair (command line, suffix) to the invocations vector. | |
| 237 std::map<std::string, std::string> experiment_params; | |
| 238 if (!variations::GetVariationParamsByFeature(kExperimentalEngineFeature, | |
| 239 &experiment_params)) { | |
| 240 ReportExperimentError(kBadVariationParams); | |
| 241 return; | |
| 242 } | |
| 243 | |
| 244 re2::RE2 args_pattern("args(\\d+)"); | |
| 245 std::map<int, std::string> arg_strings; | |
| 246 | |
| 247 re2::RE2 suffix_pattern("suffix(\\d+)"); | |
| 248 std::map<int, std::string> suffixes; | |
| 249 | |
| 250 re2::RE2 valid_suffix_pattern("[A-Za-z0-9]+"); | |
| 251 | |
| 252 int max_index = 0; | |
| 253 for (const auto& param : experiment_params) { | |
| 254 const std::string& key = param.first; | |
| 255 const std::string& value = param.second; | |
| 256 | |
| 257 int index = 0; | |
| 258 if (re2::RE2::FullMatch(key, args_pattern, &index)) { | |
| 259 arg_strings[index] = value; | |
| 260 } else if (re2::RE2::FullMatch(key, suffix_pattern, &index)) { | |
| 261 if (!re2::RE2::FullMatch(value, valid_suffix_pattern)) { | |
| 262 ReportExperimentError(kBadVariationParams); | |
| 263 return; | |
| 264 } | |
| 265 suffixes[index] = value; | |
| 266 } else { | |
| 267 // Unrecognized parameter. | |
| 268 continue; | |
| 269 } | |
| 270 | |
| 271 if (index < 0) { | |
| 272 ReportExperimentError(kBadVariationParams); | |
| 273 return; | |
| 274 } | |
| 275 max_index = std::max(index, max_index); | |
| 276 } | |
| 277 | |
| 278 std::queue<safe_browsing::SwReporterInvocation> invocations; | |
| 279 | |
| 280 for (int index = 0; index <= max_index; ++index) { | |
| 281 auto args = arg_strings.find(index); | |
| 282 auto suffix = suffixes.find(index); | |
| 283 if (args == arg_strings.end() || suffix == suffixes.end()) { | |
| 284 ReportExperimentError(kBadVariationParams); | |
| 285 return; | |
| 286 } | |
| 287 | |
| 288 // Now we have a string to parse into a CommandLine object, but it | |
| 289 // doesn't start with the exe path. We can't create the CommandLine with | |
| 290 // the path and then append the argument string, because CommandLine only | |
| 291 // has methods to append individual arguments. So tack a placeholder | |
| 292 // which is guaranteed not to need quoting to the beginning of the | |
| 293 // argument string, then replace it with the real path, which may contain | |
| 294 // spaces and other weirdness. | |
| 295 auto command_line = base::CommandLine::FromString(base::UTF8ToUTF16( | |
| 296 base::StringPrintf("placeholder %s", args->second))); | |
| 297 command_line.SetProgram(exe_path); | |
| 298 | |
| 299 // Add the histogram suffix to the command-line as well, so that the | |
| 300 // reporter will add the same suffix to registry keys where it writes | |
| 301 // metrics. | |
| 302 command_line.AppendSwitchASCII("registry-suffix", suffix->second); | |
| 303 | |
| 304 safe_browsing::SwReporterInvocation invocation(command_line); | |
| 305 invocation.suffix = suffix->second; | |
| 306 invocation.is_experimental = true; | |
| 307 invocations.push(invocation); | |
| 308 } | |
| 309 | |
| 310 // If there were no command-lines and suffixes given, run once with no args | |
| 311 // and an empty suffix. (The control group would be set up this way.) | |
| 312 if (invocations.empty()) { | |
| 313 safe_browsing::SwReporterInvocation invocation(exe_path); | |
| 314 invocation.is_experimental = true; | |
| 315 invocations.push(invocation); | |
| 316 } | |
| 317 | |
| 318 safe_browsing::RunSwReporters(invocations, version, | |
| 319 base::ThreadTaskRunnerHandle::Get(), | |
| 320 base::WorkerPool::GetTaskRunner(true)); | |
| 321 } | |
| 166 }; | 322 }; |
| 167 | 323 |
| 168 } // namespace | 324 } // namespace |
| 169 | 325 |
| 170 void RegisterSwReporterComponent(ComponentUpdateService* cus) { | 326 void RegisterSwReporterComponent(ComponentUpdateService* cus) { |
| 171 if (!safe_browsing::IsSwReporterEnabled()) | 327 if (!safe_browsing::IsSwReporterEnabled()) |
| 172 return; | 328 return; |
| 173 | 329 |
| 174 // Check if we have information from Cleaner and record UMA statistics. | 330 // Check if we have information from Cleaner and record UMA statistics. |
| 175 base::string16 cleaner_key_name( | 331 base::string16 cleaner_key_name( |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 259 } | 415 } |
| 260 | 416 |
| 261 void RegisterProfilePrefsForSwReporter( | 417 void RegisterProfilePrefsForSwReporter( |
| 262 user_prefs::PrefRegistrySyncable* registry) { | 418 user_prefs::PrefRegistrySyncable* registry) { |
| 263 registry->RegisterStringPref(prefs::kSwReporterPromptVersion, ""); | 419 registry->RegisterStringPref(prefs::kSwReporterPromptVersion, ""); |
| 264 | 420 |
| 265 registry->RegisterStringPref(prefs::kSwReporterPromptSeed, ""); | 421 registry->RegisterStringPref(prefs::kSwReporterPromptSeed, ""); |
| 266 } | 422 } |
| 267 | 423 |
| 268 } // namespace component_updater | 424 } // namespace component_updater |
| OLD | NEW |