forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuninstall.cc
1227 lines (1078 loc) · 50.8 KB
/
uninstall.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// This file defines the methods useful for uninstalling Chrome.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "chrome/installer/setup/uninstall.h"
#include <windows.h>
#include <shlobj.h>
#include <stddef.h>
#include <stdint.h>
#include <initializer_list>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/base_paths.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/path_service.h"
#include "base/process/kill.h"
#include "base/process/launch.h"
#include "base/process/process_iterator.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/win/registry.h"
#include "base/win/shortcut.h"
#include "build/branding_buildflags.h"
#include "chrome/chrome_elf/blocklist_constants.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_result_codes.h"
#include "chrome/install_static/install_util.h"
#include "chrome/installer/setup/brand_behaviors.h"
#include "chrome/installer/setup/install.h"
#include "chrome/installer/setup/install_worker.h"
#include "chrome/installer/setup/installer_state.h"
#include "chrome/installer/setup/launch_chrome.h"
#include "chrome/installer/setup/modify_params.h"
#include "chrome/installer/setup/setup_constants.h"
#include "chrome/installer/setup/setup_util.h"
#include "chrome/installer/setup/user_hive_visitor.h"
#include "chrome/installer/util/auto_launch_util.h"
#include "chrome/installer/util/delete_after_reboot_helper.h"
#include "chrome/installer/util/firewall_manager_win.h"
#include "chrome/installer/util/google_update_constants.h"
#include "chrome/installer/util/google_update_settings.h"
#include "chrome/installer/util/helper.h"
#include "chrome/installer/util/install_service_work_item.h"
#include "chrome/installer/util/install_util.h"
#include "chrome/installer/util/installation_state.h"
#include "chrome/installer/util/logging_installer.h"
#include "chrome/installer/util/registry_util.h"
#include "chrome/installer/util/self_cleaning_temp_dir.h"
#include "chrome/installer/util/shell_util.h"
#include "chrome/installer/util/util_constants.h"
#include "chrome/installer/util/work_item.h"
#include "chrome/windows_services/elevated_tracing_service/service_integration.h"
#include "content/public/common/result_codes.h"
#include "rlz/lib/rlz_lib_clear.h"
#include "rlz/lib/supplementary_branding.h"
using base::win::RegKey;
namespace installer {
namespace {
// Avoid leaving behind a Temp dir. If one exists, ask SelfCleaningTempDir to
// clean it up for us. This may involve scheduling it for deletion after
// reboot. Don't report that a reboot is required in this case, however.
// TODO(erikwright): Shouldn't this still lead to
// ScheduleParentAndGrandparentForDeletion?
void DeleteInstallTempDir(const base::FilePath& target_path) {
base::FilePath temp_path(
target_path.DirName().Append(installer::kInstallTempDir));
if (base::DirectoryExists(temp_path)) {
SelfCleaningTempDir temp_dir;
if (!temp_dir.Initialize(target_path.DirName(),
installer::kInstallTempDir) ||
!temp_dir.Delete()) {
LOG(ERROR) << "Failed to delete temp dir " << temp_path.value();
}
}
}
// Processes uninstall WorkItems from install_worker in no-rollback-list.
void ProcessChromeWorkItems(const InstallerState& installer_state) {
std::unique_ptr<WorkItemList> work_item_list(WorkItem::CreateWorkItemList());
work_item_list->set_log_message(
"Cleanup OS upgrade command and deprecated per-user registrations");
work_item_list->set_best_effort(true);
work_item_list->set_rollback_enabled(false);
AddOsUpgradeWorkItems(installer_state, base::FilePath(), base::Version(),
work_item_list.get());
// Perform a best-effort cleanup of per-user keys. On system-level installs
// this will only cleanup keys for the user running the uninstall but it was
// considered that this was good enough (better than triggering Active Setup
// for all users solely for this cleanup).
AddCleanupDeprecatedPerUserRegistrationsWorkItems(work_item_list.get());
work_item_list->Do();
}
void ClearRlzProductState() {
const rlz_lib::AccessPoint points[] = {
rlz_lib::CHROME_OMNIBOX, rlz_lib::CHROME_HOME_PAGE,
rlz_lib::CHROME_APP_LIST, rlz_lib::NO_ACCESS_POINT};
rlz_lib::ClearProductState(rlz_lib::CHROME, points);
// If chrome has been reactivated, clear all events for this brand as well.
std::wstring reactivation_brand_wide;
if (GoogleUpdateSettings::GetReactivationBrand(&reactivation_brand_wide)) {
std::string reactivation_brand(base::WideToASCII(reactivation_brand_wide));
rlz_lib::SupplementaryBranding branding(reactivation_brand.c_str());
rlz_lib::ClearProductState(rlz_lib::CHROME, points);
}
}
// Removes all files from the installer directory. Returns false in case of an
// error.
bool RemoveInstallerFiles(const base::FilePath& installer_directory) {
base::FileEnumerator file_enumerator(
installer_directory, false,
base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES);
bool success = true;
for (base::FilePath to_delete = file_enumerator.Next(); !to_delete.empty();
to_delete = file_enumerator.Next()) {
VLOG(1) << "Deleting installer path " << to_delete.value();
if (!base::DeletePathRecursively(to_delete)) {
LOG(ERROR) << "Failed to delete path: " << to_delete.value();
success = false;
}
}
return success;
}
// Filter for processes whose base name matches and whose path starts with a
// specified prefix.
class ProcessPathPrefixFilter : public base::ProcessFilter {
public:
explicit ProcessPathPrefixFilter(
base::FilePath::StringViewType process_path_prefix)
: process_path_prefix_(process_path_prefix) {}
// base::ProcessFilter:
bool Includes(const base::ProcessEntry& entry) const override {
// Test if |entry|'s file path starts with the prefix we're looking for.
base::Process process(::OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION,
FALSE, entry.th32ProcessID));
if (!process.IsValid())
return false;
DWORD path_len = MAX_PATH;
wchar_t path_string[MAX_PATH];
if (::QueryFullProcessImageName(process.Handle(), 0, path_string,
&path_len)) {
base::FilePath file_path(path_string);
return base::StartsWith(file_path.value(), process_path_prefix_,
base::CompareCase::INSENSITIVE_ASCII);
}
PLOG(WARNING) << "QueryFullProcessImageName failed for PID "
<< entry.th32ProcessID;
return false;
}
private:
const base::FilePath::StringViewType process_path_prefix_;
};
// Kills all Chrome processes in |target_path|, immediately.
void CloseAllChromeProcesses(const base::FilePath& target_path) {
ProcessPathPrefixFilter target_path_filter(target_path.value());
base::CleanupProcesses(installer::kChromeExe, base::TimeDelta(),
content::RESULT_CODE_HUNG, &target_path_filter);
}
// Updates shortcuts to |old_target_exe| that have non-empty args, making them
// target |new_target_exe| instead. The non-empty args requirement is a
// heuristic to determine whether a shortcut is "user-generated". This routine
// can only be called for user-level installs.
void RetargetUserShortcutsWithArgs(const InstallerState& installer_state,
const base::FilePath& old_target_exe,
const base::FilePath& new_target_exe) {
if (installer_state.system_install()) {
NOTREACHED();
}
ShellUtil::ShellChange install_level = ShellUtil::CURRENT_USER;
// Retarget all shortcuts that point to |old_target_exe| from all
// ShellUtil::ShortcutLocations.
VLOG(1) << "Retargeting shortcuts.";
for (int location = ShellUtil::SHORTCUT_LOCATION_FIRST;
location <= ShellUtil::SHORTCUT_LOCATION_LAST; ++location) {
if (!ShellUtil::RetargetShortcutsWithArgs(
static_cast<ShellUtil::ShortcutLocation>(location), install_level,
old_target_exe, new_target_exe)) {
LOG(WARNING) << "Failed to retarget shortcuts in ShortcutLocation: "
<< location;
}
}
}
// Deletes shortcuts from Start menu, Desktop, Quick Launch, taskbar, and
// secondary tiles on the Start Screen (Win8+). Only shortcuts pointing to any
// of |target_paths| will be removed.
void DeleteShortcuts(const InstallerState& installer_state,
const std::vector<base::FilePath>& target_paths) {
// The per-user shortcut for this user, if present on a system-level install,
// has already been deleted in chrome_browser_main_win.cc::DoUninstallTasks().
ShellUtil::ShellChange install_level = installer_state.system_install()
? ShellUtil::SYSTEM_LEVEL
: ShellUtil::CURRENT_USER;
VLOG(1) << "Deleting shortcuts.";
ShellUtil::RemoveAllShortcuts(install_level, target_paths);
}
bool ScheduleParentAndGrandparentForDeletion(const base::FilePath& path) {
base::FilePath parent_dir = path.DirName();
bool ret = ScheduleFileSystemEntityForDeletion(parent_dir);
if (!ret) {
LOG(ERROR) << "Failed to schedule parent dir for deletion: "
<< parent_dir.value();
} else {
base::FilePath grandparent_dir(parent_dir.DirName());
ret = ScheduleFileSystemEntityForDeletion(grandparent_dir);
if (!ret) {
LOG(ERROR) << "Failed to schedule grandparent dir for deletion: "
<< grandparent_dir.value();
}
}
return ret;
}
// Deletes the given directory if it is empty. Returns DELETE_SUCCEEDED if the
// directory is deleted, DELETE_NOT_EMPTY if it is not empty, and DELETE_FAILED
// otherwise.
DeleteResult DeleteEmptyDir(const base::FilePath& path) {
if (!base::IsDirectoryEmpty(path))
return DELETE_NOT_EMPTY;
if (base::DeletePathRecursively(path))
return DELETE_SUCCEEDED;
LOG(ERROR) << "Failed to delete folder: " << path.value();
return DELETE_FAILED;
}
// Get the user data directory.
base::FilePath GetUserDataDir() {
base::FilePath path;
if (!base::PathService::Get(chrome::DIR_USER_DATA, &path))
return base::FilePath();
return path;
}
// Creates a copy of the local state file and returns a path to the copy.
base::FilePath BackupLocalStateFile(const base::FilePath& user_data_dir) {
base::FilePath backup;
base::FilePath state_file(user_data_dir.Append(chrome::kLocalStateFilename));
if (!base::CreateTemporaryFile(&backup))
LOG(ERROR) << "Failed to create temporary file for Local State.";
else
base::CopyFile(state_file, backup);
return backup;
}
// Deletes a given user data directory as well as the containing product
// directories if they are empty (e.g., "Google\Chrome").
DeleteResult DeleteUserDataDir(const base::FilePath& user_data_dir) {
if (user_data_dir.empty())
return DELETE_SUCCEEDED;
DeleteResult result = DELETE_SUCCEEDED;
VLOG(1) << "Deleting user profile " << user_data_dir.value();
if (!base::DeletePathRecursively(user_data_dir)) {
LOG(ERROR) << "Failed to delete user profile dir: "
<< user_data_dir.value();
result = DELETE_FAILED;
}
const base::FilePath product_dir1(user_data_dir.DirName());
if (!product_dir1.empty() &&
DeleteEmptyDir(product_dir1) == DELETE_SUCCEEDED) {
const base::FilePath product_dir2(product_dir1.DirName());
if (!product_dir2.empty())
DeleteEmptyDir(product_dir2);
}
return result;
}
DeleteResult DeleteChromeFilesAndFolders(const InstallerState& installer_state,
const base::FilePath& setup_exe) {
const base::FilePath& target_path = installer_state.target_path();
if (target_path.empty()) {
LOG(ERROR) << "DeleteChromeFilesAndFolders: no installation destination "
<< "path.";
return DELETE_FAILED; // Nothing else we can do to uninstall, so we return.
}
DeleteInstallTempDir(target_path);
DeleteResult result = DELETE_SUCCEEDED;
base::FilePath installer_directory;
if (target_path.IsParent(setup_exe))
installer_directory = setup_exe.DirName();
// Enumerate all the files in target_path recursively (breadth-first).
// We delete a file or folder unless it is a parent/child of the installer
// directory. For parents of the installer directory, we will later recurse
// and delete all the children (that are not also parents/children of the
// installer directory).
base::FileEnumerator file_enumerator(
target_path, true,
base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES);
for (base::FilePath to_delete = file_enumerator.Next(); !to_delete.empty();
to_delete = file_enumerator.Next()) {
if (!installer_directory.empty() &&
(to_delete == installer_directory ||
installer_directory.IsParent(to_delete) ||
to_delete.IsParent(installer_directory))) {
continue;
}
VLOG(1) << "Deleting install path " << to_delete.value();
if (!base::DeletePathRecursively(to_delete)) {
LOG(ERROR) << "Failed to delete path (1st try): " << to_delete.value();
// Try closing any running Chrome processes and deleting files once again.
CloseAllChromeProcesses(target_path);
if (!base::DeletePathRecursively(to_delete)) {
LOG(ERROR) << "Failed to delete path (2nd try): " << to_delete.value();
result = DELETE_FAILED;
break;
}
}
}
return result;
}
// This method checks if Chrome is currently running or if the user has
// cancelled the uninstall operation by clicking Cancel on the confirmation
// box that Chrome pops up.
InstallStatus IsChromeActiveOrUserCancelled(
const InstallerState& installer_state) {
int32_t exit_code = content::RESULT_CODE_NORMAL_EXIT;
base::CommandLine options(base::CommandLine::NO_PROGRAM);
options.AppendSwitch(installer::switches::kUninstall);
// Here we want to save user from frustration (in case of Chrome crashes)
// and continue with the uninstallation as long as chrome.exe process exit
// code is NOT one of the following:
// - UNINSTALL_CHROME_ALIVE - chrome.exe is currently running
// - UNINSTALL_USER_CANCEL - User cancelled uninstallation
// - HUNG - chrome.exe was killed by HuntForZombieProcesses() (until we can
// give this method some brains and not kill chrome.exe launched
// by us, we will not uninstall if we get this return code).
VLOG(1) << "Launching Chrome to do uninstall tasks.";
if (LaunchChromeAndWait(installer_state.target_path(), options, &exit_code)) {
VLOG(1) << "chrome.exe launched for uninstall confirmation returned: "
<< exit_code;
if ((exit_code == CHROME_RESULT_CODE_UNINSTALL_CHROME_ALIVE) ||
(exit_code == CHROME_RESULT_CODE_UNINSTALL_USER_CANCEL) ||
(exit_code == content::RESULT_CODE_HUNG)) {
return installer::UNINSTALL_CANCELLED;
}
if (exit_code == CHROME_RESULT_CODE_UNINSTALL_DELETE_PROFILE) {
return installer::UNINSTALL_DELETE_PROFILE;
}
} else {
PLOG(ERROR) << "Failed to launch chrome.exe for uninstall confirmation.";
}
return installer::UNINSTALL_CONFIRMED;
}
bool ShouldDeleteProfile(const base::CommandLine& cmd_line,
InstallStatus status) {
return status == installer::UNINSTALL_DELETE_PROFILE ||
cmd_line.HasSwitch(installer::switches::kDeleteProfile);
}
// Removes XP-era filetype registration making Chrome the default browser.
// MSDN (see http://msdn.microsoft.com/library/windows/desktop/cc144148.aspx)
// tells us not to do this, but certain applications break following
// uninstallation if we don't.
void RemoveFiletypeRegistration(const InstallerState& installer_state,
HKEY root,
const std::wstring& browser_entry_suffix) {
std::wstring classes_path(ShellUtil::kRegClasses);
classes_path.push_back(base::FilePath::kSeparators[0]);
const std::wstring prog_id(install_static::GetBrowserProgIdPrefix() +
browser_entry_suffix);
// Delete each filetype association if it references this Chrome. Take care
// not to delete the association if it references a system-level install of
// Chrome (only a risk if the suffix is empty). Don't delete the whole key
// since other apps may have stored data there.
std::vector<const wchar_t*> cleared_assocs;
if (installer_state.system_install() || !browser_entry_suffix.empty() ||
!base::win::RegKey(HKEY_LOCAL_MACHINE, (classes_path + prog_id).c_str(),
KEY_QUERY_VALUE)
.Valid()) {
ValueEquals prog_id_pred(prog_id);
for (const wchar_t* const* filetype =
&ShellUtil::kPotentialFileAssociations[0];
*filetype != nullptr; ++filetype) {
if (DeleteRegistryValueIf(
root, (classes_path + *filetype).c_str(), WorkItem::kWow64Default,
nullptr, prog_id_pred) == ConditionalDeleteResult::DELETED) {
cleared_assocs.push_back(*filetype);
}
}
}
// For all filetype associations in HKLM that have just been removed, attempt
// to restore some reasonable value. We have no definitive way of knowing
// what handlers are the most appropriate, so we use a fixed mapping based on
// the default values for a fresh install of Windows.
if (root == HKEY_LOCAL_MACHINE) {
std::wstring assoc;
base::win::RegKey key;
for (size_t i = 0; i < cleared_assocs.size(); ++i) {
const wchar_t* replacement_prog_id = nullptr;
assoc.assign(cleared_assocs[i]);
// Inelegant, but simpler than a pure data-driven approach.
if (assoc == L".htm" || assoc == L".html")
replacement_prog_id = L"htmlfile";
else if (assoc == L".xht" || assoc == L".xhtml")
replacement_prog_id = L"xhtmlfile";
if (!replacement_prog_id) {
LOG(WARNING) << "No known replacement ProgID for " << assoc
<< " files.";
} else if (key.Open(HKEY_LOCAL_MACHINE,
(classes_path + replacement_prog_id).c_str(),
KEY_QUERY_VALUE) == ERROR_SUCCESS &&
(key.Open(HKEY_LOCAL_MACHINE, (classes_path + assoc).c_str(),
KEY_SET_VALUE) != ERROR_SUCCESS ||
key.WriteValue(nullptr, replacement_prog_id) !=
ERROR_SUCCESS)) {
// The replacement ProgID is registered on the computer but the attempt
// to set it for the filetype failed.
LOG(ERROR) << "Failed to restore system-level filetype association "
<< assoc << " = " << replacement_prog_id;
}
}
}
}
bool DeleteUserRegistryKeys(const std::vector<const std::wstring*>* key_paths,
const wchar_t* user_sid,
base::win::RegKey* key) {
for (const auto* key_path : *key_paths) {
LONG result = key->DeleteKey(key_path->c_str());
if (result == ERROR_SUCCESS) {
VLOG(1) << "Deleted " << user_sid << "\\" << *key_path;
} else if (result != ERROR_FILE_NOT_FOUND) {
::SetLastError(result);
PLOG(ERROR) << "Failed deleting " << user_sid << "\\" << *key_path;
}
}
return true;
}
// Removes Active Setup entries from the registry. This cannot be done through
// a work items list as usual because of different paths based on conditionals,
// but otherwise respects the no rollback/best effort uninstall mentality.
// This will only apply for system-level installs of Chrome/Chromium and will be
// a no-op for all other types of installs.
void UninstallActiveSetupEntries(const InstallerState& installer_state) {
VLOG(1) << "Uninstalling registry entries for Active Setup.";
if (!installer_state.system_install()) {
VLOG(1) << "No Active Setup processing to do for user-level install.";
return;
}
const std::wstring active_setup_path(install_static::GetActiveSetupPath());
DeleteRegistryKey(HKEY_LOCAL_MACHINE, active_setup_path,
WorkItem::kWow64Default);
// Windows leaves keys behind in HKCU\\Software\\(Wow6432Node\\)?Microsoft\\
// Active Setup\\Installed Components\\{guid}
// for every user that logged in since system-level Chrome was installed. This
// is a problem because Windows compares the value of the Version subkey in
// there with the value of the Version subkey in the matching HKLM entries
// before running Chrome's Active Setup so if Chrome was to be
// uninstalled/reinstalled by an admin, some users may not go through Active
// Setup again as desired.
//
// It is however very hard to delete those values as the registry hives for
// other users are not loaded by default under HKEY_USERS (unless a user is
// logged on or has a process impersonating them).
//
// Following our best effort uninstall practices, try to delete the value in
// all users hives. If a given user's hive is not loaded, try to load it to
// proceed with the deletion (failure to do so is ignored).
// Windows automatically adds Wow6432Node when creating/deleting the HKLM key,
// but doesn't seem to do so when manually deleting the user-level keys it
// created.
std::wstring alternate_active_setup_path(active_setup_path);
alternate_active_setup_path.insert(std::size("Software\\") - 1,
L"Wow6432Node\\");
VLOG(1) << "Uninstall per-user Active Setup keys.";
std::vector<const std::wstring*> paths = {&active_setup_path,
&alternate_active_setup_path};
VisitUserHives(
base::BindRepeating(&DeleteUserRegistryKeys, base::Unretained(&paths)));
}
// Removes the persistent blocklist state for the current user. Note: this will
// not remove the state for users other than the one uninstalling Chrome on a
// system-level install (http://crbug.com/388725). Doing so would require
// extracting the per-user registry hive iteration from
// UninstallActiveSetupEntries so that it could service multiple tasks.
void RemoveBlocklistState() {
DeleteRegistryKey(HKEY_CURRENT_USER,
install_static::GetRegistryPath().append(
blocklist::kRegistryBeaconKeyName),
0); // wow64_access
}
// Removes the browser's persistent state in the Windows registry for the
// current user. Note: this will not remove the state for users other than the
// one uninstalling Chrome on a system-level install; see RemoveBlocklistState
// for details.
void RemoveDistributionRegistryState() {
// Delete the contents of the distribution key except for those parts used by
// outsiders to configure Chrome.
DeleteRegistryKeyPartial(HKEY_CURRENT_USER, install_static::GetRegistryPath(),
{L"Extensions", L"NativeMessagingHosts"});
}
// Deletes {root}\Software\Classes\{prog_id} registry key.
bool DeleteProgIdFromSoftwareClasses(HKEY root, const std::wstring& prog_id) {
std::wstring reg_prog_id(ShellUtil::kRegClasses);
reg_prog_id.push_back(base::FilePath::kSeparators[0]);
reg_prog_id.append(prog_id);
return DeleteRegistryKey(root, reg_prog_id, WorkItem::kWow64Default);
}
} // namespace
DeleteResult DeleteChromeDirectoriesIfEmpty(
const base::FilePath& application_directory) {
DeleteResult result(DeleteEmptyDir(application_directory));
if (result == DELETE_SUCCEEDED) {
// Now check and delete if the parent directories are empty
// For example Google\Chrome or Chromium
const base::FilePath product_directory(application_directory.DirName());
if (!product_directory.empty()) {
result = DeleteEmptyDir(product_directory);
if (result == DELETE_SUCCEEDED) {
const base::FilePath vendor_directory(product_directory.DirName());
if (!vendor_directory.empty())
result = DeleteEmptyDir(vendor_directory);
}
}
}
if (result == DELETE_NOT_EMPTY)
result = DELETE_SUCCEEDED;
return result;
}
void DeleteWerRegistryKeys(const installer::InstallerState& installer_state) {
// Delete WER runtime exception helper module dll registry entries
// for currently uninstalled Chrome version and all previous versions if any.
std::unique_ptr<WorkItemList> work_item_list(WorkItem::CreateWorkItemList());
AddOldWerHelperRegistrationCleanupItems(installer_state.root_key(),
installer_state.target_path(),
work_item_list.get());
work_item_list->Do();
}
bool DeleteChromeRegistrationKeys(const InstallerState& installer_state,
HKEY root,
const std::wstring& browser_entry_suffix,
InstallStatus* exit_code) {
DCHECK(exit_code);
const base::FilePath chrome_exe(
installer_state.target_path().Append(kChromeExe));
// Delete {root}\Software\Classes\ChromeHTML.
const std::wstring html_prog_id(install_static::GetBrowserProgIdPrefix() +
browser_entry_suffix);
DeleteProgIdFromSoftwareClasses(root, html_prog_id);
// Delete {root}\Software\Classes\Chrome.
// Append the requested suffix manually here as ShellUtil::GetBrowserModelId
// would try to figure out the currently installed suffix.
const std::wstring chrome_prog_id(install_static::GetBaseAppId() +
browser_entry_suffix);
DeleteProgIdFromSoftwareClasses(root, chrome_prog_id);
// TODO(crbug.com/40384442): Delete ChromePDF ProgId once support for
// PDF docs has landed.
// Delete Software\Classes\CLSID\|toast_activator_clsid|.
const std::wstring toast_activator_reg_path =
InstallUtil::GetToastActivatorRegistryPath();
if (!toast_activator_reg_path.empty()) {
DeleteRegistryKey(root, toast_activator_reg_path, WorkItem::kWow64Default);
} else {
LOG(DFATAL) << "Cannot retrieve the toast activator registry path";
}
if (installer_state.system_install()) {
if (!InstallServiceWorkItem::DeleteService(
install_static::GetElevationServiceName(),
install_static::GetClientStateKeyPath(),
{install_static::GetElevatorClsid()},
{install_static::GetElevatorIid()})) {
LOG(WARNING) << "Failed to delete "
<< install_static::GetElevationServiceName();
}
if (!InstallServiceWorkItem::DeleteService(
install_static::GetTracingServiceName(),
install_static::GetClientStateKeyPath(),
{install_static::GetTracingServiceClsid()},
{install_static::GetTracingServiceIid()})) {
LOG(WARNING) << "Failed to delete "
<< install_static::GetTracingServiceName();
}
// Delete any storage written by the elevated tracing service.
base::FilePath path;
if (base::PathService::Get(base::DIR_SYSTEM_TEMP, &path)) {
path = path.Append(
base::FilePath(elevated_tracing_service::GetStorageDirBasename()));
if (base::DeletePathRecursively(path)) {
VLOG(1) << "Deleted elevated_tracing_service state in " << path;
} else {
PLOG(WARNING) << "Error deleting elevated_tracing_service state in "
<< path;
}
}
}
// Delete all Start Menu Internet registrations that refer to this Chrome.
{
using base::win::RegistryKeyIterator;
ProgramCompare open_command_pred(chrome_exe);
std::wstring client_name;
std::wstring client_key;
std::wstring open_key;
for (RegistryKeyIterator iter(root, ShellUtil::kRegStartMenuInternet);
iter.Valid(); ++iter) {
client_name.assign(iter.Name());
client_key.assign(ShellUtil::kRegStartMenuInternet)
.append(1, L'\\')
.append(client_name);
open_key.assign(client_key).append(ShellUtil::kRegShellOpen);
if (DeleteRegistryKeyIf(
root, client_key, open_key, WorkItem::kWow64Default, nullptr,
open_command_pred) != ConditionalDeleteResult::NOT_FOUND) {
// Delete the default value of SOFTWARE\Clients\StartMenuInternet if it
// references this Chrome (i.e., if it was made the default browser).
DeleteRegistryValueIf(root, ShellUtil::kRegStartMenuInternet,
WorkItem::kWow64Default, nullptr,
ValueEquals(client_name));
// Also delete the value for the default user if we're operating in
// HKLM.
if (root == HKEY_LOCAL_MACHINE) {
DeleteRegistryValueIf(HKEY_USERS,
std::wstring(L".DEFAULT\\")
.append(ShellUtil::kRegStartMenuInternet)
.c_str(),
WorkItem::kWow64Default, nullptr,
ValueEquals(client_name));
}
}
}
}
// Delete Software\RegisteredApplications\Chromium
DeleteRegistryValue(
root, ShellUtil::kRegRegisteredApplications, WorkItem::kWow64Default,
install_static::GetBaseAppName().append(browser_entry_suffix));
// Delete the App Paths and Applications keys that let Explorer find Chrome:
// http://msdn.microsoft.com/en-us/library/windows/desktop/ee872121
std::wstring app_key(ShellUtil::kRegClasses);
app_key.push_back(base::FilePath::kSeparators[0]);
app_key.append(L"Applications");
app_key.push_back(base::FilePath::kSeparators[0]);
app_key.append(installer::kChromeExe);
DeleteRegistryKey(root, app_key, WorkItem::kWow64Default);
std::wstring app_path_key(ShellUtil::kAppPathsRegistryKey);
app_path_key.push_back(base::FilePath::kSeparators[0]);
app_path_key.append(installer::kChromeExe);
DeleteRegistryKey(root, app_path_key, WorkItem::kWow64Default);
// Cleanup OpenWithList and OpenWithProgids:
// http://msdn.microsoft.com/en-us/library/bb166549
std::wstring file_assoc_key;
std::wstring open_with_list_key;
std::wstring open_with_progids_key;
for (int i = 0; ShellUtil::kPotentialFileAssociations[i] != nullptr; ++i) {
file_assoc_key.assign(ShellUtil::kRegClasses);
file_assoc_key.push_back(base::FilePath::kSeparators[0]);
file_assoc_key.append(ShellUtil::kPotentialFileAssociations[i]);
file_assoc_key.push_back(base::FilePath::kSeparators[0]);
open_with_list_key.assign(file_assoc_key);
open_with_list_key.append(L"OpenWithList");
open_with_list_key.push_back(base::FilePath::kSeparators[0]);
open_with_list_key.append(installer::kChromeExe);
DeleteRegistryKey(root, open_with_list_key, WorkItem::kWow64Default);
open_with_progids_key.assign(file_assoc_key);
open_with_progids_key.append(ShellUtil::kRegOpenWithProgids);
DeleteRegistryValue(root, open_with_progids_key, WorkItem::kWow64Default,
html_prog_id);
}
// Cleanup in case Chrome had been made the default browser.
// Delete the default value of SOFTWARE\Clients\StartMenuInternet if it
// references this Chrome. Do this explicitly here for the case where HKCU is
// being processed; the iteration above will have no hits since registration
// lives in HKLM.
DeleteRegistryValueIf(
root, ShellUtil::kRegStartMenuInternet, WorkItem::kWow64Default, nullptr,
ValueEquals(
install_static::GetBaseAppName().append(browser_entry_suffix)));
// Delete each protocol association if it references this Chrome.
ProgramCompare open_command_pred(chrome_exe);
std::wstring parent_key(ShellUtil::kRegClasses);
parent_key.push_back(base::FilePath::kSeparators[0]);
const std::wstring::size_type base_length = parent_key.size();
std::wstring child_key;
for (const wchar_t* const* proto =
&ShellUtil::kPotentialProtocolAssociations[0];
*proto != nullptr; ++proto) {
parent_key.resize(base_length);
parent_key.append(*proto);
child_key.assign(parent_key).append(ShellUtil::kRegShellOpen);
DeleteRegistryKeyIf(root, parent_key, child_key, WorkItem::kWow64Default,
nullptr, open_command_pred);
}
RemoveFiletypeRegistration(installer_state, root, browser_entry_suffix);
*exit_code = installer::UNINSTALL_SUCCESSFUL;
return true;
}
void RemoveChromeLegacyRegistryKeys(const base::FilePath& chrome_exe) {
// We used to register Chrome to handle crx files, but this turned out
// to be not worth the hassle. Remove these old registry entries if
// they exist. See: http://codereview.chromium.org/210007
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
const wchar_t kChromeExtProgId[] = L"ChromeExt";
#else
const wchar_t kChromeExtProgId[] = L"ChromiumExt";
#endif // BUILDFLAG(GOOGLE_CHROME_BRANDING
HKEY roots[] = {HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER};
for (size_t i = 0; i < std::size(roots); ++i) {
std::wstring suffix;
if (roots[i] == HKEY_LOCAL_MACHINE)
suffix = ShellUtil::GetCurrentInstallationSuffix(chrome_exe);
// Delete Software\Classes\ChromeExt,
std::wstring ext_prog_id(ShellUtil::kRegClasses);
ext_prog_id.push_back(base::FilePath::kSeparators[0]);
ext_prog_id.append(kChromeExtProgId);
ext_prog_id.append(suffix);
DeleteRegistryKey(roots[i], ext_prog_id, WorkItem::kWow64Default);
// Delete Software\Classes\.crx,
std::wstring ext_association(ShellUtil::kRegClasses);
ext_association.append(L"\\");
ext_association.append(L".crx");
DeleteRegistryKey(roots[i], ext_association, WorkItem::kWow64Default);
}
}
void UninstallFirewallRules(const base::FilePath& chrome_exe) {
std::unique_ptr<FirewallManager> manager =
FirewallManager::Create(chrome_exe);
if (manager)
manager->RemoveFirewallRules();
}
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
// Run os_update_handler.exe with --uninstall switch, and system-level, if
// install is a system install. Waits for os_update_handler.exe process to exit
// so that the exe file can be deleted. `installer_dir` is the setup.exe
// location and os_update_handler.exe is in its parent dir.
void UninstallOsUpdateHandler(const base::FilePath& installer_dir,
const InstallerState& installer_state) {
const base::FilePath os_update_handler_exe =
installer_dir.DirName().Append(installer::kOsUpdateHandlerExe);
constexpr base::TimeDelta kOsUpdateUninstallTimeout = base::Seconds(5);
base::CommandLine uninstall_cmd(os_update_handler_exe);
uninstall_cmd.AppendSwitch(installer::switches::kUninstall);
if (installer_state.system_install()) {
uninstall_cmd.AppendSwitch(installer::switches::kSystemLevel);
}
const std::wstring cmd_string = uninstall_cmd.GetCommandLineString();
VLOG(1) << "Launching: " << cmd_string;
const base::Process process = base::LaunchProcess(uninstall_cmd, {});
int exit_code = 0;
if (!process.IsValid()) {
PLOG(ERROR) << "Failed to launch (" << cmd_string << ")";
} else if (!process.WaitForExitWithTimeout(kOsUpdateUninstallTimeout,
&exit_code)) {
// The GetExitCodeProcess failed or timed-out.
LOG(ERROR) << "Command (" << cmd_string << ") is taking more than "
<< kOsUpdateUninstallTimeout.InMilliseconds()
<< " milliseconds to complete. Terminating it.";
process.Terminate(0, /*wait=*/true);
} else if (exit_code != 0) {
LOG(ERROR) << "Command (" << cmd_string << ") exited with code "
<< exit_code;
}
}
#endif // BUILDFLAG(GOOGLE_CHROME_BRANDING)
InstallStatus UninstallProduct(const ModifyParams& modify_params,
bool remove_all,
bool force_uninstall,
const base::CommandLine& cmd_line) {
const InstallationState& original_state = *modify_params.installation_state;
const InstallerState& installer_state = *modify_params.installer_state;
const base::FilePath& setup_exe = *modify_params.setup_path;
const ProductState* const product_state =
original_state.GetProductState(installer_state.system_install());
if (product_state) {
VLOG(1) << "version on the system: "
<< product_state->version().GetString();
} else if (!force_uninstall) {
LOG(ERROR) << "Chrome not found for uninstall.";
return installer::CHROME_NOT_INSTALLED;
}
InstallStatus status = installer::UNINSTALL_CONFIRMED;
const base::FilePath chrome_exe(
installer_state.target_path().Append(installer::kChromeExe));
VLOG(1) << "UninstallProduct: Chrome";
if (force_uninstall) {
// Since --force-uninstall command line option is used, we are going to
// do silent uninstall. Try to close all running Chrome instances.
CloseAllChromeProcesses(installer_state.target_path());
} else {
// no --force-uninstall so lets show some UI dialog boxes.
status = IsChromeActiveOrUserCancelled(installer_state);
if (status != installer::UNINSTALL_CONFIRMED &&
status != installer::UNINSTALL_DELETE_PROFILE)
return status;
const std::wstring suffix(
ShellUtil::GetCurrentInstallationSuffix(chrome_exe));
// Check if we need admin rights to cleanup HKLM (the conditions for
// requiring a cleanup are the same as the conditions to do the actual
// cleanup where DeleteChromeRegistrationKeys() is invoked for
// HKEY_LOCAL_MACHINE below). If we do, try to launch another uninstaller
// (silent) in elevated mode to do HKLM cleanup.
// And continue uninstalling in the current process also to do HKCU cleanup.
if (remove_all &&
ShellUtil::QuickIsChromeRegisteredInHKLM(chrome_exe, suffix) &&
!::IsUserAnAdmin() &&
!cmd_line.HasSwitch(installer::switches::kRunAsAdmin)) {
base::CommandLine new_cmd(base::CommandLine::NO_PROGRAM);
new_cmd.AppendArguments(cmd_line, true);
// Append --run-as-admin flag to let the new instance of setup.exe know
// that we already tried to launch ourselves as admin.
new_cmd.AppendSwitch(installer::switches::kRunAsAdmin);
// Append --remove-chrome-registration to remove registry keys only.
new_cmd.AppendSwitch(installer::switches::kRemoveChromeRegistration);
if (!suffix.empty()) {
new_cmd.AppendSwitchNative(
installer::switches::kRegisterChromeBrowserSuffix, suffix);
}
DWORD exit_code = installer::UNKNOWN_STATUS;
InstallUtil::ExecuteExeAsAdmin(new_cmd, &exit_code);
}
}
// Chrome is not in use so lets uninstall Chrome by deleting various files
// and registry entries. Here we will just make best effort and keep going
// in case of errors.
ClearRlzProductState();
auto_launch_util::DisableBackgroundStartAtLogin();
base::FilePath chrome_proxy_exe(
installer_state.target_path().Append(installer::kChromeProxyExe));
// If user-level chrome is self-destructing as a result of encountering a
// system-level chrome, retarget owned non-default shortcuts (app shortcuts,
// profile shortcuts, etc.) to the system-level chrome.
if (cmd_line.HasSwitch(installer::switches::kSelfDestruct) &&
!installer_state.system_install()) {
VLOG(1) << "Retargeting user-generated Chrome shortcuts.";
const base::FilePath system_install_path(
GetInstalledDirectory(/*system_install=*/true));
if (system_install_path.empty()) {
LOG(ERROR) << "Retarget failed: system-level Chrome install directory "
"not found.";
} else {
const base::FilePath system_chrome_path(
system_install_path.Append(installer::kChromeExe));
if (base::PathExists(system_chrome_path)) {
RetargetUserShortcutsWithArgs(installer_state, chrome_exe,
system_chrome_path);
} else {
LOG(ERROR) << "Retarget failed: system-level Chrome not found.";
}
}
// Retarget owned app shortcuts to the system-level chrome_proxy.
const base::FilePath system_chrome_proxy_path(
system_install_path.Append(installer::kChromeProxyExe));
VLOG(1) << "Retargeting user-generated Chrome Proxy shortcuts.";
if (base::PathExists(system_chrome_proxy_path)) {
RetargetUserShortcutsWithArgs(installer_state, chrome_proxy_exe,
system_chrome_proxy_path);
} else {
LOG(ERROR) << "Retarget failed: system-level Chrome Proxy not found.";
}
}
DeleteShortcuts(installer_state, {chrome_exe, std::move(chrome_proxy_exe)});
// Delete the registry keys (Uninstall key and Version key).
HKEY reg_root = installer_state.root_key();
// Note that we must retrieve the distribution-specific data before deleting
// the browser's Clients key.
std::wstring distribution_data(GetDistributionData());
// Remove Control Panel uninstall link.
DeleteRegistryKey(reg_root, install_static::GetUninstallRegistryPath(),
KEY_WOW64_32KEY);
// Remove Omaha product key.
DeleteRegistryKey(reg_root, install_static::GetClientsKeyPath(),
KEY_WOW64_32KEY);
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
UninstallOsUpdateHandler(setup_exe.DirName(), installer_state);
#endif // BUILDFLAG(GOOGLE_CHROME_BRANDING)
// Also try to delete the MSI value in the ClientState key (it might not be
// there). This is due to a Google Update behaviour where an uninstall and a
// rapid reinstall might result in stale values from the old ClientState key
// being picked up on reinstall.
DeleteRegistryValue(installer_state.root_key(),
install_static::GetClientStateKeyPath(), KEY_WOW64_32KEY,
google_update::kRegMSIField);
InstallStatus ret = installer::UNKNOWN_STATUS;
const std::wstring suffix(
ShellUtil::GetCurrentInstallationSuffix(chrome_exe));
// Remove all Chrome registration keys.
// Registration data is put in HKCU for both system level and user level
// installs.
DeleteChromeRegistrationKeys(installer_state, HKEY_CURRENT_USER, suffix,
&ret);
// If the user's Chrome is registered with a suffix: it is possible that old
// unsuffixed registrations were left in HKCU (e.g. if this install was
// previously installed with no suffix in HKCU (old suffix rules if the user
// is not an admin (or declined UAC at first run)) and later had to be
// suffixed when fully registered in HKLM (e.g. when later making Chrome
// default through the UI)).