delete_reg_key_work_item.cc revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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.
4
5#include "chrome/installer/util/delete_reg_key_work_item.h"
6
7#include <shlwapi.h>
8
9#include "base/logging.h"
10#include "base/win/registry.h"
11#include "chrome/installer/util/install_util.h"
12
13using base::win::RegKey;
14
15DeleteRegKeyWorkItem::~DeleteRegKeyWorkItem() {
16}
17
18DeleteRegKeyWorkItem::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);
30}
31
32bool DeleteRegKeyWorkItem::Do() {
33  if (path_.empty())
34    return false;
35
36  RegistryKeyBackup backup;
37
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;
43    }
44  }
45
46  // Delete the key.
47  if (!InstallUtil::DeleteRegistryKey(
48          predefined_root_, path_.c_str(), wow64_access_)) {
49    return ignore_failure_;
50  }
51
52  // We've succeeded, so remember any backup we may have made.
53  backup_.swap(backup);
54
55  return true;
56}
57
58void DeleteRegKeyWorkItem::Rollback() {
59  if (ignore_failure_)
60    return;
61
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_);
67
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.";
72}
73