Index: chrome/browser/importer/ie_importer_test_registry_overrider_win.cc |
diff --git a/chrome/browser/importer/ie_importer_test_registry_overrider_win.cc b/chrome/browser/importer/ie_importer_test_registry_overrider_win.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a419954b5e9297d6c3b3c7bb596ab382566c0e7e |
--- /dev/null |
+++ b/chrome/browser/importer/ie_importer_test_registry_overrider_win.cc |
@@ -0,0 +1,81 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/importer/ie_importer_test_registry_overrider_win.h" |
+ |
+#include <windows.h> |
+#include <processenv.h> |
+ |
+#include "base/string16.h" |
+#include "base/win/registry.h" |
+ |
+namespace { |
+ |
+const char16 kTestHKCUOverrideKey[] = L"SOFTWARE\\Chromium Unit Tests"; |
+const char16 kTestHKCUOverrideSubKey[] = |
+ L"SOFTWARE\\Chromium Unit Tests\\HKCU Override"; |
+const char16 kTestHKCUOverrideEnvironmentVariable[] = |
+ L"IE_IMPORTER_TEST_OVERRIDE_HKCU"; |
+const char16 kTestHKCUOverrideEnvironmentVariableValue[] = |
robertshield
2013/04/26 19:20:53
nit: don't wrap
gab
2013/04/29 02:00:43
Done.
|
+ L"1"; |
+ |
+} // namespace |
+ |
+//////////////////////////////////////////////////////////////////////////////// |
+// IEImporterTestRegistryOverrider, public: |
+ |
+IEImporterTestRegistryOverrider::~IEImporterTestRegistryOverrider() { |
+ if (override_active_) { |
+ if (RegOverridePredefKey(HKEY_CURRENT_USER, NULL) != ERROR_SUCCESS) |
+ LOG(ERROR) << "Failed to unset registry override"; |
+ |
+ if (override_set_by_current_process_) { |
+ base::win::RegKey key(HKEY_CURRENT_USER, kTestHKCUOverrideKey, |
+ KEY_ALL_ACCESS); |
+ key.DeleteKey(L""); |
+ } |
+ } |
+} |
+ |
+bool IEImporterTestRegistryOverrider::SetRegistryOverride() { |
+ override_set_by_current_process_ = true; |
+ return StartRegistryOverride() && |
+ ::SetEnvironmentVariable(kTestHKCUOverrideEnvironmentVariable, |
+ kTestHKCUOverrideEnvironmentVariableValue) != 0; |
robertshield
2013/04/26 19:20:53
use the stuff in base\environment.h here
gab
2013/04/29 02:00:43
Done.
|
+} |
+ |
+bool IEImporterTestRegistryOverrider::StartRegistryOverrideIfNeeded() { |
+ char16 buffer[arraysize(kTestHKCUOverrideEnvironmentVariableValue)]; |
+ DWORD result = ::GetEnvironmentVariable( |
+ kTestHKCUOverrideEnvironmentVariable, buffer, |
+ arraysize(buffer)); |
robertshield
2013/04/26 19:20:53
use the stuff in base\environment.h here
gab
2013/04/29 02:00:43
Done.
|
+ if (result == 1 && |
+ buffer[0] == kTestHKCUOverrideEnvironmentVariableValue[0]) { |
+ // If the variable was set, GetEnvironmentVariable should read it into |
+ // |buffer| and return the size of the string read (without the |
+ // null-terminating character). |
+ return StartRegistryOverride(); |
+ } else { |
+ // Failing to read the value should mean it was not found. |
+ return ::GetLastError() == ERROR_ENVVAR_NOT_FOUND; |
+ } |
+} |
+ |
+//////////////////////////////////////////////////////////////////////////////// |
+// IEImporterTestRegistryOverrider, private: |
+ |
+bool IEImporterTestRegistryOverrider::StartRegistryOverride() { |
+ base::win::RegKey temp_hkcu_hive_key; |
+ DWORD result = temp_hkcu_hive_key.Create(HKEY_CURRENT_USER, |
+ kTestHKCUOverrideSubKey, |
+ KEY_ALL_ACCESS); |
+ if (result != ERROR_SUCCESS || !temp_hkcu_hive_key.Valid()) { |
+ DPLOG(ERROR) << result; |
+ return false; |
+ } |
+ |
+ override_active_ = true; |
+ return RegOverridePredefKey(HKEY_CURRENT_USER, |
+ temp_hkcu_hive_key.Handle()) == ERROR_SUCCESS; |
+} |