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"
10 #include "base/base_paths.h"
11 #include "base/bind.h"
12 #include "base/command_line.h"
13 #include "base/compiler_specific.h"
14 #include "base/files/file_path.h"
15 #include "base/files/file_util.h"
16 #include "base/logging.h"
17 #include "base/path_service.h"
18 #include "base/prefs/pref_registry_simple.h"
19 #include "base/prefs/pref_service.h"
20 #include "base/process/launch.h"
21 #include "base/strings/string_util.h"
22 #include "base/values.h"
23 #include "components/component_updater/component_updater_paths.h"
24 #include "components/component_updater/component_updater_service.h"
25 #include "components/component_updater/pref_names.h"
26 #include "content/public/browser/browser_thread.h"
28 using content::BrowserThread
;
30 namespace component_updater
{
34 // CRX hash. The extension id is: npdjjkjlcidkjlamlmmdelcjbcpdjocm.
35 const uint8_t kSha2Hash
[] = {0xdf, 0x39, 0x9a, 0x9b, 0x28, 0x3a, 0x9b, 0x0c,
36 0xbc, 0xc3, 0x4b, 0x29, 0x12, 0xf3, 0x9e, 0x2c,
37 0x19, 0x7a, 0x71, 0x4b, 0x0a, 0x7c, 0x80, 0x1c,
38 0xf6, 0x29, 0x7c, 0x0a, 0x5f, 0xea, 0x67, 0xb7};
40 // File name of the recovery binary on different platforms.
41 const base::FilePath::CharType kRecoveryFileName
[] =
43 FILE_PATH_LITERAL("ChromeRecovery.exe");
44 #else // OS_LINUX, OS_MACOSX, etc.
45 FILE_PATH_LITERAL("ChromeRecovery");
48 const char kRecoveryManifestName
[] = "ChromeRecovery";
52 class RecoveryComponentInstaller
: public ComponentInstaller
{
54 explicit RecoveryComponentInstaller(const Version
& version
,
57 virtual ~RecoveryComponentInstaller() {}
59 virtual void OnUpdateError(int error
) override
;
61 virtual bool Install(const base::DictionaryValue
& manifest
,
62 const base::FilePath
& unpack_path
) override
;
64 virtual bool GetInstalledFile(const std::string
& file
,
65 base::FilePath
* installed_file
) override
;
68 Version current_version_
;
72 void RecoveryRegisterHelper(ComponentUpdateService
* cus
, PrefService
* prefs
) {
73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
74 Version
version(prefs
->GetString(prefs::kRecoveryComponentVersion
));
75 if (!version
.IsValid()) {
80 CrxComponent recovery
;
81 recovery
.name
= "recovery";
82 recovery
.installer
= new RecoveryComponentInstaller(version
, prefs
);
83 recovery
.version
= version
;
84 recovery
.pk_hash
.assign(kSha2Hash
, &kSha2Hash
[sizeof(kSha2Hash
)]);
85 if (cus
->RegisterComponent(recovery
) != ComponentUpdateService::kOk
) {
86 NOTREACHED() << "Recovery component registration failed.";
90 void RecoveryUpdateVersionHelper(const Version
& version
, PrefService
* prefs
) {
91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
92 prefs
->SetString(prefs::kRecoveryComponentVersion
, version
.GetString());
95 RecoveryComponentInstaller::RecoveryComponentInstaller(const Version
& version
,
97 : current_version_(version
), prefs_(prefs
) {
98 DCHECK(version
.IsValid());
101 void RecoveryComponentInstaller::OnUpdateError(int error
) {
102 NOTREACHED() << "Recovery component update error: " << error
;
105 bool RecoveryComponentInstaller::Install(const base::DictionaryValue
& manifest
,
106 const base::FilePath
& unpack_path
) {
108 manifest
.GetStringASCII("name", &name
);
109 if (name
!= kRecoveryManifestName
)
111 std::string proposed_version
;
112 manifest
.GetStringASCII("version", &proposed_version
);
113 Version
version(proposed_version
.c_str());
114 if (!version
.IsValid())
116 if (current_version_
.CompareTo(version
) >= 0)
119 // Passed the basic tests. Copy the installation to a permanent directory.
121 if (!PathService::Get(DIR_RECOVERY_BASE
, &path
))
123 path
= path
.AppendASCII(version
.GetString());
124 if (base::PathExists(path
) && !base::DeleteFile(path
, true))
126 if (!base::Move(unpack_path
, path
)) {
127 DVLOG(1) << "Recovery component move failed.";
131 base::FilePath main_file
= path
.Append(kRecoveryFileName
);
132 if (!base::PathExists(main_file
))
134 // Run the recovery component.
135 CommandLine
cmdline(main_file
);
136 std::string arguments
;
137 if (manifest
.GetStringASCII("x-recovery-args", &arguments
))
138 cmdline
.AppendArg(arguments
);
139 std::string add_version
;
140 if (manifest
.GetStringASCII("x-recovery-add-version", &add_version
)) {
141 if (add_version
== "yes")
142 cmdline
.AppendSwitchASCII("version", current_version_
.GetString());
144 current_version_
= version
;
146 BrowserThread::PostTask(
149 base::Bind(&RecoveryUpdateVersionHelper
, version
, prefs_
));
151 return base::LaunchProcess(cmdline
, base::LaunchOptions(), NULL
);
154 bool RecoveryComponentInstaller::GetInstalledFile(
155 const std::string
& file
,
156 base::FilePath
* installed_file
) {
160 void RegisterRecoveryComponent(ComponentUpdateService
* cus
,
161 PrefService
* prefs
) {
162 #if !defined(OS_CHROMEOS)
163 // We delay execute the registration because we are not required in
164 // the critical path during browser startup.
165 BrowserThread::PostDelayedTask(
168 base::Bind(&RecoveryRegisterHelper
, cus
, prefs
),
169 base::TimeDelta::FromSeconds(6));
173 void RegisterPrefsForRecoveryComponent(PrefRegistrySimple
* registry
) {
174 registry
->RegisterStringPref(prefs::kRecoveryComponentVersion
, "0.0.0.0");
177 } // namespace component_updater