OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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/installer/mini_installer/regkey.h" | |
6 | |
7 #include "chrome/installer/mini_installer/mini_installer_constants.h" | |
8 #include "chrome/installer/mini_installer/mini_string.h" | |
9 | |
10 namespace mini_installer { | |
11 | |
12 LONG RegKey::Open(HKEY key, const wchar_t* sub_key, REGSAM access) { | |
13 Close(); | |
14 return ::RegOpenKeyEx(key, sub_key, NULL, access, &key_); | |
15 } | |
16 | |
17 LONG RegKey::ReadSZValue(const wchar_t* value_name, | |
18 wchar_t* value, | |
19 size_t value_size) const { | |
20 DWORD type = 0; | |
21 DWORD byte_length = static_cast<DWORD>(value_size * sizeof(wchar_t)); | |
22 LONG result = ::RegQueryValueEx(key_, value_name, NULL, &type, | |
23 reinterpret_cast<BYTE*>(value), | |
24 &byte_length); | |
25 if (result == ERROR_SUCCESS) { | |
26 if (type != REG_SZ) { | |
27 result = ERROR_NOT_SUPPORTED; | |
28 } else if (byte_length == 0) { | |
29 *value = L'\0'; | |
30 } else if (value[byte_length/sizeof(wchar_t) - 1] != L'\0') { | |
31 if ((byte_length / sizeof(wchar_t)) < value_size) | |
32 value[byte_length / sizeof(wchar_t)] = L'\0'; | |
33 else | |
34 result = ERROR_MORE_DATA; | |
35 } | |
36 } | |
37 return result; | |
38 } | |
39 | |
40 LONG RegKey::ReadDWValue(const wchar_t* value_name, DWORD* value) const { | |
41 DWORD type = 0; | |
42 DWORD byte_length = sizeof(*value); | |
43 LONG result = ::RegQueryValueEx(key_, value_name, NULL, &type, | |
44 reinterpret_cast<BYTE*>(value), | |
45 &byte_length); | |
46 if (result == ERROR_SUCCESS) { | |
47 if (type != REG_DWORD) { | |
48 result = ERROR_NOT_SUPPORTED; | |
49 } else if (byte_length != sizeof(*value)) { | |
50 result = ERROR_NO_DATA; | |
51 } | |
52 } | |
53 return result; | |
54 } | |
55 | |
56 LONG RegKey::WriteSZValue(const wchar_t* value_name, const wchar_t* value) { | |
57 return ::RegSetValueEx(key_, value_name, 0, REG_SZ, | |
58 reinterpret_cast<const BYTE*>(value), | |
59 (lstrlen(value) + 1) * sizeof(wchar_t)); | |
60 } | |
61 | |
62 LONG RegKey::WriteDWValue(const wchar_t* value_name, DWORD value) { | |
63 return ::RegSetValueEx(key_, value_name, 0, REG_DWORD, | |
64 reinterpret_cast<const BYTE*>(&value), | |
65 sizeof(value)); | |
66 } | |
67 | |
68 void RegKey::Close() { | |
69 if (key_ != NULL) { | |
70 ::RegCloseKey(key_); | |
71 key_ = NULL; | |
72 } | |
73 } | |
74 | |
75 | |
76 // static | |
77 bool RegKey::ReadSZValue(HKEY root_key, const wchar_t *sub_key, | |
78 const wchar_t *value_name, wchar_t *value, | |
79 size_t size) { | |
80 RegKey key; | |
81 return (key.Open(root_key, sub_key, KEY_QUERY_VALUE) == ERROR_SUCCESS && | |
82 key.ReadSZValue(value_name, value, size) == ERROR_SUCCESS); | |
83 } | |
84 | |
85 // Opens the Google Update ClientState key for a product. This finds only | |
86 // registry entries for Chrome; it does not support the Chromium registry | |
87 // layout. | |
88 bool OpenClientStateKey(HKEY root_key, const wchar_t* app_guid, | |
89 REGSAM access, RegKey* key) { | |
90 StackString<MAX_PATH> client_state_key; | |
91 return client_state_key.assign(kClientStateKeyBase) && | |
92 client_state_key.append(app_guid) && | |
93 (key->Open(root_key, | |
94 client_state_key.get(), | |
95 access | KEY_WOW64_32KEY) == ERROR_SUCCESS); | |
96 } | |
97 | |
98 } // namespace mini_installer | |
OLD | NEW |