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.
7 #include "base/files/file_path.h"
8 #include "base/logging.h"
9 #include "base/win/registry.h"
10 #include "chrome/installer/util/create_reg_key_work_item.h"
11 #include "chrome/installer/util/install_util.h"
12 #include "chrome/installer/util/logging_installer.h"
14 using base::win::RegKey
;
18 // TODO: refactor this because it is only used once.
19 void UpOneDirectoryOrEmpty(std::wstring
* dir
) {
20 base::FilePath path
= base::FilePath(*dir
);
21 base::FilePath directory
= path
.DirName();
22 // If there is no separator, we will get back kCurrentDirectory.
23 // In this case, clear dir.
24 if (directory
== path
|| directory
.value() ==
25 base::FilePath::kCurrentDirectory
)
28 *dir
= directory
.value();
33 CreateRegKeyWorkItem::~CreateRegKeyWorkItem() {
36 CreateRegKeyWorkItem::CreateRegKeyWorkItem(HKEY predefined_root
,
37 const std::wstring
& path
,
39 : predefined_root_(predefined_root
),
41 wow64_access_(wow64_access
),
43 DCHECK(wow64_access
== 0 ||
44 wow64_access
== KEY_WOW64_32KEY
||
45 wow64_access
== KEY_WOW64_64KEY
);
48 bool CreateRegKeyWorkItem::Do() {
50 // Nothing needs to be done here.
51 VLOG(1) << "no key to create";
56 std::wstring key_path
;
58 // To create keys, we iterate from back to front.
59 for (size_t i
= key_list_
.size(); i
> 0; i
--) {
61 key_path
.assign(key_list_
[i
- 1]);
63 if (key
.CreateWithDisposition(predefined_root_
,
66 KEY_READ
| wow64_access_
) == ERROR_SUCCESS
) {
67 if (disposition
== REG_OPENED_EXISTING_KEY
) {
69 // This should not happen. Someone created a subkey under the key
71 LOG(ERROR
) << key_path
<< " exists, this is not expected.";
74 // Remove the key path from list if it is already present.
76 } else if (disposition
== REG_CREATED_NEW_KEY
) {
77 VLOG(1) << "created " << key_path
;
80 LOG(ERROR
) << "unkown disposition";
84 LOG(ERROR
) << "Failed to create " << key_path
;
92 void CreateRegKeyWorkItem::Rollback() {
96 std::wstring key_path
;
97 // To delete keys, we iterate from front to back.
98 std::vector
<std::wstring
>::iterator itr
;
99 for (itr
= key_list_
.begin(); itr
!= key_list_
.end(); ++itr
) {
100 key_path
.assign(*itr
);
101 RegKey
key(predefined_root_
, L
"", KEY_WRITE
| wow64_access_
);
102 if (key
.DeleteEmptyKey(key_path
.c_str()) == ERROR_SUCCESS
) {
103 VLOG(1) << "rollback: delete " << key_path
;
105 VLOG(1) << "rollback: can not delete " << key_path
;
106 // The key might have been deleted, but we don't reliably know what
107 // error code(s) are returned in this case. So we just keep tring delete
112 key_created_
= false;
117 bool CreateRegKeyWorkItem::InitKeyList() {
121 std::wstring
key_path(path_
);
124 key_list_
.push_back(key_path
);
125 // This is pure string operation so it does not matter whether the
126 // path is file path or registry path.
127 UpOneDirectoryOrEmpty(&key_path
);
128 } while (!key_path
.empty());