- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / installer / util / delete_reg_key_work_item.cc
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
12 using base::win::RegKey;
13
14 DeleteRegKeyWorkItem::~DeleteRegKeyWorkItem() {
15 }
16
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());
24 }
25
26 bool DeleteRegKeyWorkItem::Do() {
27   if (path_.empty())
28     return false;
29
30   RegistryKeyBackup backup;
31
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;
37     }
38   }
39
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_;
46   }
47
48   // We've succeeded, so remember any backup we may have made.
49   backup_.swap(backup);
50
51   return true;
52 }
53
54 void DeleteRegKeyWorkItem::Rollback() {
55   if (ignore_failure_)
56     return;
57
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;
64   }
65
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.";
70 }