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

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

Issue 333193002: Adding a SW reporter component updater (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merged to ToT. 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_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) > 0)
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 base::FilePath install_dir() {
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 std::string ID() {
169 CrxComponent component;
170 component.version = Version("0.0.0.0");
171 GetPkHash(&component.pk_hash);
172 return component_updater::GetCrxComponentID(component);
173 }
174
175 static base::FilePath VersionPath() { return base::FilePath(version_dir_); }
176
177 private:
178 static void GetPkHash(std::vector<uint8>* hash) {
179 DCHECK(hash);
180 hash->assign(kSha256Hash, kSha256Hash + sizeof(kSha256Hash));
181 }
182
183 PrefService* prefs_;
184 static wchar_t version_dir_[_MAX_PATH];
185 };
186
187 wchar_t SwReporterInstallerTraits::version_dir_[] = {};
188
189 void RegisterComponent(ComponentUpdateService* cus, PrefService* prefs) {
190 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
191 scoped_ptr<ComponentInstallerTraits> traits(
192 new SwReporterInstallerTraits(prefs));
193 // |cus| will take ownership of |installer| during installer->Register(cus).
194 DefaultComponentInstaller* installer =
195 new DefaultComponentInstaller(traits.Pass());
196 installer->Register(cus);
197 }
198
199 // We need a conditional version of register component so that it can be called
200 // back on the UI thread after validating on the File thread that the component
201 // path exists and we must re-register on startup for example.
202 void MaybeRegisterComponent(ComponentUpdateService* cus,
203 PrefService* prefs,
204 bool register_component) {
205 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
206 if (register_component)
207 RegisterComponent(cus, prefs);
208 }
209
210 } // namespace
211
212 void ExecuteSwReporter(ComponentUpdateService* cus, PrefService* prefs) {
213 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
214 // This is an explicit call, so let's forget about previous incomplete
215 // execution attempts and start from scratch.
216 prefs->SetInteger(prefs::kSwReporterExecuteTryCount, kMaxRetry);
217 ReportUmaStep(SW_REPORTER_EXPLICIT_REQUEST);
218 const std::vector<std::string> registered_components(cus->GetComponentIDs());
219 if (std::find(registered_components.begin(),
220 registered_components.end(),
221 SwReporterInstallerTraits::ID()) ==
222 registered_components.end()) {
223 RegisterComponent(cus, prefs);
224 } else if (!SwReporterInstallerTraits::VersionPath().empty()) {
225 // Here, we already have a fully registered and installed component
226 // available for immediate use. This doesn't handle cases where the version
227 // folder is there but the executable is not within in. This is a corruption
228 // we don't want to handle here.
229 ExecuteReporter(SwReporterInstallerTraits::VersionPath());
230 }
231 // If the component is registered but the version path is not available, it
232 // means the component was not fully installed yet, and it should run the
233 // reporter when ComponentReady is called.
234 }
235
236 void ExecutePendingSwReporter(ComponentUpdateService* cus, PrefService* prefs) {
237 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
238
239 // Register the existing component for updates.
240 base::PostTaskAndReplyWithResult(
241 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE),
242 FROM_HERE,
243 base::Bind(&base::PathExists, SwReporterInstallerTraits::install_dir()),
244 base::Bind(&MaybeRegisterComponent, cus, prefs));
245
246 // Run the reporter if there is a pending execution request.
247 int execute_try_count = prefs->GetInteger(prefs::kSwReporterExecuteTryCount);
248 if (execute_try_count > 0) {
249 // Retrieve the results if the pending request has completed.
250 base::win::RegKey srt_key(
251 HKEY_CURRENT_USER, kSoftwareRemovalToolRegistryKey, KEY_READ);
252 DWORD exit_code = -1;
253 if (srt_key.Valid() &&
254 srt_key.ReadValueDW(kExitCodeRegistryValueName, &exit_code) ==
255 ERROR_SUCCESS) {
256 ReportUmaStep(SW_REPORTER_REGISTRY_EXIT_CODE);
257 ReportAndClearExitCode(exit_code);
258 return;
259 }
260
261 // The previous request has not completed. The reporter will run again
262 // when ComponentReady is called or the request is abandoned if it has
263 // been tried too many times.
264 prefs->SetInteger(prefs::kSwReporterExecuteTryCount, --execute_try_count);
265 if (execute_try_count > 0)
266 ReportUmaStep(SW_REPORTER_STARTUP_RETRY);
267 else
268 ReportUmaStep(SW_REPORTER_RETRIED_TOO_MANY_TIMES);
269 }
270 }
271
272 void RegisterPrefsForSwReporter(PrefRegistrySimple* registry) {
273 registry->RegisterIntegerPref(prefs::kSwReporterExecuteTryCount, 0);
274 }
275
276 } // namespace component_updater
OLDNEW
« no previous file with comments | « chrome/browser/component_updater/sw_reporter_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