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_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_path.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/prefs/pref_service.h" | |
22 #include "base/process/kill.h" | |
23 #include "base/process/launch.h" | |
24 #include "base/task_runner_util.h" | |
25 #include "base/threading/worker_pool.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/browser/component_updater/component_updater_utils.h" | |
30 #include "chrome/browser/component_updater/default_component_installer.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_EXPLICIT_REQUEST = 0, | |
45 SW_REPORTER_STARTUP_RETRY = 1, | |
46 SW_REPORTER_RETRIED_TOO_MANY_TIMES = 2, | |
47 SW_REPORTER_START_EXECUTION = 3, | |
48 SW_REPORTER_FAILED_TO_START = 4, | |
49 SW_REPORTER_REGISTRY_EXIT_CODE = 5, | |
50 SW_REPORTER_MAX, | |
51 }; | |
52 | |
53 // The maximum number of times to retry a download on startup. | |
54 const int kMaxRetry = 7; | |
55 | |
56 // CRX hash. The extension id is: gkmgaooipdjhmangpemjhigmamcehddo. The hash was | |
57 // generated in Python with something like this: | |
58 // hashlib.sha256().update(open("<file>.crx").read()[16:16+294]).digest(). | |
59 const uint8 kSha256Hash[] = {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 base::FilePath::CharType kSwReporterExeName[] = | |
65 FILE_PATH_LITERAL("software_reporter_tool.exe"); | |
66 | |
67 // Where to fetch the reporter exit code in the registry. | |
68 const wchar_t kSoftwareRemovalToolRegistryKey[] = | |
69 L"Software\\Google\\Software Removal Tool"; | |
70 const wchar_t kExitCodeRegistryValueName[] = L"ExitCode"; | |
71 | |
72 void ReportUmaStep(SwReporterUmaValue value) { | |
73 UMA_HISTOGRAM_ENUMERATION("SoftwareReporter.Step", value, SW_REPORTER_MAX); | |
74 } | |
75 | |
76 // This function is called on the UI thread to report the SwReporter exit code | |
77 // and then clear it from the registry as well as clear the execution state | |
78 // from the local state. This could be called from an interruptible worker | |
79 // thread so should be resilient to unexpected shutdown. | |
80 void ReportAndClearExitCode(int exit_code) { | |
81 UMA_HISTOGRAM_SPARSE_SLOWLY("SoftwareReporter.ExitCode", exit_code); | |
82 | |
83 base::win::RegKey srt_key( | |
84 HKEY_CURRENT_USER, kSoftwareRemovalToolRegistryKey, KEY_WRITE); | |
85 srt_key.DeleteValue(kExitCodeRegistryValueName); | |
86 | |
87 // Now that we are done we can reset the try count. | |
88 g_browser_process->local_state()->SetInteger( | |
89 prefs::kSwReporterExecuteTryCount, 0); | |
90 } | |
91 | |
92 // This function is called from a worker thread to launch the SwReporter and | |
93 // wait for termination to collect its exit code. This task could be interrupted | |
94 // by a shutdown at anytime, so it shouldn't depend on anything external that | |
95 // could be shutdown beforehand. | |
96 void LaunchAndWaitForExit(const base::FilePath& exe_path) { | |
97 const base::CommandLine reporter_command_line(exe_path); | |
98 base::ProcessHandle scan_reporter_process = base::kNullProcessHandle; | |
99 if (!base::LaunchProcess(reporter_command_line, | |
100 base::LaunchOptions(), | |
101 &scan_reporter_process)) { | |
102 ReportUmaStep(SW_REPORTER_FAILED_TO_START); | |
103 return; | |
104 } | |
105 ReportUmaStep(SW_REPORTER_START_EXECUTION); | |
106 | |
107 int exit_code = -1; | |
108 bool success = base::WaitForExitCode(scan_reporter_process, &exit_code); | |
109 DCHECK(success); | |
110 base::CloseProcessHandle(scan_reporter_process); | |
111 scan_reporter_process = base::kNullProcessHandle; | |
112 // It's OK if this doesn't complete, the work will continue on next startup. | |
113 BrowserThread::PostTask(BrowserThread::UI, | |
114 FROM_HERE, | |
115 base::Bind(&ReportAndClearExitCode, exit_code)); | |
116 } | |
117 | |
118 void ExecuteReporter(const base::FilePath& install_dir) { | |
119 base::WorkerPool::PostTask( | |
120 FROM_HERE, | |
121 base::Bind(&LaunchAndWaitForExit, install_dir.Append(kSwReporterExeName)), | |
122 true); | |
123 } | |
124 | |
125 class SwReporterInstallerTraits : public ComponentInstallerTraits { | |
126 public: | |
127 explicit SwReporterInstallerTraits(PrefService* prefs) : prefs_(prefs) {} | |
128 | |
129 virtual ~SwReporterInstallerTraits() {} | |
130 | |
131 virtual bool VerifyInstallation(const base::FilePath& dir) const { | |
132 return base::PathExists(dir.Append(kSwReporterExeName)); | |
133 } | |
134 | |
135 virtual bool CanAutoUpdate() const { return true; } | |
136 | |
137 virtual bool OnCustomInstall(const base::DictionaryValue& manifest, | |
138 const base::FilePath& install_dir) { | |
139 return true; | |
140 } | |
141 | |
142 virtual void ComponentReady(const base::Version& version, | |
143 const base::FilePath& install_dir, | |
144 scoped_ptr<base::DictionaryValue> manifest) { | |
145 wcsncpy_s(version_dir_, | |
146 _MAX_PATH, | |
147 install_dir.value().c_str(), | |
148 install_dir.value().size()); | |
149 // Only execute the reporter if there is still a pending request for it.. | |
150 if (prefs_->GetInteger(prefs::kSwReporterExecuteTryCount)) | |
151 ExecuteReporter(install_dir); | |
152 } | |
153 | |
154 virtual base::FilePath GetBaseDirectory() const { return install_dir(); } | |
155 | |
156 virtual void GetHash(std::vector<uint8>* hash) const { GetPkHash(hash); } | |
157 | |
158 virtual std::string GetName() const { return "Software Reporter Tool"; } | |
159 | |
160 static const base::FilePath install_dir() { | |
Sorin Jianu
2014/06/20 05:37:38
This const not needed on the return value.
MAD
2014/06/20 14:40:15
Done.
| |
161 // The base directory on windows looks like: | |
162 // <profile>\AppData\Local\Google\Chrome\User Data\SwReporter\. | |
163 base::FilePath result; | |
164 PathService::Get(chrome::DIR_USER_DATA, &result); | |
165 return result.Append(FILE_PATH_LITERAL("SwReporter")); | |
166 } | |
167 | |
168 static const std::string ID() { | |
Sorin Jianu
2014/06/20 05:37:38
This const is not needed here.
MAD
2014/06/20 14:40:15
Done.
| |
169 CrxComponent component; | |
170 component.version = Version("0.0.0.0"); | |
171 GetPkHash(&component.pk_hash); | |
172 return component_updater::GetCrxComponentID(component); | |
Sorin Jianu
2014/06/20 05:37:38
I will provide a function to convert a component h
MAD
2014/06/20 14:40:15
Thanks!
| |
173 } | |
174 | |
175 static const base::FilePath VersionPath() { | |
Sorin Jianu
2014/06/20 05:37:38
const not needed.
MAD
2014/06/20 14:40:15
Done.
| |
176 return base::FilePath(version_dir_); | |
177 } | |
178 | |
179 private: | |
180 static void GetPkHash(std::vector<uint8>* hash) { | |
181 DCHECK(hash); | |
182 hash->assign(kSha256Hash, kSha256Hash + sizeof(kSha256Hash)); | |
183 } | |
184 | |
185 PrefService* prefs_; | |
186 static wchar_t version_dir_[_MAX_PATH]; | |
187 }; | |
188 | |
189 wchar_t SwReporterInstallerTraits::version_dir_[] = {}; | |
190 | |
191 void RegisterComponent(ComponentUpdateService* cus, PrefService* prefs) { | |
Sorin Jianu
2014/06/20 05:37:38
Need to ask waffles@ if the threading model is cor
MAD
2014/06/20 14:40:15
Done.
| |
192 scoped_ptr<ComponentInstallerTraits> traits( | |
193 new SwReporterInstallerTraits(prefs)); | |
194 // |cus| will take ownership of |installer| during installer->Register(cus). | |
195 DefaultComponentInstaller* installer = | |
196 new DefaultComponentInstaller(traits.Pass()); | |
197 installer->Register(cus); | |
198 } | |
199 | |
200 bool IsComponentInstalled() { | |
201 return base::PathExists( | |
202 SwReporterInstallerTraits::VersionPath().Append(kSwReporterExeName)); | |
203 } | |
204 | |
205 void ExecuteAndOrRegisterReporter(ComponentUpdateService* cus, | |
206 PrefService* prefs, | |
207 bool execute) { | |
208 const std::vector<std::string> registered_components(cus->GetComponentIDs()); | |
209 if (std::find(registered_components.begin(), | |
210 registered_components.end(), | |
211 SwReporterInstallerTraits::ID()) == | |
212 registered_components.end()) { | |
213 RegisterComponent(cus, prefs); | |
214 } else if (execute) { | |
215 ExecuteReporter(SwReporterInstallerTraits::VersionPath()); | |
216 } | |
217 } | |
218 | |
219 } // namespace | |
220 | |
221 void ExecuteSwReporter(ComponentUpdateService* cus, PrefService* prefs) { | |
222 // This is an explicit call, so let's forget about previous incomplete | |
223 // execution attempts and start from scratch. | |
224 prefs->SetInteger(prefs::kSwReporterExecuteTryCount, kMaxRetry); | |
225 ReportUmaStep(SW_REPORTER_EXPLICIT_REQUEST); | |
226 base::PostTaskAndReplyWithResult( | |
Sorin Jianu
2014/06/20 05:37:38
Do we need to post and reply? Would just posting t
MAD
2014/06/20 14:40:15
The execute bool is computed on the file thread ba
| |
227 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE), | |
228 FROM_HERE, | |
229 base::Bind(&IsComponentInstalled), | |
230 base::Bind(&ExecuteAndOrRegisterReporter, cus, prefs)); | |
231 } | |
232 | |
233 void ExecutePendingSwReporter(ComponentUpdateService* cus, PrefService* prefs) { | |
234 // Only continue registration / execute if we have a pending execution. | |
235 int execute_try_count = prefs->GetInteger(prefs::kSwReporterExecuteTryCount); | |
236 if (execute_try_count) { | |
237 // We have a pending execution, let's check if it completed or not. | |
238 base::win::RegKey srt_key( | |
239 HKEY_CURRENT_USER, kSoftwareRemovalToolRegistryKey, KEY_READ); | |
240 DWORD exit_code = -1; | |
241 if (srt_key.Valid() && | |
242 srt_key.ReadValueDW(kExitCodeRegistryValueName, &exit_code) == | |
243 ERROR_SUCCESS) { | |
244 ReportUmaStep(SW_REPORTER_REGISTRY_EXIT_CODE); | |
245 ReportAndClearExitCode(exit_code); | |
246 return; | |
247 } | |
248 | |
249 // If it didn't complete, let's make sure we didn't go beyond the maximum | |
250 // retry count yet. | |
251 prefs->SetInteger(prefs::kSwReporterExecuteTryCount, --execute_try_count); | |
252 if (!execute_try_count) | |
253 ReportUmaStep(SW_REPORTER_RETRIED_TOO_MANY_TIMES); | |
254 else | |
255 ReportUmaStep(SW_REPORTER_STARTUP_RETRY); | |
256 } | |
257 // If we already have an install dir, we need to register to get updates. | |
258 bool previously_installed = | |
Sorin Jianu
2014/06/20 05:37:38
The value of |previously_installed| will always be
MAD
2014/06/20 14:40:14
Done.
| |
259 base::PathExists(SwReporterInstallerTraits::VersionPath()); | |
260 if (previously_installed) { | |
261 if (execute_try_count) { | |
262 base::PostTaskAndReplyWithResult( | |
263 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE), | |
264 FROM_HERE, | |
265 base::Bind(&IsComponentInstalled), | |
266 base::Bind(&ExecuteAndOrRegisterReporter, cus, prefs)); | |
267 } else { | |
268 ExecuteAndOrRegisterReporter(cus, prefs, false); | |
269 } | |
270 } | |
271 } | |
272 | |
273 void RegisterPrefsForSwReporter(PrefRegistrySimple* registry) { | |
274 registry->RegisterIntegerPref(prefs::kSwReporterExecuteTryCount, 0); | |
275 } | |
276 | |
277 } // namespace component_updater | |
OLD | NEW |