OLD | NEW |
---|---|
(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/metrics/sparse_histogram.h" | |
20 #include "base/path_service.h" | |
21 #include "base/prefs/pref_registry_simple.h" | |
22 #include "base/prefs/pref_service.h" | |
23 #include "base/process/kill.h" | |
24 #include "base/process/launch.h" | |
25 #include "base/task_runner_util.h" | |
26 #include "base/threading/worker_pool.h" | |
27 #include "base/values.h" | |
28 #include "base/win/registry.h" | |
29 #include "chrome/browser/browser_process.h" | |
30 #include "chrome/browser/component_updater/component_updater_service.h" | |
31 #include "chrome/common/chrome_paths.h" | |
32 #include "chrome/common/pref_names.h" | |
33 #include "content/public/browser/browser_thread.h" | |
34 | |
35 using content::BrowserThread; | |
36 | |
37 namespace component_updater { | |
38 | |
39 namespace { | |
40 | |
41 // These values are used to send UMA information and are replicated in the | |
42 // histograms.xml file, so the order MUST NOT CHANGE. | |
43 enum SwReporterUmaValue { | |
44 SW_REPORTER_START_DOWNLOAD = 0, | |
45 SW_REPORTER_RETRYING_DOWNLOAD, | |
46 SW_REPORTER_RETRIED_TOO_MANY_DOWNLOADS, | |
47 SW_REPORTER_START_EXECUTION, | |
48 SW_REPORTER_RETRYING_EXECUTION, | |
49 SW_REPORTER_RETRIED_TOO_MANY_EXECUTIONS, | |
50 SW_REPORTER_COMPONENT_UPDATE_ERROR, | |
51 SW_REPORTER_MAX, | |
52 }; | |
53 | |
54 // The maximum number of times to retry a download on startup. | |
55 const int kMaxRetry = 7; | |
56 | |
57 // CRX hash. The extension id is: gkmgaooipdjhmangpemjhigmamcehddo. | |
58 // hashlib.sha256().update(open(".crx").read()[16:16+294]).digest(). | |
cpu_(ooo_6.6-7.5)
2014/06/18 01:58:36
a comment with python in a c++ file, we must go de
MAD
2014/06/18 14:49:53
Yeah, it took me a while to figure out how these v
| |
59 const uint8 kSha2Hash[] = {0x6a, 0xc6, 0x0e, 0xe8, 0xf3, 0x97, 0xc0, 0xd6, | |
60 0xf4, 0xc9, 0x78, 0x6c, 0x0c, 0x24, 0x73, 0x3e, | |
61 0x05, 0xa5, 0x62, 0x4b, 0x2e, 0xc7, 0xb7, 0x1c, | |
62 0x5f, 0xea, 0xf0, 0x88, 0xf6, 0x97, 0x9b, 0xc7}; | |
63 | |
64 const char kSwReporterManifestName[] = "Software Reporter Tool"; | |
65 | |
66 const base::FilePath::CharType kSwReporterExeName[] = | |
67 FILE_PATH_LITERAL("software_reporter_tool.exe"); | |
68 const base::FilePath::CharType kSwReporterBaseDirectory[] = | |
69 FILE_PATH_LITERAL("SwReporter"); | |
70 | |
71 // If we don't have a SwReporter component, this is the version we claim. | |
72 const char kNullVersion[] = "0.0.0.0"; | |
73 | |
74 // Where to fetch the reporter exit code in the registry. | |
75 const wchar_t kSoftwareRemovalToolRegistryKey[] = | |
76 L"Software\\Google\\Software Removal Tool"; | |
77 const wchar_t kExitCodeRegistryValueName[] = L"ExitCode"; | |
78 | |
79 void ReportSwReporterStep(SwReporterUmaValue value) { | |
80 UMA_HISTOGRAM_ENUMERATION( | |
81 "ComponentUpdater.SwReporterStep", value, SW_REPORTER_MAX); | |
82 } | |
83 | |
84 // This function is called on the UI thread to report the SwReporter exit code | |
85 // and then to clear it from the registry as well as clear the execution state | |
86 // from the local state. | |
87 void ReportAndClearSwReporterExitCode(int exit_code) { | |
88 UMA_HISTOGRAM_SPARSE_SLOWLY("ComponentUpdater.SwReporterExitCode", exit_code); | |
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. | |
101 int WaitForProcessTermination(base::ProcessHandle process_handle) { | |
102 int exit_code = -1; | |
103 bool success = base::WaitForExitCode(process_handle, &exit_code); | |
104 DCHECK(success); | |
105 ::CloseHandle(process_handle); | |
106 process_handle = NULL; | |
107 return exit_code; | |
108 } | |
109 | |
110 void MaybeExecuteReporter(const base::FilePath& new_version_path, | |
111 PrefService* prefs) { | |
112 // If we get here without a register or an execute try count, this means that | |
113 // the component have been silently updated to a new version, so we don't want | |
114 // to execute. | |
115 int execute_try_count = | |
116 prefs->GetInteger(prefs::kSwReporterComponentExecuteTryCount); | |
117 if (execute_try_count == 0 && | |
118 prefs->GetInteger(prefs::kSwReporterComponentRegisterTryCount) == 0) { | |
119 return; | |
120 } | |
121 | |
122 // We're done with the registration so clear the number of retries, | |
123 prefs->SetInteger(prefs::kSwReporterComponentRegisterTryCount, 0); | |
124 | |
125 ReportSwReporterStep(execute_try_count ? SW_REPORTER_RETRYING_EXECUTION | |
126 : SW_REPORTER_START_EXECUTION); | |
127 prefs->SetInteger(prefs::kSwReporterComponentExecuteTryCount, | |
128 execute_try_count + 1); | |
129 | |
130 // Installation is done. Now execute the reporter. | |
131 base::CommandLine reporter_command_line( | |
132 new_version_path.Append(kSwReporterExeName)); | |
133 base::ProcessHandle scan_reporter_process = NULL; | |
134 bool success = base::LaunchProcess( | |
135 reporter_command_line, base::LaunchOptions(), &scan_reporter_process); | |
136 if (success) { | |
137 success = base::PostTaskAndReplyWithResult( | |
138 base::WorkerPool::GetTaskRunner(true), | |
139 FROM_HERE, | |
140 base::Bind(&WaitForProcessTermination, scan_reporter_process), | |
141 base::Bind(&ReportAndClearSwReporterExitCode)); | |
142 // WaitForProcessTermination will close the handle. | |
143 scan_reporter_process = NULL; | |
144 } else { | |
145 NOTREACHED() << "Could not execute from " << new_version_path.value(); | |
146 } | |
147 } | |
148 | |
149 // The base directory on windows looks like: | |
150 // <profile>\AppData\Local\Google\Chrome\User Data\SwReporter\. | |
151 base::FilePath GetSwReporterBaseDirectory() { | |
152 base::FilePath result; | |
153 PathService::Get(chrome::DIR_USER_DATA, &result); | |
154 return result.Append(kSwReporterBaseDirectory); | |
155 } | |
156 | |
157 // SwReporter has version encoded in the path itself so we need to enumerate the | |
158 // directories to find the full path. On success it returns something like: | |
159 // <profile>\AppData\Local\Google\Chrome\User Data\SwReporter\1.0.1.5\. | |
160 bool GetLatestSwReporterDirectory(base::FilePath* latest_version_path, | |
161 Version* latest_version, | |
162 std::vector<base::FilePath>* older_dirs) { | |
163 base::FilePath base_dir = GetSwReporterBaseDirectory(); | |
164 bool found = false; | |
165 base::FileEnumerator file_enumerator( | |
166 base_dir, false, base::FileEnumerator::DIRECTORIES); | |
167 for (base::FilePath path = file_enumerator.Next(); !path.value().empty(); | |
168 path = file_enumerator.Next()) { | |
169 Version version(path.BaseName().MaybeAsASCII()); | |
170 if (!version.IsValid()) | |
171 continue; | |
172 if (version.CompareTo(*latest_version) > 0 && | |
173 base::PathExists(path.Append(kSwReporterExeName))) { | |
174 if (found && older_dirs) | |
175 older_dirs->push_back(*latest_version_path); | |
176 *latest_version = version; | |
177 *latest_version_path = path; | |
178 found = true; | |
179 } else { | |
180 if (older_dirs) | |
181 older_dirs->push_back(path); | |
182 } | |
183 } | |
184 return found; | |
185 } | |
186 | |
187 class SwReporterComponentInstaller : public ComponentInstaller { | |
188 public: | |
189 explicit SwReporterComponentInstaller(const Version& version, | |
190 PrefService* prefs); | |
191 virtual ~SwReporterComponentInstaller() {} | |
192 | |
193 // ComponentInstaller overrides. | |
194 virtual void OnUpdateError(int error) OVERRIDE; | |
195 virtual bool Install(const base::DictionaryValue& manifest, | |
196 const base::FilePath& unpack_path) OVERRIDE; | |
197 virtual bool GetInstalledFile(const std::string& file, | |
198 base::FilePath* installed_file) OVERRIDE; | |
199 | |
200 private: | |
201 Version current_version_; | |
202 PrefService* prefs_; | |
203 }; | |
204 | |
205 SwReporterComponentInstaller::SwReporterComponentInstaller( | |
206 const Version& version, | |
207 PrefService* prefs) | |
208 : current_version_(version), prefs_(prefs) { | |
209 DCHECK(version.IsValid()); | |
210 DCHECK(prefs_); | |
211 } | |
212 | |
213 void SwReporterComponentInstaller::OnUpdateError(int error) { | |
214 ReportSwReporterStep(SW_REPORTER_COMPONENT_UPDATE_ERROR); | |
215 NOTREACHED() << "SwReporter update error: " << error; | |
216 } | |
217 | |
218 bool SwReporterComponentInstaller::Install( | |
219 const base::DictionaryValue& manifest, | |
220 const base::FilePath& unpack_path) { | |
221 std::string name; | |
222 manifest.GetStringASCII("name", &name); | |
223 if (name != kSwReporterManifestName) | |
224 return false; | |
225 | |
226 std::string proposed_version; | |
227 manifest.GetStringASCII("version", &proposed_version); | |
228 Version new_version(proposed_version.c_str()); | |
229 if (!new_version.IsValid()) | |
230 return false; | |
231 | |
232 base::FilePath sw_reporter_path(GetSwReporterBaseDirectory()); | |
233 base::FilePath new_version_path( | |
234 sw_reporter_path.AppendASCII(proposed_version)); | |
235 if (current_version_.CompareTo(new_version) < 0) { | |
236 // If the latest version identified is current_version_, then | |
237 // new_version_path should not exist. | |
238 DCHECK(!base::PathExists(new_version_path)); | |
239 if (!base::PathExists(unpack_path.Append(kSwReporterExeName)) || | |
240 !base::Move(unpack_path, new_version_path)) { | |
241 PLOG(ERROR) << "Couldn't move files to new_version_path.\n" | |
242 << unpack_path.value() << " -> " << new_version_path.value(); | |
243 return false; | |
244 } | |
245 } else { | |
246 // We should never get an older version than the current one. | |
247 DCHECK(current_version_.CompareTo(new_version) == 0); | |
248 if (!base::PathExists(new_version_path.Append(kSwReporterExeName))) { | |
249 PLOG(INFO) << "existing version_path doesn't contain file."; | |
250 return false; | |
251 } | |
252 } | |
253 | |
254 // Installation is over, now check if we have a pending execution. | |
255 BrowserThread::PostTask( | |
cpu_(ooo_6.6-7.5)
2014/06/18 01:58:36
in general this is kind of weird, I would expect t
MAD
2014/06/18 14:49:53
We use base::LaunchProcess, I was surprised that i
| |
256 BrowserThread::UI, | |
257 FROM_HERE, | |
258 base::Bind(&MaybeExecuteReporter, new_version_path, prefs_)); | |
259 return true; | |
260 } | |
261 | |
262 bool SwReporterComponentInstaller::GetInstalledFile( | |
263 const std::string& file, | |
264 base::FilePath* installed_file) { | |
265 return false; | |
266 } | |
267 | |
268 // Complete the registration of the component on the UI thread... | |
269 void RegisterSwReporterComponent(ComponentUpdateService* cus, | |
270 const Version& version, | |
271 PrefService* prefs) { | |
272 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
273 | |
274 CrxComponent SwReporter; | |
275 SwReporter.name = "SwReporter"; | |
276 SwReporter.installer = new SwReporterComponentInstaller(version, prefs); | |
277 SwReporter.version = version; | |
278 SwReporter.pk_hash.assign(kSha2Hash, &kSha2Hash[sizeof(kSha2Hash)]); | |
279 if (cus->RegisterComponent(SwReporter) == ComponentUpdateService::kError) | |
280 NOTREACHED() << "SwReporter component registration fail"; | |
281 } | |
282 | |
283 // Check if there already is a version of SwReporter installed, and if so | |
284 // register it. | |
285 void ExecuteSwReporterComponentOnFileThread(ComponentUpdateService* cus, | |
286 PrefService* prefs) { | |
287 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
288 base::FilePath base_dir = GetSwReporterBaseDirectory(); | |
289 if (!base::PathExists(base_dir)) { | |
290 if (!base::CreateDirectory(base_dir)) { | |
291 NOTREACHED() << "Could not create SwReporter directory."; | |
cpu_(ooo_6.6-7.5)
2014/06/18 01:58:36
I would like to see a bunch of NOTREACHED gone and
MAD
2014/06/18 14:49:53
Done.
| |
292 return; | |
293 } | |
294 } | |
295 | |
296 base::FilePath latest_version_path; | |
297 Version latest_version(kNullVersion); | |
cpu_(ooo_6.6-7.5)
2014/06/18 01:58:36
this is the only use of kNullVersion, feel free to
MAD
2014/06/18 14:49:53
Done.
| |
298 std::vector<base::FilePath> older_dirs; | |
299 if (!GetLatestSwReporterDirectory( | |
300 &latest_version_path, &latest_version, &older_dirs)) { | |
301 BrowserThread::PostTask( | |
302 BrowserThread::UI, | |
303 FROM_HERE, | |
304 base::Bind(&RegisterSwReporterComponent, cus, latest_version, prefs)); | |
305 } else { | |
306 BrowserThread::PostTask( | |
307 BrowserThread::UI, | |
308 FROM_HERE, | |
309 base::Bind(&MaybeExecuteReporter, latest_version_path, prefs)); | |
310 } | |
311 // Remove older versions of SwReporter. | |
cpu_(ooo_6.6-7.5)
2014/06/18 01:58:36
Consider firing this deletion on the threadpool or
MAD
2014/06/18 14:49:53
This code is gone now..
Note that this was inpire
| |
312 for (std::vector<base::FilePath>::iterator iter = older_dirs.begin(); | |
313 iter != older_dirs.end(); | |
314 ++iter) { | |
315 base::DeleteFile(*iter, true); | |
cpu_(ooo_6.6-7.5)
2014/06/18 01:58:36
funny enough, this delete is probably going to fai
MAD
2014/06/18 14:49:53
That code is gone now...
| |
316 } | |
317 } | |
318 | |
319 } // namespace | |
320 | |
321 void ExecuteSwReporterComponent(ComponentUpdateService* cus, | |
322 PrefService* prefs) { | |
323 int tries = prefs->GetInteger(prefs::kSwReporterComponentRegisterTryCount); | |
324 if (tries > kMaxRetry) { | |
325 ReportSwReporterStep(SW_REPORTER_RETRIED_TOO_MANY_DOWNLOADS); | |
326 // Let's forget about this attempt. | |
327 prefs->SetInteger(prefs::kSwReporterComponentRegisterTryCount, 0); | |
328 return; | |
329 } | |
cpu_(ooo_6.6-7.5)
2014/06/18 01:58:36
I am confused about the usage of Maybe in the func
MAD
2014/06/18 14:49:53
Done.
| |
330 | |
331 ReportSwReporterStep(tries ? SW_REPORTER_RETRYING_DOWNLOAD | |
332 : SW_REPORTER_START_DOWNLOAD); | |
333 prefs->SetInteger(prefs::kSwReporterComponentRegisterTryCount, tries + 1); | |
334 BrowserThread::PostTask( | |
335 BrowserThread::FILE, | |
336 FROM_HERE, | |
337 base::Bind(&ExecuteSwReporterComponentOnFileThread, cus, prefs)); | |
338 } | |
339 | |
340 void MaybeExecuteSwReporterComponent(ComponentUpdateService* cus, | |
341 PrefService* prefs) { | |
342 // Only register if we have a pending registration / execution. | |
343 if (prefs->GetInteger(prefs::kSwReporterComponentRegisterTryCount) == 0) { | |
344 if (prefs->GetInteger(prefs::kSwReporterComponentExecuteTryCount) == 0) | |
345 return; | |
346 // We have a pending execution, let's check if it completed or not. | |
347 base::win::RegKey srt_key( | |
348 HKEY_CURRENT_USER, kSoftwareRemovalToolRegistryKey, KEY_READ); | |
349 DWORD exit_code = -1; | |
350 if (srt_key.Valid() && | |
351 srt_key.ReadValueDW(kExitCodeRegistryValueName, &exit_code) == | |
352 ERROR_SUCCESS) { | |
353 ReportAndClearSwReporterExitCode(exit_code); | |
354 return; | |
355 } | |
356 | |
357 // If it didn't complete, let's make sure we didn't go beyond the maximum | |
358 // retry count yet. | |
359 if (prefs->GetInteger(prefs::kSwReporterComponentExecuteTryCount) > | |
360 kMaxRetry) { | |
361 ReportSwReporterStep(SW_REPORTER_RETRIED_TOO_MANY_EXECUTIONS); | |
362 // Let's forget about this attempt. | |
363 prefs->SetInteger(prefs::kSwReporterComponentExecuteTryCount, 0); | |
364 return; | |
365 } | |
366 } | |
367 | |
368 ExecuteSwReporterComponent(cus, prefs); | |
369 } | |
370 | |
371 void RegisterPrefsForSwReporterComponent(PrefRegistrySimple* registry) { | |
372 registry->RegisterIntegerPref(prefs::kSwReporterComponentRegisterTryCount, 0); | |
373 registry->RegisterIntegerPref(prefs::kSwReporterComponentExecuteTryCount, 0); | |
374 } | |
375 | |
376 } // namespace component_updater | |
OLD | NEW |