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 |
15 // order for multiple tests to be able to use this functionality in parallel. | 13 // SetTestRegistryOverride(), consumers in this process (or in any child |
16 // Also provides functionality (via an environment variable) to let child | 14 // processes created after the key has been generated) can obtain the key via |
17 // processes check whether they should also override. This class is thread-safe. | 15 // GetTestRegistryOverride(). IEImporterTestRegistryOverrider will delete the |
18 // Always unsets any existing override upon being deleted. | 16 // temporary key upon being deleted itself. |
19 class IEImporterTestRegistryOverrider { | 17 class IEImporterTestRegistryOverrider { |
20 public: | 18 public: |
21 IEImporterTestRegistryOverrider(); | 19 IEImporterTestRegistryOverrider(); |
22 | 20 |
23 // Unsets any existing registry override and deletes the temporary registry | 21 // Unsets any existing registry override and deletes the temporary registry |
24 // key if |override_initiated_|. | 22 // key if |override_initiated_|. |
25 ~IEImporterTestRegistryOverrider(); | 23 ~IEImporterTestRegistryOverrider(); |
26 | 24 |
27 // Calls StartRegistryOverride() and sets an environment variable so that | 25 // Sets an environment variable so that future calls to |
28 // future calls to StartRegistryOverrideIfNeeded() from child processes also | 26 // GetTestRegistryOverride() from this process and child processes result in |
29 // result in an overriden HKEY_CURRENT_USER hive. This method should only be | 27 // the test key being returned to the caller. This method should only be |
30 // called once across all processes. | 28 // called once. Returns false on failure to set the environment variable. |
31 // Returns false on unexpected failure. | 29 // NOTE: This method does not create the key itself. |
32 bool SetRegistryOverride(); | 30 bool SetTestRegistryOverride(); |
33 | 31 |
34 // Calls StartRegistryOverride() if the environment was set by a parent | 32 // Returns a test key if one was chosen and set by a call to |
35 // process via SetRegistryOverride(). | 33 // SetTestRegistryOverride(); returns the empty string if none. |
36 // Returns false on unexpected failure. | 34 static base::string16 GetTestRegistryOverride(); |
37 bool StartRegistryOverrideIfNeeded(); | |
38 | 35 |
39 private: | 36 private: |
40 // Overrides HKCU to point to a test key (ending with |subkey|) from this | |
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 | 37 // Whether this object is the one that initiated the override via |
55 // SetRegistryOverride() (and thus will be the one responsible for deleting | 38 // SetTestRegistryOverride() (and thus will be the one responsible for |
56 // the temporary key when done). | 39 // deleting the temporary key when done). |
57 bool override_initiated_; | 40 bool override_initiated_; |
58 | 41 |
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 | |
66 DISALLOW_COPY_AND_ASSIGN(IEImporterTestRegistryOverrider); | 42 DISALLOW_COPY_AND_ASSIGN(IEImporterTestRegistryOverrider); |
67 }; | 43 }; |
68 | 44 |
69 #endif // CHROME_BROWSER_IMPORTER_IE_IMPORTER_TEST_REGISTRY_OVERRIDER_WIN_H_ | 45 #endif // CHROME_BROWSER_IMPORTER_IE_IMPORTER_TEST_REGISTRY_OVERRIDER_WIN_H_ |
OLD | NEW |