1 // Copyright (c) 2012 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.
5 #include "chrome/browser/component_updater/recovery_component_installer.h"
7 #include "base/base_paths.h"
9 #include "base/command_line.h"
10 #include "base/compiler_specific.h"
11 #include "base/file_util.h"
12 #include "base/files/file_path.h"
13 #include "base/logging.h"
14 #include "base/path_service.h"
15 #include "base/prefs/pref_registry_simple.h"
16 #include "base/prefs/pref_service.h"
17 #include "base/process/launch.h"
18 #include "base/strings/string_util.h"
19 #include "base/values.h"
20 #include "chrome/browser/component_updater/component_updater_service.h"
21 #include "chrome/common/chrome_version_info.h"
22 #include "chrome/common/pref_names.h"
23 #include "content/public/browser/browser_thread.h"
25 using content::BrowserThread;
29 // CRX hash. The extension id is: npdjjkjlcidkjlamlmmdelcjbcpdjocm.
30 const uint8 kSha2Hash[] = {0xdf, 0x39, 0x9a, 0x9b, 0x28, 0x3a, 0x9b, 0x0c,
31 0xbc, 0xc3, 0x4b, 0x29, 0x12, 0xf3, 0x9e, 0x2c,
32 0x19, 0x7a, 0x71, 0x4b, 0x0a, 0x7c, 0x80, 0x1c,
33 0xf6, 0x29, 0x7c, 0x0a, 0x5f, 0xea, 0x67, 0xb7};
35 // File name of the recovery binary on different platforms.
36 const base::FilePath::CharType kRecoveryFileName[] =
38 FILE_PATH_LITERAL("ChromeRecovery.exe");
39 #else // OS_LINUX, OS_MACOSX, etc.
40 FILE_PATH_LITERAL("ChromeRecovery");
43 const char kRecoveryManifestName[] = "ChromeRecovery";
47 class RecoveryComponentInstaller : public ComponentInstaller {
49 explicit RecoveryComponentInstaller(const Version& version,
52 virtual ~RecoveryComponentInstaller() {}
54 virtual void OnUpdateError(int error) OVERRIDE;
56 virtual bool Install(const base::DictionaryValue& manifest,
57 const base::FilePath& unpack_path) OVERRIDE;
59 virtual bool GetInstalledFile(const std::string& file,
60 base::FilePath* installed_file) OVERRIDE;
63 Version current_version_;
67 void RecoveryRegisterHelper(ComponentUpdateService* cus,
69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
70 Version version(prefs->GetString(prefs::kRecoveryComponentVersion));
71 if (!version.IsValid()) {
76 CrxComponent recovery;
77 recovery.name = "recovery";
78 recovery.installer = new RecoveryComponentInstaller(version, prefs);
79 recovery.version = version;
80 recovery.pk_hash.assign(kSha2Hash, &kSha2Hash[sizeof(kSha2Hash)]);
81 if (cus->RegisterComponent(recovery) != ComponentUpdateService::kOk) {
82 NOTREACHED() << "Recovery component registration failed.";
86 void RecoveryUpdateVersionHelper(const Version& version, PrefService* prefs) {
87 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
88 prefs->SetString(prefs::kRecoveryComponentVersion, version.GetString());
91 RecoveryComponentInstaller::RecoveryComponentInstaller(
92 const Version& version, PrefService* prefs)
93 : current_version_(version), prefs_(prefs){
94 DCHECK(version.IsValid());
97 void RecoveryComponentInstaller::OnUpdateError(int error) {
98 NOTREACHED() << "Recovery component update error: " << error;
101 bool RecoveryComponentInstaller::Install(const base::DictionaryValue& manifest,
102 const base::FilePath& unpack_path) {
104 manifest.GetStringASCII("name", &name);
105 if (name != kRecoveryManifestName)
107 std::string proposed_version;
108 manifest.GetStringASCII("version", &proposed_version);
109 Version version(proposed_version.c_str());
110 if (!version.IsValid())
112 if (current_version_.CompareTo(version) >= 0)
114 base::FilePath main_file = unpack_path.Append(kRecoveryFileName);
115 if (!base::PathExists(main_file))
117 // Passed the basic tests. The installation continues with the
118 // recovery component itself running from the temp directory.
119 CommandLine cmdline(main_file);
120 std::string arguments;
121 if (manifest.GetStringASCII("x-recovery-args", &arguments))
122 cmdline.AppendArg(arguments);
123 std::string add_version;
124 if (manifest.GetStringASCII("x-recovery-add-version", &add_version)) {
125 if (add_version == "yes")
126 cmdline.AppendSwitchASCII("version", current_version_.GetString());
128 current_version_ = version;
130 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
131 base::Bind(&RecoveryUpdateVersionHelper, version, prefs_));
133 return base::LaunchProcess(cmdline, base::LaunchOptions(), NULL);
136 bool RecoveryComponentInstaller::GetInstalledFile(
137 const std::string& file, base::FilePath* installed_file) {
141 void RegisterRecoveryComponent(ComponentUpdateService* cus,
142 PrefService* prefs) {
143 #if !defined(OS_CHROMEOS)
144 // We delay execute the registration because we are not required in
145 // the critical path during browser startup.
146 BrowserThread::PostDelayedTask(
149 base::Bind(&RecoveryRegisterHelper, cus, prefs),
150 base::TimeDelta::FromSeconds(6));
154 void RegisterPrefsForRecoveryComponent(PrefRegistrySimple* registry) {
155 registry->RegisterStringPref(prefs::kRecoveryComponentVersion, "0.0.0.0");