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

Side by Side Diff: base/test/test_reg_util_win.cc

Issue 1446363003: Deleted OS_WIN and all Windows specific files from base. (Closed) Base URL: https://github.com/domokit/mojo.git@base_tests
Patch Set: Created 5 years, 1 month 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 unified diff | Download patch
« no previous file with comments | « base/test/test_reg_util_win.h ('k') | base/test/test_reg_util_win_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "base/test/test_reg_util_win.h"
6
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"
13
14 namespace registry_util {
15
16 namespace {
17
18 const wchar_t kTimestampDelimiter[] = L"$";
19 const wchar_t kTempTestKeyPath[] = L"Software\\Chromium\\TempTestKeys";
20
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;
29 }
30
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 if (!Tokenize(key_name, base::string16(kTimestampDelimiter), &tokens))
37 continue;
38 int64 key_name_as_number = 0;
39
40 if (!base::StringToInt64(tokens[0], &key_name_as_number)) {
41 test_root_key.DeleteKey(key_name.c_str());
42 continue;
43 }
44
45 base::Time key_time = base::Time::FromInternalValue(key_name_as_number);
46 base::TimeDelta age = now - key_time;
47
48 if (age > base::TimeDelta::FromHours(24))
49 test_root_key.DeleteKey(key_name.c_str());
50 }
51 }
52
53 base::string16 GenerateTempKeyPath(const base::string16& test_key_root,
54 const base::Time& timestamp) {
55 base::string16 key_path = test_key_root;
56 key_path += L"\\" + base::Int64ToString16(timestamp.ToInternalValue());
57 key_path += kTimestampDelimiter + base::ASCIIToUTF16(base::GenerateGUID());
58
59 return key_path;
60 }
61
62 } // namespace
63
64 RegistryOverrideManager::ScopedRegistryKeyOverride::ScopedRegistryKeyOverride(
65 HKEY override,
66 const base::string16& key_path)
67 : override_(override) {
68 EXPECT_EQ(
69 ERROR_SUCCESS,
70 temp_key_.Create(HKEY_CURRENT_USER, key_path.c_str(), KEY_ALL_ACCESS));
71 EXPECT_EQ(ERROR_SUCCESS,
72 ::RegOverridePredefKey(override_, temp_key_.Handle()));
73 }
74
75 RegistryOverrideManager::
76 ScopedRegistryKeyOverride::~ScopedRegistryKeyOverride() {
77 ::RegOverridePredefKey(override_, NULL);
78 temp_key_.DeleteKey(L"");
79 }
80
81 RegistryOverrideManager::RegistryOverrideManager()
82 : timestamp_(base::Time::Now()), test_key_root_(kTempTestKeyPath) {
83 DeleteStaleTestKeys(timestamp_, test_key_root_);
84 }
85
86 RegistryOverrideManager::RegistryOverrideManager(
87 const base::Time& timestamp,
88 const base::string16& test_key_root)
89 : timestamp_(timestamp), test_key_root_(test_key_root) {
90 DeleteStaleTestKeys(timestamp_, test_key_root_);
91 }
92
93 RegistryOverrideManager::~RegistryOverrideManager() {}
94
95 void RegistryOverrideManager::OverrideRegistry(HKEY override) {
96 base::string16 key_path = GenerateTempKeyPath(test_key_root_, timestamp_);
97 overrides_.push_back(new ScopedRegistryKeyOverride(override, key_path));
98 }
99
100 base::string16 GenerateTempKeyPath() {
101 return GenerateTempKeyPath(base::string16(kTempTestKeyPath),
102 base::Time::Now());
103 }
104
105 } // namespace registry_util
OLDNEW
« no previous file with comments | « base/test/test_reg_util_win.h ('k') | base/test/test_reg_util_win_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698