recovery_component_installer.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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 "base/base_paths.h" 8#include "base/bind.h" 9#include "base/command_line.h" 10#include "base/compiler_specific.h" 11#include "base/file_path.h" 12#include "base/file_util.h" 13#include "base/logging.h" 14#include "base/path_service.h" 15#include "base/process_util.h" 16#include "base/string_util.h" 17#include "base/values.h" 18#include "chrome/browser/component_updater/component_updater_service.h" 19#include "chrome/browser/prefs/pref_service.h" 20#include "chrome/common/chrome_version_info.h" 21#include "chrome/common/pref_names.h" 22#include "content/public/browser/browser_thread.h" 23 24using content::BrowserThread; 25 26namespace { 27 28// CRX hash. The extension id is: npdjjkjlcidkjlamlmmdelcjbcpdjocm. 29const uint8 sha2_hash[] = {0xdf, 0x39, 0x9a, 0x9b, 0x28, 0x3a, 0x9b, 0x0c, 30 0xbc, 0xc3, 0x4b, 0x29, 0x12, 0xf3, 0x9e, 0x2c, 31 0x19, 0x7a, 0x71, 0x4b, 0x0a, 0x7c, 0x80, 0x1c, 32 0xf6, 0x29, 0x7c, 0x0a, 0x5f, 0xea, 0x67, 0xb7}; 33 34// File name of the recovery binary on different platforms. 35const FilePath::CharType kRecoveryFileName[] = 36#if defined(OS_WIN) 37 FILE_PATH_LITERAL("ChromeRecovery.exe"); 38#else // OS_LINUX, OS_MACOSX, etc. 39 FILE_PATH_LITERAL("ChromeRecovery"); 40#endif 41 42const char kRecoveryManifestName[] = "ChromeRecovery"; 43 44} // namespace 45 46class RecoveryComponentInstaller : public ComponentInstaller { 47 public: 48 explicit RecoveryComponentInstaller(const Version& version, 49 PrefService* prefs); 50 51 virtual ~RecoveryComponentInstaller() {} 52 53 virtual void OnUpdateError(int error) OVERRIDE; 54 55 virtual bool Install(base::DictionaryValue* manifest, 56 const FilePath& unpack_path) OVERRIDE; 57 58 private: 59 Version current_version_; 60 PrefService* prefs_; 61}; 62 63void RecoveryRegisterHelper(ComponentUpdateService* cus, PrefService* prefs) { 64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 65 prefs->RegisterStringPref(prefs::kRecoveryComponentVersion, "0.0.0.0"); 66 Version version(prefs->GetString(prefs::kRecoveryComponentVersion)); 67 if (!version.IsValid()) { 68 NOTREACHED(); 69 return; 70 } 71 72 CrxComponent recovery; 73 recovery.name = "recovery"; 74 recovery.installer = new RecoveryComponentInstaller(version, prefs); 75 recovery.version = version; 76 recovery.pk_hash.assign(sha2_hash, &sha2_hash[sizeof(sha2_hash)]); 77 if (cus->RegisterComponent(recovery) != ComponentUpdateService::kOk) { 78 NOTREACHED() << "Recovery component registration failed."; 79 } 80} 81 82void RecoveryUpdateVersionHelper(const Version& version, PrefService* prefs) { 83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 84 prefs->SetString(prefs::kRecoveryComponentVersion, version.GetString()); 85} 86 87RecoveryComponentInstaller::RecoveryComponentInstaller( 88 const Version& version, PrefService* prefs) 89 : current_version_(version), prefs_(prefs){ 90 DCHECK(version.IsValid()); 91} 92 93void RecoveryComponentInstaller::OnUpdateError(int error) { 94 NOTREACHED() << "Recovery component update error: " << error; 95} 96 97bool RecoveryComponentInstaller::Install(base::DictionaryValue* manifest, 98 const FilePath& unpack_path) { 99 std::string name; 100 manifest->GetStringASCII("name", &name); 101 if (name != kRecoveryManifestName) 102 return false; 103 std::string proposed_version; 104 manifest->GetStringASCII("version", &proposed_version); 105 Version version(proposed_version.c_str()); 106 if (!version.IsValid()) 107 return false; 108 if (current_version_.CompareTo(version) >= 0) 109 return false; 110 FilePath main_file = unpack_path.Append(kRecoveryFileName); 111 if (!file_util::PathExists(main_file)) 112 return false; 113 // Passed the basic tests. The installation continues with the 114 // recovery component itself running from the temp directory. 115 CommandLine cmdline(main_file); 116 std::string arguments; 117 if (manifest->GetStringASCII("x-recovery-args", &arguments)) 118 cmdline.AppendArg(arguments); 119 std::string add_version; 120 if (manifest->GetStringASCII("x-recovery-add-version", &add_version)) { 121 if (add_version == "yes") 122 cmdline.AppendSwitchASCII("version", current_version_.GetString()); 123 } 124 current_version_ = version; 125 if (prefs_) { 126 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 127 base::Bind(&RecoveryUpdateVersionHelper, version, prefs_)); 128 } 129 return base::LaunchProcess(cmdline, base::LaunchOptions(), NULL); 130} 131 132void RegisterRecoveryComponent(ComponentUpdateService* cus, 133 PrefService* prefs) { 134#if !defined(OS_CHROMEOS) 135 // We delay execute the registration because we are not required in 136 // the critical path during browser startup. 137 BrowserThread::PostDelayedTask( 138 BrowserThread::UI, 139 FROM_HERE, 140 base::Bind(&RecoveryRegisterHelper, cus, prefs), 141 base::TimeDelta::FromSeconds(6)); 142#endif 143} 144