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

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: More OWNER comments, using a bool, before changing it back to cus->GetIDs 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 pending_registration_ = true;
129 }
130
131 virtual ~SwReporterInstallerTraits() {}
132
133 virtual bool VerifyInstallation(const base::FilePath& dir) const {
134 return base::PathExists(dir.Append(kSwReporterExeName));
135 }
136
137 virtual bool CanAutoUpdate() const { return true; }
138
139 virtual bool OnCustomInstall(const base::DictionaryValue& manifest,
140 const base::FilePath& install_dir) {
141 return true;
142 }
143
144 virtual void ComponentReady(const base::Version& version,
145 const base::FilePath& install_dir,
146 scoped_ptr<base::DictionaryValue> manifest) {
147 wcsncpy_s(version_dir_,
148 _MAX_PATH,
149 install_dir.value().c_str(),
150 install_dir.value().size());
151 // Only execute the reporter if there is still a pending request for it..
Sorin Jianu 2014/06/20 05:09:58 extra . at the end.
MAD 2014/06/20 14:40:14 Done.
152 if (prefs_->GetInteger(prefs::kSwReporterExecuteTryCount))
153 ExecuteReporter(install_dir);
154 }
155
156 virtual base::FilePath GetBaseDirectory() const { return install_dir(); }
157
158 virtual void GetHash(std::vector<uint8>* hash) const { GetPkHash(hash); }
159
160 virtual std::string GetName() const { return "Software Reporter Tool"; }
161
162 static const base::FilePath install_dir() {
163 // The base directory on windows looks like:
164 // <profile>\AppData\Local\Google\Chrome\User Data\SwReporter\.
165 base::FilePath result;
166 PathService::Get(chrome::DIR_USER_DATA, &result);
167 return result.Append(FILE_PATH_LITERAL("SwReporter"));
168 }
169
170 static const std::string ID() {
171 CrxComponent component;
172 component.version = Version("0.0.0.0");
173 GetPkHash(&component.pk_hash);
174 return component_updater::GetCrxComponentID(component);
175 }
176
177 static bool pending_registration() { return pending_registration_; }
178 static const base::FilePath VersionPath() {
179 return base::FilePath(version_dir_);
180 }
181
182 private:
183 static void GetPkHash(std::vector<uint8>* hash) {
184 DCHECK(hash);
185 hash->assign(kSha256Hash, kSha256Hash + sizeof(kSha256Hash));
186 }
187
188 PrefService* prefs_;
189 static bool pending_registration_;
190 static wchar_t version_dir_[_MAX_PATH];
191 };
192
193 bool SwReporterInstallerTraits::pending_registration_ = false;
194 wchar_t SwReporterInstallerTraits::version_dir_[] = {};
195
196 void RegisterComponent(ComponentUpdateService* cus, PrefService* prefs) {
197 scoped_ptr<ComponentInstallerTraits> traits(
198 new SwReporterInstallerTraits(prefs));
199 // |cus| will take ownership of |installer| during installer->Register(cus).
200 DefaultComponentInstaller* installer =
201 new DefaultComponentInstaller(traits.Pass());
202 installer->Register(cus);
203 }
204
205 bool IsComponentInstalled() {
206 return base::PathExists(
207 SwReporterInstallerTraits::VersionPath().Append(kSwReporterExeName));
208 }
209
210 void ExecuteAndOrRegisterReporter(ComponentUpdateService* cus,
211 PrefService* prefs,
212 bool execute) {
213 if (!SwReporterInstallerTraits::pending_registration())
Sorin Jianu 2014/06/20 05:09:58 Fore readability sake, we could get rid of ! and s
MAD 2014/06/20 14:40:14 Pending registration is gone...
214 RegisterComponent(cus, prefs);
215 else if (execute)
216 ExecuteReporter(SwReporterInstallerTraits::VersionPath());
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(
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) {
Sorin Jianu 2014/06/20 05:09:58 Might want to use > 0, to avoid some potential int
MAD 2014/06/20 14:40:14 Done.
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;
Sorin Jianu 2014/06/20 05:09:58 My thinking is that we want to register all the ti
MAD 2014/06/20 14:40:14 This doesn't run the reporter, just looks to see i
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)
Sorin Jianu 2014/06/20 05:09:58 could use > instead of !, see above.
MAD 2014/06/20 14:40:14 Done.
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.
Sorin Jianu 2014/06/20 05:09:58 Let's assume that SWR is installed and nothing is
MAD 2014/06/20 14:40:14 Ho, right I meant to check the root dir of the com
258 if (base::PathExists(SwReporterInstallerTraits::VersionPath()) ||
259 execute_try_count) {
260 base::PostTaskAndReplyWithResult(
261 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE),
262 FROM_HERE,
263 base::Bind(&IsComponentInstalled),
264 base::Bind(&ExecuteAndOrRegisterReporter, cus, prefs));
265 }
266 }
267
268 void RegisterPrefsForSwReporter(PrefRegistrySimple* registry) {
269 registry->RegisterIntegerPref(prefs::kSwReporterExecuteTryCount, 0);
270 }
271
272 } // 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