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 <stdint.h> | 7 #include <stdint.h> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/base_paths.h" | 10 #include "base/base_paths.h" |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
14 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
15 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
16 #include "base/json/json_file_value_serializer.h" | |
16 #include "base/logging.h" | 17 #include "base/logging.h" |
18 #include "base/memory/scoped_ptr.h" | |
17 #include "base/path_service.h" | 19 #include "base/path_service.h" |
18 #include "base/prefs/pref_registry_simple.h" | 20 #include "base/prefs/pref_registry_simple.h" |
19 #include "base/prefs/pref_service.h" | 21 #include "base/prefs/pref_service.h" |
22 #include "base/process/kill.h" | |
20 #include "base/process/launch.h" | 23 #include "base/process/launch.h" |
21 #include "base/strings/string_util.h" | 24 #include "base/threading/worker_pool.h" |
22 #include "base/values.h" | 25 #include "chrome/common/chrome_switches.h" |
26 #include "chrome/common/pref_names.h" | |
23 #include "components/component_updater/component_updater_paths.h" | 27 #include "components/component_updater/component_updater_paths.h" |
24 #include "components/component_updater/component_updater_service.h" | 28 #include "components/component_updater/component_updater_service.h" |
25 #include "components/component_updater/pref_names.h" | 29 #include "components/component_updater/pref_names.h" |
26 #include "content/public/browser/browser_thread.h" | 30 #include "content/public/browser/browser_thread.h" |
27 | 31 |
28 using content::BrowserThread; | 32 using content::BrowserThread; |
29 | 33 |
30 namespace component_updater { | 34 namespace component_updater { |
31 | 35 |
32 namespace { | 36 namespace { |
33 | 37 |
34 // CRX hash. The extension id is: npdjjkjlcidkjlamlmmdelcjbcpdjocm. | 38 // CRX hash. The extension id is: npdjjkjlcidkjlamlmmdelcjbcpdjocm. |
35 const uint8_t kSha2Hash[] = {0xdf, 0x39, 0x9a, 0x9b, 0x28, 0x3a, 0x9b, 0x0c, | 39 const uint8_t kSha2Hash[] = {0xdf, 0x39, 0x9a, 0x9b, 0x28, 0x3a, 0x9b, 0x0c, |
36 0xbc, 0xc3, 0x4b, 0x29, 0x12, 0xf3, 0x9e, 0x2c, | 40 0xbc, 0xc3, 0x4b, 0x29, 0x12, 0xf3, 0x9e, 0x2c, |
37 0x19, 0x7a, 0x71, 0x4b, 0x0a, 0x7c, 0x80, 0x1c, | 41 0x19, 0x7a, 0x71, 0x4b, 0x0a, 0x7c, 0x80, 0x1c, |
38 0xf6, 0x29, 0x7c, 0x0a, 0x5f, 0xea, 0x67, 0xb7}; | 42 0xf6, 0x29, 0x7c, 0x0a, 0x5f, 0xea, 0x67, 0xb7}; |
39 | 43 |
40 // File name of the recovery binary on different platforms. | 44 // File name of the recovery binary on different platforms. |
41 const base::FilePath::CharType kRecoveryFileName[] = | 45 const base::FilePath::CharType kRecoveryFileName[] = |
42 #if defined(OS_WIN) | 46 #if defined(OS_WIN) |
43 FILE_PATH_LITERAL("ChromeRecovery.exe"); | 47 FILE_PATH_LITERAL("ChromeRecovery.exe"); |
44 #else // OS_LINUX, OS_MACOSX, etc. | 48 #else // OS_LINUX, OS_MACOSX, etc. |
45 FILE_PATH_LITERAL("ChromeRecovery"); | 49 FILE_PATH_LITERAL("ChromeRecovery"); |
46 #endif | 50 #endif |
47 | 51 |
48 const char kRecoveryManifestName[] = "ChromeRecovery"; | 52 const char kRecoveryManifestName[] = "ChromeRecovery"; |
49 | 53 |
54 // ChromeRecovery process exit codes. | |
55 enum ChromeRecoveryExitCode { | |
56 EXIT_CODE_RECOVERY_SUCCEEDED = 0, | |
57 EXIT_CODE_RECOVERY_SKIPPED = 1, | |
58 EXIT_CODE_ELEVATION_NEEDED = 2, | |
59 }; | |
60 | |
61 // Checks if elevated recovery simulation switch was present on the command | |
62 // line. This is for testing purpose. | |
63 bool SimulatingElevatedRecovery() { | |
64 return CommandLine::ForCurrentProcess()-> | |
65 HasSwitch(switches::kSimulateElevatedRecovery); | |
66 } | |
67 | |
68 #if defined(OS_WIN) | |
69 scoped_ptr<base::DictionaryValue> ReadManifest(const base::FilePath& manifest) { | |
70 JSONFileValueSerializer serializer(manifest); | |
71 std::string error; | |
72 scoped_ptr<base::Value> root(serializer.Deserialize(NULL, &error)); | |
73 if (root.get() && root->IsType(base::Value::TYPE_DICTIONARY)) { | |
74 return scoped_ptr<base::DictionaryValue>( | |
75 static_cast<base::DictionaryValue*>(root.release())); | |
76 } | |
77 return scoped_ptr<base::DictionaryValue>(); | |
78 } | |
79 | |
80 void DoElevatedInstallRecoveryComponent(const base::FilePath& path) { | |
81 const base::FilePath main_file = path.Append(kRecoveryFileName); | |
82 const base::FilePath manifest_file = | |
83 path.Append(FILE_PATH_LITERAL("manifest.json")); | |
84 if (!base::PathExists(main_file) || !base::PathExists(manifest_file)) | |
85 return; | |
86 | |
87 scoped_ptr<base::DictionaryValue> manifest(ReadManifest(manifest_file)); | |
88 std::string name; | |
89 manifest->GetStringASCII("name", &name); | |
90 if (name != kRecoveryManifestName) | |
91 return; | |
92 std::string proposed_version; | |
93 manifest->GetStringASCII("version", &proposed_version); | |
94 const Version version(proposed_version.c_str()); | |
95 if (!version.IsValid()) | |
96 return; | |
97 | |
98 CommandLine cmdline(main_file); | |
99 std::string arguments; | |
100 if (manifest->GetStringASCII("x-recovery-args", &arguments)) | |
101 cmdline.AppendArg(arguments); | |
102 std::string add_version; | |
103 if (manifest->GetStringASCII("x-recovery-add-version", &add_version) && | |
104 add_version == "yes") { | |
105 cmdline.AppendSwitchASCII("version", version.GetString()); | |
106 } | |
107 | |
108 base::LaunchOptions options; | |
109 options.start_hidden = true; | |
110 base::LaunchElevatedProcess(cmdline, options); | |
111 } | |
112 | |
113 void ElevatedInstallRecoveryComponent(const base::FilePath& installer_path) { | |
114 base::WorkerPool::PostTask( | |
waffles
2014/12/06 01:20:35
I think this is OK.
Threads from the worker pool
xiaoling
2014/12/06 02:21:38
Acknowledged.
| |
115 FROM_HERE, | |
116 base::Bind(&DoElevatedInstallRecoveryComponent, installer_path), | |
117 true); | |
118 } | |
119 #endif | |
120 | |
50 } // namespace | 121 } // namespace |
51 | 122 |
123 // Component installer that is responsible to repair the chrome installation | |
124 // or repair the Google update installation. This is a last resort safety | |
125 // mechanism. | |
126 // For user Chrome, recovery component just installs silently. For machine | |
127 // Chrome, elevation may be needed. If that happens, the installer will set | |
128 // preference flag prefs::kRecoveryComponentNeedsElevation to request that. | |
129 // There is a global error service monitors this flag and will pop up | |
130 // bubble if the flag is set to true. | |
131 // See chrome/browser/recovery/recovery_install_global_error.cc for details. | |
52 class RecoveryComponentInstaller : public ComponentInstaller { | 132 class RecoveryComponentInstaller : public ComponentInstaller { |
53 public: | 133 public: |
54 explicit RecoveryComponentInstaller(const Version& version, | 134 RecoveryComponentInstaller(const Version& version, PrefService* prefs); |
55 PrefService* prefs); | |
56 | |
57 ~RecoveryComponentInstaller() override {} | 135 ~RecoveryComponentInstaller() override {} |
58 | 136 |
59 void OnUpdateError(int error) override; | 137 void OnUpdateError(int error) override; |
60 | 138 |
61 bool Install(const base::DictionaryValue& manifest, | 139 bool Install(const base::DictionaryValue& manifest, |
62 const base::FilePath& unpack_path) override; | 140 const base::FilePath& unpack_path) override; |
63 | 141 |
64 bool GetInstalledFile(const std::string& file, | 142 bool GetInstalledFile(const std::string& file, |
65 base::FilePath* installed_file) override; | 143 base::FilePath* installed_file) override; |
66 | 144 |
67 private: | 145 private: |
146 bool RunInstallCommand(const CommandLine& cmdline, | |
147 const base::FilePath& installer_path) const; | |
148 | |
68 Version current_version_; | 149 Version current_version_; |
69 PrefService* prefs_; | 150 PrefService* prefs_; |
70 }; | 151 }; |
71 | 152 |
153 void SimulateElevatedRecoveryHelper(PrefService* prefs) { | |
154 prefs->SetBoolean(prefs::kRecoveryComponentNeedsElevation, true); | |
155 } | |
156 | |
72 void RecoveryRegisterHelper(ComponentUpdateService* cus, PrefService* prefs) { | 157 void RecoveryRegisterHelper(ComponentUpdateService* cus, PrefService* prefs) { |
73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 158 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
74 Version version(prefs->GetString(prefs::kRecoveryComponentVersion)); | 159 Version version(prefs->GetString(prefs::kRecoveryComponentVersion)); |
75 if (!version.IsValid()) { | 160 if (!version.IsValid()) { |
76 NOTREACHED(); | 161 NOTREACHED(); |
77 return; | 162 return; |
78 } | 163 } |
79 | 164 |
80 CrxComponent recovery; | 165 CrxComponent recovery; |
81 recovery.name = "recovery"; | 166 recovery.name = "recovery"; |
82 recovery.installer = new RecoveryComponentInstaller(version, prefs); | 167 recovery.installer = new RecoveryComponentInstaller(version, prefs); |
83 recovery.version = version; | 168 recovery.version = version; |
84 recovery.pk_hash.assign(kSha2Hash, &kSha2Hash[sizeof(kSha2Hash)]); | 169 recovery.pk_hash.assign(kSha2Hash, &kSha2Hash[sizeof(kSha2Hash)]); |
85 if (cus->RegisterComponent(recovery) != ComponentUpdateService::kOk) { | 170 if (cus->RegisterComponent(recovery) != ComponentUpdateService::kOk) { |
86 NOTREACHED() << "Recovery component registration failed."; | 171 NOTREACHED() << "Recovery component registration failed."; |
87 } | 172 } |
88 } | 173 } |
89 | 174 |
90 void RecoveryUpdateVersionHelper(const Version& version, PrefService* prefs) { | 175 void RecoveryUpdateVersionHelper(const Version& version, PrefService* prefs) { |
91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 176 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
92 prefs->SetString(prefs::kRecoveryComponentVersion, version.GetString()); | 177 prefs->SetString(prefs::kRecoveryComponentVersion, version.GetString()); |
93 } | 178 } |
94 | 179 |
180 void SetPrefsForElevatedRecoveryInstall(const base::FilePath& unpack_path, | |
181 PrefService* prefs) { | |
182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
183 prefs->SetFilePath(prefs::kRecoveryComponentUnpackPath, unpack_path); | |
184 prefs->SetBoolean(prefs::kRecoveryComponentNeedsElevation, true); | |
185 } | |
186 | |
95 RecoveryComponentInstaller::RecoveryComponentInstaller(const Version& version, | 187 RecoveryComponentInstaller::RecoveryComponentInstaller(const Version& version, |
96 PrefService* prefs) | 188 PrefService* prefs) |
97 : current_version_(version), prefs_(prefs) { | 189 : current_version_(version), prefs_(prefs) { |
98 DCHECK(version.IsValid()); | 190 DCHECK(version.IsValid()); |
99 } | 191 } |
100 | 192 |
101 void RecoveryComponentInstaller::OnUpdateError(int error) { | 193 void RecoveryComponentInstaller::OnUpdateError(int error) { |
102 NOTREACHED() << "Recovery component update error: " << error; | 194 NOTREACHED() << "Recovery component update error: " << error; |
103 } | 195 } |
104 | 196 |
197 #if defined(OS_WIN) | |
198 bool RecoveryComponentInstaller::RunInstallCommand( | |
199 const CommandLine& cmdline, const base::FilePath& installer_folder) const { | |
200 base::ProcessHandle process_handle; | |
201 base::LaunchOptions options; | |
202 options.start_hidden = true; | |
203 if (!base::LaunchProcess(cmdline, options, &process_handle)) | |
204 return false; | |
205 | |
206 int installer_exit_code = 0; | |
207 const base::TimeDelta kMaxWaitTime = base::TimeDelta::FromSeconds(600); | |
waffles
2014/12/06 01:20:35
In the worst case, this can block Chrome shutdown
xiaoling
2014/12/06 02:21:38
Done.
| |
208 if (base::WaitForExitCodeWithTimeout(process_handle, | |
209 &installer_exit_code, | |
210 kMaxWaitTime) && | |
211 installer_exit_code == EXIT_CODE_ELEVATION_NEEDED) { | |
212 BrowserThread::PostTask( | |
213 BrowserThread::UI, | |
214 FROM_HERE, | |
215 base::Bind(&SetPrefsForElevatedRecoveryInstall, | |
216 installer_folder, | |
217 prefs_)); | |
218 } | |
219 | |
220 // Returns true regardless of exit code since from updater service | |
221 // perspective the install is done, even we may need to do elevated | |
222 // install later. | |
223 return true; | |
224 } | |
225 #else | |
226 bool RecoveryComponentInstaller::RunInstallCommand( | |
227 const CommandLine& cmdline, const base::FilePath&) const { | |
228 return base::LaunchProcess(cmdline, base::LaunchOptions(), NULL); | |
229 } | |
230 #endif | |
231 | |
105 bool RecoveryComponentInstaller::Install(const base::DictionaryValue& manifest, | 232 bool RecoveryComponentInstaller::Install(const base::DictionaryValue& manifest, |
106 const base::FilePath& unpack_path) { | 233 const base::FilePath& unpack_path) { |
107 std::string name; | 234 std::string name; |
108 manifest.GetStringASCII("name", &name); | 235 manifest.GetStringASCII("name", &name); |
109 if (name != kRecoveryManifestName) | 236 if (name != kRecoveryManifestName) |
110 return false; | 237 return false; |
111 std::string proposed_version; | 238 std::string proposed_version; |
112 manifest.GetStringASCII("version", &proposed_version); | 239 manifest.GetStringASCII("version", &proposed_version); |
113 Version version(proposed_version.c_str()); | 240 Version version(proposed_version.c_str()); |
114 if (!version.IsValid()) | 241 if (!version.IsValid()) |
115 return false; | 242 return false; |
116 if (current_version_.CompareTo(version) >= 0) | 243 if (current_version_.CompareTo(version) >= 0) |
117 return false; | 244 return false; |
118 | 245 |
119 // Passed the basic tests. Copy the installation to a permanent directory. | 246 // Passed the basic tests. Copy the installation to a permanent directory. |
120 base::FilePath path; | 247 base::FilePath path; |
121 if (!PathService::Get(DIR_RECOVERY_BASE, &path)) | 248 if (!PathService::Get(DIR_RECOVERY_BASE, &path)) |
122 return false; | 249 return false; |
123 if (!base::PathExists(path)) { | 250 if (!base::PathExists(path) && !base::CreateDirectory(path)) |
124 if (!base::CreateDirectory(path)) { | |
125 return false; | 251 return false; |
126 } | |
127 } | |
128 path = path.AppendASCII(version.GetString()); | 252 path = path.AppendASCII(version.GetString()); |
129 if (base::PathExists(path) && !base::DeleteFile(path, true)) | 253 if (base::PathExists(path) && !base::DeleteFile(path, true)) |
130 return false; | 254 return false; |
131 if (!base::Move(unpack_path, path)) { | 255 if (!base::Move(unpack_path, path)) { |
132 DVLOG(1) << "Recovery component move failed."; | 256 DVLOG(1) << "Recovery component move failed."; |
133 return false; | 257 return false; |
134 } | 258 } |
135 | 259 |
136 base::FilePath main_file = path.Append(kRecoveryFileName); | 260 base::FilePath main_file = path.Append(kRecoveryFileName); |
137 if (!base::PathExists(main_file)) | 261 if (!base::PathExists(main_file)) |
138 return false; | 262 return false; |
139 // Run the recovery component. | 263 // Run the recovery component. |
140 CommandLine cmdline(main_file); | 264 CommandLine cmdline(main_file); |
141 std::string arguments; | 265 std::string arguments; |
142 if (manifest.GetStringASCII("x-recovery-args", &arguments)) | 266 if (manifest.GetStringASCII("x-recovery-args", &arguments)) |
143 cmdline.AppendArg(arguments); | 267 cmdline.AppendArg(arguments); |
144 std::string add_version; | 268 std::string add_version; |
145 if (manifest.GetStringASCII("x-recovery-add-version", &add_version)) { | 269 if (manifest.GetStringASCII("x-recovery-add-version", &add_version) && |
146 if (add_version == "yes") | 270 add_version == "yes") { |
147 cmdline.AppendSwitchASCII("version", current_version_.GetString()); | 271 cmdline.AppendSwitchASCII("version", current_version_.GetString()); |
148 } | 272 } |
273 | |
274 if (!RunInstallCommand(cmdline, path)) { | |
275 return false; | |
276 } | |
277 | |
149 current_version_ = version; | 278 current_version_ = version; |
150 if (prefs_) { | 279 if (prefs_) { |
151 BrowserThread::PostTask( | 280 BrowserThread::PostTask( |
152 BrowserThread::UI, | 281 BrowserThread::UI, |
153 FROM_HERE, | 282 FROM_HERE, |
154 base::Bind(&RecoveryUpdateVersionHelper, version, prefs_)); | 283 base::Bind(&RecoveryUpdateVersionHelper, version, prefs_)); |
155 } | 284 } |
156 return base::LaunchProcess(cmdline, base::LaunchOptions(), NULL); | 285 return true; |
157 } | 286 } |
158 | 287 |
159 bool RecoveryComponentInstaller::GetInstalledFile( | 288 bool RecoveryComponentInstaller::GetInstalledFile( |
160 const std::string& file, | 289 const std::string& file, |
161 base::FilePath* installed_file) { | 290 base::FilePath* installed_file) { |
162 return false; | 291 return false; |
163 } | 292 } |
164 | 293 |
165 void RegisterRecoveryComponent(ComponentUpdateService* cus, | 294 void RegisterRecoveryComponent(ComponentUpdateService* cus, |
166 PrefService* prefs) { | 295 PrefService* prefs) { |
167 #if !defined(OS_CHROMEOS) | 296 #if !defined(OS_CHROMEOS) |
297 if (SimulatingElevatedRecovery()) { | |
298 BrowserThread::PostTask( | |
299 BrowserThread::UI, | |
300 FROM_HERE, | |
301 base::Bind(&SimulateElevatedRecoveryHelper, prefs)); | |
302 } | |
303 | |
168 // We delay execute the registration because we are not required in | 304 // We delay execute the registration because we are not required in |
169 // the critical path during browser startup. | 305 // the critical path during browser startup. |
170 BrowserThread::PostDelayedTask( | 306 BrowserThread::PostDelayedTask( |
171 BrowserThread::UI, | 307 BrowserThread::UI, |
172 FROM_HERE, | 308 FROM_HERE, |
173 base::Bind(&RecoveryRegisterHelper, cus, prefs), | 309 base::Bind(&RecoveryRegisterHelper, cus, prefs), |
174 base::TimeDelta::FromSeconds(6)); | 310 base::TimeDelta::FromSeconds(6)); |
175 #endif | 311 #endif |
176 } | 312 } |
177 | 313 |
178 void RegisterPrefsForRecoveryComponent(PrefRegistrySimple* registry) { | 314 void RegisterPrefsForRecoveryComponent(PrefRegistrySimple* registry) { |
179 registry->RegisterStringPref(prefs::kRecoveryComponentVersion, "0.0.0.0"); | 315 registry->RegisterStringPref(prefs::kRecoveryComponentVersion, "0.0.0.0"); |
316 registry->RegisterFilePathPref(prefs::kRecoveryComponentUnpackPath, | |
317 base::FilePath()); | |
318 registry->RegisterBooleanPref(prefs::kRecoveryComponentNeedsElevation, false); | |
180 } | 319 } |
181 | 320 |
182 void AcceptedElevatedRecoveryInstall(PrefService* prefs) { | 321 void AcceptedElevatedRecoveryInstall(PrefService* prefs) { |
183 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 322 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
323 | |
324 #if defined(OS_WIN) | |
325 ElevatedInstallRecoveryComponent( | |
326 prefs->GetFilePath(prefs::kRecoveryComponentUnpackPath)); | |
327 #endif | |
328 prefs->SetBoolean(prefs::kRecoveryComponentNeedsElevation, false); | |
184 } | 329 } |
185 | 330 |
186 void DeclinedElevatedRecoveryInstall(PrefService* prefs) { | 331 void DeclinedElevatedRecoveryInstall(PrefService* prefs) { |
187 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 332 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
333 prefs->SetBoolean(prefs::kRecoveryComponentNeedsElevation, false); | |
188 } | 334 } |
189 | 335 |
190 } // namespace component_updater | 336 } // namespace component_updater |
OLD | NEW |