15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2011 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CHROME_INSTALLER_UTIL_REGISTRY_KEY_BACKUP_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHROME_INSTALLER_UTIL_REGISTRY_KEY_BACKUP_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <windows.h>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A container for a registry key, its values, and its subkeys.  We don't use
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// more obvious methods for various reasons:
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// - RegCopyTree isn't supported pre-Vista, so we'd have to do something
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   different for XP anyway.
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// - SHCopyKey can't copy subkeys into a volatile destination, so we'd have to
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   worry about polluting the registry.
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// We don't persist security attributes since we only delete keys that we own,
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// and we don't set custom attributes on them anyway.
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class RegistryKeyBackup {
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RegistryKeyBackup();
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~RegistryKeyBackup();
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Recursively reads |key_path| into this instance.  Backing up a non-existent
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // key is valid.  Returns true if the backup was successful; false otherwise,
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // in which case the state of this instance is not modified.
29cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  bool Initialize(HKEY root, const wchar_t* key_path, REGSAM wow64_acccess);
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Writes the contents of this instance into |key|.  The contents of
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |key_path| are not modified If this instance is uninitialized or was
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // initialized from a non-existent key.
34cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  bool WriteTo(HKEY root, const wchar_t* key_path, REGSAM wow64_acccess) const;
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void swap(RegistryKeyBackup& other) {
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    key_data_.swap(other.key_data_);
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class KeyData;
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The values and subkeys of the backed-up key.
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<KeyData> key_data_;
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(RegistryKeyBackup);
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // CHROME_INSTALLER_UTIL_REGISTRY_KEY_BACKUP_H_
50