OLD | NEW |
| (Empty) |
1 // Copyright 2005-2009 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 | |
16 #include "omaha/base/registry_store.h" | |
17 #include <vector> | |
18 #include "omaha/base/debug.h" | |
19 #include "omaha/base/reg_key.h" | |
20 | |
21 namespace omaha { | |
22 | |
23 bool RegistryStore::Open(const TCHAR* key_path) { | |
24 key_path_ = key_path; | |
25 return true; | |
26 } | |
27 | |
28 bool RegistryStore::Close() { | |
29 key_path_.Empty(); | |
30 return true; | |
31 } | |
32 | |
33 bool RegistryStore::Clear() { | |
34 if (RegKey::HasKey(key_path_)) { | |
35 return SUCCEEDED(RegKey::DeleteKey(key_path_, false)); | |
36 } else { | |
37 return true; | |
38 } | |
39 } | |
40 | |
41 bool RegistryStore::Read(const TCHAR* name, std::vector<byte>* data) const { | |
42 ASSERT1(name); | |
43 ASSERT1(data); | |
44 | |
45 byte* sdata = NULL; | |
46 DWORD sdata_size = 0; | |
47 HRESULT hr = RegKey::GetValue(key_path_, name, &sdata, &sdata_size); | |
48 if (FAILED(hr) || !sdata || !sdata_size) | |
49 return false; | |
50 | |
51 data->resize(sdata_size); | |
52 memcpy(&data->front(), sdata, sdata_size); | |
53 | |
54 delete[] sdata; | |
55 | |
56 return true; | |
57 } | |
58 | |
59 bool RegistryStore::Write(const TCHAR* name, byte* data, int data_size) { | |
60 ASSERT1(name); | |
61 ASSERT1(data); | |
62 ASSERT1(data_size); | |
63 | |
64 return SUCCEEDED(RegKey::SetValue(key_path_, name, data, data_size)); | |
65 } | |
66 | |
67 bool RegistryStore::Exists(const TCHAR* name) { | |
68 ASSERT1(name); | |
69 | |
70 return RegKey::HasValue(key_path_, name); | |
71 } | |
72 | |
73 bool RegistryStore::Remove(const TCHAR* name) { | |
74 ASSERT1(name); | |
75 | |
76 return SUCCEEDED(RegKey::DeleteValue(key_path_, name)); | |
77 } | |
78 | |
79 bool RegistryStore::GetValueCount(uint32* value_count) { | |
80 ASSERT1(value_count); | |
81 | |
82 CString key_name(key_path_); | |
83 HKEY h_key = RegKey::GetRootKeyInfo(&key_name); | |
84 | |
85 RegKey reg_key; | |
86 if (FAILED(reg_key.Open(h_key, key_name.GetString(), KEY_READ))) | |
87 return false; | |
88 | |
89 *value_count = reg_key.GetValueCount(); | |
90 | |
91 reg_key.Close(); | |
92 | |
93 return true; | |
94 } | |
95 | |
96 bool RegistryStore::GetValueNameAt(int index, CString* value_name) { | |
97 ASSERT1(index >= 0); | |
98 ASSERT1(value_name); | |
99 | |
100 CString key_name(key_path_); | |
101 HKEY h_key = RegKey::GetRootKeyInfo(&key_name); | |
102 | |
103 RegKey reg_key; | |
104 if (FAILED(reg_key.Open(h_key, key_name.GetString(), KEY_READ))) | |
105 return false; | |
106 | |
107 HRESULT hr = reg_key.GetValueNameAt(index, value_name, NULL); | |
108 | |
109 reg_key.Close(); | |
110 | |
111 return SUCCEEDED(hr); | |
112 } | |
113 | |
114 } // namespace omaha | |
115 | |
OLD | NEW |