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
;
27 namespace component_updater
{
31 // CRX hash. The extension id is: npdjjkjlcidkjlamlmmdelcjbcpdjocm.
32 const uint8 kSha2Hash
[] = {0xdf, 0x39, 0x9a, 0x9b, 0x28, 0x3a, 0x9b, 0x0c,
33 0xbc, 0xc3, 0x4b, 0x29, 0x12, 0xf3, 0x9e, 0x2c,
34 0x19, 0x7a, 0x71, 0x4b, 0x0a, 0x7c, 0x80, 0x1c,
35 0xf6, 0x29, 0x7c, 0x0a, 0x5f, 0xea, 0x67, 0xb7};
37 // File name of the recovery binary on different platforms.
38 const base::FilePath::CharType kRecoveryFileName
[] =
40 FILE_PATH_LITERAL("ChromeRecovery.exe");
41 #else // OS_LINUX, OS_MACOSX, etc.
42 FILE_PATH_LITERAL("ChromeRecovery");
45 const char kRecoveryManifestName
[] = "ChromeRecovery";
49 class RecoveryComponentInstaller
: public ComponentInstaller
{
51 explicit RecoveryComponentInstaller(const Version
& version
,
54 virtual ~RecoveryComponentInstaller() {}
56 virtual void OnUpdateError(int error
) OVERRIDE
;
58 virtual bool Install(const base::DictionaryValue
& manifest
,
59 const base::FilePath
& unpack_path
) OVERRIDE
;
61 virtual bool GetInstalledFile(const std::string
& file
,
62 base::FilePath
* installed_file
) OVERRIDE
;
65 Version current_version_
;
69 void RecoveryRegisterHelper(ComponentUpdateService
* cus
,
71 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
72 Version
version(prefs
->GetString(prefs::kRecoveryComponentVersion
));
73 if (!version
.IsValid()) {
78 CrxComponent recovery
;
79 recovery
.name
= "recovery";
80 recovery
.installer
= new RecoveryComponentInstaller(version
, prefs
);
81 recovery
.version
= version
;
82 recovery
.pk_hash
.assign(kSha2Hash
, &kSha2Hash
[sizeof(kSha2Hash
)]);
83 if (cus
->RegisterComponent(recovery
) != ComponentUpdateService::kOk
) {
84 NOTREACHED() << "Recovery component registration failed.";
88 void RecoveryUpdateVersionHelper(const Version
& version
, PrefService
* prefs
) {
89 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
90 prefs
->SetString(prefs::kRecoveryComponentVersion
, version
.GetString());
93 RecoveryComponentInstaller::RecoveryComponentInstaller(
94 const Version
& version
, PrefService
* prefs
)
95 : current_version_(version
), prefs_(prefs
){
96 DCHECK(version
.IsValid());
99 void RecoveryComponentInstaller::OnUpdateError(int error
) {
100 NOTREACHED() << "Recovery component update error: " << error
;
103 bool RecoveryComponentInstaller::Install(const base::DictionaryValue
& manifest
,
104 const base::FilePath
& unpack_path
) {
106 manifest
.GetStringASCII("name", &name
);
107 if (name
!= kRecoveryManifestName
)
109 std::string proposed_version
;
110 manifest
.GetStringASCII("version", &proposed_version
);
111 Version
version(proposed_version
.c_str());
112 if (!version
.IsValid())
114 if (current_version_
.CompareTo(version
) >= 0)
116 base::FilePath main_file
= unpack_path
.Append(kRecoveryFileName
);
117 if (!base::PathExists(main_file
))
119 // Passed the basic tests. The installation continues with the
120 // recovery component itself running from the temp directory.
121 CommandLine
cmdline(main_file
);
122 std::string arguments
;
123 if (manifest
.GetStringASCII("x-recovery-args", &arguments
))
124 cmdline
.AppendArg(arguments
);
125 std::string add_version
;
126 if (manifest
.GetStringASCII("x-recovery-add-version", &add_version
)) {
127 if (add_version
== "yes")
128 cmdline
.AppendSwitchASCII("version", current_version_
.GetString());
130 current_version_
= version
;
132 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
133 base::Bind(&RecoveryUpdateVersionHelper
, version
, prefs_
));
135 return base::LaunchProcess(cmdline
, base::LaunchOptions(), NULL
);
138 bool RecoveryComponentInstaller::GetInstalledFile(
139 const std::string
& file
, base::FilePath
* installed_file
) {
143 void RegisterRecoveryComponent(ComponentUpdateService
* cus
,
144 PrefService
* prefs
) {
145 #if !defined(OS_CHROMEOS)
146 // We delay execute the registration because we are not required in
147 // the critical path during browser startup.
148 BrowserThread::PostDelayedTask(
151 base::Bind(&RecoveryRegisterHelper
, cus
, prefs
),
152 base::TimeDelta::FromSeconds(6));
156 void RegisterPrefsForRecoveryComponent(PrefRegistrySimple
* registry
) {
157 registry
->RegisterStringPref(prefs::kRecoveryComponentVersion
, "0.0.0.0");
160 } // namespace component_updater