Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/importer/ie_importer_test_registry_overrider_win.h" | |
| 6 | |
| 7 #include <windows.h> | |
| 8 | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/environment.h" | |
| 12 #include "base/guid.h" | |
| 13 #include "base/memory/scoped_ptr.h" | |
| 14 #include "base/strings/string_number_conversions.h" | |
| 15 #include "base/utf_string_conversions.h" | |
| 16 #include "base/win/registry.h" | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 // The key to which a random subkey will be appended. This key itself will never | |
| 21 // be deleted. | |
| 22 const wchar_t kTestHKCUOverrideKey[] = L"SOFTWARE\\Chromium Unit Tests"; | |
| 23 const char kTestHKCUOverrideEnvironmentVariable[] = | |
| 24 "IE_IMPORTER_TEST_OVERRIDE_HKCU"; | |
| 25 | |
| 26 base::string16 GetTestKeyFromSubKey(const base::string16& subkey) { | |
| 27 base::string16 key(kTestHKCUOverrideKey); | |
| 28 key.push_back(L'\\'); | |
| 29 key.append(subkey); | |
| 30 return key; | |
| 31 } | |
| 32 | |
| 33 bool GetSubKeyFromEnvironment(base::string16* subkey) { | |
| 34 scoped_ptr<base::Environment> env(base::Environment::Create()); | |
| 35 std::string value; | |
| 36 bool result = env->GetVar(kTestHKCUOverrideEnvironmentVariable, &value); | |
| 37 if (result && subkey) | |
| 38 *subkey = UTF8ToUTF16(value); | |
| 39 return result; | |
| 40 } | |
| 41 | |
| 42 } // namespace | |
| 43 | |
| 44 //////////////////////////////////////////////////////////////////////////////// | |
| 45 // IEImporterTestRegistryOverrider, public: | |
| 46 | |
| 47 IEImporterTestRegistryOverrider::IEImporterTestRegistryOverrider() | |
| 48 : override_performed_(false), override_initiated_(false) { | |
| 49 } | |
| 50 | |
| 51 IEImporterTestRegistryOverrider::~IEImporterTestRegistryOverrider() { | |
| 52 base::AutoLock auto_lock(lock_.Get()); | |
| 53 if (override_performed_) { | |
| 54 override_active_in_process_ = false; | |
| 55 | |
| 56 DWORD result = RegOverridePredefKey(HKEY_CURRENT_USER, NULL); | |
| 57 DLOG_IF(ERROR, result != ERROR_SUCCESS) | |
| 58 << "Failed to unset current override"; | |
| 59 | |
| 60 if (override_initiated_) { | |
| 61 base::string16 subkey; | |
| 62 if (!GetSubKeyFromEnvironment(&subkey)) { | |
| 63 NOTREACHED(); | |
| 64 } else { | |
| 65 base::string16 key(GetTestKeyFromSubKey(subkey)); | |
| 66 base::win::RegKey reg_key(HKEY_CURRENT_USER, key.c_str(), | |
| 67 KEY_ALL_ACCESS); | |
| 68 DCHECK(reg_key.Valid()); | |
| 69 reg_key.DeleteKey(L""); | |
| 70 } | |
| 71 | |
| 72 scoped_ptr<base::Environment> env(base::Environment::Create()); | |
| 73 env->UnSetVar(kTestHKCUOverrideEnvironmentVariable); | |
| 74 } | |
| 75 } | |
| 76 } | |
| 77 | |
| 78 bool IEImporterTestRegistryOverrider::SetRegistryOverride() { | |
| 79 base::AutoLock auto_lock(lock_.Get()); | |
| 80 DCHECK(!GetSubKeyFromEnvironment(NULL)); | |
| 81 override_initiated_ = true; | |
| 82 scoped_ptr<base::Environment> env(base::Environment::Create()); | |
| 83 const base::string16 random_subkey(UTF8ToUTF16(base::GenerateGUID())); | |
| 84 return StartRegistryOverride(random_subkey) && | |
| 85 env->SetVar(kTestHKCUOverrideEnvironmentVariable, | |
| 86 UTF16ToUTF8(random_subkey)); | |
| 87 } | |
| 88 | |
| 89 bool IEImporterTestRegistryOverrider::StartRegistryOverrideIfNeeded() { | |
| 90 base::string16 subkey; | |
| 91 if (!GetSubKeyFromEnvironment(&subkey)) | |
|
robertshield
2013/05/03 13:52:37
why is it safe to not have this line protected by
gab
2013/05/03 14:49:10
Done.
| |
| 92 return true; | |
| 93 | |
| 94 base::AutoLock auto_lock(lock_.Get()); | |
| 95 return StartRegistryOverride(subkey); | |
| 96 } | |
| 97 | |
| 98 //////////////////////////////////////////////////////////////////////////////// | |
| 99 // IEImporterTestRegistryOverrider, private: | |
| 100 | |
| 101 bool IEImporterTestRegistryOverrider::StartRegistryOverride( | |
| 102 const base::string16& subkey) { | |
| 103 lock_.Get().AssertAcquired(); | |
| 104 // Override the registry whether |override_active_in_process_| or not as some | |
|
robertshield
2013/05/03 13:52:37
It seems like it would be better to allow only a s
gab
2013/05/03 14:49:10
Not doing this as per F2F discussion.
| |
| 105 // Windows API calls seem to reset the override. However, make sure to undo | |
|
robertshield
2013/05/03 13:52:37
which API calls reset the override? If the overrid
gab
2013/05/03 14:49:10
Seems to be IUrlHistoryStg2::AddUrl (as per the co
| |
| 106 // any existing override as this method will otherwise generate a fake key | |
| 107 // within the current fake key. | |
| 108 DWORD result = RegOverridePredefKey(HKEY_CURRENT_USER, NULL); | |
| 109 DLOG_IF(ERROR, result != ERROR_SUCCESS) << "Failed to unset current override"; | |
| 110 | |
| 111 const base::string16 key(GetTestKeyFromSubKey(subkey)); | |
| 112 base::win::RegKey temp_hkcu_hive_key; | |
| 113 result = temp_hkcu_hive_key.Create(HKEY_CURRENT_USER, key.c_str(), | |
| 114 KEY_ALL_ACCESS); | |
| 115 if (result != ERROR_SUCCESS || !temp_hkcu_hive_key.Valid()) { | |
| 116 DPLOG(ERROR) << result; | |
| 117 return false; | |
| 118 } | |
| 119 | |
| 120 if (!override_active_in_process_) { | |
| 121 // Only take ownership of the override if it wasn't already set in this | |
| 122 // process. | |
| 123 override_performed_ = true; | |
| 124 override_active_in_process_ = true; | |
| 125 } | |
| 126 return RegOverridePredefKey(HKEY_CURRENT_USER, | |
| 127 temp_hkcu_hive_key.Handle()) == ERROR_SUCCESS; | |
| 128 } | |
| 129 | |
| 130 // static | |
| 131 bool IEImporterTestRegistryOverrider::override_active_in_process_ = false; | |
| 132 | |
| 133 // static | |
| 134 base::LazyInstance<base::Lock>::Leaky IEImporterTestRegistryOverrider::lock_ = | |
| 135 LAZY_INSTANCE_INITIALIZER; | |
| OLD | NEW |