Chromium Code Reviews| 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..366a81019a5d48745740f23f6b3309f8edd6398c |
| --- /dev/null |
| +++ b/chrome/browser/component_updater/sw_reporter_component_installer_win.cc |
| @@ -0,0 +1,363 @@ |
| +// 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/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/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 { |
| + |
| +// UMA identifiers. |
| +const char kSwReporterComponentHistogramName[] = "component_updater.SwReporter"; |
| +const char kSwReporterComponentExiCodeHistogramName[] = |
| + "component_updater.SwReporterExitCode"; |
| +const int kMaxSwReporterExitCodeUMAValue = 100; |
| + |
| +enum { |
| + 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_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(). |
| +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"; |
| + |
| +// 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) { |
| + DCHECK(exit_code < kMaxSwReporterExitCodeUMAValue); |
| + UMA_HISTOGRAM_ENUMERATION(kSwReporterComponentExiCodeHistogramName, |
| + exit_code, |
| + kMaxSwReporterExitCodeUMAValue); |
| + |
| + 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 to be sent to |
| +// ReportAndClearSwReporterExitCode on the UI thread. |
| +void WaitForProcessTermination(base::ProcessHandle process_handle) { |
| + int exit_code = -1; |
| + bool success = base::WaitForExitCode(process_handle, &exit_code); |
| + DCHECK(success); |
| + BrowserThread::PostTask( |
| + BrowserThread::UI, |
| + FROM_HERE, |
| + base::Bind(&ReportAndClearSwReporterExitCode, exit_code)); |
| +} |
| + |
| +// 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\. |
| +void GetLatestSwReporterDirectory(base::FilePath* result, |
| + Version* latest, |
| + 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) > 0 && |
| + base::PathExists(path.Append(kSwReporterExeName))) { |
| + if (found && older_dirs) |
| + older_dirs->push_back(*result); |
| + *latest = version; |
| + *result = path; |
| + found = true; |
| + } else { |
| + if (older_dirs) |
| + older_dirs->push_back(path); |
| + } |
| + } |
| +} |
| + |
| +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: |
| + void UpdateInstallPrefsOnUIThread(); |
| + |
| + 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) { |
| + 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 (!base::PathExists(unpack_path.Append(kSwReporterExeName)) || |
| + base::PathExists(new_version_path) || |
|
robertshield
2014/06/16 03:47:53
Is it intended to return an error if the binary al
MAD
2014/06/16 13:03:55
Good catch, I copied this from the swift shader, a
|
| + !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; |
| + } |
| + } |
| + |
| + // Passed the basic tests, update the prefs on the UI thread. |
| + BrowserThread::PostTask( |
| + BrowserThread::UI, |
| + FROM_HERE, |
| + base::Bind(&SwReporterComponentInstaller::UpdateInstallPrefsOnUIThread, |
| + base::Unretained(this))); |
| + |
| + // 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) { |
| + base::WorkerPool::PostTask( |
| + FROM_HERE, |
| + base::Bind(&WaitForProcessTermination, scan_reporter_process), |
| + true); |
| + return true; |
| + } |
| + return false; |
| +} |
| + |
| +bool SwReporterComponentInstaller::GetInstalledFile( |
| + const std::string& file, |
| + base::FilePath* installed_file) { |
| + return false; |
| +} |
| + |
| +void SwReporterComponentInstaller::UpdateInstallPrefsOnUIThread() { |
| + // We're done with the registration so clean the number of retries, |
| + prefs_->SetInteger(prefs::kSwReporterComponentRegisterTryCount, 0); |
| + |
| + int tries = prefs_->GetInteger(prefs::kSwReporterComponentExecuteTryCount); |
| + UMA_HISTOGRAM_ENUMERATION( |
| + kSwReporterComponentHistogramName, |
| + tries ? SW_REPORTER_RETRYING_EXECUTION : SW_REPORTER_START_EXECUTION, |
| + SW_REPORTER_MAX); |
| + prefs_->SetInteger(prefs::kSwReporterComponentExecuteTryCount, tries + 1); |
| +} |
| + |
| +// Complete the registration of the component on the UI thread... |
| +void FinishSwReporterUpdateRegistration(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 RegisterSwReporterComponentOnFileThread(ComponentUpdateService* cus, |
| + PrefService* prefs) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| + base::FilePath path = GetSwReporterBaseDirectory(); |
| + if (!base::PathExists(path)) { |
| + if (!base::CreateDirectory(path)) { |
| + NOTREACHED() << "Could not create SwReporter directory."; |
| + return; |
| + } |
| + } |
| + |
| + Version version(kNullVersion); |
| + std::vector<base::FilePath> older_dirs; |
| + GetLatestSwReporterDirectory(&path, &version, &older_dirs); |
| + |
| + BrowserThread::PostTask( |
| + BrowserThread::UI, |
| + FROM_HERE, |
| + base::Bind(&FinishSwReporterUpdateRegistration, cus, version, prefs)); |
| + |
| + // Remove older versions of SwReporter. |
| + for (std::vector<base::FilePath>::iterator iter = older_dirs.begin(); |
| + iter != older_dirs.end(); |
| + ++iter) { |
| + base::DeleteFile(*iter, true); |
| + } |
| +} |
| + |
| +} // namespace |
| + |
| +void RegisterSwReporterComponent(ComponentUpdateService* cus, |
| + PrefService* prefs) { |
| + int tries = prefs->GetInteger(prefs::kSwReporterComponentRegisterTryCount); |
| + if (tries > kMaxRetry) { |
| + UMA_HISTOGRAM_ENUMERATION(kSwReporterComponentHistogramName, |
| + SW_REPORTER_RETRIED_TOO_MANY_DOWNLOADS, |
| + SW_REPORTER_MAX); |
| + // Let's forget about this attempt. |
| + prefs->SetInteger(prefs::kSwReporterComponentRegisterTryCount, 0); |
| + return; |
| + } |
| + |
| + UMA_HISTOGRAM_ENUMERATION( |
| + kSwReporterComponentHistogramName, |
| + tries ? SW_REPORTER_RETRYING_DOWNLOAD : SW_REPORTER_START_DOWNLOAD, |
| + SW_REPORTER_MAX); |
| + prefs->SetInteger(prefs::kSwReporterComponentRegisterTryCount, tries + 1); |
| + BrowserThread::PostTask( |
| + BrowserThread::FILE, |
| + FROM_HERE, |
| + base::Bind(&RegisterSwReporterComponentOnFileThread, cus, prefs)); |
| +} |
| + |
| +void MaybeRegisterSwReporterComponent(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) { |
| + UMA_HISTOGRAM_ENUMERATION(kSwReporterComponentHistogramName, |
| + SW_REPORTER_RETRIED_TOO_MANY_EXECUTIONS, |
| + SW_REPORTER_MAX); |
| + // Let's forget about this attempt. |
| + prefs->SetInteger(prefs::kSwReporterComponentExecuteTryCount, 0); |
| + return; |
| + } |
| + } |
| + |
| + RegisterSwReporterComponent(cus, prefs); |
| +} |
| + |
| +void RegisterPrefsForSwReporterComponent(PrefRegistrySimple* registry) { |
| + registry->RegisterIntegerPref(prefs::kSwReporterComponentRegisterTryCount, 0); |
| + registry->RegisterIntegerPref(prefs::kSwReporterComponentExecuteTryCount, 0); |
| +} |
| + |
| +} // namespace component_updater |