This source file includes following definitions.
- GetTestKeyFromEnvironment
- GetTestRegistryOverride
#include "chrome/common/importer/ie_importer_test_registry_overrider_win.h"
#include <windows.h>
#include <string>
#include "base/environment.h"
#include "base/guid.h"
#include "base/memory/scoped_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "base/win/registry.h"
namespace {
const wchar_t kTestHKCUOverrideKeyPrefix[] = L"SOFTWARE\\Chromium Unit Tests\\";
const char kTestHKCUOverrideEnvironmentVariable[] =
"IE_IMPORTER_TEST_OVERRIDE_HKCU";
bool GetTestKeyFromEnvironment(base::string16* key) {
scoped_ptr<base::Environment> env(base::Environment::Create());
std::string value;
bool result = env->GetVar(kTestHKCUOverrideEnvironmentVariable, &value);
if (result)
*key = base::UTF8ToUTF16(value);
return result;
}
}
IEImporterTestRegistryOverrider::IEImporterTestRegistryOverrider()
: temporary_key_(kTestHKCUOverrideKeyPrefix +
base::UTF8ToUTF16(base::GenerateGUID())) {
DCHECK(!GetTestKeyFromEnvironment(NULL));
scoped_ptr<base::Environment> env(base::Environment::Create());
bool success = env->SetVar(kTestHKCUOverrideEnvironmentVariable,
base::UTF16ToUTF8(temporary_key_));
DCHECK(success);
}
IEImporterTestRegistryOverrider::~IEImporterTestRegistryOverrider() {
base::win::RegKey reg_key(HKEY_CURRENT_USER, temporary_key_.c_str(),
KEY_ALL_ACCESS);
DCHECK(reg_key.Valid());
reg_key.DeleteKey(L"");
scoped_ptr<base::Environment> env(base::Environment::Create());
bool success = env->UnSetVar(kTestHKCUOverrideEnvironmentVariable);
DCHECK(success);
}
base::string16 IEImporterTestRegistryOverrider::GetTestRegistryOverride() {
base::string16 key;
if (!GetTestKeyFromEnvironment(&key))
return base::string16();
return key;
}