Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(185)

Side by Side Diff: chrome/browser/component_updater/recovery_component_installer.cc

Issue 359443002: Elevated install of recovery component when needed (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: comment out unused function on chromeos Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 #if !defined(OS_CHROMEOS)
62 // Checks if elevated recovery simulation switch was present on the command
63 // line. This is for testing purpose.
64 bool SimulatingElevatedRecovery() {
65 return CommandLine::ForCurrentProcess()->
66 HasSwitch(switches::kSimulateElevatedRecovery);
67 }
68 #endif
69
70 #if defined(OS_WIN)
71 scoped_ptr<base::DictionaryValue> ReadManifest(const base::FilePath& manifest) {
72 JSONFileValueSerializer serializer(manifest);
73 std::string error;
74 scoped_ptr<base::Value> root(serializer.Deserialize(NULL, &error));
75 if (root.get() && root->IsType(base::Value::TYPE_DICTIONARY)) {
76 return scoped_ptr<base::DictionaryValue>(
77 static_cast<base::DictionaryValue*>(root.release()));
78 }
79 return scoped_ptr<base::DictionaryValue>();
80 }
81
82 void DoElevatedInstallRecoveryComponent(const base::FilePath& path) {
83 const base::FilePath main_file = path.Append(kRecoveryFileName);
84 const base::FilePath manifest_file =
85 path.Append(FILE_PATH_LITERAL("manifest.json"));
86 if (!base::PathExists(main_file) || !base::PathExists(manifest_file))
87 return;
88
89 scoped_ptr<base::DictionaryValue> manifest(ReadManifest(manifest_file));
90 std::string name;
91 manifest->GetStringASCII("name", &name);
92 if (name != kRecoveryManifestName)
93 return;
94 std::string proposed_version;
95 manifest->GetStringASCII("version", &proposed_version);
96 const Version version(proposed_version.c_str());
97 if (!version.IsValid())
98 return;
99
100 CommandLine cmdline(main_file);
101 std::string arguments;
102 if (manifest->GetStringASCII("x-recovery-args", &arguments))
103 cmdline.AppendArg(arguments);
104 std::string add_version;
105 if (manifest->GetStringASCII("x-recovery-add-version", &add_version) &&
106 add_version == "yes") {
107 cmdline.AppendSwitchASCII("version", version.GetString());
108 }
109
110 base::LaunchOptions options;
111 options.start_hidden = true;
112 base::LaunchElevatedProcess(cmdline, options);
113 }
114
115 void ElevatedInstallRecoveryComponent(const base::FilePath& installer_path) {
116 base::WorkerPool::PostTask(
117 FROM_HERE,
118 base::Bind(&DoElevatedInstallRecoveryComponent, installer_path),
119 true);
120 }
121 #endif
122
50 } // namespace 123 } // namespace
51 124
125 // Component installer that is responsible to repair the chrome installation
126 // or repair the Google update installation. This is a last resort safety
127 // mechanism.
128 // For user Chrome, recovery component just installs silently. For machine
129 // Chrome, elevation may be needed. If that happens, the installer will set
130 // preference flag prefs::kRecoveryComponentNeedsElevation to request that.
131 // There is a global error service monitors this flag and will pop up
132 // bubble if the flag is set to true.
133 // See chrome/browser/recovery/recovery_install_global_error.cc for details.
52 class RecoveryComponentInstaller : public ComponentInstaller { 134 class RecoveryComponentInstaller : public ComponentInstaller {
53 public: 135 public:
54 explicit RecoveryComponentInstaller(const Version& version, 136 RecoveryComponentInstaller(const Version& version, PrefService* prefs);
55 PrefService* prefs);
56
57 ~RecoveryComponentInstaller() override {} 137 ~RecoveryComponentInstaller() override {}
58 138
59 void OnUpdateError(int error) override; 139 void OnUpdateError(int error) override;
60 140
61 bool Install(const base::DictionaryValue& manifest, 141 bool Install(const base::DictionaryValue& manifest,
62 const base::FilePath& unpack_path) override; 142 const base::FilePath& unpack_path) override;
63 143
64 bool GetInstalledFile(const std::string& file, 144 bool GetInstalledFile(const std::string& file,
65 base::FilePath* installed_file) override; 145 base::FilePath* installed_file) override;
66 146
67 private: 147 private:
148 bool RunInstallCommand(const CommandLine& cmdline,
149 const base::FilePath& installer_folder) const;
150
68 Version current_version_; 151 Version current_version_;
69 PrefService* prefs_; 152 PrefService* prefs_;
70 }; 153 };
71 154
155 void SimulateElevatedRecoveryHelper(PrefService* prefs) {
156 prefs->SetBoolean(prefs::kRecoveryComponentNeedsElevation, true);
157 }
158
72 void RecoveryRegisterHelper(ComponentUpdateService* cus, PrefService* prefs) { 159 void RecoveryRegisterHelper(ComponentUpdateService* cus, PrefService* prefs) {
73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 160 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
74 Version version(prefs->GetString(prefs::kRecoveryComponentVersion)); 161 Version version(prefs->GetString(prefs::kRecoveryComponentVersion));
75 if (!version.IsValid()) { 162 if (!version.IsValid()) {
76 NOTREACHED(); 163 NOTREACHED();
77 return; 164 return;
78 } 165 }
79 166
80 CrxComponent recovery; 167 CrxComponent recovery;
81 recovery.name = "recovery"; 168 recovery.name = "recovery";
82 recovery.installer = new RecoveryComponentInstaller(version, prefs); 169 recovery.installer = new RecoveryComponentInstaller(version, prefs);
83 recovery.version = version; 170 recovery.version = version;
84 recovery.pk_hash.assign(kSha2Hash, &kSha2Hash[sizeof(kSha2Hash)]); 171 recovery.pk_hash.assign(kSha2Hash, &kSha2Hash[sizeof(kSha2Hash)]);
85 if (cus->RegisterComponent(recovery) != ComponentUpdateService::kOk) { 172 if (cus->RegisterComponent(recovery) != ComponentUpdateService::kOk) {
86 NOTREACHED() << "Recovery component registration failed."; 173 NOTREACHED() << "Recovery component registration failed.";
87 } 174 }
88 } 175 }
89 176
90 void RecoveryUpdateVersionHelper(const Version& version, PrefService* prefs) { 177 void RecoveryUpdateVersionHelper(const Version& version, PrefService* prefs) {
91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 178 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
92 prefs->SetString(prefs::kRecoveryComponentVersion, version.GetString()); 179 prefs->SetString(prefs::kRecoveryComponentVersion, version.GetString());
93 } 180 }
94 181
182 void SetPrefsForElevatedRecoveryInstall(const base::FilePath& unpack_path,
183 PrefService* prefs) {
184 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
185 prefs->SetFilePath(prefs::kRecoveryComponentUnpackPath, unpack_path);
186 prefs->SetBoolean(prefs::kRecoveryComponentNeedsElevation, true);
187 }
188
95 RecoveryComponentInstaller::RecoveryComponentInstaller(const Version& version, 189 RecoveryComponentInstaller::RecoveryComponentInstaller(const Version& version,
96 PrefService* prefs) 190 PrefService* prefs)
97 : current_version_(version), prefs_(prefs) { 191 : current_version_(version), prefs_(prefs) {
98 DCHECK(version.IsValid()); 192 DCHECK(version.IsValid());
99 } 193 }
100 194
101 void RecoveryComponentInstaller::OnUpdateError(int error) { 195 void RecoveryComponentInstaller::OnUpdateError(int error) {
102 NOTREACHED() << "Recovery component update error: " << error; 196 NOTREACHED() << "Recovery component update error: " << error;
103 } 197 }
104 198
199 #if defined(OS_WIN)
200 void WaitForInstallToComplete(base::ProcessHandle process_handle,
201 const base::FilePath& installer_folder,
202 PrefService* prefs) {
203 int installer_exit_code = 0;
204 const base::TimeDelta kMaxWaitTime = base::TimeDelta::FromSeconds(600);
205 if (base::WaitForExitCodeWithTimeout(process_handle,
206 &installer_exit_code,
207 kMaxWaitTime) &&
208 installer_exit_code == EXIT_CODE_ELEVATION_NEEDED) {
209 BrowserThread::PostTask(
210 BrowserThread::UI,
211 FROM_HERE,
212 base::Bind(&SetPrefsForElevatedRecoveryInstall,
213 installer_folder,
214 prefs));
215 }
216 base::CloseProcessHandle(process_handle);
217 }
218
219 bool RecoveryComponentInstaller::RunInstallCommand(
220 const CommandLine& cmdline, const base::FilePath& installer_folder) const {
221 base::ProcessHandle process_handle;
222 base::LaunchOptions options;
223 options.start_hidden = true;
224 if (!base::LaunchProcess(cmdline, options, &process_handle))
225 return false;
226
227 // Let worker pool thread wait for us so we don't block Chrome shutdown.
228 base::WorkerPool::PostTask(
229 FROM_HERE,
230 base::Bind(&WaitForInstallToComplete,
231 process_handle, installer_folder, prefs_),
232 true);
233
234 // Returns true regardless of install result since from updater service
235 // perspective the install is done, even we may need to do elevated
236 // install later.
237 return true;
238 }
239 #else
240 bool RecoveryComponentInstaller::RunInstallCommand(
241 const CommandLine& cmdline, const base::FilePath&) const {
242 return base::LaunchProcess(cmdline, base::LaunchOptions(), NULL);
243 }
244 #endif
245
105 bool RecoveryComponentInstaller::Install(const base::DictionaryValue& manifest, 246 bool RecoveryComponentInstaller::Install(const base::DictionaryValue& manifest,
106 const base::FilePath& unpack_path) { 247 const base::FilePath& unpack_path) {
107 std::string name; 248 std::string name;
108 manifest.GetStringASCII("name", &name); 249 manifest.GetStringASCII("name", &name);
109 if (name != kRecoveryManifestName) 250 if (name != kRecoveryManifestName)
110 return false; 251 return false;
111 std::string proposed_version; 252 std::string proposed_version;
112 manifest.GetStringASCII("version", &proposed_version); 253 manifest.GetStringASCII("version", &proposed_version);
113 Version version(proposed_version.c_str()); 254 Version version(proposed_version.c_str());
114 if (!version.IsValid()) 255 if (!version.IsValid())
115 return false; 256 return false;
116 if (current_version_.CompareTo(version) >= 0) 257 if (current_version_.CompareTo(version) >= 0)
117 return false; 258 return false;
118 259
119 // Passed the basic tests. Copy the installation to a permanent directory. 260 // Passed the basic tests. Copy the installation to a permanent directory.
120 base::FilePath path; 261 base::FilePath path;
121 if (!PathService::Get(DIR_RECOVERY_BASE, &path)) 262 if (!PathService::Get(DIR_RECOVERY_BASE, &path))
122 return false; 263 return false;
123 if (!base::PathExists(path)) { 264 if (!base::PathExists(path) && !base::CreateDirectory(path))
124 if (!base::CreateDirectory(path)) {
125 return false; 265 return false;
126 }
127 }
128 path = path.AppendASCII(version.GetString()); 266 path = path.AppendASCII(version.GetString());
129 if (base::PathExists(path) && !base::DeleteFile(path, true)) 267 if (base::PathExists(path) && !base::DeleteFile(path, true))
130 return false; 268 return false;
131 if (!base::Move(unpack_path, path)) { 269 if (!base::Move(unpack_path, path)) {
132 DVLOG(1) << "Recovery component move failed."; 270 DVLOG(1) << "Recovery component move failed.";
133 return false; 271 return false;
134 } 272 }
135 273
136 base::FilePath main_file = path.Append(kRecoveryFileName); 274 base::FilePath main_file = path.Append(kRecoveryFileName);
137 if (!base::PathExists(main_file)) 275 if (!base::PathExists(main_file))
138 return false; 276 return false;
139 // Run the recovery component. 277 // Run the recovery component.
140 CommandLine cmdline(main_file); 278 CommandLine cmdline(main_file);
141 std::string arguments; 279 std::string arguments;
142 if (manifest.GetStringASCII("x-recovery-args", &arguments)) 280 if (manifest.GetStringASCII("x-recovery-args", &arguments))
143 cmdline.AppendArg(arguments); 281 cmdline.AppendArg(arguments);
144 std::string add_version; 282 std::string add_version;
145 if (manifest.GetStringASCII("x-recovery-add-version", &add_version)) { 283 if (manifest.GetStringASCII("x-recovery-add-version", &add_version) &&
146 if (add_version == "yes") 284 add_version == "yes") {
147 cmdline.AppendSwitchASCII("version", current_version_.GetString()); 285 cmdline.AppendSwitchASCII("version", current_version_.GetString());
148 } 286 }
287
288 if (!RunInstallCommand(cmdline, path)) {
289 return false;
290 }
291
149 current_version_ = version; 292 current_version_ = version;
150 if (prefs_) { 293 if (prefs_) {
151 BrowserThread::PostTask( 294 BrowserThread::PostTask(
152 BrowserThread::UI, 295 BrowserThread::UI,
153 FROM_HERE, 296 FROM_HERE,
154 base::Bind(&RecoveryUpdateVersionHelper, version, prefs_)); 297 base::Bind(&RecoveryUpdateVersionHelper, version, prefs_));
155 } 298 }
156 return base::LaunchProcess(cmdline, base::LaunchOptions(), NULL); 299 return true;
157 } 300 }
158 301
159 bool RecoveryComponentInstaller::GetInstalledFile( 302 bool RecoveryComponentInstaller::GetInstalledFile(
160 const std::string& file, 303 const std::string& file,
161 base::FilePath* installed_file) { 304 base::FilePath* installed_file) {
162 return false; 305 return false;
163 } 306 }
164 307
165 void RegisterRecoveryComponent(ComponentUpdateService* cus, 308 void RegisterRecoveryComponent(ComponentUpdateService* cus,
166 PrefService* prefs) { 309 PrefService* prefs) {
167 #if !defined(OS_CHROMEOS) 310 #if !defined(OS_CHROMEOS)
311 if (SimulatingElevatedRecovery()) {
312 BrowserThread::PostTask(
313 BrowserThread::UI,
314 FROM_HERE,
315 base::Bind(&SimulateElevatedRecoveryHelper, prefs));
316 }
317
168 // We delay execute the registration because we are not required in 318 // We delay execute the registration because we are not required in
169 // the critical path during browser startup. 319 // the critical path during browser startup.
170 BrowserThread::PostDelayedTask( 320 BrowserThread::PostDelayedTask(
171 BrowserThread::UI, 321 BrowserThread::UI,
172 FROM_HERE, 322 FROM_HERE,
173 base::Bind(&RecoveryRegisterHelper, cus, prefs), 323 base::Bind(&RecoveryRegisterHelper, cus, prefs),
174 base::TimeDelta::FromSeconds(6)); 324 base::TimeDelta::FromSeconds(6));
175 #endif 325 #endif
176 } 326 }
177 327
178 void RegisterPrefsForRecoveryComponent(PrefRegistrySimple* registry) { 328 void RegisterPrefsForRecoveryComponent(PrefRegistrySimple* registry) {
179 registry->RegisterStringPref(prefs::kRecoveryComponentVersion, "0.0.0.0"); 329 registry->RegisterStringPref(prefs::kRecoveryComponentVersion, "0.0.0.0");
330 registry->RegisterFilePathPref(prefs::kRecoveryComponentUnpackPath,
331 base::FilePath());
332 registry->RegisterBooleanPref(prefs::kRecoveryComponentNeedsElevation, false);
180 } 333 }
181 334
182 void AcceptedElevatedRecoveryInstall(PrefService* prefs) { 335 void AcceptedElevatedRecoveryInstall(PrefService* prefs) {
183 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 336 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
337
338 #if defined(OS_WIN)
339 ElevatedInstallRecoveryComponent(
340 prefs->GetFilePath(prefs::kRecoveryComponentUnpackPath));
341 #endif
342 prefs->SetBoolean(prefs::kRecoveryComponentNeedsElevation, false);
184 } 343 }
185 344
186 void DeclinedElevatedRecoveryInstall(PrefService* prefs) { 345 void DeclinedElevatedRecoveryInstall(PrefService* prefs) {
187 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 346 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
347 prefs->SetBoolean(prefs::kRecoveryComponentNeedsElevation, false);
188 } 348 }
189 349
190 } // namespace component_updater 350 } // namespace component_updater
OLDNEW
« no previous file with comments | « chrome/browser/component_updater/recovery_component_installer.h ('k') | components/component_updater/pref_names.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698