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 <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/base_paths.h" | 10 #include "base/base_paths.h" |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
14 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
15 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
17 #include "base/metrics/histogram.h" | 17 #include "base/metrics/histogram.h" |
18 #include "base/metrics/sparse_histogram.h" | 18 #include "base/metrics/sparse_histogram.h" |
19 #include "base/path_service.h" | 19 #include "base/path_service.h" |
20 #include "base/prefs/pref_registry_simple.h" | 20 #include "base/prefs/pref_registry_simple.h" |
21 #include "base/prefs/pref_service.h" | 21 #include "base/prefs/pref_service.h" |
22 #include "base/process/kill.h" | 22 #include "base/process/kill.h" |
23 #include "base/process/launch.h" | 23 #include "base/process/launch.h" |
24 #include "base/task_runner_util.h" | 24 #include "base/task_runner_util.h" |
25 #include "base/threading/worker_pool.h" | 25 #include "base/threading/worker_pool.h" |
| 26 #include "base/time/time.h" |
26 #include "base/win/registry.h" | 27 #include "base/win/registry.h" |
27 #include "chrome/browser/browser_process.h" | 28 #include "chrome/browser/browser_process.h" |
| 29 #include "chrome/browser/metrics/chrome_metrics_service_accessor.h" |
| 30 #include "chrome/common/pref_names.h" |
28 #include "components/component_updater/component_updater_paths.h" | 31 #include "components/component_updater/component_updater_paths.h" |
29 #include "components/component_updater/component_updater_service.h" | 32 #include "components/component_updater/component_updater_service.h" |
30 #include "components/component_updater/component_updater_utils.h" | 33 #include "components/component_updater/component_updater_utils.h" |
31 #include "components/component_updater/default_component_installer.h" | 34 #include "components/component_updater/default_component_installer.h" |
32 #include "components/component_updater/pref_names.h" | 35 #include "components/component_updater/pref_names.h" |
33 #include "content/public/browser/browser_thread.h" | 36 #include "content/public/browser/browser_thread.h" |
34 | 37 |
35 using content::BrowserThread; | 38 using content::BrowserThread; |
36 | 39 |
37 namespace component_updater { | 40 namespace component_updater { |
38 | 41 |
39 namespace { | 42 namespace { |
40 | 43 |
41 // These values are used to send UMA information and are replicated in the | 44 // These values are used to send UMA information and are replicated in the |
42 // histograms.xml file, so the order MUST NOT CHANGE. | 45 // histograms.xml file, so the order MUST NOT CHANGE. |
43 enum SwReporterUmaValue { | 46 enum SwReporterUmaValue { |
44 SW_REPORTER_EXPLICIT_REQUEST = 0, | 47 SW_REPORTER_EXPLICIT_REQUEST = 0, // Deprecated. |
45 SW_REPORTER_STARTUP_RETRY = 1, | 48 SW_REPORTER_STARTUP_RETRY = 1, // Deprecated. |
46 SW_REPORTER_RETRIED_TOO_MANY_TIMES = 2, | 49 SW_REPORTER_RETRIED_TOO_MANY_TIMES = 2, // Deprecated. |
47 SW_REPORTER_START_EXECUTION = 3, | 50 SW_REPORTER_START_EXECUTION = 3, |
48 SW_REPORTER_FAILED_TO_START = 4, | 51 SW_REPORTER_FAILED_TO_START = 4, |
49 SW_REPORTER_REGISTRY_EXIT_CODE = 5, | 52 SW_REPORTER_REGISTRY_EXIT_CODE = 5, |
50 SW_REPORTER_RESET_RETRIES = 6, | 53 SW_REPORTER_RESET_RETRIES = 6, // Deprecated. |
51 SW_REPORTER_MAX, | 54 SW_REPORTER_MAX, |
52 }; | 55 }; |
53 | 56 |
54 // The maximum number of times to retry a download on startup. | 57 // The maximum number of times to retry a download on startup. |
55 const int kMaxRetry = 20; | 58 const int kMaxRetry = 20; |
56 | 59 |
| 60 // The number of days to wait before triggering another sw reporter run. |
| 61 const int kDaysBetweenSwReporterRuns = 7; |
| 62 |
57 // CRX hash. The extension id is: gkmgaooipdjhmangpemjhigmamcehddo. The hash was | 63 // CRX hash. The extension id is: gkmgaooipdjhmangpemjhigmamcehddo. The hash was |
58 // generated in Python with something like this: | 64 // generated in Python with something like this: |
59 // hashlib.sha256().update(open("<file>.crx").read()[16:16+294]).digest(). | 65 // hashlib.sha256().update(open("<file>.crx").read()[16:16+294]).digest(). |
60 const uint8 kSha256Hash[] = {0x6a, 0xc6, 0x0e, 0xe8, 0xf3, 0x97, 0xc0, 0xd6, | 66 const uint8 kSha256Hash[] = {0x6a, 0xc6, 0x0e, 0xe8, 0xf3, 0x97, 0xc0, 0xd6, |
61 0xf4, 0xc9, 0x78, 0x6c, 0x0c, 0x24, 0x73, 0x3e, | 67 0xf4, 0xc9, 0x78, 0x6c, 0x0c, 0x24, 0x73, 0x3e, |
62 0x05, 0xa5, 0x62, 0x4b, 0x2e, 0xc7, 0xb7, 0x1c, | 68 0x05, 0xa5, 0x62, 0x4b, 0x2e, 0xc7, 0xb7, 0x1c, |
63 0x5f, 0xea, 0xf0, 0x88, 0xf6, 0x97, 0x9b, 0xc7}; | 69 0x5f, 0xea, 0xf0, 0x88, 0xf6, 0x97, 0x9b, 0xc7}; |
64 | 70 |
65 const base::FilePath::CharType kSwReporterExeName[] = | 71 const base::FilePath::CharType kSwReporterExeName[] = |
66 FILE_PATH_LITERAL("software_reporter_tool.exe"); | 72 FILE_PATH_LITERAL("software_reporter_tool.exe"); |
(...skipping 10 matching lines...) Expand all Loading... |
77 // This function is called on the UI thread to report the SwReporter exit code | 83 // This function is called on the UI thread to report the SwReporter exit code |
78 // and then clear it from the registry as well as clear the execution state | 84 // and then clear it from the registry as well as clear the execution state |
79 // from the local state. This could be called from an interruptible worker | 85 // from the local state. This could be called from an interruptible worker |
80 // thread so should be resilient to unexpected shutdown. | 86 // thread so should be resilient to unexpected shutdown. |
81 void ReportAndClearExitCode(int exit_code) { | 87 void ReportAndClearExitCode(int exit_code) { |
82 UMA_HISTOGRAM_SPARSE_SLOWLY("SoftwareReporter.ExitCode", exit_code); | 88 UMA_HISTOGRAM_SPARSE_SLOWLY("SoftwareReporter.ExitCode", exit_code); |
83 | 89 |
84 base::win::RegKey srt_key( | 90 base::win::RegKey srt_key( |
85 HKEY_CURRENT_USER, kSoftwareRemovalToolRegistryKey, KEY_WRITE); | 91 HKEY_CURRENT_USER, kSoftwareRemovalToolRegistryKey, KEY_WRITE); |
86 srt_key.DeleteValue(kExitCodeRegistryValueName); | 92 srt_key.DeleteValue(kExitCodeRegistryValueName); |
87 | |
88 // Now that we are done we can reset the try count. | |
89 g_browser_process->local_state()->SetInteger( | |
90 prefs::kSwReporterExecuteTryCount, 0); | |
91 } | 93 } |
92 | 94 |
93 // This function is called from a worker thread to launch the SwReporter and | 95 // This function is called from a worker thread to launch the SwReporter and |
94 // wait for termination to collect its exit code. This task could be interrupted | 96 // wait for termination to collect its exit code. This task could be interrupted |
95 // by a shutdown at anytime, so it shouldn't depend on anything external that | 97 // by a shutdown at anytime, so it shouldn't depend on anything external that |
96 // could be shutdown beforehand. | 98 // could be shutdown beforehand. |
97 void LaunchAndWaitForExit(const base::FilePath& exe_path) { | 99 void LaunchAndWaitForExit(const base::FilePath& exe_path) { |
98 const base::CommandLine reporter_command_line(exe_path); | 100 const base::CommandLine reporter_command_line(exe_path); |
99 base::ProcessHandle scan_reporter_process = base::kNullProcessHandle; | 101 base::ProcessHandle scan_reporter_process = base::kNullProcessHandle; |
100 if (!base::LaunchProcess(reporter_command_line, | 102 if (!base::LaunchProcess(reporter_command_line, |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 virtual bool CanAutoUpdate() const { return true; } | 138 virtual bool CanAutoUpdate() const { return true; } |
137 | 139 |
138 virtual bool OnCustomInstall(const base::DictionaryValue& manifest, | 140 virtual bool OnCustomInstall(const base::DictionaryValue& manifest, |
139 const base::FilePath& install_dir) { | 141 const base::FilePath& install_dir) { |
140 return true; | 142 return true; |
141 } | 143 } |
142 | 144 |
143 virtual void ComponentReady(const base::Version& version, | 145 virtual void ComponentReady(const base::Version& version, |
144 const base::FilePath& install_dir, | 146 const base::FilePath& install_dir, |
145 scoped_ptr<base::DictionaryValue> manifest) { | 147 scoped_ptr<base::DictionaryValue> manifest) { |
| 148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 149 |
146 wcsncpy_s(version_dir_, | 150 wcsncpy_s(version_dir_, |
147 _MAX_PATH, | 151 _MAX_PATH, |
148 install_dir.value().c_str(), | 152 install_dir.value().c_str(), |
149 install_dir.value().size()); | 153 install_dir.value().size()); |
150 // Only execute the reporter if there is still a pending request for it. | 154 |
151 if (prefs_->GetInteger(prefs::kSwReporterExecuteTryCount) > 0) | 155 // A previous run may have results in the registry, so check and report |
| 156 // them if present. |
| 157 base::win::RegKey srt_key( |
| 158 HKEY_CURRENT_USER, kSoftwareRemovalToolRegistryKey, KEY_READ); |
| 159 DWORD exit_code; |
| 160 if (srt_key.Valid() && |
| 161 srt_key.ReadValueDW(kExitCodeRegistryValueName, &exit_code) == |
| 162 ERROR_SUCCESS) { |
| 163 ReportUmaStep(SW_REPORTER_REGISTRY_EXIT_CODE); |
| 164 ReportAndClearExitCode(exit_code); |
| 165 } |
| 166 |
| 167 // If we can't access local state, we can't see when we last ran, so |
| 168 // just exit without running. |
| 169 if (!g_browser_process || !g_browser_process->local_state()) |
| 170 return; |
| 171 |
| 172 // Run the reporter if it hasn't been triggered in the |
| 173 // kDaysBetweenSwReporterRuns days. |
| 174 const base::Time last_time_triggered = base::Time::FromInternalValue( |
| 175 g_browser_process->local_state()->GetInt64( |
| 176 prefs::kSwReporterLastTimeTriggered)); |
| 177 if ((base::Time::Now() - last_time_triggered).InDays() >= |
| 178 kDaysBetweenSwReporterRuns) { |
| 179 g_browser_process->local_state()->SetInt64( |
| 180 prefs::kSwReporterLastTimeTriggered, |
| 181 base::Time::Now().ToInternalValue()); |
| 182 |
152 ExecuteReporter(install_dir); | 183 ExecuteReporter(install_dir); |
| 184 } |
153 } | 185 } |
154 | 186 |
155 virtual base::FilePath GetBaseDirectory() const { return install_dir(); } | 187 virtual base::FilePath GetBaseDirectory() const { return install_dir(); } |
156 | 188 |
157 virtual void GetHash(std::vector<uint8>* hash) const { GetPkHash(hash); } | 189 virtual void GetHash(std::vector<uint8>* hash) const { GetPkHash(hash); } |
158 | 190 |
159 virtual std::string GetName() const { return "Software Reporter Tool"; } | 191 virtual std::string GetName() const { return "Software Reporter Tool"; } |
160 | 192 |
161 static base::FilePath install_dir() { | 193 static base::FilePath install_dir() { |
162 // The base directory on windows looks like: | 194 // The base directory on windows looks like: |
(...skipping 17 matching lines...) Expand all Loading... |
180 DCHECK(hash); | 212 DCHECK(hash); |
181 hash->assign(kSha256Hash, kSha256Hash + sizeof(kSha256Hash)); | 213 hash->assign(kSha256Hash, kSha256Hash + sizeof(kSha256Hash)); |
182 } | 214 } |
183 | 215 |
184 PrefService* prefs_; | 216 PrefService* prefs_; |
185 static wchar_t version_dir_[_MAX_PATH]; | 217 static wchar_t version_dir_[_MAX_PATH]; |
186 }; | 218 }; |
187 | 219 |
188 wchar_t SwReporterInstallerTraits::version_dir_[] = {}; | 220 wchar_t SwReporterInstallerTraits::version_dir_[] = {}; |
189 | 221 |
190 void RegisterComponent(ComponentUpdateService* cus, PrefService* prefs) { | 222 } // namespace |
191 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 223 |
| 224 void RegisterSwReporterComponent(ComponentUpdateService* cus, |
| 225 PrefService* prefs) { |
| 226 // The Sw reporter shouldn't run if the user isn't reporting metrics. |
| 227 if (!ChromeMetricsServiceAccessor::IsMetricsReportingEnabled() && false) |
| 228 return; |
| 229 |
| 230 // Install the component. |
192 scoped_ptr<ComponentInstallerTraits> traits( | 231 scoped_ptr<ComponentInstallerTraits> traits( |
193 new SwReporterInstallerTraits(prefs)); | 232 new SwReporterInstallerTraits(prefs)); |
194 // |cus| will take ownership of |installer| during installer->Register(cus). | 233 // |cus| will take ownership of |installer| during installer->Register(cus). |
195 DefaultComponentInstaller* installer = | 234 DefaultComponentInstaller* installer = |
196 new DefaultComponentInstaller(traits.Pass()); | 235 new DefaultComponentInstaller(traits.Pass()); |
197 installer->Register(cus); | 236 installer->Register(cus); |
198 } | 237 } |
199 | 238 |
200 // We need a conditional version of register component so that it can be called | |
201 // back on the UI thread after validating on the File thread that the component | |
202 // path exists and we must re-register on startup for example. | |
203 void MaybeRegisterComponent(ComponentUpdateService* cus, | |
204 PrefService* prefs, | |
205 bool register_component) { | |
206 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
207 if (register_component) | |
208 RegisterComponent(cus, prefs); | |
209 } | |
210 | |
211 } // namespace | |
212 | |
213 void ExecuteSwReporter(ComponentUpdateService* cus, PrefService* prefs) { | |
214 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
215 // If we have a pending execution, send metrics about it so we can account for | |
216 // missing executions. | |
217 if (prefs->GetInteger(prefs::kSwReporterExecuteTryCount) > 0) | |
218 ReportUmaStep(SW_REPORTER_RESET_RETRIES); | |
219 // This is an explicit call, so let's forget about previous incomplete | |
220 // execution attempts and start from scratch. | |
221 prefs->SetInteger(prefs::kSwReporterExecuteTryCount, kMaxRetry); | |
222 ReportUmaStep(SW_REPORTER_EXPLICIT_REQUEST); | |
223 const std::vector<std::string> registered_components(cus->GetComponentIDs()); | |
224 if (std::find(registered_components.begin(), | |
225 registered_components.end(), | |
226 SwReporterInstallerTraits::ID()) == | |
227 registered_components.end()) { | |
228 RegisterComponent(cus, prefs); | |
229 } else if (!SwReporterInstallerTraits::VersionPath().empty()) { | |
230 // Here, we already have a fully registered and installed component | |
231 // available for immediate use. This doesn't handle cases where the version | |
232 // folder is there but the executable is not within in. This is a corruption | |
233 // we don't want to handle here. | |
234 ExecuteReporter(SwReporterInstallerTraits::VersionPath()); | |
235 } | |
236 // If the component is registered but the version path is not available, it | |
237 // means the component was not fully installed yet, and it should run the | |
238 // reporter when ComponentReady is called. | |
239 } | |
240 | |
241 void ExecutePendingSwReporter(ComponentUpdateService* cus, PrefService* prefs) { | |
242 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
243 | |
244 // Register the existing component for updates. | |
245 base::PostTaskAndReplyWithResult( | |
246 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE), | |
247 FROM_HERE, | |
248 base::Bind(&base::PathExists, SwReporterInstallerTraits::install_dir()), | |
249 base::Bind(&MaybeRegisterComponent, cus, prefs)); | |
250 | |
251 // Run the reporter if there is a pending execution request. | |
252 int execute_try_count = prefs->GetInteger(prefs::kSwReporterExecuteTryCount); | |
253 if (execute_try_count > 0) { | |
254 // Retrieve the results if the pending request has completed. | |
255 base::win::RegKey srt_key( | |
256 HKEY_CURRENT_USER, kSoftwareRemovalToolRegistryKey, KEY_READ); | |
257 DWORD exit_code; | |
258 if (srt_key.Valid() && | |
259 srt_key.ReadValueDW(kExitCodeRegistryValueName, &exit_code) == | |
260 ERROR_SUCCESS) { | |
261 ReportUmaStep(SW_REPORTER_REGISTRY_EXIT_CODE); | |
262 ReportAndClearExitCode(exit_code); | |
263 return; | |
264 } | |
265 | |
266 // The previous request has not completed. The reporter will run again | |
267 // when ComponentReady is called or the request is abandoned if it has | |
268 // been tried too many times. | |
269 prefs->SetInteger(prefs::kSwReporterExecuteTryCount, --execute_try_count); | |
270 if (execute_try_count > 0) | |
271 ReportUmaStep(SW_REPORTER_STARTUP_RETRY); | |
272 else | |
273 ReportUmaStep(SW_REPORTER_RETRIED_TOO_MANY_TIMES); | |
274 } | |
275 } | |
276 | |
277 void RegisterPrefsForSwReporter(PrefRegistrySimple* registry) { | 239 void RegisterPrefsForSwReporter(PrefRegistrySimple* registry) { |
278 registry->RegisterIntegerPref(prefs::kSwReporterExecuteTryCount, 0); | 240 registry->RegisterInt64Pref(prefs::kSwReporterLastTimeTriggered, 0); |
279 } | 241 } |
280 | 242 |
281 } // namespace component_updater | 243 } // namespace component_updater |
OLD | NEW |