| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2011 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/copy_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 CopyRegKeyWorkItem::~CopyRegKeyWorkItem() { |
| 15 } |
| 16 |
| 17 CopyRegKeyWorkItem::CopyRegKeyWorkItem(HKEY predefined_root, |
| 18 const std::wstring& source_key_path, |
| 19 const std::wstring& dest_key_path) |
| 20 : predefined_root_(predefined_root), |
| 21 source_key_path_(source_key_path), |
| 22 dest_key_path_(dest_key_path) { |
| 23 DCHECK(predefined_root); |
| 24 // It's a safe bet that we don't want to copy or overwrite one of the root |
| 25 // trees. |
| 26 DCHECK(!source_key_path.empty()); |
| 27 DCHECK(!dest_key_path.empty()); |
| 28 } |
| 29 |
| 30 bool CopyRegKeyWorkItem::Do() { |
| 31 if (source_key_path_.empty() || dest_key_path_.empty()) |
| 32 return false; |
| 33 |
| 34 RegistryKeyBackup backup; |
| 35 RegKey dest_key; |
| 36 |
| 37 // Only try to make a backup if we're not configured to ignore failures. |
| 38 if (!ignore_failure_) { |
| 39 if (!backup.Initialize(predefined_root_, dest_key_path_.c_str())) { |
| 40 LOG(ERROR) << "Failed to backup destination for registry key copy."; |
| 41 return false; |
| 42 } |
| 43 } |
| 44 |
| 45 // Delete the destination before attempting to copy. |
| 46 LONG result = SHDeleteKey(predefined_root_, dest_key_path_.c_str()); |
| 47 if (result != ERROR_SUCCESS && result != ERROR_FILE_NOT_FOUND) { |
| 48 LOG(ERROR) << "Failed to delete key at " << dest_key_path_ << ", result: " |
| 49 << result; |
| 50 } else { |
| 51 // We've just modified the registry, so remember any backup we may have |
| 52 // made so that Rollback can take us back where we started. |
| 53 backup_.swap(backup); |
| 54 // Make the copy. |
| 55 result = dest_key.Create(predefined_root_, dest_key_path_.c_str(), |
| 56 KEY_WRITE); |
| 57 if (result != ERROR_SUCCESS) { |
| 58 LOG(ERROR) << "Failed to open destination key at " << dest_key_path_ |
| 59 << ", result: " << result; |
| 60 } else { |
| 61 result = SHCopyKey(predefined_root_, source_key_path_.c_str(), |
| 62 dest_key.Handle(), 0); |
| 63 switch (result) { |
| 64 case ERROR_FILE_NOT_FOUND: |
| 65 // The source didn't exist, so neither should the destination. |
| 66 dest_key.Close(); |
| 67 SHDeleteKey(predefined_root_, dest_key_path_.c_str()); |
| 68 // Handle like a success. |
| 69 result = ERROR_SUCCESS; |
| 70 // -- FALL THROUGH TO SUCCESS CASE -- |
| 71 case ERROR_SUCCESS: |
| 72 break; |
| 73 default: |
| 74 LOG(ERROR) << "Failed to copy key from " << source_key_path_ << " to " |
| 75 << dest_key_path_ << ", result: " << result; |
| 76 break; |
| 77 } |
| 78 } |
| 79 } |
| 80 |
| 81 return ignore_failure_ ? true : (result == ERROR_SUCCESS); |
| 82 } |
| 83 |
| 84 void CopyRegKeyWorkItem::Rollback() { |
| 85 if (ignore_failure_) |
| 86 return; |
| 87 |
| 88 // Delete anything in the key before restoring the backup in case someone else |
| 89 // put new data in the key after Do(). |
| 90 LONG result = SHDeleteKey(predefined_root_, dest_key_path_.c_str()); |
| 91 if (result != ERROR_SUCCESS && result != ERROR_FILE_NOT_FOUND) { |
| 92 LOG(ERROR) << "Failed to delete key at " << dest_key_path_ |
| 93 << " in rollback, result: " << result; |
| 94 } |
| 95 |
| 96 // Restore the old contents. The restoration takes on its default security |
| 97 // attributes; any custom attributes are lost. |
| 98 if (!backup_.WriteTo(predefined_root_, dest_key_path_.c_str())) |
| 99 LOG(ERROR) << "Failed to restore key in rollback."; |
| 100 } |
| OLD | NEW |