Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / installer / util / delete_reg_key_work_item.cc
blob9f482311037ffe99013136d97d75e2a730913278
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/installer/util/delete_reg_key_work_item.h"
7 #include <shlwapi.h>
9 #include "base/logging.h"
10 #include "base/win/registry.h"
11 #include "chrome/installer/util/install_util.h"
13 using base::win::RegKey;
15 DeleteRegKeyWorkItem::~DeleteRegKeyWorkItem() {
18 DeleteRegKeyWorkItem::DeleteRegKeyWorkItem(HKEY predefined_root,
19 const std::wstring& path,
20 REGSAM wow64_access)
21 : predefined_root_(predefined_root),
22 path_(path),
23 wow64_access_(wow64_access) {
24 DCHECK(predefined_root);
25 // It's a safe bet that we don't want to delete one of the root trees.
26 DCHECK(!path.empty());
27 DCHECK(wow64_access == 0 ||
28 wow64_access == KEY_WOW64_32KEY ||
29 wow64_access == KEY_WOW64_64KEY);
32 bool DeleteRegKeyWorkItem::Do() {
33 if (path_.empty())
34 return false;
36 RegistryKeyBackup backup;
38 // Only try to make a backup if we're not configured to ignore failures.
39 if (!ignore_failure_) {
40 if (!backup.Initialize(predefined_root_, path_.c_str(), wow64_access_)) {
41 LOG(ERROR) << "Failed to backup destination for registry key copy.";
42 return false;
46 // Delete the key.
47 if (!InstallUtil::DeleteRegistryKey(
48 predefined_root_, path_.c_str(), wow64_access_)) {
49 return ignore_failure_;
52 // We've succeeded, so remember any backup we may have made.
53 backup_.swap(backup);
55 return true;
58 void DeleteRegKeyWorkItem::Rollback() {
59 if (ignore_failure_)
60 return;
62 // Delete anything in the key before restoring the backup in case someone else
63 // put new data in the key after Do().
64 InstallUtil::DeleteRegistryKey(predefined_root_,
65 path_.c_str(),
66 wow64_access_);
68 // Restore the old contents. The restoration takes on its default security
69 // attributes; any custom attributes are lost.
70 if (!backup_.WriteTo(predefined_root_, path_.c_str(), wow64_access_))
71 LOG(ERROR) << "Failed to restore key in rollback.";