Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(698)

Side by Side Diff: chrome/browser/component_updater/sw_reporter_component_installer_win.cc

Issue 333193002: Adding a SW reporter component updater (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: CamelCase for histogram names Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #include "chrome/browser/component_updater/sw_reporter_component_installer_win.h "
6
7 #include <string>
8 #include <vector>
9
10 #include "base/base_paths.h"
11 #include "base/bind.h"
12 #include "base/bind_helpers.h"
13 #include "base/command_line.h"
14 #include "base/file_util.h"
15 #include "base/files/file_enumerator.h"
16 #include "base/files/file_path.h"
17 #include "base/logging.h"
18 #include "base/metrics/histogram.h"
19 #include "base/path_service.h"
20 #include "base/prefs/pref_registry_simple.h"
21 #include "base/prefs/pref_service.h"
22 #include "base/process/kill.h"
23 #include "base/process/launch.h"
24 #include "base/threading/worker_pool.h"
25 #include "base/values.h"
26 #include "base/win/registry.h"
27 #include "chrome/browser/browser_process.h"
28 #include "chrome/browser/component_updater/component_updater_service.h"
29 #include "chrome/common/chrome_paths.h"
30 #include "chrome/common/pref_names.h"
31 #include "content/public/browser/browser_thread.h"
32
33 using content::BrowserThread;
34
35 namespace component_updater {
36
37 namespace {
38
39 // UMA identifiers.
40 const char kSwReporterComponentHistogramName[] = "ComponentUpdater.SwReporter";
41 const char kSwReporterComponentExiCodeHistogramName[] =
42 "ComponentUpdater.SwReporterExitCode";
43 const int kMaxSwReporterExitCodeUMAValue = 100;
44
45 enum {
Alexei Svitkine (slow) 2014/06/16 21:50:05 Name this enum.
MAD 2014/06/17 01:03:37 Done.
46 SW_REPORTER_START_DOWNLOAD = 0,
47 SW_REPORTER_RETRYING_DOWNLOAD,
48 SW_REPORTER_RETRIED_TOO_MANY_DOWNLOADS,
49 SW_REPORTER_START_EXECUTION,
50 SW_REPORTER_RETRYING_EXECUTION,
51 SW_REPORTER_RETRIED_TOO_MANY_EXECUTIONS,
52 SW_REPORTER_COMPONENT_UPDATE_ERROR,
53 SW_REPORTER_MAX,
54 };
Alexei Svitkine (slow) 2014/06/16 21:50:05 Add a comment that these are used in an UMA histog
MAD 2014/06/17 01:03:37 Done.
55
56 // The maximum number of times to retry a download on startup.
57 const int kMaxRetry = 7;
58
59 // CRX hash. The extension id is: gkmgaooipdjhmangpemjhigmamcehddo.
60 // hashlib.sha256().update(open(".crx").read()[16:16+294]).digest().
61 const uint8 kSha2Hash[] = {0x6a, 0xc6, 0x0e, 0xe8, 0xf3, 0x97, 0xc0, 0xd6,
62 0xf4, 0xc9, 0x78, 0x6c, 0x0c, 0x24, 0x73, 0x3e,
63 0x05, 0xa5, 0x62, 0x4b, 0x2e, 0xc7, 0xb7, 0x1c,
64 0x5f, 0xea, 0xf0, 0x88, 0xf6, 0x97, 0x9b, 0xc7};
65
66 const char kSwReporterManifestName[] = "Software Reporter Tool";
67
68 const base::FilePath::CharType kSwReporterExeName[] =
69 FILE_PATH_LITERAL("software_reporter_tool.exe");
70 const base::FilePath::CharType kSwReporterBaseDirectory[] =
71 FILE_PATH_LITERAL("SwReporter");
72
73 // If we don't have a SwReporter component, this is the version we claim.
74 const char kNullVersion[] = "0.0.0.0";
75
76 // Where to fetch the reporter exit code in the registry.
77 const wchar_t kSoftwareRemovalToolRegistryKey[] =
78 L"Software\\Google\\Software Removal Tool";
79 const wchar_t kExitCodeRegistryValueName[] = L"ExitCode";
80
81 // This function is called on the UI thread to report the SwReporter exit code
82 // and then to clear it from the registry as well as clear the execution state
83 // from the local state.
84 void ReportAndClearSwReporterExitCode(int exit_code) {
85 DCHECK(exit_code < kMaxSwReporterExitCodeUMAValue);
86 UMA_HISTOGRAM_ENUMERATION(kSwReporterComponentExiCodeHistogramName,
Alexei Svitkine (slow) 2014/06/16 21:50:05 Use UMA_HISTOGRAM_SPARSE_SLOWLY() here, then you d
MAD 2014/06/17 01:03:37 Done.
87 exit_code,
88 kMaxSwReporterExitCodeUMAValue);
89
90 base::win::RegKey srt_key(
91 HKEY_CURRENT_USER, kSoftwareRemovalToolRegistryKey, KEY_WRITE);
92 srt_key.DeleteValue(kExitCodeRegistryValueName);
93
94 // Now that we are done we can reset the try count.
95 g_browser_process->local_state()->SetInteger(
96 prefs::kSwReporterComponentExecuteTryCount, 0);
97 }
98
99 // This function is called from a worker thread to wait for the SwReporter to
100 // complete and then collect it's exit code to be sent to
101 // ReportAndClearSwReporterExitCode on the UI thread.
102 void WaitForProcessTermination(base::ProcessHandle process_handle) {
103 int exit_code = -1;
104 bool success = base::WaitForExitCode(process_handle, &exit_code);
105 DCHECK(success);
106 BrowserThread::PostTask(
107 BrowserThread::UI,
108 FROM_HERE,
109 base::Bind(&ReportAndClearSwReporterExitCode, exit_code));
110 }
111
112 // The base directory on windows looks like:
113 // <profile>\AppData\Local\Google\Chrome\User Data\SwReporter\.
114 base::FilePath GetSwReporterBaseDirectory() {
115 base::FilePath result;
116 PathService::Get(chrome::DIR_USER_DATA, &result);
117 return result.Append(kSwReporterBaseDirectory);
118 }
119
120 // SwReporter has version encoded in the path itself so we need to enumerate the
121 // directories to find the full path. On success it returns something like:
122 // <profile>\AppData\Local\Google\Chrome\User Data\SwReporter\1.0.1.5\.
123 void GetLatestSwReporterDirectory(base::FilePath* result,
124 Version* latest,
125 std::vector<base::FilePath>* older_dirs) {
126 base::FilePath base_dir = GetSwReporterBaseDirectory();
127 bool found = false;
128 base::FileEnumerator file_enumerator(
129 base_dir, false, base::FileEnumerator::DIRECTORIES);
130 for (base::FilePath path = file_enumerator.Next(); !path.value().empty();
131 path = file_enumerator.Next()) {
132 Version version(path.BaseName().MaybeAsASCII());
133 if (!version.IsValid())
134 continue;
135 if (version.CompareTo(*latest) > 0 &&
136 base::PathExists(path.Append(kSwReporterExeName))) {
137 if (found && older_dirs)
138 older_dirs->push_back(*result);
139 *latest = version;
140 *result = path;
141 found = true;
142 } else {
143 if (older_dirs)
144 older_dirs->push_back(path);
145 }
146 }
147 }
148
149 class SwReporterComponentInstaller : public ComponentInstaller {
150 public:
151 explicit SwReporterComponentInstaller(const Version& version,
152 PrefService* prefs);
153 virtual ~SwReporterComponentInstaller() {}
154
155 // ComponentInstaller overrides.
156 virtual void OnUpdateError(int error) OVERRIDE;
157 virtual bool Install(const base::DictionaryValue& manifest,
158 const base::FilePath& unpack_path) OVERRIDE;
159 virtual bool GetInstalledFile(const std::string& file,
160 base::FilePath* installed_file) OVERRIDE;
161
162 private:
163 void UpdateInstallPrefsOnUIThread();
164
165 Version current_version_;
166 PrefService* prefs_;
167 };
168
169 SwReporterComponentInstaller::SwReporterComponentInstaller(
170 const Version& version,
171 PrefService* prefs)
172 : current_version_(version), prefs_(prefs) {
173 DCHECK(version.IsValid());
174 DCHECK(prefs_);
175 }
176
177 void SwReporterComponentInstaller::OnUpdateError(int error) {
178 UMA_HISTOGRAM_ENUMERATION(kSwReporterComponentHistogramName,
179 SW_REPORTER_COMPONENT_UPDATE_ERROR,
180 SW_REPORTER_MAX);
181 NOTREACHED() << "SwReporter update error: " << error;
182 }
183
184 bool SwReporterComponentInstaller::Install(
185 const base::DictionaryValue& manifest,
186 const base::FilePath& unpack_path) {
187 std::string name;
188 manifest.GetStringASCII("name", &name);
189 if (name != kSwReporterManifestName)
190 return false;
191
192 std::string proposed_version;
193 manifest.GetStringASCII("version", &proposed_version);
194 Version new_version(proposed_version.c_str());
195 if (!new_version.IsValid())
196 return false;
197
198 base::FilePath sw_reporter_path(GetSwReporterBaseDirectory());
199 base::FilePath new_version_path(
200 sw_reporter_path.AppendASCII(proposed_version));
201 if (current_version_.CompareTo(new_version) < 0) {
202 // If the latest version identified is current_version_, then
203 // new_version_path should not exist.
204 DCHECK(!base::PathExists(new_version_path));
205 if (!base::PathExists(unpack_path.Append(kSwReporterExeName)) ||
206 !base::Move(unpack_path, new_version_path)) {
207 PLOG(ERROR) << "Couldn't move files to new_version_path.\n"
208 << unpack_path.value() << " -> " << new_version_path.value();
209 return false;
210 }
211 } else {
212 // We should never get an older version than the current one.
213 DCHECK(current_version_.CompareTo(new_version) == 0);
214 if (!base::PathExists(new_version_path.Append(kSwReporterExeName))) {
215 PLOG(INFO) << "existing version_path doesn't contain file.";
216 return false;
217 }
218 }
219
220 // Passed the basic tests, update the prefs on the UI thread.
221 BrowserThread::PostTask(
222 BrowserThread::UI,
223 FROM_HERE,
224 base::Bind(&SwReporterComponentInstaller::UpdateInstallPrefsOnUIThread,
225 base::Unretained(this)));
226
227 // Installation is done. Now execute the reporter.
228 base::CommandLine reporter_command_line(
229 new_version_path.Append(kSwReporterExeName));
230 base::ProcessHandle scan_reporter_process = NULL;
231 bool success = base::LaunchProcess(
232 reporter_command_line, base::LaunchOptions(), &scan_reporter_process);
233 if (success) {
234 base::WorkerPool::PostTask(
Alexei Svitkine (slow) 2014/06/16 21:50:05 Nit: Consider using PostTaskAndReplyWithResult() h
MAD 2014/06/17 01:03:37 But this would only work if the tasked posted from
Alexei Svitkine (slow) 2014/06/17 15:01:52 You're right, sorry for the bad suggestion.
235 FROM_HERE,
236 base::Bind(&WaitForProcessTermination, scan_reporter_process),
237 true);
238 return true;
239 }
240 return false;
241 }
242
243 bool SwReporterComponentInstaller::GetInstalledFile(
244 const std::string& file,
245 base::FilePath* installed_file) {
246 return false;
247 }
248
249 void SwReporterComponentInstaller::UpdateInstallPrefsOnUIThread() {
250 // We're done with the registration so clean the number of retries,
251 prefs_->SetInteger(prefs::kSwReporterComponentRegisterTryCount, 0);
252
253 int tries = prefs_->GetInteger(prefs::kSwReporterComponentExecuteTryCount);
254 UMA_HISTOGRAM_ENUMERATION(
255 kSwReporterComponentHistogramName,
256 tries ? SW_REPORTER_RETRYING_EXECUTION : SW_REPORTER_START_EXECUTION,
257 SW_REPORTER_MAX);
258 prefs_->SetInteger(prefs::kSwReporterComponentExecuteTryCount, tries + 1);
259 }
260
261 // Complete the registration of the component on the UI thread...
262 void FinishSwReporterUpdateRegistration(ComponentUpdateService* cus,
263 const Version& version,
264 PrefService* prefs) {
265 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
266
267 CrxComponent SwReporter;
268 SwReporter.name = "SwReporter";
269 SwReporter.installer = new SwReporterComponentInstaller(version, prefs);
270 SwReporter.version = version;
271 SwReporter.pk_hash.assign(kSha2Hash, &kSha2Hash[sizeof(kSha2Hash)]);
272 if (cus->RegisterComponent(SwReporter) == ComponentUpdateService::kError)
273 NOTREACHED() << "SwReporter component registration fail";
274 }
275
276 // Check if there already is a version of SwReporter installed, and if so
277 // register it.
278 void RegisterSwReporterComponentOnFileThread(ComponentUpdateService* cus,
279 PrefService* prefs) {
280 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
281 base::FilePath path = GetSwReporterBaseDirectory();
282 if (!base::PathExists(path)) {
283 if (!base::CreateDirectory(path)) {
284 NOTREACHED() << "Could not create SwReporter directory.";
285 return;
286 }
287 }
288
289 Version version(kNullVersion);
290 std::vector<base::FilePath> older_dirs;
291 GetLatestSwReporterDirectory(&path, &version, &older_dirs);
292
293 BrowserThread::PostTask(
294 BrowserThread::UI,
295 FROM_HERE,
296 base::Bind(&FinishSwReporterUpdateRegistration, cus, version, prefs));
297
298 // Remove older versions of SwReporter.
299 for (std::vector<base::FilePath>::iterator iter = older_dirs.begin();
300 iter != older_dirs.end();
301 ++iter) {
302 base::DeleteFile(*iter, true);
303 }
304 }
305
306 } // namespace
307
308 void RegisterSwReporterComponent(ComponentUpdateService* cus,
309 PrefService* prefs) {
310 int tries = prefs->GetInteger(prefs::kSwReporterComponentRegisterTryCount);
311 if (tries > kMaxRetry) {
312 UMA_HISTOGRAM_ENUMERATION(kSwReporterComponentHistogramName,
313 SW_REPORTER_RETRIED_TOO_MANY_DOWNLOADS,
314 SW_REPORTER_MAX);
315 // Let's forget about this attempt.
316 prefs->SetInteger(prefs::kSwReporterComponentRegisterTryCount, 0);
317 return;
318 }
319
320 UMA_HISTOGRAM_ENUMERATION(
321 kSwReporterComponentHistogramName,
322 tries ? SW_REPORTER_RETRYING_DOWNLOAD : SW_REPORTER_START_DOWNLOAD,
323 SW_REPORTER_MAX);
324 prefs->SetInteger(prefs::kSwReporterComponentRegisterTryCount, tries + 1);
325 BrowserThread::PostTask(
326 BrowserThread::FILE,
327 FROM_HERE,
328 base::Bind(&RegisterSwReporterComponentOnFileThread, cus, prefs));
329 }
330
331 void MaybeRegisterSwReporterComponent(ComponentUpdateService* cus,
332 PrefService* prefs) {
333 // Only register if we have a pending registration / execution.
334 if (prefs->GetInteger(prefs::kSwReporterComponentRegisterTryCount) == 0) {
335 if (prefs->GetInteger(prefs::kSwReporterComponentExecuteTryCount) == 0)
336 return;
337 // We have a pending execution, let's check if it completed or not.
338 base::win::RegKey srt_key(
339 HKEY_CURRENT_USER, kSoftwareRemovalToolRegistryKey, KEY_READ);
340 DWORD exit_code = -1;
341 if (srt_key.Valid() &&
342 srt_key.ReadValueDW(kExitCodeRegistryValueName, &exit_code) ==
343 ERROR_SUCCESS) {
344 ReportAndClearSwReporterExitCode(exit_code);
345 return;
346 }
347
348 // If it didn't complete, let's make sure we didn't go beyond the maximum
349 // retry count yet.
350 if (prefs->GetInteger(prefs::kSwReporterComponentExecuteTryCount) >
351 kMaxRetry) {
352 UMA_HISTOGRAM_ENUMERATION(kSwReporterComponentHistogramName,
353 SW_REPORTER_RETRIED_TOO_MANY_EXECUTIONS,
354 SW_REPORTER_MAX);
Alexei Svitkine (slow) 2014/06/16 21:50:05 Nit: Make a function in the anon namespace that lo
MAD 2014/06/17 01:03:37 Done.
355 // Let's forget about this attempt.
356 prefs->SetInteger(prefs::kSwReporterComponentExecuteTryCount, 0);
357 return;
358 }
359 }
360
361 RegisterSwReporterComponent(cus, prefs);
362 }
363
364 void RegisterPrefsForSwReporterComponent(PrefRegistrySimple* registry) {
365 registry->RegisterIntegerPref(prefs::kSwReporterComponentRegisterTryCount, 0);
366 registry->RegisterIntegerPref(prefs::kSwReporterComponentExecuteTryCount, 0);
367 }
368
369 } // namespace component_updater
OLDNEW
« no previous file with comments | « chrome/browser/component_updater/sw_reporter_component_installer_win.h ('k') | chrome/browser/prefs/browser_prefs.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698