Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4131)

Unified Diff: chrome/browser/importer/ie_importer_test_registry_overrider_win.cc

Issue 14143010: Introduce IEImporterTestRegistryOverrider to be able to override the registry for a test and flag t… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rework Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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;

Powered by Google App Engine
This is Rietveld 408576698