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"
9 #include "base/base_paths.h"
10 #include "base/bind.h"
11 #include "base/command_line.h"
12 #include "base/compiler_specific.h"
13 #include "base/files/file_path.h"
14 #include "base/files/file_util.h"
15 #include "base/logging.h"
16 #include "base/path_service.h"
17 #include "base/prefs/pref_registry_simple.h"
18 #include "base/prefs/pref_service.h"
19 #include "base/process/launch.h"
20 #include "base/strings/string_util.h"
21 #include "base/values.h"
22 #include "components/component_updater/component_updater_paths.h"
23 #include "components/component_updater/component_updater_service.h"
24 #include "components/component_updater/pref_names.h"
25 #include "content/public/browser/browser_thread.h"
27 using content::BrowserThread
;
29 namespace component_updater
{
33 // CRX hash. The extension id is: npdjjkjlcidkjlamlmmdelcjbcpdjocm.
34 const uint8 kSha2Hash
[] = {0xdf, 0x39, 0x9a, 0x9b, 0x28, 0x3a, 0x9b, 0x0c,
35 0xbc, 0xc3, 0x4b, 0x29, 0x12, 0xf3, 0x9e, 0x2c,
36 0x19, 0x7a, 0x71, 0x4b, 0x0a, 0x7c, 0x80, 0x1c,
37 0xf6, 0x29, 0x7c, 0x0a, 0x5f, 0xea, 0x67, 0xb7};
39 // File name of the recovery binary on different platforms.
40 const base::FilePath::CharType kRecoveryFileName
[] =
42 FILE_PATH_LITERAL("ChromeRecovery.exe");
43 #else // OS_LINUX, OS_MACOSX, etc.
44 FILE_PATH_LITERAL("ChromeRecovery");
47 const char kRecoveryManifestName
[] = "ChromeRecovery";
51 class RecoveryComponentInstaller
: public ComponentInstaller
{
53 explicit RecoveryComponentInstaller(const Version
& version
,
56 virtual ~RecoveryComponentInstaller() {}
58 virtual void OnUpdateError(int error
) OVERRIDE
;
60 virtual bool Install(const base::DictionaryValue
& manifest
,
61 const base::FilePath
& unpack_path
) OVERRIDE
;
63 virtual bool GetInstalledFile(const std::string
& file
,
64 base::FilePath
* installed_file
) OVERRIDE
;
67 Version current_version_
;
71 void RecoveryRegisterHelper(ComponentUpdateService
* cus
, PrefService
* prefs
) {
72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
73 Version
version(prefs
->GetString(prefs::kRecoveryComponentVersion
));
74 if (!version
.IsValid()) {
79 CrxComponent recovery
;
80 recovery
.name
= "recovery";
81 recovery
.installer
= new RecoveryComponentInstaller(version
, prefs
);
82 recovery
.version
= version
;
83 recovery
.pk_hash
.assign(kSha2Hash
, &kSha2Hash
[sizeof(kSha2Hash
)]);
84 if (cus
->RegisterComponent(recovery
) != ComponentUpdateService::kOk
) {
85 NOTREACHED() << "Recovery component registration failed.";
89 void RecoveryUpdateVersionHelper(const Version
& version
, PrefService
* prefs
) {
90 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
91 prefs
->SetString(prefs::kRecoveryComponentVersion
, version
.GetString());
94 RecoveryComponentInstaller::RecoveryComponentInstaller(const Version
& version
,
96 : current_version_(version
), prefs_(prefs
) {
97 DCHECK(version
.IsValid());
100 void RecoveryComponentInstaller::OnUpdateError(int error
) {
101 NOTREACHED() << "Recovery component update error: " << error
;
104 bool RecoveryComponentInstaller::Install(const base::DictionaryValue
& manifest
,
105 const base::FilePath
& unpack_path
) {
107 manifest
.GetStringASCII("name", &name
);
108 if (name
!= kRecoveryManifestName
)
110 std::string proposed_version
;
111 manifest
.GetStringASCII("version", &proposed_version
);
112 Version
version(proposed_version
.c_str());
113 if (!version
.IsValid())
115 if (current_version_
.CompareTo(version
) >= 0)
118 // Passed the basic tests. Copy the installation to a permanent directory.
120 if (!PathService::Get(DIR_RECOVERY_BASE
, &path
))
122 path
= path
.AppendASCII(version
.GetString());
123 if (base::PathExists(path
) && !base::DeleteFile(path
, true))
125 if (!base::Move(unpack_path
, path
)) {
126 DVLOG(1) << "Recovery component move failed.";
130 base::FilePath main_file
= path
.Append(kRecoveryFileName
);
131 if (!base::PathExists(main_file
))
133 // Run the recovery component.
134 CommandLine
cmdline(main_file
);
135 std::string arguments
;
136 if (manifest
.GetStringASCII("x-recovery-args", &arguments
))
137 cmdline
.AppendArg(arguments
);
138 std::string add_version
;
139 if (manifest
.GetStringASCII("x-recovery-add-version", &add_version
)) {
140 if (add_version
== "yes")
141 cmdline
.AppendSwitchASCII("version", current_version_
.GetString());
143 current_version_
= version
;
145 BrowserThread::PostTask(
148 base::Bind(&RecoveryUpdateVersionHelper
, version
, prefs_
));
150 return base::LaunchProcess(cmdline
, base::LaunchOptions(), NULL
);
153 bool RecoveryComponentInstaller::GetInstalledFile(
154 const std::string
& file
,
155 base::FilePath
* installed_file
) {
159 void RegisterRecoveryComponent(ComponentUpdateService
* cus
,
160 PrefService
* prefs
) {
161 #if !defined(OS_CHROMEOS)
162 // We delay execute the registration because we are not required in
163 // the critical path during browser startup.
164 BrowserThread::PostDelayedTask(
167 base::Bind(&RecoveryRegisterHelper
, cus
, prefs
),
168 base::TimeDelta::FromSeconds(6));
172 void RegisterPrefsForRecoveryComponent(PrefRegistrySimple
* registry
) {
173 registry
->RegisterStringPref(prefs::kRecoveryComponentVersion
, "0.0.0.0");
176 } // namespace component_updater