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/memory/scoped_ptr.h" | |
| 13 #include "base/win/registry.h" | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 const wchar_t kTestHKCUOverrideKey[] = L"SOFTWARE\\Chromium Unit Tests"; | |
| 18 const wchar_t kTestHKCUOverrideSubKey[] = | |
| 19 L"SOFTWARE\\Chromium Unit Tests\\HKCU Override"; | |
| 20 const char kTestHKCUOverrideEnvironmentVariable[] = | |
| 21 "IE_IMPORTER_TEST_OVERRIDE_HKCU"; | |
| 22 const char kTestHKCUOverrideEnvironmentVariableValue[] = "1"; | |
| 23 | |
| 24 } // namespace | |
| 25 | |
| 26 //////////////////////////////////////////////////////////////////////////////// | |
| 27 // IEImporterTestRegistryOverrider, public: | |
| 28 | |
| 29 IEImporterTestRegistryOverrider::IEImporterTestRegistryOverrider() | |
| 30 : in_process_override_started_by_this_(false), | |
| 31 override_set_by_this_(false) { | |
| 32 } | |
| 33 | |
| 34 IEImporterTestRegistryOverrider::~IEImporterTestRegistryOverrider() { | |
| 35 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
| |
| 36 if (RegOverridePredefKey(HKEY_CURRENT_USER, NULL) != ERROR_SUCCESS) | |
| 37 LOG(ERROR) << "Failed to unset registry override"; | |
| 38 | |
| 39 if (override_set_by_this_) { | |
| 40 base::win::RegKey key(HKEY_CURRENT_USER, kTestHKCUOverrideKey, | |
| 41 KEY_ALL_ACCESS); | |
| 42 key.DeleteKey(L""); | |
| 43 | |
| 44 scoped_ptr<base::Environment> env(base::Environment::Create()); | |
| 45 env->UnSetVar(kTestHKCUOverrideEnvironmentVariable); | |
| 46 } | |
| 47 } | |
| 48 } | |
| 49 | |
| 50 bool IEImporterTestRegistryOverrider::SetRegistryOverride() { | |
| 51 DCHECK(!IsEnvironmentSet()); | |
| 52 override_set_by_this_ = true; | |
| 53 scoped_ptr<base::Environment> env(base::Environment::Create()); | |
| 54 return StartRegistryOverride() && | |
| 55 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
| |
| 56 kTestHKCUOverrideEnvironmentVariableValue); | |
| 57 } | |
| 58 | |
| 59 bool IEImporterTestRegistryOverrider::StartRegistryOverrideIfNeeded() { | |
| 60 if (!IsEnvironmentSet()) | |
| 61 return true; | |
| 62 return StartRegistryOverride(); | |
| 63 } | |
| 64 | |
| 65 //////////////////////////////////////////////////////////////////////////////// | |
| 66 // IEImporterTestRegistryOverrider, private: | |
| 67 | |
| 68 bool IEImporterTestRegistryOverrider::StartRegistryOverride() { | |
| 69 base::AutoLock auto_lock(lock_.Get()); | |
| 70 if (override_active_in_process_) | |
| 71 return true; | |
| 72 | |
| 73 base::win::RegKey temp_hkcu_hive_key; | |
| 74 DWORD result = temp_hkcu_hive_key.Create(HKEY_CURRENT_USER, | |
| 75 kTestHKCUOverrideSubKey, | |
| 76 KEY_ALL_ACCESS); | |
| 77 if (result != ERROR_SUCCESS || !temp_hkcu_hive_key.Valid()) { | |
| 78 DPLOG(ERROR) << result; | |
| 79 return false; | |
| 80 } | |
| 81 | |
| 82 override_active_in_process_ = true; | |
| 83 in_process_override_started_by_this_ = true; | |
| 84 return RegOverridePredefKey(HKEY_CURRENT_USER, | |
| 85 temp_hkcu_hive_key.Handle()) == ERROR_SUCCESS; | |
| 86 } | |
| 87 | |
| 88 bool IEImporterTestRegistryOverrider::IsEnvironmentSet() { | |
| 89 scoped_ptr<base::Environment> env(base::Environment::Create()); | |
| 90 std::string value; | |
| 91 return env->GetVar(kTestHKCUOverrideEnvironmentVariable, &value) && | |
| 92 value == kTestHKCUOverrideEnvironmentVariableValue; | |
| 93 } | |
| 94 | |
| 95 // static | |
| 96 bool IEImporterTestRegistryOverrider::override_active_in_process_ = false; | |
| 97 base::LazyInstance<base::Lock>::Leaky IEImporterTestRegistryOverrider::lock_ = | |
| 98 LAZY_INSTANCE_INITIALIZER; | |
| OLD | NEW |