recovery_component_installer.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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_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" 24 25using content::BrowserThread; 26 27namespace component_updater { 28 29namespace { 30 31// CRX hash. The extension id is: npdjjkjlcidkjlamlmmdelcjbcpdjocm. 32const 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}; 36 37// File name of the recovery binary on different platforms. 38const base::FilePath::CharType kRecoveryFileName[] = 39#if defined(OS_WIN) 40 FILE_PATH_LITERAL("ChromeRecovery.exe"); 41#else // OS_LINUX, OS_MACOSX, etc. 42 FILE_PATH_LITERAL("ChromeRecovery"); 43#endif 44 45const char kRecoveryManifestName[] = "ChromeRecovery"; 46 47} // namespace 48 49class RecoveryComponentInstaller : public ComponentInstaller { 50 public: 51 explicit RecoveryComponentInstaller(const Version& version, 52 PrefService* prefs); 53 54 virtual ~RecoveryComponentInstaller() {} 55 56 virtual void OnUpdateError(int error) OVERRIDE; 57 58 virtual bool Install(const base::DictionaryValue& manifest, 59 const base::FilePath& unpack_path) OVERRIDE; 60 61 virtual bool GetInstalledFile(const std::string& file, 62 base::FilePath* installed_file) OVERRIDE; 63 64 private: 65 Version current_version_; 66 PrefService* prefs_; 67}; 68 69void RecoveryRegisterHelper(ComponentUpdateService* cus, 70 PrefService* prefs) { 71 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 72 Version version(prefs->GetString(prefs::kRecoveryComponentVersion)); 73 if (!version.IsValid()) { 74 NOTREACHED(); 75 return; 76 } 77 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."; 85 } 86} 87 88void RecoveryUpdateVersionHelper(const Version& version, PrefService* prefs) { 89 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 90 prefs->SetString(prefs::kRecoveryComponentVersion, version.GetString()); 91} 92 93RecoveryComponentInstaller::RecoveryComponentInstaller( 94 const Version& version, PrefService* prefs) 95 : current_version_(version), prefs_(prefs){ 96 DCHECK(version.IsValid()); 97} 98 99void RecoveryComponentInstaller::OnUpdateError(int error) { 100 NOTREACHED() << "Recovery component update error: " << error; 101} 102 103bool RecoveryComponentInstaller::Install(const base::DictionaryValue& manifest, 104 const base::FilePath& unpack_path) { 105 std::string name; 106 manifest.GetStringASCII("name", &name); 107 if (name != kRecoveryManifestName) 108 return false; 109 std::string proposed_version; 110 manifest.GetStringASCII("version", &proposed_version); 111 Version version(proposed_version.c_str()); 112 if (!version.IsValid()) 113 return false; 114 if (current_version_.CompareTo(version) >= 0) 115 return false; 116 base::FilePath main_file = unpack_path.Append(kRecoveryFileName); 117 if (!base::PathExists(main_file)) 118 return false; 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()); 129 } 130 current_version_ = version; 131 if (prefs_) { 132 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 133 base::Bind(&RecoveryUpdateVersionHelper, version, prefs_)); 134 } 135 return base::LaunchProcess(cmdline, base::LaunchOptions(), NULL); 136} 137 138bool RecoveryComponentInstaller::GetInstalledFile( 139 const std::string& file, base::FilePath* installed_file) { 140 return false; 141} 142 143void 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( 149 BrowserThread::UI, 150 FROM_HERE, 151 base::Bind(&RecoveryRegisterHelper, cus, prefs), 152 base::TimeDelta::FromSeconds(6)); 153#endif 154} 155 156void RegisterPrefsForRecoveryComponent(PrefRegistrySimple* registry) { 157 registry->RegisterStringPref(prefs::kRecoveryComponentVersion, "0.0.0.0"); 158} 159 160} // namespace component_updater 161 162