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"
9 #include "base/logging.h"
10 #include "base/win/registry.h"
12 using base::win::RegKey;
14 DeleteRegKeyWorkItem::~DeleteRegKeyWorkItem() {
17 DeleteRegKeyWorkItem::DeleteRegKeyWorkItem(HKEY predefined_root,
18 const std::wstring& path)
19 : predefined_root_(predefined_root),
21 DCHECK(predefined_root);
22 // It's a safe bet that we don't want to delete one of the root trees.
23 DCHECK(!path.empty());
26 bool DeleteRegKeyWorkItem::Do() {
30 RegistryKeyBackup backup;
32 // Only try to make a backup if we're not configured to ignore failures.
33 if (!ignore_failure_) {
34 if (!backup.Initialize(predefined_root_, path_.c_str())) {
35 LOG(ERROR) << "Failed to backup destination for registry key copy.";
41 LONG result = SHDeleteKey(predefined_root_, path_.c_str());
42 if (result != ERROR_SUCCESS && result != ERROR_FILE_NOT_FOUND) {
43 LOG(ERROR) << "Failed to delete key at " << path_ << ", result: "
45 return ignore_failure_;
48 // We've succeeded, so remember any backup we may have made.
54 void DeleteRegKeyWorkItem::Rollback() {
58 // Delete anything in the key before restoring the backup in case someone else
59 // put new data in the key after Do().
60 LONG result = SHDeleteKey(predefined_root_, path_.c_str());
61 if (result != ERROR_SUCCESS && result != ERROR_FILE_NOT_FOUND) {
62 LOG(ERROR) << "Failed to delete key at " << path_ << " in rollback, "
66 // Restore the old contents. The restoration takes on its default security
67 // attributes; any custom attributes are lost.
68 if (!backup_.WriteTo(predefined_root_, path_.c_str()))
69 LOG(ERROR) << "Failed to restore key in rollback.";