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

Unified Diff: chrome/browser/component_updater/sw_reporter_component_installer_win.cc

Issue 333193002: Adding a SW reporter component updater (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed execution bug... 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/component_updater/sw_reporter_component_installer_win.cc
diff --git a/chrome/browser/component_updater/sw_reporter_component_installer_win.cc b/chrome/browser/component_updater/sw_reporter_component_installer_win.cc
new file mode 100644
index 0000000000000000000000000000000000000000..a3dfd245a9ef25adc339bb9705b837c87ed13a3d
--- /dev/null
+++ b/chrome/browser/component_updater/sw_reporter_component_installer_win.cc
@@ -0,0 +1,376 @@
+// Copyright (c) 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/component_updater/sw_reporter_component_installer_win.h"
+
+#include <string>
+#include <vector>
+
+#include "base/base_paths.h"
+#include "base/bind.h"
+#include "base/bind_helpers.h"
+#include "base/command_line.h"
+#include "base/file_util.h"
+#include "base/files/file_enumerator.h"
+#include "base/files/file_path.h"
+#include "base/logging.h"
+#include "base/metrics/histogram.h"
+#include "base/metrics/sparse_histogram.h"
+#include "base/path_service.h"
+#include "base/prefs/pref_registry_simple.h"
+#include "base/prefs/pref_service.h"
+#include "base/process/kill.h"
+#include "base/process/launch.h"
+#include "base/task_runner_util.h"
+#include "base/threading/worker_pool.h"
+#include "base/values.h"
+#include "base/win/registry.h"
+#include "chrome/browser/browser_process.h"
+#include "chrome/browser/component_updater/component_updater_service.h"
+#include "chrome/common/chrome_paths.h"
+#include "chrome/common/pref_names.h"
+#include "content/public/browser/browser_thread.h"
+
+using content::BrowserThread;
+
+namespace component_updater {
+
+namespace {
+
+// These values are used to send UMA information and are replicated in the
+// histograms.xml file, so the order MUST NOT CHANGE.
+enum SwReporterUmaValue {
+ SW_REPORTER_START_DOWNLOAD = 0,
+ SW_REPORTER_RETRYING_DOWNLOAD,
+ SW_REPORTER_RETRIED_TOO_MANY_DOWNLOADS,
+ SW_REPORTER_START_EXECUTION,
+ SW_REPORTER_RETRYING_EXECUTION,
+ SW_REPORTER_RETRIED_TOO_MANY_EXECUTIONS,
+ SW_REPORTER_COMPONENT_UPDATE_ERROR,
+ SW_REPORTER_MAX,
+};
+
+// The maximum number of times to retry a download on startup.
+const int kMaxRetry = 7;
+
+// CRX hash. The extension id is: gkmgaooipdjhmangpemjhigmamcehddo.
+// 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
+const uint8 kSha2Hash[] = {0x6a, 0xc6, 0x0e, 0xe8, 0xf3, 0x97, 0xc0, 0xd6,
+ 0xf4, 0xc9, 0x78, 0x6c, 0x0c, 0x24, 0x73, 0x3e,
+ 0x05, 0xa5, 0x62, 0x4b, 0x2e, 0xc7, 0xb7, 0x1c,
+ 0x5f, 0xea, 0xf0, 0x88, 0xf6, 0x97, 0x9b, 0xc7};
+
+const char kSwReporterManifestName[] = "Software Reporter Tool";
+
+const base::FilePath::CharType kSwReporterExeName[] =
+ FILE_PATH_LITERAL("software_reporter_tool.exe");
+const base::FilePath::CharType kSwReporterBaseDirectory[] =
+ FILE_PATH_LITERAL("SwReporter");
+
+// If we don't have a SwReporter component, this is the version we claim.
+const char kNullVersion[] = "0.0.0.0";
+
+// Where to fetch the reporter exit code in the registry.
+const wchar_t kSoftwareRemovalToolRegistryKey[] =
+ L"Software\\Google\\Software Removal Tool";
+const wchar_t kExitCodeRegistryValueName[] = L"ExitCode";
+
+void ReportSwReporterStep(SwReporterUmaValue value) {
+ UMA_HISTOGRAM_ENUMERATION(
+ "ComponentUpdater.SwReporterStep", value, SW_REPORTER_MAX);
+}
+
+// This function is called on the UI thread to report the SwReporter exit code
+// and then to clear it from the registry as well as clear the execution state
+// from the local state.
+void ReportAndClearSwReporterExitCode(int exit_code) {
+ UMA_HISTOGRAM_SPARSE_SLOWLY("ComponentUpdater.SwReporterExitCode", exit_code);
+
+ base::win::RegKey srt_key(
+ HKEY_CURRENT_USER, kSoftwareRemovalToolRegistryKey, KEY_WRITE);
+ srt_key.DeleteValue(kExitCodeRegistryValueName);
+
+ // Now that we are done we can reset the try count.
+ g_browser_process->local_state()->SetInteger(
+ prefs::kSwReporterComponentExecuteTryCount, 0);
+}
+
+// This function is called from a worker thread to wait for the SwReporter to
+// complete and then collect it's exit code.
+int WaitForProcessTermination(base::ProcessHandle process_handle) {
+ int exit_code = -1;
+ bool success = base::WaitForExitCode(process_handle, &exit_code);
+ DCHECK(success);
+ ::CloseHandle(process_handle);
+ process_handle = NULL;
+ return exit_code;
+}
+
+void MaybeExecuteReporter(const base::FilePath& new_version_path,
+ PrefService* prefs) {
+ // If we get here without a register or an execute try count, this means that
+ // the component have been silently updated to a new version, so we don't want
+ // to execute.
+ int execute_try_count =
+ prefs->GetInteger(prefs::kSwReporterComponentExecuteTryCount);
+ if (execute_try_count == 0 &&
+ prefs->GetInteger(prefs::kSwReporterComponentRegisterTryCount) == 0) {
+ return;
+ }
+
+ // We're done with the registration so clear the number of retries,
+ prefs->SetInteger(prefs::kSwReporterComponentRegisterTryCount, 0);
+
+ ReportSwReporterStep(execute_try_count ? SW_REPORTER_RETRYING_EXECUTION
+ : SW_REPORTER_START_EXECUTION);
+ prefs->SetInteger(prefs::kSwReporterComponentExecuteTryCount,
+ execute_try_count + 1);
+
+ // Installation is done. Now execute the reporter.
+ base::CommandLine reporter_command_line(
+ new_version_path.Append(kSwReporterExeName));
+ base::ProcessHandle scan_reporter_process = NULL;
+ bool success = base::LaunchProcess(
+ reporter_command_line, base::LaunchOptions(), &scan_reporter_process);
+ if (success) {
+ success = base::PostTaskAndReplyWithResult(
+ base::WorkerPool::GetTaskRunner(true),
+ FROM_HERE,
+ base::Bind(&WaitForProcessTermination, scan_reporter_process),
+ base::Bind(&ReportAndClearSwReporterExitCode));
+ // WaitForProcessTermination will close the handle.
+ scan_reporter_process = NULL;
+ } else {
+ NOTREACHED() << "Could not execute from " << new_version_path.value();
+ }
+}
+
+// The base directory on windows looks like:
+// <profile>\AppData\Local\Google\Chrome\User Data\SwReporter\.
+base::FilePath GetSwReporterBaseDirectory() {
+ base::FilePath result;
+ PathService::Get(chrome::DIR_USER_DATA, &result);
+ return result.Append(kSwReporterBaseDirectory);
+}
+
+// SwReporter has version encoded in the path itself so we need to enumerate the
+// directories to find the full path. On success it returns something like:
+// <profile>\AppData\Local\Google\Chrome\User Data\SwReporter\1.0.1.5\.
+bool GetLatestSwReporterDirectory(base::FilePath* latest_version_path,
+ Version* latest_version,
+ std::vector<base::FilePath>* older_dirs) {
+ base::FilePath base_dir = GetSwReporterBaseDirectory();
+ bool found = false;
+ base::FileEnumerator file_enumerator(
+ base_dir, false, base::FileEnumerator::DIRECTORIES);
+ for (base::FilePath path = file_enumerator.Next(); !path.value().empty();
+ path = file_enumerator.Next()) {
+ Version version(path.BaseName().MaybeAsASCII());
+ if (!version.IsValid())
+ continue;
+ if (version.CompareTo(*latest_version) > 0 &&
+ base::PathExists(path.Append(kSwReporterExeName))) {
+ if (found && older_dirs)
+ older_dirs->push_back(*latest_version_path);
+ *latest_version = version;
+ *latest_version_path = path;
+ found = true;
+ } else {
+ if (older_dirs)
+ older_dirs->push_back(path);
+ }
+ }
+ return found;
+}
+
+class SwReporterComponentInstaller : public ComponentInstaller {
+ public:
+ explicit SwReporterComponentInstaller(const Version& version,
+ PrefService* prefs);
+ virtual ~SwReporterComponentInstaller() {}
+
+ // ComponentInstaller overrides.
+ virtual void OnUpdateError(int error) OVERRIDE;
+ virtual bool Install(const base::DictionaryValue& manifest,
+ const base::FilePath& unpack_path) OVERRIDE;
+ virtual bool GetInstalledFile(const std::string& file,
+ base::FilePath* installed_file) OVERRIDE;
+
+ private:
+ Version current_version_;
+ PrefService* prefs_;
+};
+
+SwReporterComponentInstaller::SwReporterComponentInstaller(
+ const Version& version,
+ PrefService* prefs)
+ : current_version_(version), prefs_(prefs) {
+ DCHECK(version.IsValid());
+ DCHECK(prefs_);
+}
+
+void SwReporterComponentInstaller::OnUpdateError(int error) {
+ ReportSwReporterStep(SW_REPORTER_COMPONENT_UPDATE_ERROR);
+ NOTREACHED() << "SwReporter update error: " << error;
+}
+
+bool SwReporterComponentInstaller::Install(
+ const base::DictionaryValue& manifest,
+ const base::FilePath& unpack_path) {
+ std::string name;
+ manifest.GetStringASCII("name", &name);
+ if (name != kSwReporterManifestName)
+ return false;
+
+ std::string proposed_version;
+ manifest.GetStringASCII("version", &proposed_version);
+ Version new_version(proposed_version.c_str());
+ if (!new_version.IsValid())
+ return false;
+
+ base::FilePath sw_reporter_path(GetSwReporterBaseDirectory());
+ base::FilePath new_version_path(
+ sw_reporter_path.AppendASCII(proposed_version));
+ if (current_version_.CompareTo(new_version) < 0) {
+ // If the latest version identified is current_version_, then
+ // new_version_path should not exist.
+ DCHECK(!base::PathExists(new_version_path));
+ if (!base::PathExists(unpack_path.Append(kSwReporterExeName)) ||
+ !base::Move(unpack_path, new_version_path)) {
+ PLOG(ERROR) << "Couldn't move files to new_version_path.\n"
+ << unpack_path.value() << " -> " << new_version_path.value();
+ return false;
+ }
+ } else {
+ // We should never get an older version than the current one.
+ DCHECK(current_version_.CompareTo(new_version) == 0);
+ if (!base::PathExists(new_version_path.Append(kSwReporterExeName))) {
+ PLOG(INFO) << "existing version_path doesn't contain file.";
+ return false;
+ }
+ }
+
+ // Installation is over, now check if we have a pending execution.
+ 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
+ BrowserThread::UI,
+ FROM_HERE,
+ base::Bind(&MaybeExecuteReporter, new_version_path, prefs_));
+ return true;
+}
+
+bool SwReporterComponentInstaller::GetInstalledFile(
+ const std::string& file,
+ base::FilePath* installed_file) {
+ return false;
+}
+
+// Complete the registration of the component on the UI thread...
+void RegisterSwReporterComponent(ComponentUpdateService* cus,
+ const Version& version,
+ PrefService* prefs) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+
+ CrxComponent SwReporter;
+ SwReporter.name = "SwReporter";
+ SwReporter.installer = new SwReporterComponentInstaller(version, prefs);
+ SwReporter.version = version;
+ SwReporter.pk_hash.assign(kSha2Hash, &kSha2Hash[sizeof(kSha2Hash)]);
+ if (cus->RegisterComponent(SwReporter) == ComponentUpdateService::kError)
+ NOTREACHED() << "SwReporter component registration fail";
+}
+
+// Check if there already is a version of SwReporter installed, and if so
+// register it.
+void ExecuteSwReporterComponentOnFileThread(ComponentUpdateService* cus,
+ PrefService* prefs) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
+ base::FilePath base_dir = GetSwReporterBaseDirectory();
+ if (!base::PathExists(base_dir)) {
+ if (!base::CreateDirectory(base_dir)) {
+ 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.
+ return;
+ }
+ }
+
+ base::FilePath latest_version_path;
+ 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.
+ std::vector<base::FilePath> older_dirs;
+ if (!GetLatestSwReporterDirectory(
+ &latest_version_path, &latest_version, &older_dirs)) {
+ BrowserThread::PostTask(
+ BrowserThread::UI,
+ FROM_HERE,
+ base::Bind(&RegisterSwReporterComponent, cus, latest_version, prefs));
+ } else {
+ BrowserThread::PostTask(
+ BrowserThread::UI,
+ FROM_HERE,
+ base::Bind(&MaybeExecuteReporter, latest_version_path, prefs));
+ }
+ // 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
+ for (std::vector<base::FilePath>::iterator iter = older_dirs.begin();
+ iter != older_dirs.end();
+ ++iter) {
+ 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...
+ }
+}
+
+} // namespace
+
+void ExecuteSwReporterComponent(ComponentUpdateService* cus,
+ PrefService* prefs) {
+ int tries = prefs->GetInteger(prefs::kSwReporterComponentRegisterTryCount);
+ if (tries > kMaxRetry) {
+ ReportSwReporterStep(SW_REPORTER_RETRIED_TOO_MANY_DOWNLOADS);
+ // Let's forget about this attempt.
+ prefs->SetInteger(prefs::kSwReporterComponentRegisterTryCount, 0);
+ return;
+ }
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.
+
+ ReportSwReporterStep(tries ? SW_REPORTER_RETRYING_DOWNLOAD
+ : SW_REPORTER_START_DOWNLOAD);
+ prefs->SetInteger(prefs::kSwReporterComponentRegisterTryCount, tries + 1);
+ BrowserThread::PostTask(
+ BrowserThread::FILE,
+ FROM_HERE,
+ base::Bind(&ExecuteSwReporterComponentOnFileThread, cus, prefs));
+}
+
+void MaybeExecuteSwReporterComponent(ComponentUpdateService* cus,
+ PrefService* prefs) {
+ // Only register if we have a pending registration / execution.
+ if (prefs->GetInteger(prefs::kSwReporterComponentRegisterTryCount) == 0) {
+ if (prefs->GetInteger(prefs::kSwReporterComponentExecuteTryCount) == 0)
+ return;
+ // We have a pending execution, let's check if it completed or not.
+ base::win::RegKey srt_key(
+ HKEY_CURRENT_USER, kSoftwareRemovalToolRegistryKey, KEY_READ);
+ DWORD exit_code = -1;
+ if (srt_key.Valid() &&
+ srt_key.ReadValueDW(kExitCodeRegistryValueName, &exit_code) ==
+ ERROR_SUCCESS) {
+ ReportAndClearSwReporterExitCode(exit_code);
+ return;
+ }
+
+ // If it didn't complete, let's make sure we didn't go beyond the maximum
+ // retry count yet.
+ if (prefs->GetInteger(prefs::kSwReporterComponentExecuteTryCount) >
+ kMaxRetry) {
+ ReportSwReporterStep(SW_REPORTER_RETRIED_TOO_MANY_EXECUTIONS);
+ // Let's forget about this attempt.
+ prefs->SetInteger(prefs::kSwReporterComponentExecuteTryCount, 0);
+ return;
+ }
+ }
+
+ ExecuteSwReporterComponent(cus, prefs);
+}
+
+void RegisterPrefsForSwReporterComponent(PrefRegistrySimple* registry) {
+ registry->RegisterIntegerPref(prefs::kSwReporterComponentRegisterTryCount, 0);
+ registry->RegisterIntegerPref(prefs::kSwReporterComponentExecuteTryCount, 0);
+}
+
+} // namespace component_updater
« no previous file with comments | « chrome/browser/component_updater/sw_reporter_component_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