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