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.
4
5 #include "chrome/browser/component_updater/recovery_component_installer.h"
6
7 #include <string>
8
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/file_util.h"
14 #include "base/files/file_path.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 "chrome/browser/component_updater/component_updater_service.h"
23 #include "chrome/common/chrome_paths.h"
24 #include "chrome/common/chrome_version_info.h"
25 #include "chrome/common/pref_names.h"
26 #include "content/public/browser/browser_thread.h"
27
28 using content::BrowserThread;
29
30 namespace component_updater {
31
32 namespace {
33
34 // CRX hash. The extension id is: npdjjkjlcidkjlamlmmdelcjbcpdjocm.
35 const uint8 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};
39
40 // File name of the recovery binary on different platforms.
41 const base::FilePath::CharType kRecoveryFileName[] =
42 #if defined(OS_WIN)
43 FILE_PATH_LITERAL("ChromeRecovery.exe");
44 #else // OS_LINUX, OS_MACOSX, etc.
45 FILE_PATH_LITERAL("ChromeRecovery");
46 #endif
47
48 const char kRecoveryManifestName[] = "ChromeRecovery";
49
50 } // namespace
51
52 class RecoveryComponentInstaller : public ComponentInstaller {
53 public:
54 explicit RecoveryComponentInstaller(const Version& version,
55 PrefService* prefs);
56
~RecoveryComponentInstaller()57 virtual ~RecoveryComponentInstaller() {}
58
59 virtual void OnUpdateError(int error) OVERRIDE;
60
61 virtual bool Install(const base::DictionaryValue& manifest,
62 const base::FilePath& unpack_path) OVERRIDE;
63
64 virtual bool GetInstalledFile(const std::string& file,
65 base::FilePath* installed_file) OVERRIDE;
66
67 private:
68 Version current_version_;
69 PrefService* prefs_;
70 };
71
RecoveryRegisterHelper(ComponentUpdateService * cus,PrefService * prefs)72 void RecoveryRegisterHelper(ComponentUpdateService* cus, PrefService* prefs) {
73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
74 Version version(prefs->GetString(prefs::kRecoveryComponentVersion));
75 if (!version.IsValid()) {
76 NOTREACHED();
77 return;
78 }
79
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.";
87 }
88 }
89
RecoveryUpdateVersionHelper(const Version & version,PrefService * prefs)90 void RecoveryUpdateVersionHelper(const Version& version, PrefService* prefs) {
91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
92 prefs->SetString(prefs::kRecoveryComponentVersion, version.GetString());
93 }
94
RecoveryComponentInstaller(const Version & version,PrefService * prefs)95 RecoveryComponentInstaller::RecoveryComponentInstaller(const Version& version,
96 PrefService* prefs)
97 : current_version_(version), prefs_(prefs) {
98 DCHECK(version.IsValid());
99 }
100
OnUpdateError(int error)101 void RecoveryComponentInstaller::OnUpdateError(int error) {
102 NOTREACHED() << "Recovery component update error: " << error;
103 }
104
Install(const base::DictionaryValue & manifest,const base::FilePath & unpack_path)105 bool RecoveryComponentInstaller::Install(const base::DictionaryValue& manifest,
106 const base::FilePath& unpack_path) {
107 std::string name;
108 manifest.GetStringASCII("name", &name);
109 if (name != kRecoveryManifestName)
110 return false;
111 std::string proposed_version;
112 manifest.GetStringASCII("version", &proposed_version);
113 Version version(proposed_version.c_str());
114 if (!version.IsValid())
115 return false;
116 if (current_version_.CompareTo(version) >= 0)
117 return false;
118
119 // Passed the basic tests. Copy the installation to a permanent directory.
120 base::FilePath path;
121 if (!PathService::Get(chrome::DIR_RECOVERY_BASE, &path))
122 return false;
123 path = path.AppendASCII(version.GetString());
124 if (base::PathExists(path) && !base::DeleteFile(path, true))
125 return false;
126 if (!base::Move(unpack_path, path)) {
127 DVLOG(1) << "Recovery component move failed.";
128 return false;
129 }
130
131 base::FilePath main_file = path.Append(kRecoveryFileName);
132 if (!base::PathExists(main_file))
133 return false;
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());
143 }
144 current_version_ = version;
145 if (prefs_) {
146 BrowserThread::PostTask(
147 BrowserThread::UI,
148 FROM_HERE,
149 base::Bind(&RecoveryUpdateVersionHelper, version, prefs_));
150 }
151 return base::LaunchProcess(cmdline, base::LaunchOptions(), NULL);
152 }
153
GetInstalledFile(const std::string & file,base::FilePath * installed_file)154 bool RecoveryComponentInstaller::GetInstalledFile(
155 const std::string& file,
156 base::FilePath* installed_file) {
157 return false;
158 }
159
RegisterRecoveryComponent(ComponentUpdateService * cus,PrefService * prefs)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(
166 BrowserThread::UI,
167 FROM_HERE,
168 base::Bind(&RecoveryRegisterHelper, cus, prefs),
169 base::TimeDelta::FromSeconds(6));
170 #endif
171 }
172
RegisterPrefsForRecoveryComponent(PrefRegistrySimple * registry)173 void RegisterPrefsForRecoveryComponent(PrefRegistrySimple* registry) {
174 registry->RegisterStringPref(prefs::kRecoveryComponentVersion, "0.0.0.0");
175 }
176
177 } // namespace component_updater
178