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

Side by Side Diff: chrome/installer/setup/setup_main.cc

Issue 2333853002: Support --delete-old-versions in setup.exe. (Closed)
Patch Set: self-review Created 4 years, 2 months 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
« no previous file with comments | « chrome/installer/setup/install.cc ('k') | chrome/installer/setup/setup_singleton.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/installer/setup/setup_main.h" 5 #include "chrome/installer/setup/setup_main.h"
6 6
7 #include <windows.h> 7 #include <windows.h>
8 #include <msi.h> 8 #include <msi.h>
9 #include <shellapi.h> 9 #include <shellapi.h>
10 #include <shlobj.h> 10 #include <shlobj.h>
11 #include <stddef.h> 11 #include <stddef.h>
12 #include <stdint.h> 12 #include <stdint.h>
13 13
14 #include <memory> 14 #include <memory>
15 #include <string> 15 #include <string>
16 16
17 #include "base/at_exit.h" 17 #include "base/at_exit.h"
18 #include "base/command_line.h" 18 #include "base/command_line.h"
19 #include "base/file_version_info.h" 19 #include "base/file_version_info.h"
20 #include "base/files/file_path.h" 20 #include "base/files/file_path.h"
21 #include "base/files/file_util.h" 21 #include "base/files/file_util.h"
22 #include "base/files/scoped_temp_dir.h" 22 #include "base/files/scoped_temp_dir.h"
23 #include "base/macros.h" 23 #include "base/macros.h"
24 #include "base/metrics/histogram_macros.h" 24 #include "base/metrics/histogram_macros.h"
25 #include "base/numerics/safe_conversions.h" 25 #include "base/numerics/safe_conversions.h"
26 #include "base/path_service.h" 26 #include "base/path_service.h"
27 #include "base/process/launch.h" 27 #include "base/process/launch.h"
28 #include "base/process/memory.h" 28 #include "base/process/memory.h"
29 #include "base/process/process.h"
29 #include "base/process/process_metrics.h" 30 #include "base/process/process_metrics.h"
30 #include "base/strings/string16.h" 31 #include "base/strings/string16.h"
31 #include "base/strings/string_number_conversions.h" 32 #include "base/strings/string_number_conversions.h"
32 #include "base/strings/string_util.h" 33 #include "base/strings/string_util.h"
33 #include "base/strings/stringprintf.h" 34 #include "base/strings/stringprintf.h"
34 #include "base/strings/utf_string_conversions.h" 35 #include "base/strings/utf_string_conversions.h"
35 #include "base/time/time.h" 36 #include "base/time/time.h"
36 #include "base/values.h" 37 #include "base/values.h"
37 #include "base/version.h" 38 #include "base/version.h"
38 #include "base/win/process_startup_helper.h" 39 #include "base/win/process_startup_helper.h"
(...skipping 10 matching lines...) Expand all
49 #include "chrome/installer/setup/install.h" 50 #include "chrome/installer/setup/install.h"
50 #include "chrome/installer/setup/install_worker.h" 51 #include "chrome/installer/setup/install_worker.h"
51 #include "chrome/installer/setup/installer_crash_reporting.h" 52 #include "chrome/installer/setup/installer_crash_reporting.h"
52 #include "chrome/installer/setup/persistent_histogram_storage.h" 53 #include "chrome/installer/setup/persistent_histogram_storage.h"
53 #include "chrome/installer/setup/setup_constants.h" 54 #include "chrome/installer/setup/setup_constants.h"
54 #include "chrome/installer/setup/setup_singleton.h" 55 #include "chrome/installer/setup/setup_singleton.h"
55 #include "chrome/installer/setup/setup_util.h" 56 #include "chrome/installer/setup/setup_util.h"
56 #include "chrome/installer/setup/uninstall.h" 57 #include "chrome/installer/setup/uninstall.h"
57 #include "chrome/installer/util/browser_distribution.h" 58 #include "chrome/installer/util/browser_distribution.h"
58 #include "chrome/installer/util/delete_after_reboot_helper.h" 59 #include "chrome/installer/util/delete_after_reboot_helper.h"
60 #include "chrome/installer/util/delete_old_versions.h"
59 #include "chrome/installer/util/delete_tree_work_item.h" 61 #include "chrome/installer/util/delete_tree_work_item.h"
60 #include "chrome/installer/util/google_update_constants.h" 62 #include "chrome/installer/util/google_update_constants.h"
61 #include "chrome/installer/util/google_update_settings.h" 63 #include "chrome/installer/util/google_update_settings.h"
62 #include "chrome/installer/util/google_update_util.h" 64 #include "chrome/installer/util/google_update_util.h"
63 #include "chrome/installer/util/helper.h" 65 #include "chrome/installer/util/helper.h"
64 #include "chrome/installer/util/html_dialog.h" 66 #include "chrome/installer/util/html_dialog.h"
65 #include "chrome/installer/util/install_util.h" 67 #include "chrome/installer/util/install_util.h"
66 #include "chrome/installer/util/installation_state.h" 68 #include "chrome/installer/util/installation_state.h"
67 #include "chrome/installer/util/installation_validator.h" 69 #include "chrome/installer/util/installation_validator.h"
68 #include "chrome/installer/util/installer_state.h" 70 #include "chrome/installer/util/installer_state.h"
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after
379 Product* chrome = installer_state->AddProduct(&multi_chrome); 381 Product* chrome = installer_state->AddProduct(&multi_chrome);
380 VLOG(1) << "Broken install of product to be included for repair: " 382 VLOG(1) << "Broken install of product to be included for repair: "
381 << chrome->distribution()->GetDisplayName(); 383 << chrome->distribution()->GetDisplayName();
382 } // else uninstall the binaries in UninstallBinariesIfUnused. 384 } // else uninstall the binaries in UninstallBinariesIfUnused.
383 } 385 }
384 } 386 }
385 } 387 }
386 } 388 }
387 } 389 }
388 390
391 void RecordNumDeleteOldVersionsAttempsBeforeAbort(int num_attempts) {
392 UMA_HISTOGRAM_COUNTS("Setup.Install.NumDeleteOldVersionsAttemptsBeforeAbort",
rkaplow 2016/09/28 14:31:30 nit, I've refactored the COUNT histograms which no
fdoray 2016/10/04 20:22:43 Done.
393 num_attempts);
394 }
395
396 // Repetitively attempts to delete all files that belong to old versions of
397 // Chrome from |install_dir|. Waits 15 seconds before the first attempt and 5
398 // minutes after each unsuccessful attempt. Returns when no files that belong to
399 // an old version of Chrome remain or when another process tries to acquire the
400 // SetupSingleton.
401 installer::InstallStatus RepeatDeleteOldVersions(
402 const base::FilePath& install_dir,
403 const installer::SetupSingleton& setup_singleton) {
404 constexpr int kMaxNumAttempts = 12;
405 int num_attempts = 0;
406
407 while (num_attempts < kMaxNumAttempts) {
408 // Wait 15 seconds before the first attempt because trying to delete old
409 // files right away is likely to fail. Indeed, this is called in 2
410 // occasions:
411 // - When the installer fails to delete old files after a not-in-use update:
412 // retrying immediately is likely to fail again.
413 // - When executables are successfully renamed on Chrome startup or
414 // shutdown: old files can't be deleted because Chrome is still in use.
415 // Wait 5 minutes after an unsuccessful attempt because retrying immediately
416 // is likely to fail again.
417 const base::TimeDelta max_wait_time = num_attempts == 0
418 ? base::TimeDelta::FromSeconds(15)
419 : base::TimeDelta::FromMinutes(5);
420 if (setup_singleton.WaitForInterrupt(max_wait_time)) {
421 VLOG(1) << "Exiting --delete-old-versions process because another "
422 "process tries to acquire the SetupSingleton.";
423 RecordNumDeleteOldVersionsAttempsBeforeAbort(num_attempts);
424 return installer::SETUP_SINGLETON_RELEASED;
425 }
426
427 const bool was_backgrounded =
428 base::Process::Current().SetProcessBackgrounded(true);
429 const bool delete_old_versions_success =
430 installer::DeleteOldVersions(install_dir);
431 base::Process::Current().SetProcessBackgrounded(was_backgrounded);
grt (UTC plus 2) 2016/09/27 20:50:42 if (was_backgrounded) base::Process::Current().S
fdoray 2016/10/04 20:22:43 Done. if (!was_backgrounded)
grt (UTC plus 2) 2016/10/05 08:17:33 This is backwards in the same way it was before, n
fdoray 2016/10/05 12:19:25 you're right. I wrongly assumed that SetProcessBac
432 ++num_attempts;
433
434 if (delete_old_versions_success) {
435 VLOG(1) << "Successfully deleted all old files from "
436 "--delete-old-versions process.";
437 UMA_HISTOGRAM_COUNTS(
438 "Setup.Install.NumDeleteOldVersionsAttemptsBeforeSuccess",
439 num_attempts);
440 return installer::DELETE_OLD_VERSIONS_SUCCESS;
441 } else if (num_attempts == 1) {
442 VLOG(1) << "Failed to delete all old files from --delete-old-versions "
443 "process. Will retry every five minutes.";
444 }
445 }
446
447 VLOG(1) << "Exiting --delete-old-versions process after retrying too many "
448 "times to delete all old files.";
449 DCHECK_EQ(num_attempts, kMaxNumAttempts);
450 RecordNumDeleteOldVersionsAttempsBeforeAbort(num_attempts);
451 return installer::DELETE_OLD_VERSIONS_TOO_MANY_ATTEMPTS;
452 }
453
389 // This function is called when --rename-chrome-exe option is specified on 454 // This function is called when --rename-chrome-exe option is specified on
390 // setup.exe command line. This function assumes an in-use update has happened 455 // setup.exe command line. This function assumes an in-use update has happened
391 // for Chrome so there should be a file called new_chrome.exe on the file 456 // for Chrome so there should be a file called new_chrome.exe on the file
392 // system and a key called 'opv' in the registry. This function will move 457 // system and a key called 'opv' in the registry. This function will move
393 // new_chrome.exe to chrome.exe and delete 'opv' key in one atomic operation. 458 // new_chrome.exe to chrome.exe and delete 'opv' key in one atomic operation.
394 // This function also deletes elevation policies associated with the old version 459 // This function also deletes elevation policies associated with the old version
395 // if they exist. 460 // if they exist. |setup_exe| is the path to the current executable.
396 installer::InstallStatus RenameChromeExecutables( 461 installer::InstallStatus RenameChromeExecutables(
462 const base::FilePath& setup_exe,
397 const InstallationState& original_state, 463 const InstallationState& original_state,
398 InstallerState* installer_state) { 464 InstallerState* installer_state) {
399 // See what products are already installed in multi mode. When we do the 465 // See what products are already installed in multi mode. When we do the
400 // rename for multi installs, we must update all installations since they 466 // rename for multi installs, we must update all installations since they
401 // share the binaries. 467 // share the binaries.
402 AddExistingMultiInstalls(original_state, installer_state); 468 AddExistingMultiInstalls(original_state, installer_state);
403 const base::FilePath &target_path = installer_state->target_path(); 469 const base::FilePath &target_path = installer_state->target_path();
404 base::FilePath chrome_exe(target_path.Append(installer::kChromeExe)); 470 base::FilePath chrome_exe(target_path.Append(installer::kChromeExe));
405 base::FilePath chrome_new_exe(target_path.Append(installer::kChromeNewExe)); 471 base::FilePath chrome_new_exe(target_path.Append(installer::kChromeNewExe));
406 base::FilePath chrome_old_exe(target_path.Append(installer::kChromeOldExe)); 472 base::FilePath chrome_old_exe(target_path.Append(installer::kChromeOldExe));
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
447 install_list->AddDeleteRegValueWorkItem(reg_root, 513 install_list->AddDeleteRegValueWorkItem(reg_root,
448 version_key, 514 version_key,
449 KEY_WOW64_32KEY, 515 KEY_WOW64_32KEY,
450 google_update::kRegRenameCmdField); 516 google_update::kRegRenameCmdField);
451 } 517 }
452 // old_chrome.exe is still in use in most cases, so ignore failures here. 518 // old_chrome.exe is still in use in most cases, so ignore failures here.
453 install_list->AddDeleteTreeWorkItem(chrome_old_exe, temp_path.path()) 519 install_list->AddDeleteTreeWorkItem(chrome_old_exe, temp_path.path())
454 ->set_best_effort(true); 520 ->set_best_effort(true);
455 521
456 installer::InstallStatus ret = installer::RENAME_SUCCESSFUL; 522 installer::InstallStatus ret = installer::RENAME_SUCCESSFUL;
457 if (!install_list->Do()) { 523 if (install_list->Do()) {
524 installer::LaunchDeleteOldVersionsProcess(setup_exe, *installer_state);
525 } else {
458 LOG(ERROR) << "Renaming of executables failed. Rolling back any changes."; 526 LOG(ERROR) << "Renaming of executables failed. Rolling back any changes.";
459 install_list->Rollback(); 527 install_list->Rollback();
460 ret = installer::RENAME_FAILED; 528 ret = installer::RENAME_FAILED;
461 } 529 }
462 // temp_path's dtor will take care of deleting or scheduling itself for 530 // temp_path's dtor will take care of deleting or scheduling itself for
463 // deletion at reboot when this scope closes. 531 // deletion at reboot when this scope closes.
464 VLOG(1) << "Deleting temporary directory " << temp_path.path().value(); 532 VLOG(1) << "Deleting temporary directory " << temp_path.path().value();
465 533
466 return ret; 534 return ret;
467 } 535 }
(...skipping 761 matching lines...) Expand 10 before | Expand all | Expand 10 after
1229 status = installer::IN_USE_UPDATED; 1297 status = installer::IN_USE_UPDATED;
1230 } else { 1298 } else {
1231 if (ShellUtil::RegisterChromeBrowser(chrome_install->distribution(), 1299 if (ShellUtil::RegisterChromeBrowser(chrome_install->distribution(),
1232 chrome_exe, suffix, false)) 1300 chrome_exe, suffix, false))
1233 status = installer::IN_USE_UPDATED; 1301 status = installer::IN_USE_UPDATED;
1234 } 1302 }
1235 } else { 1303 } else {
1236 LOG(DFATAL) << "Can't register browser - Chrome distribution not found"; 1304 LOG(DFATAL) << "Can't register browser - Chrome distribution not found";
1237 } 1305 }
1238 *exit_code = InstallUtil::GetInstallReturnCode(status); 1306 *exit_code = InstallUtil::GetInstallReturnCode(status);
1239 } else if (cmd_line.HasSwitch(installer::switches::kRenameChromeExe)) { 1307 } else if (cmd_line.HasSwitch(installer::switches::kDeleteOldVersions) ||
1240 // If --rename-chrome-exe is specified, we want to rename the executables 1308 cmd_line.HasSwitch(installer::switches::kRenameChromeExe)) {
1241 // and exit.
1242 std::unique_ptr<installer::SetupSingleton> setup_singleton( 1309 std::unique_ptr<installer::SetupSingleton> setup_singleton(
1243 installer::SetupSingleton::Acquire( 1310 installer::SetupSingleton::Acquire(
1244 cmd_line, MasterPreferences::ForCurrentProcess(), original_state, 1311 cmd_line, MasterPreferences::ForCurrentProcess(), original_state,
1245 installer_state)); 1312 installer_state));
1246 if (!setup_singleton) 1313 if (!setup_singleton) {
1247 *exit_code = installer::SETUP_SINGLETON_ACQUISITION_FAILED; 1314 *exit_code = installer::SETUP_SINGLETON_ACQUISITION_FAILED;
1248 else 1315 } else if (cmd_line.HasSwitch(installer::switches::kDeleteOldVersions)) {
1249 *exit_code = RenameChromeExecutables(*original_state, installer_state); 1316 // In multi-install mode, determine what products are installed to
1317 // populate the target_path() used below.
1318 AddExistingMultiInstalls(*original_state, installer_state);
1319
1320 *exit_code = RepeatDeleteOldVersions(installer_state->target_path(),
1321 *setup_singleton);
1322 } else {
1323 DCHECK(cmd_line.HasSwitch(installer::switches::kRenameChromeExe));
1324 *exit_code =
1325 RenameChromeExecutables(setup_exe, *original_state, installer_state);
1326 }
1250 } else if (cmd_line.HasSwitch( 1327 } else if (cmd_line.HasSwitch(
1251 installer::switches::kRemoveChromeRegistration)) { 1328 installer::switches::kRemoveChromeRegistration)) {
1252 // This is almost reverse of --register-chrome-browser option above. 1329 // This is almost reverse of --register-chrome-browser option above.
1253 // Here we delete Chrome browser registration. This option should only 1330 // Here we delete Chrome browser registration. This option should only
1254 // be used when setup.exe is launched with admin rights. We do not 1331 // be used when setup.exe is launched with admin rights. We do not
1255 // make any user specific changes in this option. 1332 // make any user specific changes in this option.
1256 base::string16 suffix; 1333 base::string16 suffix;
1257 if (cmd_line.HasSwitch( 1334 if (cmd_line.HasSwitch(
1258 installer::switches::kRegisterChromeBrowserSuffix)) { 1335 installer::switches::kRegisterChromeBrowserSuffix)) {
1259 suffix = cmd_line.GetSwitchValueNative( 1336 suffix = cmd_line.GetSwitchValueNative(
(...skipping 652 matching lines...) Expand 10 before | Expand all | Expand 10 after
1912 // Note that we allow the status installer::UNINSTALL_REQUIRES_REBOOT 1989 // Note that we allow the status installer::UNINSTALL_REQUIRES_REBOOT
1913 // to pass through, since this is only returned on uninstall which is 1990 // to pass through, since this is only returned on uninstall which is
1914 // never invoked directly by Google Update. 1991 // never invoked directly by Google Update.
1915 return_code = InstallUtil::GetInstallReturnCode(install_status); 1992 return_code = InstallUtil::GetInstallReturnCode(install_status);
1916 } 1993 }
1917 1994
1918 VLOG(1) << "Installation complete, returning: " << return_code; 1995 VLOG(1) << "Installation complete, returning: " << return_code;
1919 1996
1920 return return_code; 1997 return return_code;
1921 } 1998 }
OLDNEW
« no previous file with comments | « chrome/installer/setup/install.cc ('k') | chrome/installer/setup/setup_singleton.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698