Fix a crash in the LegacyRenderWidgetHostHWND destruction code path which occurs...
[chromium-blink-merge.git] / base / test / test_reg_util_win.cc
blob3c5753eff88c6c19d173cf3a652ecf8b902d73aa
1 // Copyright (c) 2011 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.
5 #include "base/test/test_reg_util_win.h"
7 #include "base/guid.h"
8 #include "base/logging.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace registry_util {
16 namespace {
18 const wchar_t kTimestampDelimiter[] = L"$";
19 const wchar_t kTempTestKeyPath[] = L"Software\\Chromium\\TempTestKeys";
21 void DeleteStaleTestKeys(const base::Time& now,
22 const base::string16& test_key_root) {
23 base::win::RegKey test_root_key;
24 if (test_root_key.Open(HKEY_CURRENT_USER,
25 test_key_root.c_str(),
26 KEY_ALL_ACCESS) != ERROR_SUCCESS) {
27 // This will occur on first-run, but is harmless.
28 return;
31 base::win::RegistryKeyIterator iterator_test_root_key(HKEY_CURRENT_USER,
32 test_key_root.c_str());
33 for (; iterator_test_root_key.Valid(); ++iterator_test_root_key) {
34 base::string16 key_name = iterator_test_root_key.Name();
35 std::vector<base::string16> tokens;
36 Tokenize(key_name, base::string16(kTimestampDelimiter), &tokens);
37 int64 key_name_as_number = 0;
39 if (!base::StringToInt64(tokens[0], &key_name_as_number)) {
40 test_root_key.DeleteKey(key_name.c_str());
41 continue;
44 base::Time key_time = base::Time::FromInternalValue(key_name_as_number);
45 base::TimeDelta age = now - key_time;
47 if (age > base::TimeDelta::FromHours(24))
48 test_root_key.DeleteKey(key_name.c_str());
52 base::string16 GenerateTempKeyPath(const base::string16& test_key_root,
53 const base::Time& timestamp) {
54 base::string16 key_path = test_key_root;
55 key_path += L"\\" + base::Int64ToString16(timestamp.ToInternalValue());
56 key_path += kTimestampDelimiter + base::ASCIIToWide(base::GenerateGUID());
58 return key_path;
61 } // namespace
63 RegistryOverrideManager::ScopedRegistryKeyOverride::ScopedRegistryKeyOverride(
64 HKEY override,
65 const base::string16& key_path)
66 : override_(override) {
67 EXPECT_EQ(
68 ERROR_SUCCESS,
69 temp_key_.Create(HKEY_CURRENT_USER, key_path.c_str(), KEY_ALL_ACCESS));
70 EXPECT_EQ(ERROR_SUCCESS,
71 ::RegOverridePredefKey(override_, temp_key_.Handle()));
74 RegistryOverrideManager::
75 ScopedRegistryKeyOverride::~ScopedRegistryKeyOverride() {
76 ::RegOverridePredefKey(override_, NULL);
77 temp_key_.DeleteKey(L"");
80 RegistryOverrideManager::RegistryOverrideManager()
81 : timestamp_(base::Time::Now()), test_key_root_(kTempTestKeyPath) {
82 DeleteStaleTestKeys(timestamp_, test_key_root_);
85 RegistryOverrideManager::RegistryOverrideManager(
86 const base::Time& timestamp,
87 const base::string16& test_key_root)
88 : timestamp_(timestamp), test_key_root_(test_key_root) {
89 DeleteStaleTestKeys(timestamp_, test_key_root_);
92 RegistryOverrideManager::~RegistryOverrideManager() {}
94 void RegistryOverrideManager::OverrideRegistry(
95 HKEY override,
96 const base::string16& /*override_name*/) {
97 base::string16 key_path = GenerateTempKeyPath(test_key_root_, timestamp_);
98 overrides_.push_back(new ScopedRegistryKeyOverride(override, key_path));
101 base::string16 GenerateTempKeyPath() {
102 return GenerateTempKeyPath(base::string16(kTempTestKeyPath),
103 base::Time::Now());
106 } // namespace registry_util