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

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: addressed comments 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 // Checks if elevated recovery simulation switch was present on the command
62 // line. This is for testing purpose.
63 bool SimulatingElevatedRecovery() {
64 const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
Sorin Jianu 2014/12/06 00:41:53 do you still want to have the local?
xiaoling 2014/12/06 00:56:27 Done.
65 return cmd_line->HasSwitch(switches::kSimulateElevatedRecovery);
66 }
67
68 scoped_ptr<base::DictionaryValue> ReadManifest(const base::FilePath& manifest) {
69 JSONFileValueSerializer serializer(manifest);
70 std::string error;
71 scoped_ptr<base::Value> root(serializer.Deserialize(NULL, &error));
72 if (root.get() && root->IsType(base::Value::TYPE_DICTIONARY)) {
73 return scoped_ptr<base::DictionaryValue>(
74 static_cast<base::DictionaryValue*>(root.release()));
75 }
76 return scoped_ptr<base::DictionaryValue>();
77 }
78
79 #if defined(OS_WIN)
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 Version version(proposed_version.c_str());
Sorin Jianu 2014/12/06 00:41:53 const?
xiaoling 2014/12/06 00:56:27 Done.
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(
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 SetRecoveryComponentNeedsElevationPrefs(
181 bool elevation_needed, PrefService* prefs) {
182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
183 prefs->SetBoolean(prefs::kRecoveryComponentNeedsElevation, elevation_needed);
184 }
185
186 void SetRecoveryComponentNeedsUnpackPath(const base::FilePath& unpack_path,
187 PrefService* prefs) {
188 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
189 prefs->SetFilePath(prefs::kRecoveryComponentUnpackPath, unpack_path);
190 }
191
95 RecoveryComponentInstaller::RecoveryComponentInstaller(const Version& version, 192 RecoveryComponentInstaller::RecoveryComponentInstaller(const Version& version,
96 PrefService* prefs) 193 PrefService* prefs)
97 : current_version_(version), prefs_(prefs) { 194 : current_version_(version), prefs_(prefs) {
98 DCHECK(version.IsValid()); 195 DCHECK(version.IsValid());
99 } 196 }
100 197
101 void RecoveryComponentInstaller::OnUpdateError(int error) { 198 void RecoveryComponentInstaller::OnUpdateError(int error) {
102 NOTREACHED() << "Recovery component update error: " << error; 199 NOTREACHED() << "Recovery component update error: " << error;
103 } 200 }
104 201
202 #if defined(OS_WIN)
203 bool RecoveryComponentInstaller::RunInstallCommand(
204 const CommandLine& cmdline, const base::FilePath& installer_folder) const {
205 base::ProcessHandle process_handle;
206 base::LaunchOptions options;
207 options.start_hidden = true;
208 if (!base::LaunchProcess(cmdline, options, &process_handle))
209 return false;
210
211 int installer_exit_code = 0;
212 const base::TimeDelta kMaxWaitTime = base::TimeDelta::FromSeconds(600);
213 if (base::WaitForExitCodeWithTimeout(process_handle,
214 &installer_exit_code,
215 kMaxWaitTime) &&
216 installer_exit_code == EXIT_CODE_ELEVATION_NEEDED) {
217 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
Sorin Jianu 2014/12/06 00:41:53 we could refactor this as two calls which occur he
xiaoling 2014/12/06 00:56:27 Done.
218 base::Bind(&SetRecoveryComponentNeedsUnpackPath,
219 installer_folder,
220 prefs_));
221 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
222 base::Bind(&SetRecoveryComponentNeedsElevationPrefs, true, prefs_));
223 }
224
225 // Returns true regardless of exit code since from updater service
226 // perspective the install is done, even we may need to do elevated
227 // install later.
228 return true;
229 }
230 #else
231 bool RecoveryComponentInstaller::RunInstallCommand(
232 const CommandLine& cmdline, const base::FilePath&) const {
233 return base::LaunchProcess(cmdline, base::LaunchOptions(), NULL);
234 }
235 #endif
236
105 bool RecoveryComponentInstaller::Install(const base::DictionaryValue& manifest, 237 bool RecoveryComponentInstaller::Install(const base::DictionaryValue& manifest,
106 const base::FilePath& unpack_path) { 238 const base::FilePath& unpack_path) {
107 std::string name; 239 std::string name;
108 manifest.GetStringASCII("name", &name); 240 manifest.GetStringASCII("name", &name);
109 if (name != kRecoveryManifestName) 241 if (name != kRecoveryManifestName)
110 return false; 242 return false;
111 std::string proposed_version; 243 std::string proposed_version;
112 manifest.GetStringASCII("version", &proposed_version); 244 manifest.GetStringASCII("version", &proposed_version);
113 Version version(proposed_version.c_str()); 245 Version version(proposed_version.c_str());
114 if (!version.IsValid()) 246 if (!version.IsValid())
115 return false; 247 return false;
116 if (current_version_.CompareTo(version) >= 0) 248 if (current_version_.CompareTo(version) >= 0)
117 return false; 249 return false;
118 250
119 // Passed the basic tests. Copy the installation to a permanent directory. 251 // Passed the basic tests. Copy the installation to a permanent directory.
120 base::FilePath path; 252 base::FilePath path;
121 if (!PathService::Get(DIR_RECOVERY_BASE, &path)) 253 if (!PathService::Get(DIR_RECOVERY_BASE, &path))
122 return false; 254 return false;
123 if (!base::PathExists(path)) { 255 if (!base::PathExists(path) && !base::CreateDirectory(path))
124 if (!base::CreateDirectory(path)) {
125 return false; 256 return false;
126 }
127 }
128 path = path.AppendASCII(version.GetString()); 257 path = path.AppendASCII(version.GetString());
129 if (base::PathExists(path) && !base::DeleteFile(path, true)) 258 if (base::PathExists(path) && !base::DeleteFile(path, true))
130 return false; 259 return false;
131 if (!base::Move(unpack_path, path)) { 260 if (!base::Move(unpack_path, path)) {
132 DVLOG(1) << "Recovery component move failed."; 261 DVLOG(1) << "Recovery component move failed.";
133 return false; 262 return false;
134 } 263 }
135 264
136 base::FilePath main_file = path.Append(kRecoveryFileName); 265 base::FilePath main_file = path.Append(kRecoveryFileName);
137 if (!base::PathExists(main_file)) 266 if (!base::PathExists(main_file))
138 return false; 267 return false;
139 // Run the recovery component. 268 // Run the recovery component.
140 CommandLine cmdline(main_file); 269 CommandLine cmdline(main_file);
141 std::string arguments; 270 std::string arguments;
142 if (manifest.GetStringASCII("x-recovery-args", &arguments)) 271 if (manifest.GetStringASCII("x-recovery-args", &arguments))
143 cmdline.AppendArg(arguments); 272 cmdline.AppendArg(arguments);
144 std::string add_version; 273 std::string add_version;
145 if (manifest.GetStringASCII("x-recovery-add-version", &add_version)) { 274 if (manifest.GetStringASCII("x-recovery-add-version", &add_version) &&
146 if (add_version == "yes") 275 add_version == "yes") {
147 cmdline.AppendSwitchASCII("version", current_version_.GetString()); 276 cmdline.AppendSwitchASCII("version", current_version_.GetString());
148 } 277 }
278
279 if (!RunInstallCommand(cmdline, path)) {
280 return false;
281 }
282
149 current_version_ = version; 283 current_version_ = version;
150 if (prefs_) { 284 if (prefs_) {
151 BrowserThread::PostTask( 285 BrowserThread::PostTask(
152 BrowserThread::UI, 286 BrowserThread::UI,
153 FROM_HERE, 287 FROM_HERE,
154 base::Bind(&RecoveryUpdateVersionHelper, version, prefs_)); 288 base::Bind(&RecoveryUpdateVersionHelper, version, prefs_));
155 } 289 }
156 return base::LaunchProcess(cmdline, base::LaunchOptions(), NULL); 290 return true;
157 } 291 }
158 292
159 bool RecoveryComponentInstaller::GetInstalledFile( 293 bool RecoveryComponentInstaller::GetInstalledFile(
160 const std::string& file, 294 const std::string& file,
161 base::FilePath* installed_file) { 295 base::FilePath* installed_file) {
162 return false; 296 return false;
163 } 297 }
164 298
165 void RegisterRecoveryComponent(ComponentUpdateService* cus, 299 void RegisterRecoveryComponent(ComponentUpdateService* cus,
166 PrefService* prefs) { 300 PrefService* prefs) {
301 if (SimulatingElevatedRecovery()) {
Sorin Jianu 2014/12/06 00:41:52 do we need to recovery for Chrome OS too?
xiaoling 2014/12/06 00:56:27 Done.
302 BrowserThread::PostTask(
303 BrowserThread::UI,
304 FROM_HERE,
305 base::Bind(&SimulateElevatedRecoveryHelper, prefs));
306 }
307
167 #if !defined(OS_CHROMEOS) 308 #if !defined(OS_CHROMEOS)
168 // We delay execute the registration because we are not required in 309 // We delay execute the registration because we are not required in
169 // the critical path during browser startup. 310 // the critical path during browser startup.
170 BrowserThread::PostDelayedTask( 311 BrowserThread::PostDelayedTask(
171 BrowserThread::UI, 312 BrowserThread::UI,
172 FROM_HERE, 313 FROM_HERE,
173 base::Bind(&RecoveryRegisterHelper, cus, prefs), 314 base::Bind(&RecoveryRegisterHelper, cus, prefs),
174 base::TimeDelta::FromSeconds(6)); 315 base::TimeDelta::FromSeconds(6));
175 #endif 316 #endif
176 } 317 }
177 318
178 void RegisterPrefsForRecoveryComponent(PrefRegistrySimple* registry) { 319 void RegisterPrefsForRecoveryComponent(PrefRegistrySimple* registry) {
179 registry->RegisterStringPref(prefs::kRecoveryComponentVersion, "0.0.0.0"); 320 registry->RegisterStringPref(prefs::kRecoveryComponentVersion, "0.0.0.0");
321 registry->RegisterFilePathPref(prefs::kRecoveryComponentUnpackPath,
322 base::FilePath());
323 registry->RegisterBooleanPref(prefs::kRecoveryComponentNeedsElevation, false);
180 } 324 }
181 325
182 void AcceptedElevatedRecoveryInstall(PrefService* prefs) { 326 void AcceptedElevatedRecoveryInstall(PrefService* prefs) {
183 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 327 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
328
329 #if defined(OS_WIN)
330 ElevatedInstallRecoveryComponent(
331 prefs->GetFilePath(prefs::kRecoveryComponentUnpackPath));
332 #endif
333 prefs->SetBoolean(prefs::kRecoveryComponentNeedsElevation, false);
184 } 334 }
185 335
186 void DeclinedElevatedRecoveryInstall(PrefService* prefs) { 336 void DeclinedElevatedRecoveryInstall(PrefService* prefs) {
187 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 337 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
338 prefs->SetBoolean(prefs::kRecoveryComponentNeedsElevation, false);
188 } 339 }
189 340
190 } // namespace component_updater 341 } // namespace component_updater
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698