Update broken references to image assets
[chromium-blink-merge.git] / chrome / browser / component_updater / sw_reporter_installer_win.cc
blobad33c748df4b67fd90ab88661011d0daf6915659
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
3 // found in the LICENSE file.
5 #include "chrome/browser/component_updater/sw_reporter_installer_win.h"
7 #include <stdint.h>
9 #include <map>
10 #include <string>
11 #include <vector>
13 #include "base/base_paths.h"
14 #include "base/files/file_path.h"
15 #include "base/files/file_util.h"
16 #include "base/logging.h"
17 #include "base/metrics/histogram.h"
18 #include "base/metrics/sparse_histogram.h"
19 #include "base/path_service.h"
20 #include "base/prefs/pref_registry_simple.h"
21 #include "base/strings/string_number_conversions.h"
22 #include "base/strings/string_tokenizer.h"
23 #include "base/strings/utf_string_conversions.h"
24 #include "base/time/time.h"
25 #include "base/win/registry.h"
26 #include "chrome/browser/browser_process.h"
27 #include "chrome/browser/metrics/chrome_metrics_service_accessor.h"
28 #include "chrome/browser/safe_browsing/srt_fetcher_win.h"
29 #include "chrome/browser/safe_browsing/srt_field_trial_win.h"
30 #include "components/component_updater/component_updater_paths.h"
31 #include "components/component_updater/component_updater_service.h"
32 #include "components/component_updater/default_component_installer.h"
33 #include "components/component_updater/pref_names.h"
34 #include "components/pref_registry/pref_registry_syncable.h"
35 #include "components/rappor/rappor_service.h"
36 #include "components/update_client/update_client.h"
37 #include "components/update_client/utils.h"
38 #include "content/public/browser/browser_thread.h"
40 namespace component_updater {
42 namespace {
44 // 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.
46 enum SRTCompleted {
47 SRT_COMPLETED_NOT_YET = 0,
48 SRT_COMPLETED_YES = 1,
49 SRT_COMPLETED_LATER = 2,
50 SRT_COMPLETED_MAX,
53 // CRX hash. The extension id is: gkmgaooipdjhmangpemjhigmamcehddo. The hash was
54 // generated in Python with something like this:
55 // hashlib.sha256().update(open("<file>.crx").read()[16:16+294]).digest().
56 const uint8_t kSha256Hash[] = {0x6a, 0xc6, 0x0e, 0xe8, 0xf3, 0x97, 0xc0, 0xd6,
57 0xf4, 0xc9, 0x78, 0x6c, 0x0c, 0x24, 0x73, 0x3e,
58 0x05, 0xa5, 0x62, 0x4b, 0x2e, 0xc7, 0xb7, 0x1c,
59 0x5f, 0xea, 0xf0, 0x88, 0xf6, 0x97, 0x9b, 0xc7};
61 const base::FilePath::CharType kSwReporterExeName[] =
62 FILE_PATH_LITERAL("software_reporter_tool.exe");
64 // Where to fetch the reporter's list of found uws in the registry.
65 const wchar_t kSoftwareRemovalToolRegistryKey[] =
66 L"Software\\Google\\Software Removal Tool";
67 const wchar_t kCleanerSuffixRegistryKey[] = L"Cleaner";
68 const wchar_t kEndTimeValueName[] = L"EndTime";
69 const wchar_t kExitCodeValueName[] = L"ExitCode";
70 const wchar_t kFoundUwsValueName[] = L"FoundUws";
71 const wchar_t kStartTimeValueName[] = L"StartTime";
72 const wchar_t kUploadResultsValueName[] = L"UploadResults";
73 const wchar_t kVersionValueName[] = L"Version";
75 const char kFoundUwsMetricName[] = "SoftwareReporter.FoundUwS";
76 const char kFoundUwsReadErrorMetricName[] =
77 "SoftwareReporter.FoundUwSReadError";
79 void SRTHasCompleted(SRTCompleted value) {
80 UMA_HISTOGRAM_ENUMERATION("SoftwareReporter.Cleaner.HasCompleted", value,
81 SRT_COMPLETED_MAX);
84 void ReportVersionWithUma(const base::Version& version) {
85 DCHECK(!version.components().empty());
86 // The minor version is the 2nd last component of the version,
87 // or just the first component if there is only 1.
88 uint32_t minor_version = 0;
89 if (version.components().size() > 1)
90 minor_version = version.components()[version.components().size() - 2];
91 else
92 minor_version = version.components()[0];
93 UMA_HISTOGRAM_SPARSE_SLOWLY("SoftwareReporter.MinorVersion", minor_version);
95 // The major version for X.Y.Z is X*256^3+Y*256+Z. If there are additional
96 // components, only the first three count, and if there are less than 3, the
97 // missing values are just replaced by zero. So 1 is equivalent 1.0.0.
98 DCHECK_LT(version.components()[0], 0x100U);
99 uint32_t major_version = 0x1000000 * version.components()[0];
100 if (version.components().size() >= 2) {
101 DCHECK_LT(version.components()[1], 0x10000U);
102 major_version += 0x100 * version.components()[1];
104 if (version.components().size() >= 3) {
105 DCHECK_LT(version.components()[2], 0x100U);
106 major_version += version.components()[2];
108 UMA_HISTOGRAM_SPARSE_SLOWLY("SoftwareReporter.MajorVersion", major_version);
111 void ReportUploadsWithUma(const base::string16& upload_results) {
112 base::WStringTokenizer tokenizer(upload_results, L";");
113 int failure_count = 0;
114 int success_count = 0;
115 int longest_failure_run = 0;
116 int current_failure_run = 0;
117 bool last_result = false;
118 while (tokenizer.GetNext()) {
119 if (tokenizer.token() == L"0") {
120 ++failure_count;
121 ++current_failure_run;
122 last_result = false;
123 } else {
124 ++success_count;
125 current_failure_run = 0;
126 last_result = true;
129 if (current_failure_run > longest_failure_run)
130 longest_failure_run = current_failure_run;
133 UMA_HISTOGRAM_COUNTS_100("SoftwareReporter.UploadFailureCount",
134 failure_count);
135 UMA_HISTOGRAM_COUNTS_100("SoftwareReporter.UploadSuccessCount",
136 success_count);
137 UMA_HISTOGRAM_COUNTS_100("SoftwareReporter.UploadLongestFailureRun",
138 longest_failure_run);
139 UMA_HISTOGRAM_BOOLEAN("SoftwareReporter.LastUploadResult", last_result);
142 // Reports UwS found by the software reporter tool via UMA and RAPPOR.
143 void ReportFoundUwS() {
144 base::win::RegKey reporter_key(HKEY_CURRENT_USER,
145 kSoftwareRemovalToolRegistryKey,
146 KEY_QUERY_VALUE | KEY_SET_VALUE);
147 std::vector<base::string16> found_uws_strings;
148 if (reporter_key.Valid() &&
149 reporter_key.ReadValues(kFoundUwsValueName, &found_uws_strings) ==
150 ERROR_SUCCESS) {
151 rappor::RapporService* rappor_service = g_browser_process->rappor_service();
153 bool parse_error = false;
154 for (const base::string16& uws_string : found_uws_strings) {
155 // All UwS ids are expected to be integers.
156 uint32_t uws_id = 0;
157 if (base::StringToUint(uws_string, &uws_id)) {
158 UMA_HISTOGRAM_SPARSE_SLOWLY(kFoundUwsMetricName, uws_id);
159 if (rappor_service) {
160 rappor_service->RecordSample(kFoundUwsMetricName,
161 rappor::COARSE_RAPPOR_TYPE,
162 base::UTF16ToUTF8(uws_string));
164 } else {
165 parse_error = true;
169 // Clean up the old value.
170 reporter_key.DeleteValue(kFoundUwsValueName);
172 UMA_HISTOGRAM_BOOLEAN(kFoundUwsReadErrorMetricName, parse_error);
176 class SwReporterInstallerTraits : public ComponentInstallerTraits {
177 public:
178 SwReporterInstallerTraits() {}
180 ~SwReporterInstallerTraits() override {}
182 bool VerifyInstallation(const base::DictionaryValue& manifest,
183 const base::FilePath& dir) const override {
184 return base::PathExists(dir.Append(kSwReporterExeName));
187 bool CanAutoUpdate() const override { return true; }
189 bool OnCustomInstall(const base::DictionaryValue& manifest,
190 const base::FilePath& install_dir) override {
191 return true;
194 void ComponentReady(const base::Version& version,
195 const base::FilePath& install_dir,
196 scoped_ptr<base::DictionaryValue> manifest) override {
197 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
198 ReportVersionWithUma(version);
199 safe_browsing::RunSwReporter(install_dir.Append(kSwReporterExeName),
200 version.GetString());
203 base::FilePath GetBaseDirectory() const override { return install_dir(); }
205 void GetHash(std::vector<uint8_t>* hash) const override { GetPkHash(hash); }
207 std::string GetName() const override { return "Software Reporter Tool"; }
209 static base::FilePath install_dir() {
210 // The base directory on windows looks like:
211 // <profile>\AppData\Local\Google\Chrome\User Data\SwReporter\.
212 base::FilePath result;
213 PathService::Get(DIR_SW_REPORTER, &result);
214 return result;
217 static std::string ID() {
218 update_client::CrxComponent component;
219 component.version = Version("0.0.0.0");
220 GetPkHash(&component.pk_hash);
221 return update_client::GetCrxComponentID(component);
224 private:
225 static void GetPkHash(std::vector<uint8_t>* hash) {
226 DCHECK(hash);
227 hash->assign(kSha256Hash, kSha256Hash + sizeof(kSha256Hash));
231 } // namespace
233 void RegisterSwReporterComponent(ComponentUpdateService* cus) {
234 // The Sw reporter doesn't need to run if the user isn't reporting metrics and
235 // isn't in the SRTPrompt field trial "On" group.
236 if (!ChromeMetricsServiceAccessor::IsMetricsReportingEnabled() &&
237 !safe_browsing::IsInSRTPromptFieldTrialGroups()) {
238 return;
241 // Check if we have information from Cleaner and record UMA statistics.
242 base::string16 cleaner_key_name(kSoftwareRemovalToolRegistryKey);
243 cleaner_key_name.append(1, L'\\').append(kCleanerSuffixRegistryKey);
244 base::win::RegKey cleaner_key(
245 HKEY_CURRENT_USER, cleaner_key_name.c_str(), KEY_ALL_ACCESS);
246 // Cleaner is assumed to have run if we have a start time.
247 if (cleaner_key.Valid()) {
248 if (cleaner_key.HasValue(kStartTimeValueName)) {
249 // Get version number.
250 if (cleaner_key.HasValue(kVersionValueName)) {
251 DWORD version;
252 cleaner_key.ReadValueDW(kVersionValueName, &version);
253 UMA_HISTOGRAM_SPARSE_SLOWLY("SoftwareReporter.Cleaner.Version",
254 version);
255 cleaner_key.DeleteValue(kVersionValueName);
257 // Get start & end time. If we don't have an end time, we can assume the
258 // cleaner has not completed.
259 int64 start_time_value;
260 cleaner_key.ReadInt64(kStartTimeValueName, &start_time_value);
262 bool completed = cleaner_key.HasValue(kEndTimeValueName);
263 SRTHasCompleted(completed ? SRT_COMPLETED_YES : SRT_COMPLETED_NOT_YET);
264 if (completed) {
265 int64 end_time_value;
266 cleaner_key.ReadInt64(kEndTimeValueName, &end_time_value);
267 cleaner_key.DeleteValue(kEndTimeValueName);
268 base::TimeDelta run_time(
269 base::Time::FromInternalValue(end_time_value) -
270 base::Time::FromInternalValue(start_time_value));
271 UMA_HISTOGRAM_LONG_TIMES("SoftwareReporter.Cleaner.RunningTime",
272 run_time);
274 // Get exit code. Assume nothing was found if we can't read the exit code.
275 DWORD exit_code = safe_browsing::kSwReporterNothingFound;
276 if (cleaner_key.HasValue(kExitCodeValueName)) {
277 cleaner_key.ReadValueDW(kExitCodeValueName, &exit_code);
278 UMA_HISTOGRAM_SPARSE_SLOWLY("SoftwareReporter.Cleaner.ExitCode",
279 exit_code);
280 cleaner_key.DeleteValue(kExitCodeValueName);
282 cleaner_key.DeleteValue(kStartTimeValueName);
284 if (exit_code == safe_browsing::kSwReporterPostRebootCleanupNeeded ||
285 exit_code ==
286 safe_browsing::kSwReporterDelayedPostRebootCleanupNeeded) {
287 // Check if we are running after the user has rebooted.
288 base::TimeDelta elapsed(
289 base::Time::Now() -
290 base::Time::FromInternalValue(start_time_value));
291 DCHECK_GT(elapsed.InMilliseconds(), 0);
292 UMA_HISTOGRAM_BOOLEAN(
293 "SoftwareReporter.Cleaner.HasRebooted",
294 static_cast<uint64>(elapsed.InMilliseconds()) > ::GetTickCount());
297 if (cleaner_key.HasValue(kUploadResultsValueName)) {
298 base::string16 upload_results;
299 cleaner_key.ReadValue(kUploadResultsValueName, &upload_results);
300 ReportUploadsWithUma(upload_results);
302 } else {
303 if (cleaner_key.HasValue(kEndTimeValueName)) {
304 SRTHasCompleted(SRT_COMPLETED_LATER);
305 cleaner_key.DeleteValue(kEndTimeValueName);
309 ReportFoundUwS();
311 // Install the component.
312 scoped_ptr<ComponentInstallerTraits> traits(new SwReporterInstallerTraits());
313 // |cus| will take ownership of |installer| during installer->Register(cus).
314 DefaultComponentInstaller* installer =
315 new DefaultComponentInstaller(traits.Pass());
316 installer->Register(cus, base::Closure());
319 void RegisterPrefsForSwReporter(PrefRegistrySimple* registry) {
320 registry->RegisterInt64Pref(prefs::kSwReporterLastTimeTriggered, 0);
321 registry->RegisterIntegerPref(prefs::kSwReporterLastExitCode, -1);
324 void RegisterProfilePrefsForSwReporter(
325 user_prefs::PrefRegistrySyncable* registry) {
326 registry->RegisterStringPref(prefs::kSwReporterPromptVersion, "");
328 registry->RegisterStringPref(prefs::kSwReporterPromptSeed, "");
331 } // namespace component_updater