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 <algorithm> |
10 #include <map> | 10 #include <map> |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 | 114 |
115 void ReportExperimentError(SwReporterExperimentError error) { | 115 void ReportExperimentError(SwReporterExperimentError error) { |
116 UMA_HISTOGRAM_ENUMERATION("SoftwareReporter.ExperimentErrors", error, | 116 UMA_HISTOGRAM_ENUMERATION("SoftwareReporter.ExperimentErrors", error, |
117 SW_REPORTER_EXPERIMENT_ERROR_MAX); | 117 SW_REPORTER_EXPERIMENT_ERROR_MAX); |
118 } | 118 } |
119 | 119 |
120 // Run the software reporter on the next Chrome startup after it's downloaded. | 120 // Run the software reporter on the next Chrome startup after it's downloaded. |
121 // (This is the default |reporter_runner| function passed to the | 121 // (This is the default |reporter_runner| function passed to the |
122 // |SwReporterInstallerTraits| constructor in |RegisterSwReporterComponent| | 122 // |SwReporterInstallerTraits| constructor in |RegisterSwReporterComponent| |
123 // below.) | 123 // below.) |
124 void RunSwReporterAfterStartup( | 124 void RunSwReportersAfterStartup( |
125 const safe_browsing::SwReporterInvocation& invocation, | 125 std::unique_ptr<safe_browsing::SwReporterQueue> invocations, |
126 const base::Version& version) { | 126 const base::Version& version) { |
127 content::BrowserThread::PostAfterStartupTask( | 127 content::BrowserThread::PostAfterStartupTask( |
128 FROM_HERE, base::ThreadTaskRunnerHandle::Get(), | 128 FROM_HERE, base::ThreadTaskRunnerHandle::Get(), |
129 base::Bind(&safe_browsing::RunSwReporter, invocation, version, | 129 base::Bind(&safe_browsing::RunSwReporters, |
| 130 base::Passed(std::move(invocations)), version, |
130 base::ThreadTaskRunnerHandle::Get(), | 131 base::ThreadTaskRunnerHandle::Get(), |
131 base::WorkerPool::GetTaskRunner(true))); | 132 base::WorkerPool::GetTaskRunner(true))); |
132 } | 133 } |
133 | 134 |
134 // Ensures |str| contains only alphanumeric characters and characters from | 135 // Ensures |str| contains only alphanumeric characters and characters from |
135 // |extras|, and is not longer than |max_length|. | 136 // |extras|, and is not longer than |max_length|. |
136 bool ValidateString(const std::string& str, | 137 bool ValidateString(const std::string& str, |
137 const std::string& extras, | 138 const std::string& extras, |
138 size_t max_length) { | 139 size_t max_length) { |
139 return str.size() <= max_length && | 140 return str.size() <= max_length && |
(...skipping 11 matching lines...) Expand all Loading... |
151 std::unique_ptr<base::DictionaryValue> manifest, | 152 std::unique_ptr<base::DictionaryValue> manifest, |
152 const SwReporterRunner& reporter_runner) { | 153 const SwReporterRunner& reporter_runner) { |
153 // The experiment requires launch_params so if they aren't present just | 154 // The experiment requires launch_params so if they aren't present just |
154 // return. This isn't an error because the user could get into the experiment | 155 // return. This isn't an error because the user could get into the experiment |
155 // group before they've downloaded the experiment component. | 156 // group before they've downloaded the experiment component. |
156 base::Value* launch_params = nullptr; | 157 base::Value* launch_params = nullptr; |
157 if (!manifest->Get("launch_params", &launch_params)) | 158 if (!manifest->Get("launch_params", &launch_params)) |
158 return; | 159 return; |
159 | 160 |
160 const base::ListValue* parameter_list = nullptr; | 161 const base::ListValue* parameter_list = nullptr; |
161 if (!launch_params->GetAsList(¶meter_list) || parameter_list->empty() || | 162 if (!launch_params->GetAsList(¶meter_list) || parameter_list->empty()) { |
162 // For future expansion, the manifest takes a list of invocation | |
163 // parameters, but currently we only support a single invocation. | |
164 parameter_list->GetSize() > 1) { | |
165 ReportExperimentError(SW_REPORTER_EXPERIMENT_ERROR_BAD_PARAMS); | 163 ReportExperimentError(SW_REPORTER_EXPERIMENT_ERROR_BAD_PARAMS); |
166 return; | 164 return; |
167 } | 165 } |
168 | 166 |
169 const base::DictionaryValue* invocation_params = nullptr; | 167 auto invocations = std::make_unique<safe_browsing::SwReporterQueue>(); |
170 if (!parameter_list->GetDictionary(0, &invocation_params)) { | 168 for (const auto& iter : *parameter_list) { |
171 ReportExperimentError(SW_REPORTER_EXPERIMENT_ERROR_BAD_PARAMS); | 169 const base::DictionaryValue* invocation_params = nullptr; |
172 return; | 170 if (!iter->GetAsDictionary(&invocation_params)) { |
173 } | |
174 | |
175 // Max length of the registry and histogram suffix. Fairly arbitrary: the | |
176 // Windows registry accepts much longer keys, but we need to display this | |
177 // string in histograms as well. | |
178 constexpr size_t kMaxSuffixLength = 80; | |
179 | |
180 // The suffix must be an alphanumeric string. (Empty is fine as long as the | |
181 // "suffix" key is present.) | |
182 std::string suffix; | |
183 const base::Value* suffix_value = nullptr; | |
184 if (!invocation_params->Get("suffix", &suffix_value) || | |
185 !suffix_value->GetAsString(&suffix) || | |
186 (!suffix.empty() && | |
187 !ValidateString(suffix, std::string(), kMaxSuffixLength))) { | |
188 ReportExperimentError(SW_REPORTER_EXPERIMENT_ERROR_BAD_PARAMS); | |
189 return; | |
190 } | |
191 | |
192 // Build a command line for the reporter out of the executable path and the | |
193 // arguments from the manifest. (The "arguments" key must be present, but | |
194 // it's ok if it's an empty list or a list of empty strings.) | |
195 std::vector<base::string16> argv = {exe_path.value()}; | |
196 const base::Value* arguments_value = nullptr; | |
197 const base::ListValue* arguments = nullptr; | |
198 if (!invocation_params->Get("arguments", &arguments_value) || | |
199 !arguments_value->GetAsList(&arguments)) { | |
200 ReportExperimentError(SW_REPORTER_EXPERIMENT_ERROR_BAD_PARAMS); | |
201 return; | |
202 } | |
203 | |
204 for (const auto& value : *arguments) { | |
205 base::string16 argument; | |
206 if (!value->GetAsString(&argument)) { | |
207 ReportExperimentError(SW_REPORTER_EXPERIMENT_ERROR_BAD_PARAMS); | 171 ReportExperimentError(SW_REPORTER_EXPERIMENT_ERROR_BAD_PARAMS); |
208 return; | 172 return; |
209 } | 173 } |
210 if (!argument.empty()) | 174 |
211 argv.push_back(argument); | 175 // Max length of the registry and histogram suffix. Fairly arbitrary: the |
| 176 // Windows registry accepts much longer keys, but we need to display this |
| 177 // string in histograms as well. |
| 178 constexpr size_t kMaxSuffixLength = 80; |
| 179 |
| 180 // The suffix must be an alphanumeric string. (Empty is fine as long as the |
| 181 // "suffix" key is present.) |
| 182 std::string suffix; |
| 183 if (!invocation_params->GetString("suffix", &suffix) || |
| 184 !ValidateString(suffix, std::string(), kMaxSuffixLength)) { |
| 185 ReportExperimentError(SW_REPORTER_EXPERIMENT_ERROR_BAD_PARAMS); |
| 186 return; |
| 187 } |
| 188 |
| 189 // Build a command line for the reporter out of the executable path and the |
| 190 // arguments from the manifest. (The "arguments" key must be present, but |
| 191 // it's ok if it's an empty list or a list of empty strings.) |
| 192 const base::ListValue* arguments = nullptr; |
| 193 if (!invocation_params->GetList("arguments", &arguments)) { |
| 194 ReportExperimentError(SW_REPORTER_EXPERIMENT_ERROR_BAD_PARAMS); |
| 195 return; |
| 196 } |
| 197 |
| 198 std::vector<base::string16> argv = {exe_path.value()}; |
| 199 for (const auto& value : *arguments) { |
| 200 base::string16 argument; |
| 201 if (!value->GetAsString(&argument)) { |
| 202 ReportExperimentError(SW_REPORTER_EXPERIMENT_ERROR_BAD_PARAMS); |
| 203 return; |
| 204 } |
| 205 if (!argument.empty()) |
| 206 argv.push_back(argument); |
| 207 } |
| 208 |
| 209 base::CommandLine command_line(argv); |
| 210 |
| 211 // Add the histogram suffix to the command-line as well, so that the |
| 212 // reporter will add the same suffix to registry keys where it writes |
| 213 // metrics. |
| 214 if (!suffix.empty()) |
| 215 command_line.AppendSwitchASCII("registry-suffix", suffix); |
| 216 |
| 217 // "prompt" is optional, but if present must be a boolean. |
| 218 safe_browsing::SwReporterInvocation::Flags flags = 0; |
| 219 const base::Value* prompt_value = nullptr; |
| 220 if (invocation_params->Get("prompt", &prompt_value)) { |
| 221 bool prompt = false; |
| 222 if (!prompt_value->GetAsBoolean(&prompt)) { |
| 223 ReportExperimentError(SW_REPORTER_EXPERIMENT_ERROR_BAD_PARAMS); |
| 224 return; |
| 225 } |
| 226 if (prompt) |
| 227 flags |= safe_browsing::SwReporterInvocation::FLAG_TRIGGER_PROMPT; |
| 228 } |
| 229 |
| 230 auto invocation = |
| 231 safe_browsing::SwReporterInvocation::FromCommandLine(command_line); |
| 232 invocation.suffix = suffix; |
| 233 invocation.flags = flags; |
| 234 invocations->push(invocation); |
212 } | 235 } |
213 | 236 |
214 base::CommandLine command_line(argv); | 237 DCHECK(!invocations->empty()); |
215 | 238 reporter_runner.Run(std::move(invocations), version); |
216 // Add the histogram suffix to the command-line as well, so that the | |
217 // reporter will add the same suffix to registry keys where it writes | |
218 // metrics. | |
219 if (!suffix.empty()) | |
220 command_line.AppendSwitchASCII("registry-suffix", suffix); | |
221 | |
222 auto invocation = | |
223 safe_browsing::SwReporterInvocation::FromCommandLine(command_line); | |
224 invocation.suffix = suffix; | |
225 invocation.is_experimental = true; | |
226 | |
227 reporter_runner.Run(invocation, version); | |
228 } | 239 } |
229 | 240 |
230 } // namespace | 241 } // namespace |
231 | 242 |
232 SwReporterInstallerTraits::SwReporterInstallerTraits( | 243 SwReporterInstallerTraits::SwReporterInstallerTraits( |
233 const SwReporterRunner& reporter_runner, | 244 const SwReporterRunner& reporter_runner, |
234 bool is_experimental_engine_supported) | 245 bool is_experimental_engine_supported) |
235 : reporter_runner_(reporter_runner), | 246 : reporter_runner_(reporter_runner), |
236 is_experimental_engine_supported_(is_experimental_engine_supported) {} | 247 is_experimental_engine_supported_(is_experimental_engine_supported) {} |
237 | 248 |
(...skipping 23 matching lines...) Expand all Loading... |
261 void SwReporterInstallerTraits::ComponentReady( | 272 void SwReporterInstallerTraits::ComponentReady( |
262 const base::Version& version, | 273 const base::Version& version, |
263 const base::FilePath& install_dir, | 274 const base::FilePath& install_dir, |
264 std::unique_ptr<base::DictionaryValue> manifest) { | 275 std::unique_ptr<base::DictionaryValue> manifest) { |
265 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 276 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
266 const base::FilePath exe_path(install_dir.Append(kSwReporterExeName)); | 277 const base::FilePath exe_path(install_dir.Append(kSwReporterExeName)); |
267 if (IsExperimentalEngineEnabled()) { | 278 if (IsExperimentalEngineEnabled()) { |
268 RunExperimentalSwReporter(exe_path, version, std::move(manifest), | 279 RunExperimentalSwReporter(exe_path, version, std::move(manifest), |
269 reporter_runner_); | 280 reporter_runner_); |
270 } else { | 281 } else { |
271 reporter_runner_.Run( | 282 auto invocations = std::make_unique<safe_browsing::SwReporterQueue>(); |
272 safe_browsing::SwReporterInvocation::FromFilePath(exe_path), version); | 283 invocations->push( |
| 284 safe_browsing::SwReporterInvocation::FromFilePath(exe_path)); |
| 285 reporter_runner_.Run(std::move(invocations), version); |
273 } | 286 } |
274 } | 287 } |
275 | 288 |
276 base::FilePath SwReporterInstallerTraits::GetRelativeInstallDir() const { | 289 base::FilePath SwReporterInstallerTraits::GetRelativeInstallDir() const { |
277 return base::FilePath(FILE_PATH_LITERAL("SwReporter")); | 290 return base::FilePath(FILE_PATH_LITERAL("SwReporter")); |
278 } | 291 } |
279 | 292 |
280 void SwReporterInstallerTraits::GetHash(std::vector<uint8_t>* hash) const { | 293 void SwReporterInstallerTraits::GetHash(std::vector<uint8_t>* hash) const { |
281 DCHECK(hash); | 294 DCHECK(hash); |
282 hash->assign(kSha256Hash, kSha256Hash + sizeof(kSha256Hash)); | 295 hash->assign(kSha256Hash, kSha256Hash + sizeof(kSha256Hash)); |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
398 } | 411 } |
399 | 412 |
400 // The experiment is only enabled on x86. There's no way to check this in the | 413 // The experiment is only enabled on x86. There's no way to check this in the |
401 // variations config so we'll hard-code it. | 414 // variations config so we'll hard-code it. |
402 const bool is_experimental_engine_supported = | 415 const bool is_experimental_engine_supported = |
403 base::win::OSInfo::GetInstance()->architecture() == | 416 base::win::OSInfo::GetInstance()->architecture() == |
404 base::win::OSInfo::X86_ARCHITECTURE; | 417 base::win::OSInfo::X86_ARCHITECTURE; |
405 | 418 |
406 // Install the component. | 419 // Install the component. |
407 std::unique_ptr<ComponentInstallerTraits> traits( | 420 std::unique_ptr<ComponentInstallerTraits> traits( |
408 new SwReporterInstallerTraits(base::Bind(&RunSwReporterAfterStartup), | 421 new SwReporterInstallerTraits(base::Bind(&RunSwReportersAfterStartup), |
409 is_experimental_engine_supported)); | 422 is_experimental_engine_supported)); |
410 // |cus| will take ownership of |installer| during installer->Register(cus). | 423 // |cus| will take ownership of |installer| during installer->Register(cus). |
411 DefaultComponentInstaller* installer = | 424 DefaultComponentInstaller* installer = |
412 new DefaultComponentInstaller(std::move(traits)); | 425 new DefaultComponentInstaller(std::move(traits)); |
413 installer->Register(cus, base::Closure()); | 426 installer->Register(cus, base::Closure()); |
414 } | 427 } |
415 | 428 |
416 void RegisterPrefsForSwReporter(PrefRegistrySimple* registry) { | 429 void RegisterPrefsForSwReporter(PrefRegistrySimple* registry) { |
417 registry->RegisterInt64Pref(prefs::kSwReporterLastTimeTriggered, 0); | 430 registry->RegisterInt64Pref(prefs::kSwReporterLastTimeTriggered, 0); |
418 registry->RegisterIntegerPref(prefs::kSwReporterLastExitCode, -1); | 431 registry->RegisterIntegerPref(prefs::kSwReporterLastExitCode, -1); |
419 registry->RegisterBooleanPref(prefs::kSwReporterPendingPrompt, false); | 432 registry->RegisterBooleanPref(prefs::kSwReporterPendingPrompt, false); |
420 } | 433 } |
421 | 434 |
422 void RegisterProfilePrefsForSwReporter( | 435 void RegisterProfilePrefsForSwReporter( |
423 user_prefs::PrefRegistrySyncable* registry) { | 436 user_prefs::PrefRegistrySyncable* registry) { |
424 registry->RegisterStringPref(prefs::kSwReporterPromptVersion, ""); | 437 registry->RegisterStringPref(prefs::kSwReporterPromptVersion, ""); |
425 | 438 |
426 registry->RegisterStringPref(prefs::kSwReporterPromptSeed, ""); | 439 registry->RegisterStringPref(prefs::kSwReporterPromptSeed, ""); |
427 } | 440 } |
428 | 441 |
429 } // namespace component_updater | 442 } // namespace component_updater |
OLD | NEW |