OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/component_updater/recovery_component_installer.h" | 5 #include "chrome/browser/component_updater/recovery_component_installer.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/base_paths.h" | 9 #include "base/base_paths.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
13 #include "base/file_util.h" | 13 #include "base/file_util.h" |
14 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/memory/singleton.h" |
16 #include "base/path_service.h" | 17 #include "base/path_service.h" |
17 #include "base/prefs/pref_registry_simple.h" | 18 #include "base/prefs/pref_registry_simple.h" |
18 #include "base/prefs/pref_service.h" | 19 #include "base/prefs/pref_service.h" |
| 20 #include "base/process/kill.h" |
19 #include "base/process/launch.h" | 21 #include "base/process/launch.h" |
20 #include "base/strings/string_util.h" | 22 #include "base/strings/string_util.h" |
21 #include "base/values.h" | 23 #include "base/values.h" |
| 24 #include "chrome/browser/component_updater/component_unpacker.h" |
22 #include "chrome/browser/component_updater/component_updater_service.h" | 25 #include "chrome/browser/component_updater/component_updater_service.h" |
23 #include "chrome/common/chrome_paths.h" | 26 #include "chrome/common/chrome_paths.h" |
24 #include "chrome/common/chrome_version_info.h" | 27 #include "chrome/common/chrome_version_info.h" |
25 #include "chrome/common/pref_names.h" | 28 #include "chrome/common/pref_names.h" |
26 #include "content/public/browser/browser_thread.h" | 29 #include "content/public/browser/browser_thread.h" |
27 | 30 |
28 using content::BrowserThread; | 31 using content::BrowserThread; |
29 | 32 |
30 namespace component_updater { | 33 namespace component_updater { |
31 | 34 |
32 namespace { | 35 namespace { |
33 | 36 |
34 // CRX hash. The extension id is: npdjjkjlcidkjlamlmmdelcjbcpdjocm. | 37 // CRX hash. The extension id is: npdjjkjlcidkjlamlmmdelcjbcpdjocm. |
35 const uint8 kSha2Hash[] = {0xdf, 0x39, 0x9a, 0x9b, 0x28, 0x3a, 0x9b, 0x0c, | 38 const uint8 kSha2Hash[] = {0xdf, 0x39, 0x9a, 0x9b, 0x28, 0x3a, 0x9b, 0x0c, |
36 0xbc, 0xc3, 0x4b, 0x29, 0x12, 0xf3, 0x9e, 0x2c, | 39 0xbc, 0xc3, 0x4b, 0x29, 0x12, 0xf3, 0x9e, 0x2c, |
37 0x19, 0x7a, 0x71, 0x4b, 0x0a, 0x7c, 0x80, 0x1c, | 40 0x19, 0x7a, 0x71, 0x4b, 0x0a, 0x7c, 0x80, 0x1c, |
38 0xf6, 0x29, 0x7c, 0x0a, 0x5f, 0xea, 0x67, 0xb7}; | 41 0xf6, 0x29, 0x7c, 0x0a, 0x5f, 0xea, 0x67, 0xb7}; |
39 | 42 |
40 // File name of the recovery binary on different platforms. | 43 // File name of the recovery binary on different platforms. |
41 const base::FilePath::CharType kRecoveryFileName[] = | 44 const base::FilePath::CharType kRecoveryFileName[] = |
42 #if defined(OS_WIN) | 45 #if defined(OS_WIN) |
43 FILE_PATH_LITERAL("ChromeRecovery.exe"); | 46 FILE_PATH_LITERAL("ChromeRecovery.exe"); |
44 #else // OS_LINUX, OS_MACOSX, etc. | 47 #else // OS_LINUX, OS_MACOSX, etc. |
45 FILE_PATH_LITERAL("ChromeRecovery"); | 48 FILE_PATH_LITERAL("ChromeRecovery"); |
46 #endif | 49 #endif |
47 | 50 |
48 const char kRecoveryManifestName[] = "ChromeRecovery"; | 51 const char kRecoveryManifestName[] = "ChromeRecovery"; |
49 | 52 |
| 53 // This is a contract between ChromeRecovery and this installer for the |
| 54 // meaning of ChromeRecovery process exit code. |
| 55 enum ChromeRecvoeryExitCode { |
| 56 EXIT_CODE_RECOVERY_SUCCEEDED = 0, |
| 57 EXIT_CODE_RECOVERY_SKIPPED = 1, |
| 58 EXIT_CODE_ELEVATION_NEEDED = 2, |
| 59 }; |
| 60 |
50 } // namespace | 61 } // namespace |
51 | 62 |
52 class RecoveryComponentInstaller : public ComponentInstaller { | 63 class RecoveryComponentInstaller : public ComponentInstaller { |
53 public: | 64 public: |
54 explicit RecoveryComponentInstaller(const Version& version, | 65 RecoveryComponentInstaller(const Version& version, |
55 PrefService* prefs); | 66 PrefService* prefs, |
| 67 bool elevated_install); |
56 | 68 |
57 virtual ~RecoveryComponentInstaller() {} | 69 virtual ~RecoveryComponentInstaller() {} |
58 | 70 |
59 virtual void OnUpdateError(int error) OVERRIDE; | 71 virtual void OnUpdateError(int error) OVERRIDE; |
60 | 72 |
61 virtual bool Install(const base::DictionaryValue& manifest, | 73 virtual bool Install(const base::DictionaryValue& manifest, |
62 const base::FilePath& unpack_path) OVERRIDE; | 74 const base::FilePath& unpack_path) OVERRIDE; |
63 | 75 |
| 76 virtual void OnInstallError( |
| 77 const InstallerSourceSerializer& serializer) OVERRIDE; |
| 78 |
64 virtual bool GetInstalledFile(const std::string& file, | 79 virtual bool GetInstalledFile(const std::string& file, |
65 base::FilePath* installed_file) OVERRIDE; | 80 base::FilePath* installed_file) OVERRIDE; |
66 | 81 |
67 private: | 82 private: |
| 83 static void DoneSaveInstallSourceInfo(PrefService* prefs, bool result); |
| 84 |
68 Version current_version_; | 85 Version current_version_; |
69 PrefService* prefs_; | 86 PrefService* prefs_; |
| 87 bool elevated_install_; |
| 88 int installer_exit_code_; |
70 }; | 89 }; |
71 | 90 |
72 void RecoveryRegisterHelper(ComponentUpdateService* cus, PrefService* prefs) { | 91 class ElevatedInstallWrapper { |
| 92 public: |
| 93 static ElevatedInstallWrapper* GetInstance(); |
| 94 void SaveInstallSourceInfo(const InstallerSourceSerializer& serializer, |
| 95 const base::Callback<void(bool)>& callback); |
| 96 |
| 97 void Install(PrefService* prefs, bool elevation_allowed, |
| 98 const base::Closure& callback); |
| 99 |
| 100 private: |
| 101 friend struct DefaultSingletonTraits<ElevatedInstallWrapper>; |
| 102 |
| 103 ElevatedInstallWrapper(); |
| 104 |
| 105 bool DoSaveInstallSourceInfo(const InstallerSourceSerializer& serializer); |
| 106 |
| 107 void DoInstall(PrefService* prefs, const Version& version); |
| 108 void DoneInstall(ComponentUnpacker::Error error, int extended_error); |
| 109 |
| 110 static base::FilePath GetInstallerBackupPath(); |
| 111 |
| 112 PrefService* prefs_; |
| 113 scoped_refptr<ComponentUnpacker> unpacker_; |
| 114 scoped_ptr<RecoveryComponentInstaller> rc_installer_; |
| 115 |
| 116 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; |
| 117 |
| 118 DISALLOW_COPY_AND_ASSIGN(ElevatedInstallWrapper); |
| 119 }; |
| 120 |
| 121 void RecoveryRegisterHelper(ComponentUpdateService* cus, |
| 122 PrefService* prefs) { |
73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 123 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
74 Version version(prefs->GetString(prefs::kRecoveryComponentVersion)); | 124 Version version(prefs->GetString(prefs::kRecoveryComponentVersion)); |
75 if (!version.IsValid()) { | 125 if (!version.IsValid()) { |
76 NOTREACHED(); | 126 NOTREACHED(); |
77 return; | 127 return; |
78 } | 128 } |
79 | 129 |
80 CrxComponent recovery; | 130 CrxComponent recovery; |
81 recovery.name = "recovery"; | 131 recovery.name = "recovery"; |
82 recovery.installer = new RecoveryComponentInstaller(version, prefs); | 132 recovery.installer = new RecoveryComponentInstaller(version, prefs, false); |
83 recovery.version = version; | 133 recovery.version = version; |
84 recovery.pk_hash.assign(kSha2Hash, &kSha2Hash[sizeof(kSha2Hash)]); | 134 recovery.pk_hash.assign(kSha2Hash, &kSha2Hash[sizeof(kSha2Hash)]); |
85 if (cus->RegisterComponent(recovery) != ComponentUpdateService::kOk) { | 135 if (cus->RegisterComponent(recovery) != ComponentUpdateService::kOk) { |
86 NOTREACHED() << "Recovery component registration failed."; | 136 NOTREACHED() << "Recovery component registration failed."; |
87 } | 137 } |
88 } | 138 } |
89 | 139 |
90 void RecoveryUpdateVersionHelper(const Version& version, PrefService* prefs) { | 140 void RecoveryUpdateVersionHelper(const Version& version, PrefService* prefs) { |
91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 141 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
92 prefs->SetString(prefs::kRecoveryComponentVersion, version.GetString()); | 142 prefs->SetString(prefs::kRecoveryComponentVersion, version.GetString()); |
93 } | 143 } |
94 | 144 |
95 RecoveryComponentInstaller::RecoveryComponentInstaller(const Version& version, | 145 void RecoveryUpdateElevationHelper(bool needs_elevation, PrefService* prefs) { |
96 PrefService* prefs) | 146 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
97 : current_version_(version), prefs_(prefs) { | 147 prefs->SetBoolean(prefs::kRecoveryComponentNeedsElevation, needs_elevation); |
| 148 } |
| 149 |
| 150 RecoveryComponentInstaller::RecoveryComponentInstaller( |
| 151 const Version& version, PrefService* prefs, bool elevated_install) |
| 152 : current_version_(version), |
| 153 prefs_(prefs), |
| 154 elevated_install_(elevated_install), |
| 155 installer_exit_code_(0) { |
98 DCHECK(version.IsValid()); | 156 DCHECK(version.IsValid()); |
99 } | 157 } |
100 | 158 |
101 void RecoveryComponentInstaller::OnUpdateError(int error) { | 159 void RecoveryComponentInstaller::OnUpdateError(int error) { |
102 NOTREACHED() << "Recovery component update error: " << error; | 160 NOTREACHED() << "Recovery component update error: " << error; |
103 } | 161 } |
104 | 162 |
105 bool RecoveryComponentInstaller::Install(const base::DictionaryValue& manifest, | 163 bool RecoveryComponentInstaller::Install(const base::DictionaryValue& manifest, |
106 const base::FilePath& unpack_path) { | 164 const base::FilePath& unpack_path) { |
107 std::string name; | 165 std::string name; |
108 manifest.GetStringASCII("name", &name); | 166 manifest.GetStringASCII("name", &name); |
109 if (name != kRecoveryManifestName) | 167 if (name != kRecoveryManifestName) |
110 return false; | 168 return false; |
111 std::string proposed_version; | 169 std::string proposed_version; |
112 manifest.GetStringASCII("version", &proposed_version); | 170 manifest.GetStringASCII("version", &proposed_version); |
113 Version version(proposed_version.c_str()); | 171 Version version(proposed_version.c_str()); |
114 if (!version.IsValid()) | 172 if (!version.IsValid()) |
115 return false; | 173 return false; |
116 if (current_version_.CompareTo(version) >= 0) | 174 if (current_version_.CompareTo(version) >= 0) |
117 return false; | 175 return false; |
118 | 176 base::FilePath main_file = unpack_path.Append(kRecoveryFileName); |
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)) | 177 if (!base::PathExists(main_file)) |
133 return false; | 178 return false; |
134 // Run the recovery component. | 179 // Passed the basic tests. The installation continues with the |
| 180 // recovery component itself running from the temp directory. |
135 CommandLine cmdline(main_file); | 181 CommandLine cmdline(main_file); |
136 std::string arguments; | 182 std::string arguments; |
137 if (manifest.GetStringASCII("x-recovery-args", &arguments)) | 183 if (manifest.GetStringASCII("x-recovery-args", &arguments)) |
138 cmdline.AppendArg(arguments); | 184 cmdline.AppendArg(arguments); |
139 std::string add_version; | 185 std::string add_version; |
140 if (manifest.GetStringASCII("x-recovery-add-version", &add_version)) { | 186 if (manifest.GetStringASCII("x-recovery-add-version", &add_version)) { |
141 if (add_version == "yes") | 187 if (add_version == "yes") |
142 cmdline.AppendSwitchASCII("version", current_version_.GetString()); | 188 cmdline.AppendSwitchASCII("version", current_version_.GetString()); |
143 } | 189 } |
144 current_version_ = version; | 190 current_version_ = version; |
145 if (prefs_) { | 191 if (prefs_) { |
146 BrowserThread::PostTask( | 192 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
147 BrowserThread::UI, | |
148 FROM_HERE, | |
149 base::Bind(&RecoveryUpdateVersionHelper, version, prefs_)); | 193 base::Bind(&RecoveryUpdateVersionHelper, version, prefs_)); |
150 } | 194 } |
151 return base::LaunchProcess(cmdline, base::LaunchOptions(), NULL); | 195 |
| 196 if (elevated_install_) { |
| 197 return base::LaunchElevatedProcess(cmdline, base::LaunchOptions(), NULL); |
| 198 } |
| 199 |
| 200 base::ProcessHandle process_handle; |
| 201 if (!base::LaunchProcess(cmdline, base::LaunchOptions(), &process_handle)) { |
| 202 return false; |
| 203 } |
| 204 |
| 205 installer_exit_code_ = 0; |
| 206 const base::TimeDelta kMaxWaitTime = base::TimeDelta::FromSeconds(60); |
| 207 if (!base::WaitForExitCodeWithTimeout(process_handle, |
| 208 &installer_exit_code_, |
| 209 kMaxWaitTime)) { |
| 210 // Ensure that the process terminates. |
| 211 base::KillProcess(process_handle, -1, true); |
| 212 return false; |
| 213 } |
| 214 |
| 215 return true; |
| 216 } |
| 217 |
| 218 void RecoveryComponentInstaller::OnInstallError( |
| 219 const InstallerSourceSerializer& serializer) { |
| 220 if (installer_exit_code_ == EXIT_CODE_ELEVATION_NEEDED && prefs_) { |
| 221 ElevatedInstallWrapper::GetInstance()->SaveInstallSourceInfo( |
| 222 serializer, |
| 223 base::Bind(&RecoveryComponentInstaller::DoneSaveInstallSourceInfo, |
| 224 prefs_)); |
| 225 } |
152 } | 226 } |
153 | 227 |
154 bool RecoveryComponentInstaller::GetInstalledFile( | 228 bool RecoveryComponentInstaller::GetInstalledFile( |
155 const std::string& file, | 229 const std::string& file, base::FilePath* installed_file) { |
156 base::FilePath* installed_file) { | |
157 return false; | 230 return false; |
158 } | 231 } |
159 | 232 |
| 233 void RecoveryComponentInstaller::DoneSaveInstallSourceInfo(PrefService* prefs, |
| 234 bool result) { |
| 235 if (!result) { |
| 236 DLOG(WARNING) << "Failed to save CRX file for elevated install."; |
| 237 return; |
| 238 } |
| 239 |
| 240 // Set a flag in the preference so UpgradeDetector can detect and notify user. |
| 241 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 242 base::Bind(&RecoveryUpdateElevationHelper, true, prefs)); |
| 243 } |
| 244 |
| 245 ElevatedInstallWrapper::ElevatedInstallWrapper() : prefs_(NULL) { |
| 246 // |blocking_task_runner_| is responsbile for elevated install of recovery |
| 247 // component and other related file operations (namely installer source backup |
| 248 // and cleanup). |
| 249 blocking_task_runner_ = BrowserThread::GetBlockingPool()-> |
| 250 GetSequencedTaskRunnerWithShutdownBehavior( |
| 251 BrowserThread::GetBlockingPool()->GetSequenceToken(), |
| 252 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); |
| 253 } |
| 254 |
| 255 ElevatedInstallWrapper* ElevatedInstallWrapper::GetInstance() { |
| 256 return Singleton<ElevatedInstallWrapper>::get(); |
| 257 } |
| 258 |
| 259 base::FilePath ElevatedInstallWrapper::GetInstallerBackupPath() { |
| 260 base::FilePath path; |
| 261 PathService::Get(chrome::DIR_RECOVERY_COMPONENT_INSTALLER, &path); |
| 262 return path.NormalizePathSeparators(); |
| 263 } |
| 264 |
| 265 void ElevatedInstallWrapper::SaveInstallSourceInfo( |
| 266 const InstallerSourceSerializer& serializer, |
| 267 const base::Callback<void(bool)>& callback) { |
| 268 base::PostTaskAndReplyWithResult( |
| 269 ElevatedInstallWrapper::GetInstance()->blocking_task_runner_, |
| 270 FROM_HERE, |
| 271 base::Bind(&ElevatedInstallWrapper::DoSaveInstallSourceInfo, |
| 272 base::Unretained(this), serializer), |
| 273 callback); |
| 274 } |
| 275 |
| 276 bool ElevatedInstallWrapper::DoSaveInstallSourceInfo( |
| 277 const InstallerSourceSerializer& serializer) { |
| 278 return serializer.Run(GetInstallerBackupPath()); |
| 279 } |
| 280 |
| 281 void ElevatedInstallWrapper::Install(PrefService* prefs, |
| 282 bool elevation_allowed, |
| 283 const base::Closure& callback) { |
| 284 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 285 |
| 286 BrowserThread::PostTaskAndReply(BrowserThread::UI, FROM_HERE, |
| 287 base::Bind(&RecoveryUpdateElevationHelper, false, prefs), |
| 288 callback); |
| 289 if (!elevation_allowed) { |
| 290 return; |
| 291 } |
| 292 |
| 293 Version version(prefs->GetString(prefs::kRecoveryComponentVersion)); |
| 294 blocking_task_runner_->PostTask( |
| 295 FROM_HERE, |
| 296 base::Bind(&ElevatedInstallWrapper::DoInstall, |
| 297 base::Unretained(this), |
| 298 prefs, |
| 299 version)); |
| 300 } |
| 301 |
| 302 void ElevatedInstallWrapper::DoInstall(PrefService* prefs, |
| 303 const Version& version) { |
| 304 prefs_ = prefs; |
| 305 |
| 306 rc_installer_.reset(new RecoveryComponentInstaller(version, prefs, true)); |
| 307 |
| 308 unpacker_ = ComponentUnpacker::CreateFromBackup(GetInstallerBackupPath(), |
| 309 rc_installer_.get(), |
| 310 blocking_task_runner_); |
| 311 |
| 312 if (unpacker_.get()) { |
| 313 unpacker_->Unpack(base::Bind(&ElevatedInstallWrapper::DoneInstall, |
| 314 base::Unretained(this))); |
| 315 } |
| 316 } |
| 317 |
| 318 void ElevatedInstallWrapper::DoneInstall(ComponentUnpacker::Error error, |
| 319 int extended_error) { |
| 320 base::DeleteFile(GetInstallerBackupPath(), true); |
| 321 |
| 322 prefs_ = NULL; |
| 323 unpacker_ = NULL; |
| 324 rc_installer_.reset(); |
| 325 } |
| 326 |
160 void RegisterRecoveryComponent(ComponentUpdateService* cus, | 327 void RegisterRecoveryComponent(ComponentUpdateService* cus, |
161 PrefService* prefs) { | 328 PrefService* prefs) { |
162 #if !defined(OS_CHROMEOS) | 329 #if !defined(OS_CHROMEOS) |
163 // We delay execute the registration because we are not required in | 330 // We delay execute the registration because we are not required in |
164 // the critical path during browser startup. | 331 // the critical path during browser startup. |
165 BrowserThread::PostDelayedTask( | 332 BrowserThread::PostDelayedTask( |
166 BrowserThread::UI, | 333 BrowserThread::UI, |
167 FROM_HERE, | 334 FROM_HERE, |
168 base::Bind(&RecoveryRegisterHelper, cus, prefs), | 335 base::Bind(&RecoveryRegisterHelper, cus, prefs), |
169 base::TimeDelta::FromSeconds(6)); | 336 base::TimeDelta::FromSeconds(6)); |
170 #endif | 337 #endif |
171 } | 338 } |
172 | 339 |
173 void RegisterPrefsForRecoveryComponent(PrefRegistrySimple* registry) { | 340 void RegisterPrefsForRecoveryComponent(PrefRegistrySimple* registry) { |
174 registry->RegisterStringPref(prefs::kRecoveryComponentVersion, "0.0.0.0"); | 341 registry->RegisterStringPref(prefs::kRecoveryComponentVersion, "0.0.0.0"); |
| 342 registry->RegisterBooleanPref(prefs::kRecoveryComponentNeedsElevation, false); |
| 343 } |
| 344 |
| 345 void PerformRecoveryElevated( |
| 346 PrefService* prefs, bool elevation_allowed, const base::Closure& callback) { |
| 347 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 348 ElevatedInstallWrapper::GetInstance()->Install(prefs, elevation_allowed, |
| 349 callback); |
175 } | 350 } |
176 | 351 |
177 } // namespace component_updater | 352 } // namespace component_updater |
| 353 |
OLD | NEW |