#include "chrome/browser/extensions/app_host/update.h"
#include <windows.h>
#include "base/command_line.h"
#include "base/file_version_info.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/process_util.h"
#include "base/string16.h"
#include "base/string_util.h"
#include "base/version.h"
#include "base/win/registry.h"
#include "chrome/installer/launcher_support/chrome_launcher_support.h"
namespace app_host {
namespace {
const wchar_t kGoogleRegClientsKey[] = L"Software\\Google\\Update\\Clients\\";
const char kMultiInstall[] = "multi-install";
const char kChromeAppHost[] = "app-host";
const char kVerboseLogging[] = "verbose-logging";
const wchar_t kRegVersionField[] = L"pv";
const wchar_t kBinariesAppGuid[] = L"{4DC8B4CA-1BDA-483e-B5FA-D3C12E15B62D}";
const wchar_t kBrowserAppGuid[] = L"{8A69D345-D564-463c-AFF1-A69D9E530F96}";
Version GetAppHostVersion() {
scoped_ptr<FileVersionInfo> version_info(
FileVersionInfo::CreateFileVersionInfoForCurrentModule());
Version app_host_version(WideToASCII(version_info->product_version()));
DCHECK(app_host_version.IsValid());
return app_host_version;
}
Version GetAppVersionFromRegistry(const wchar_t* app_guid) {
HKEY root_key = HKEY_LOCAL_MACHINE;
string16 client_key(kGoogleRegClientsKey);
client_key.append(app_guid);
base::win::RegKey reg_key;
string16 version_str;
if ((reg_key.Open(root_key, client_key.c_str(),
KEY_QUERY_VALUE) == ERROR_SUCCESS) &&
(reg_key.ReadValue(kRegVersionField, &version_str) == ERROR_SUCCESS)) {
return Version(WideToASCII(version_str));
}
return Version();
}
bool LaunchAppHostUpdate() {
FilePath setup_exe(
chrome_launcher_support::GetSetupExeForInstallationLevel(
chrome_launcher_support::SYSTEM_LEVEL_INSTALLATION));
if (setup_exe.empty()) {
LOG(ERROR) << "Failed to find setup.exe";
return false;
}
CommandLine cmd_line(setup_exe);
cmd_line.AppendSwitch(kMultiInstall);
cmd_line.AppendSwitch(kChromeAppHost);
cmd_line.AppendSwitch(kVerboseLogging);
LOG(INFO) << "Launching: " << cmd_line.GetCommandLineString();
return base::LaunchProcess(cmd_line, base::LaunchOptions(), NULL);
}
}
void EnsureAppHostUpToDate() {
Version new_version(GetAppVersionFromRegistry(kBinariesAppGuid));
if (!new_version.IsValid())
new_version = GetAppVersionFromRegistry(kBrowserAppGuid);
if (!new_version.IsValid())
return;
Version app_host_version(GetAppHostVersion());
if (app_host_version.CompareTo(new_version) < 0) {
LOG(INFO) << "Updating App Host from " << app_host_version.GetString()
<< " to " << new_version.GetString();
if (!LaunchAppHostUpdate())
LOG(ERROR) << "Failed to launch App Host update.";
}
}
}