Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_IMPORTER_IE_IMPORTER_TEST_REGISTRY_OVERRIDER_WIN_H_ | 5 #ifndef CHROME_BROWSER_IMPORTER_IE_IMPORTER_TEST_REGISTRY_OVERRIDER_WIN_H_ |
| 6 #define CHROME_BROWSER_IMPORTER_IE_IMPORTER_TEST_REGISTRY_OVERRIDER_WIN_H_ | 6 #define CHROME_BROWSER_IMPORTER_IE_IMPORTER_TEST_REGISTRY_OVERRIDER_WIN_H_ |
| 7 | 7 |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/lazy_instance.h" | |
| 10 #include "base/string16.h" | 9 #include "base/string16.h" |
| 11 #include "base/synchronization/lock.h" | |
| 12 | 10 |
| 13 // A helper class to override HKEY_CURRENT_USER to point to a test key from this | 11 // A helper class to let tests generate a random registry key to be used in |
| 14 // process' point of view. Uses a random guid as a subkey for the override in | 12 // HKEY_CURRENT_USER in tests. After the key has been generated via |
|
robertshield
2013/05/22 20:13:56
nit: no via
gab
2013/05/22 20:39:19
Done.
| |
| 15 // order for multiple tests to be able to use this functionality in parallel. | 13 // by constructing an IEImporterTestRegistryOverrider, consumers in this process |
| 16 // Also provides functionality (via an environment variable) to let child | 14 // (or in any child processes created after the key has been generated) can |
| 17 // processes check whether they should also override. This class is thread-safe. | 15 // obtain the key via GetTestRegistryOverride(). IEImporterTestRegistryOverrider |
| 18 // Always unsets any existing override upon being deleted. | 16 // will delete the temporary key upon being deleted itself. |
| 17 // Only one IEImporterTestRegistryOverrider should live at once in a given | |
| 18 // process hiearchy. | |
| 19 class IEImporterTestRegistryOverrider { | 19 class IEImporterTestRegistryOverrider { |
| 20 public: | 20 public: |
| 21 IEImporterTestRegistryOverrider(); | 21 IEImporterTestRegistryOverrider(); |
| 22 | |
| 23 // Unsets any existing registry override and deletes the temporary registry | |
| 24 // key if |override_initiated_|. | |
| 25 ~IEImporterTestRegistryOverrider(); | 22 ~IEImporterTestRegistryOverrider(); |
| 26 | 23 |
| 27 // Calls StartRegistryOverride() and sets an environment variable so that | 24 // Returns a test key if one was chosen and set by a call to |
| 28 // future calls to StartRegistryOverrideIfNeeded() from child processes also | 25 // SetTestRegistryOverride(); returns the empty string if none. |
| 29 // result in an overriden HKEY_CURRENT_USER hive. This method should only be | 26 static base::string16 GetTestRegistryOverride(); |
| 30 // called once across all processes. | |
| 31 // Returns false on unexpected failure. | |
| 32 bool SetRegistryOverride(); | |
| 33 | |
| 34 // Calls StartRegistryOverride() if the environment was set by a parent | |
| 35 // process via SetRegistryOverride(). | |
| 36 // Returns false on unexpected failure. | |
| 37 bool StartRegistryOverrideIfNeeded(); | |
| 38 | 27 |
| 39 private: | 28 private: |
| 40 // Overrides HKCU to point to a test key (ending with |subkey|) from this | 29 base::string16 temporary_key_; |
| 41 // process' point of view. Re-applies the override without taking ownership if | |
| 42 // |override_active_in_process_|. | |
| 43 // Returns false on unexpected failure. | |
| 44 bool StartRegistryOverride(const base::string16& subkey); | |
| 45 | |
| 46 // Reads the environment variable set by SetRegistryOverride into |subkey| if | |
| 47 // it exists. Returns true if the variable was successfully read. | |
| 48 bool GetSubKeyFromEnvironment(base::string16* subkey); | |
| 49 | |
| 50 // Whether this object started an override for the current process (and thus | |
| 51 // will be responsible for unsetting it when done). | |
| 52 bool override_performed_; | |
| 53 | |
| 54 // Whether this object is the one that initiated the override via | |
| 55 // SetRegistryOverride() (and thus will be the one responsible for deleting | |
| 56 // the temporary key when done). | |
| 57 bool override_initiated_; | |
| 58 | |
| 59 // Whether a registry override was initiated in the current process. | |
| 60 static bool override_active_in_process_; | |
| 61 | |
| 62 // This lock should be held before accessing this class' static data and any | |
| 63 // of its objects' members. | |
| 64 static base::LazyInstance<base::Lock>::Leaky lock_; | |
| 65 | 30 |
| 66 DISALLOW_COPY_AND_ASSIGN(IEImporterTestRegistryOverrider); | 31 DISALLOW_COPY_AND_ASSIGN(IEImporterTestRegistryOverrider); |
| 67 }; | 32 }; |
| 68 | 33 |
| 69 #endif // CHROME_BROWSER_IMPORTER_IE_IMPORTER_TEST_REGISTRY_OVERRIDER_WIN_H_ | 34 #endif // CHROME_BROWSER_IMPORTER_IE_IMPORTER_TEST_REGISTRY_OVERRIDER_WIN_H_ |
| OLD | NEW |