recovery_component_installer.cc revision e4256316f8b5e8d1ec0df1f7762771622a53fa63
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 <stdint.h> 8#include <string> 9 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" 27 28using content::BrowserThread; 29 30namespace component_updater { 31 32namespace { 33 34// CRX hash. The extension id is: npdjjkjlcidkjlamlmmdelcjbcpdjocm. 35const 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}; 39 40// File name of the recovery binary on different platforms. 41const 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 48const char kRecoveryManifestName[] = "ChromeRecovery"; 49 50} // namespace 51 52class RecoveryComponentInstaller : public ComponentInstaller { 53 public: 54 explicit RecoveryComponentInstaller(const Version& version, 55 PrefService* prefs); 56 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 72void 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 90void RecoveryUpdateVersionHelper(const Version& version, PrefService* prefs) { 91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 92 prefs->SetString(prefs::kRecoveryComponentVersion, version.GetString()); 93} 94 95RecoveryComponentInstaller::RecoveryComponentInstaller(const Version& version, 96 PrefService* prefs) 97 : current_version_(version), prefs_(prefs) { 98 DCHECK(version.IsValid()); 99} 100 101void RecoveryComponentInstaller::OnUpdateError(int error) { 102 NOTREACHED() << "Recovery component update error: " << error; 103} 104 105bool 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(DIR_RECOVERY_BASE, &path)) 122 return false; 123 if (!base::PathExists(path)) { 124 if (!base::CreateDirectory(path)) { 125 return false; 126 } 127 } 128 path = path.AppendASCII(version.GetString()); 129 if (base::PathExists(path) && !base::DeleteFile(path, true)) 130 return false; 131 if (!base::Move(unpack_path, path)) { 132 DVLOG(1) << "Recovery component move failed."; 133 return false; 134 } 135 136 base::FilePath main_file = path.Append(kRecoveryFileName); 137 if (!base::PathExists(main_file)) 138 return false; 139 // Run the recovery component. 140 CommandLine cmdline(main_file); 141 std::string arguments; 142 if (manifest.GetStringASCII("x-recovery-args", &arguments)) 143 cmdline.AppendArg(arguments); 144 std::string add_version; 145 if (manifest.GetStringASCII("x-recovery-add-version", &add_version)) { 146 if (add_version == "yes") 147 cmdline.AppendSwitchASCII("version", current_version_.GetString()); 148 } 149 current_version_ = version; 150 if (prefs_) { 151 BrowserThread::PostTask( 152 BrowserThread::UI, 153 FROM_HERE, 154 base::Bind(&RecoveryUpdateVersionHelper, version, prefs_)); 155 } 156 return base::LaunchProcess(cmdline, base::LaunchOptions(), NULL); 157} 158 159bool RecoveryComponentInstaller::GetInstalledFile( 160 const std::string& file, 161 base::FilePath* installed_file) { 162 return false; 163} 164 165void RegisterRecoveryComponent(ComponentUpdateService* cus, 166 PrefService* prefs) { 167#if !defined(OS_CHROMEOS) 168 // We delay execute the registration because we are not required in 169 // the critical path during browser startup. 170 BrowserThread::PostDelayedTask( 171 BrowserThread::UI, 172 FROM_HERE, 173 base::Bind(&RecoveryRegisterHelper, cus, prefs), 174 base::TimeDelta::FromSeconds(6)); 175#endif 176} 177 178void RegisterPrefsForRecoveryComponent(PrefRegistrySimple* registry) { 179 registry->RegisterStringPref(prefs::kRecoveryComponentVersion, "0.0.0.0"); 180} 181 182} // namespace component_updater 183