| 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/path_service.h" | 16 #include "base/path_service.h" |
| 17 #include "base/prefs/pref_registry_simple.h" | 17 #include "base/prefs/pref_registry_simple.h" |
| 18 #include "base/prefs/pref_service.h" | 18 #include "base/prefs/pref_service.h" |
| 19 #include "base/process/launch.h" | 19 #include "base/process/launch.h" |
| 20 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
| 21 #include "base/values.h" | 21 #include "base/values.h" |
| 22 #include "chrome/browser/component_updater/component_updater_service.h" | 22 #include "chrome/browser/component_updater/component_updater_service.h" |
| 23 #include "chrome/common/chrome_paths.h" | 23 #include "components/component_updater/common/pref_names.h" |
| 24 #include "chrome/common/chrome_version_info.h" | |
| 25 #include "chrome/common/pref_names.h" | |
| 26 #include "content/public/browser/browser_thread.h" | 24 #include "content/public/browser/browser_thread.h" |
| 27 | 25 |
| 28 using content::BrowserThread; | 26 using content::BrowserThread; |
| 29 | 27 |
| 30 namespace component_updater { | 28 namespace component_updater { |
| 31 | 29 |
| 32 namespace { | 30 namespace { |
| 33 | 31 |
| 34 // CRX hash. The extension id is: npdjjkjlcidkjlamlmmdelcjbcpdjocm. | 32 // CRX hash. The extension id is: npdjjkjlcidkjlamlmmdelcjbcpdjocm. |
| 35 const uint8 kSha2Hash[] = {0xdf, 0x39, 0x9a, 0x9b, 0x28, 0x3a, 0x9b, 0x0c, | 33 const uint8 kSha2Hash[] = {0xdf, 0x39, 0x9a, 0x9b, 0x28, 0x3a, 0x9b, 0x0c, |
| 36 0xbc, 0xc3, 0x4b, 0x29, 0x12, 0xf3, 0x9e, 0x2c, | 34 0xbc, 0xc3, 0x4b, 0x29, 0x12, 0xf3, 0x9e, 0x2c, |
| 37 0x19, 0x7a, 0x71, 0x4b, 0x0a, 0x7c, 0x80, 0x1c, | 35 0x19, 0x7a, 0x71, 0x4b, 0x0a, 0x7c, 0x80, 0x1c, |
| 38 0xf6, 0x29, 0x7c, 0x0a, 0x5f, 0xea, 0x67, 0xb7}; | 36 0xf6, 0x29, 0x7c, 0x0a, 0x5f, 0xea, 0x67, 0xb7}; |
| 39 | 37 |
| 40 // File name of the recovery binary on different platforms. | 38 // File name of the recovery binary on different platforms. |
| 41 const base::FilePath::CharType kRecoveryFileName[] = | 39 const base::FilePath::CharType kRecoveryFileName[] = |
| 42 #if defined(OS_WIN) | 40 #if defined(OS_WIN) |
| 43 FILE_PATH_LITERAL("ChromeRecovery.exe"); | 41 FILE_PATH_LITERAL("ChromeRecovery.exe"); |
| 44 #else // OS_LINUX, OS_MACOSX, etc. | 42 #else // OS_LINUX, OS_MACOSX, etc. |
| 45 FILE_PATH_LITERAL("ChromeRecovery"); | 43 FILE_PATH_LITERAL("ChromeRecovery"); |
| 46 #endif | 44 #endif |
| 47 | 45 |
| 48 const char kRecoveryManifestName[] = "ChromeRecovery"; | 46 const char kRecoveryManifestName[] = "ChromeRecovery"; |
| 49 | 47 |
| 50 } // namespace | 48 } // namespace |
| 51 | 49 |
| 52 class RecoveryComponentInstaller : public ComponentInstaller { | 50 class RecoveryComponentInstaller : public ComponentInstaller { |
| 53 public: | 51 public: |
| 54 explicit RecoveryComponentInstaller(const Version& version, | 52 explicit RecoveryComponentInstaller(const Version& version, |
| 55 PrefService* prefs); | 53 PrefService* prefs, |
| 54 const base::FilePath& base_dir); |
| 56 | 55 |
| 57 virtual ~RecoveryComponentInstaller() {} | 56 virtual ~RecoveryComponentInstaller() {} |
| 58 | 57 |
| 59 virtual void OnUpdateError(int error) OVERRIDE; | 58 virtual void OnUpdateError(int error) OVERRIDE; |
| 60 | 59 |
| 61 virtual bool Install(const base::DictionaryValue& manifest, | 60 virtual bool Install(const base::DictionaryValue& manifest, |
| 62 const base::FilePath& unpack_path) OVERRIDE; | 61 const base::FilePath& unpack_path) OVERRIDE; |
| 63 | 62 |
| 64 virtual bool GetInstalledFile(const std::string& file, | 63 virtual bool GetInstalledFile(const std::string& file, |
| 65 base::FilePath* installed_file) OVERRIDE; | 64 base::FilePath* installed_file) OVERRIDE; |
| 66 | 65 |
| 67 private: | 66 private: |
| 68 Version current_version_; | 67 Version current_version_; |
| 69 PrefService* prefs_; | 68 PrefService* prefs_; |
| 69 base::FilePath base_dir_; |
| 70 }; | 70 }; |
| 71 | 71 |
| 72 void RecoveryRegisterHelper(ComponentUpdateService* cus, PrefService* prefs) { | 72 void RecoveryRegisterHelper(ComponentUpdateService* cus, |
| 73 PrefService* prefs, |
| 74 const base::FilePath& base_dir) { |
| 73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 75 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 74 Version version(prefs->GetString(prefs::kRecoveryComponentVersion)); | 76 Version version(prefs->GetString(prefs::kRecoveryComponentVersion)); |
| 75 if (!version.IsValid()) { | 77 if (!version.IsValid()) { |
| 76 NOTREACHED(); | 78 NOTREACHED(); |
| 77 return; | 79 return; |
| 78 } | 80 } |
| 79 | 81 |
| 80 CrxComponent recovery; | 82 CrxComponent recovery; |
| 81 recovery.name = "recovery"; | 83 recovery.name = "recovery"; |
| 82 recovery.installer = new RecoveryComponentInstaller(version, prefs); | 84 recovery.installer = new RecoveryComponentInstaller(version, prefs, base_dir); |
| 83 recovery.version = version; | 85 recovery.version = version; |
| 84 recovery.pk_hash.assign(kSha2Hash, &kSha2Hash[sizeof(kSha2Hash)]); | 86 recovery.pk_hash.assign(kSha2Hash, &kSha2Hash[sizeof(kSha2Hash)]); |
| 85 if (cus->RegisterComponent(recovery) != ComponentUpdateService::kOk) { | 87 if (cus->RegisterComponent(recovery) != ComponentUpdateService::kOk) { |
| 86 NOTREACHED() << "Recovery component registration failed."; | 88 NOTREACHED() << "Recovery component registration failed."; |
| 87 } | 89 } |
| 88 } | 90 } |
| 89 | 91 |
| 90 void RecoveryUpdateVersionHelper(const Version& version, PrefService* prefs) { | 92 void RecoveryUpdateVersionHelper(const Version& version, PrefService* prefs) { |
| 91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 92 prefs->SetString(prefs::kRecoveryComponentVersion, version.GetString()); | 94 prefs->SetString(prefs::kRecoveryComponentVersion, version.GetString()); |
| 93 } | 95 } |
| 94 | 96 |
| 95 RecoveryComponentInstaller::RecoveryComponentInstaller(const Version& version, | 97 RecoveryComponentInstaller::RecoveryComponentInstaller( |
| 96 PrefService* prefs) | 98 const Version& version, |
| 97 : current_version_(version), prefs_(prefs) { | 99 PrefService* prefs, |
| 100 const base::FilePath& base_dir) |
| 101 : current_version_(version), prefs_(prefs), base_dir_(base_dir) { |
| 98 DCHECK(version.IsValid()); | 102 DCHECK(version.IsValid()); |
| 99 } | 103 } |
| 100 | 104 |
| 101 void RecoveryComponentInstaller::OnUpdateError(int error) { | 105 void RecoveryComponentInstaller::OnUpdateError(int error) { |
| 102 NOTREACHED() << "Recovery component update error: " << error; | 106 NOTREACHED() << "Recovery component update error: " << error; |
| 103 } | 107 } |
| 104 | 108 |
| 105 bool RecoveryComponentInstaller::Install(const base::DictionaryValue& manifest, | 109 bool RecoveryComponentInstaller::Install(const base::DictionaryValue& manifest, |
| 106 const base::FilePath& unpack_path) { | 110 const base::FilePath& unpack_path) { |
| 107 std::string name; | 111 std::string name; |
| 108 manifest.GetStringASCII("name", &name); | 112 manifest.GetStringASCII("name", &name); |
| 109 if (name != kRecoveryManifestName) | 113 if (name != kRecoveryManifestName) |
| 110 return false; | 114 return false; |
| 111 std::string proposed_version; | 115 std::string proposed_version; |
| 112 manifest.GetStringASCII("version", &proposed_version); | 116 manifest.GetStringASCII("version", &proposed_version); |
| 113 Version version(proposed_version.c_str()); | 117 Version version(proposed_version.c_str()); |
| 114 if (!version.IsValid()) | 118 if (!version.IsValid()) |
| 115 return false; | 119 return false; |
| 116 if (current_version_.CompareTo(version) >= 0) | 120 if (current_version_.CompareTo(version) >= 0) |
| 117 return false; | 121 return false; |
| 118 | 122 |
| 119 // Passed the basic tests. Copy the installation to a permanent directory. | 123 // Passed the basic tests. Copy the installation to a permanent directory. |
| 120 base::FilePath path; | 124 base::FilePath path = base_dir_.AppendASCII(version.GetString()); |
| 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 if (base::PathExists(path) && !base::DeleteFile(path, true)) |
| 125 return false; | 126 return false; |
| 126 if (!base::Move(unpack_path, path)) { | 127 if (!base::Move(unpack_path, path)) { |
| 127 DVLOG(1) << "Recovery component move failed."; | 128 DVLOG(1) << "Recovery component move failed."; |
| 128 return false; | 129 return false; |
| 129 } | 130 } |
| 130 | 131 |
| 131 base::FilePath main_file = path.Append(kRecoveryFileName); | 132 base::FilePath main_file = path.Append(kRecoveryFileName); |
| 132 if (!base::PathExists(main_file)) | 133 if (!base::PathExists(main_file)) |
| 133 return false; | 134 return false; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 151 return base::LaunchProcess(cmdline, base::LaunchOptions(), NULL); | 152 return base::LaunchProcess(cmdline, base::LaunchOptions(), NULL); |
| 152 } | 153 } |
| 153 | 154 |
| 154 bool RecoveryComponentInstaller::GetInstalledFile( | 155 bool RecoveryComponentInstaller::GetInstalledFile( |
| 155 const std::string& file, | 156 const std::string& file, |
| 156 base::FilePath* installed_file) { | 157 base::FilePath* installed_file) { |
| 157 return false; | 158 return false; |
| 158 } | 159 } |
| 159 | 160 |
| 160 void RegisterRecoveryComponent(ComponentUpdateService* cus, | 161 void RegisterRecoveryComponent(ComponentUpdateService* cus, |
| 161 PrefService* prefs) { | 162 PrefService* prefs, |
| 163 const base::FilePath& base_dir) { |
| 162 #if !defined(OS_CHROMEOS) | 164 #if !defined(OS_CHROMEOS) |
| 163 // We delay execute the registration because we are not required in | 165 // We delay execute the registration because we are not required in |
| 164 // the critical path during browser startup. | 166 // the critical path during browser startup. |
| 165 BrowserThread::PostDelayedTask( | 167 BrowserThread::PostDelayedTask( |
| 166 BrowserThread::UI, | 168 BrowserThread::UI, |
| 167 FROM_HERE, | 169 FROM_HERE, |
| 168 base::Bind(&RecoveryRegisterHelper, cus, prefs), | 170 base::Bind(&RecoveryRegisterHelper, cus, prefs, base_dir), |
| 169 base::TimeDelta::FromSeconds(6)); | 171 base::TimeDelta::FromSeconds(6)); |
| 170 #endif | 172 #endif |
| 171 } | 173 } |
| 172 | 174 |
| 173 void RegisterPrefsForRecoveryComponent(PrefRegistrySimple* registry) { | 175 void RegisterPrefsForRecoveryComponent(PrefRegistrySimple* registry) { |
| 174 registry->RegisterStringPref(prefs::kRecoveryComponentVersion, "0.0.0.0"); | 176 registry->RegisterStringPref(prefs::kRecoveryComponentVersion, "0.0.0.0"); |
| 175 } | 177 } |
| 176 | 178 |
| 177 } // namespace component_updater | 179 } // namespace component_updater |
| OLD | NEW |