Chromium Code Reviews| 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..7a160aeb9a3f14408474369d527c58eb6d3e559a |
| --- /dev/null |
| +++ b/chrome/browser/importer/ie_importer_test_registry_overrider_win.cc |
| @@ -0,0 +1,98 @@ |
| +// 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 <string> |
| + |
| +#include "base/environment.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/win/registry.h" |
| + |
| +namespace { |
| + |
| +const wchar_t kTestHKCUOverrideKey[] = L"SOFTWARE\\Chromium Unit Tests"; |
| +const wchar_t kTestHKCUOverrideSubKey[] = |
| + L"SOFTWARE\\Chromium Unit Tests\\HKCU Override"; |
| +const char kTestHKCUOverrideEnvironmentVariable[] = |
| + "IE_IMPORTER_TEST_OVERRIDE_HKCU"; |
| +const char kTestHKCUOverrideEnvironmentVariableValue[] = "1"; |
| + |
| +} // namespace |
| + |
| +//////////////////////////////////////////////////////////////////////////////// |
| +// IEImporterTestRegistryOverrider, public: |
| + |
| +IEImporterTestRegistryOverrider::IEImporterTestRegistryOverrider() |
| + : in_process_override_started_by_this_(false), |
| + override_set_by_this_(false) { |
| +} |
| + |
| +IEImporterTestRegistryOverrider::~IEImporterTestRegistryOverrider() { |
| + if (in_process_override_started_by_this_) { |
|
robertshield
2013/04/30 03:15:42
shouldn't this set override_active_in_process_ to
gab
2013/04/30 21:44:52
This is done via static initialization at the end
robertshield
2013/04/30 22:04:05
I think that this is the destructor and that after
gab
2013/04/30 22:56:47
Err my bad, I somehow read this comment and saw th
|
| + if (RegOverridePredefKey(HKEY_CURRENT_USER, NULL) != ERROR_SUCCESS) |
| + LOG(ERROR) << "Failed to unset registry override"; |
| + |
| + if (override_set_by_this_) { |
| + base::win::RegKey key(HKEY_CURRENT_USER, kTestHKCUOverrideKey, |
| + KEY_ALL_ACCESS); |
| + key.DeleteKey(L""); |
| + |
| + scoped_ptr<base::Environment> env(base::Environment::Create()); |
| + env->UnSetVar(kTestHKCUOverrideEnvironmentVariable); |
| + } |
| + } |
| +} |
| + |
| +bool IEImporterTestRegistryOverrider::SetRegistryOverride() { |
| + DCHECK(!IsEnvironmentSet()); |
| + override_set_by_this_ = true; |
| + scoped_ptr<base::Environment> env(base::Environment::Create()); |
| + return StartRegistryOverride() && |
| + env->SetVar(kTestHKCUOverrideEnvironmentVariable, |
|
robertshield
2013/04/30 03:15:42
should the setvar / unsetvar calls also be protect
gab
2013/04/30 21:44:52
They are protected by contract (i.e. this method s
robertshield
2013/04/30 22:04:05
I'm not sure that protected-by-contract is a featu
gab
2013/04/30 22:56:47
I disagree, callers shouldn't be confused, they sh
|
| + kTestHKCUOverrideEnvironmentVariableValue); |
| +} |
| + |
| +bool IEImporterTestRegistryOverrider::StartRegistryOverrideIfNeeded() { |
| + if (!IsEnvironmentSet()) |
| + return true; |
| + return StartRegistryOverride(); |
| +} |
| + |
| +//////////////////////////////////////////////////////////////////////////////// |
| +// IEImporterTestRegistryOverrider, private: |
| + |
| +bool IEImporterTestRegistryOverrider::StartRegistryOverride() { |
| + base::AutoLock auto_lock(lock_.Get()); |
| + if (override_active_in_process_) |
| + return true; |
| + |
| + 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_in_process_ = true; |
| + in_process_override_started_by_this_ = true; |
| + return RegOverridePredefKey(HKEY_CURRENT_USER, |
| + temp_hkcu_hive_key.Handle()) == ERROR_SUCCESS; |
| +} |
| + |
| +bool IEImporterTestRegistryOverrider::IsEnvironmentSet() { |
| + scoped_ptr<base::Environment> env(base::Environment::Create()); |
| + std::string value; |
| + return env->GetVar(kTestHKCUOverrideEnvironmentVariable, &value) && |
| + value == kTestHKCUOverrideEnvironmentVariableValue; |
| +} |
| + |
| +// static |
| +bool IEImporterTestRegistryOverrider::override_active_in_process_ = false; |
| +base::LazyInstance<base::Lock>::Leaky IEImporterTestRegistryOverrider::lock_ = |
| + LAZY_INSTANCE_INITIALIZER; |