Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / installer / util / delete_reg_key_work_item.cc
blob462698f44bc91ec6b84f446121d60668f291b33b
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"
12 using base::win::RegKey;
14 DeleteRegKeyWorkItem::~DeleteRegKeyWorkItem() {
17 DeleteRegKeyWorkItem::DeleteRegKeyWorkItem(HKEY predefined_root,
18 const std::wstring& path)
19 : predefined_root_(predefined_root),
20 path_(path) {
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() {
27 if (path_.empty())
28 return false;
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.";
36 return false;
40 // Delete the key.
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: "
44 << result;
45 return ignore_failure_;
48 // We've succeeded, so remember any backup we may have made.
49 backup_.swap(backup);
51 return true;
54 void DeleteRegKeyWorkItem::Rollback() {
55 if (ignore_failure_)
56 return;
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, "
63 "result: " << result;
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.";