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.
5 #include "rlz/win/lib/rlz_value_store_registry.h"
7 #include "base/win/registry.h"
8 #include "base/stringprintf.h"
9 #include "base/utf_string_conversions.h"
10 #include "rlz/lib/assert.h"
11 #include "rlz/lib/lib_values.h"
12 #include "rlz/lib/rlz_lib.h"
13 #include "rlz/lib/string_utils.h"
14 #include "rlz/win/lib/registry_util.h"
23 // RLZ's are stored as:
24 // <AccessPointName> = <RLZ value> @ kRootKey\kLibKeyName\kRlzsSubkeyName.
26 // Events are stored as:
27 // <AccessPointName><EventName> = 1 @
28 // HKCU\kLibKeyName\kEventsSubkeyName\GetProductName(product).
30 // The OEM Deal Confirmation Code (DCC) is stored as
31 // kDccValueName = <DCC value> @ HKLM\kLibKeyName
33 // The last ping time, per product is stored as:
34 // GetProductName(product) = <last ping time> @
35 // HKCU\kLibKeyName\kPingTimesSubkeyName.
37 // The server does not care about any of these constants.
39 const char kLibKeyName
[] = "Software\\Google\\Common\\Rlz";
40 const wchar_t kGoogleKeyName
[] = L
"Software\\Google";
41 const wchar_t kGoogleCommonKeyName
[] = L
"Software\\Google\\Common";
42 const char kRlzsSubkeyName
[] = "RLZs";
43 const char kEventsSubkeyName
[] = "Events";
44 const char kStatefulEventsSubkeyName
[] = "StatefulEvents";
45 const char kPingTimesSubkeyName
[] = "PTimes";
47 std::wstring
GetWideProductName(Product product
) {
48 return ASCIIToWide(GetProductName(product
));
51 void AppendBrandToString(std::string
* str
) {
52 std::string
brand(SupplementaryBranding::GetBrand());
54 base::StringAppendF(str
, "\\_%s", brand
.c_str());
57 // Function to get the specific registry keys.
58 bool GetRegKey(const char* name
, REGSAM access
, base::win::RegKey
* key
) {
59 std::string key_location
;
60 base::StringAppendF(&key_location
, "%s\\%s", kLibKeyName
, name
);
61 AppendBrandToString(&key_location
);
63 LONG ret
= ERROR_SUCCESS
;
64 if (access
& (KEY_SET_VALUE
| KEY_CREATE_SUB_KEY
| KEY_CREATE_LINK
)) {
65 ret
= key
->Create(HKEY_CURRENT_USER
, ASCIIToWide(key_location
).c_str(),
68 ret
= key
->Open(HKEY_CURRENT_USER
, ASCIIToWide(key_location
).c_str(),
72 return ret
== ERROR_SUCCESS
;
75 bool GetPingTimesRegKey(REGSAM access
, base::win::RegKey
* key
) {
76 return GetRegKey(kPingTimesSubkeyName
, access
, key
);
80 bool GetEventsRegKey(const char* event_type
,
81 const rlz_lib::Product
* product
,
82 REGSAM access
, base::win::RegKey
* key
) {
83 std::string key_location
;
84 base::StringAppendF(&key_location
, "%s\\%s", kLibKeyName
,
86 AppendBrandToString(&key_location
);
88 if (product
!= NULL
) {
89 std::string product_name
= GetProductName(*product
);
90 if (product_name
.empty())
93 base::StringAppendF(&key_location
, "\\%s", product_name
.c_str());
96 LONG ret
= ERROR_SUCCESS
;
97 if (access
& (KEY_SET_VALUE
| KEY_CREATE_SUB_KEY
| KEY_CREATE_LINK
)) {
98 ret
= key
->Create(HKEY_CURRENT_USER
, ASCIIToWide(key_location
).c_str(),
101 ret
= key
->Open(HKEY_CURRENT_USER
, ASCIIToWide(key_location
).c_str(),
105 return ret
== ERROR_SUCCESS
;
108 bool GetAccessPointRlzsRegKey(REGSAM access
, base::win::RegKey
* key
) {
109 return GetRegKey(kRlzsSubkeyName
, access
, key
);
112 bool ClearAllProductEventValues(rlz_lib::Product product
, const char* key
) {
113 std::wstring product_name
= GetWideProductName(product
);
114 if (product_name
.empty())
117 base::win::RegKey reg_key
;
118 GetEventsRegKey(key
, NULL
, KEY_WRITE
, ®_key
);
119 reg_key
.DeleteKey(product_name
.c_str());
121 // Verify that the value no longer exists.
122 base::win::RegKey
product_events(
123 reg_key
.Handle(), product_name
.c_str(), KEY_READ
);
124 if (product_events
.Valid()) {
125 ASSERT_STRING("ClearAllProductEvents: Key deletion failed");
132 // Deletes a registry key if it exists and has no subkeys or values.
133 // TODO: Move this to a registry_utils file and add unittest.
134 bool DeleteKeyIfEmpty(HKEY root_key
, const wchar_t* key_name
) {
136 ASSERT_STRING("DeleteKeyIfEmpty: key_name is NULL");
138 } else { // Scope needed for RegKey
139 base::win::RegKey
key(root_key
, key_name
, KEY_READ
);
141 return true; // Key does not exist - nothing to do.
143 base::win::RegistryKeyIterator
key_iter(root_key
, key_name
);
144 if (key_iter
.SubkeyCount() > 0)
145 return true; // Not empty, so nothing to do
147 base::win::RegistryValueIterator
value_iter(root_key
, key_name
);
148 if (value_iter
.ValueCount() > 0)
149 return true; // Not empty, so nothing to do
152 // The key is empty - delete it now.
153 base::win::RegKey
key(root_key
, L
"", KEY_WRITE
);
154 return key
.DeleteKey(key_name
) == ERROR_SUCCESS
;
160 std::wstring
RlzValueStoreRegistry::GetWideLibKeyName() {
161 return ASCIIToWide(kLibKeyName
);
164 bool RlzValueStoreRegistry::HasAccess(AccessType type
) {
165 return HasUserKeyAccess(type
== kWriteAccess
);
168 bool RlzValueStoreRegistry::WritePingTime(Product product
, int64 time
) {
169 base::win::RegKey key
;
170 std::wstring product_name
= GetWideProductName(product
);
171 return GetPingTimesRegKey(KEY_WRITE
, &key
) &&
172 key
.WriteValue(product_name
.c_str(), &time
, sizeof(time
),
173 REG_QWORD
) == ERROR_SUCCESS
;
176 bool RlzValueStoreRegistry::ReadPingTime(Product product
, int64
* time
) {
177 base::win::RegKey key
;
178 std::wstring product_name
= GetWideProductName(product
);
179 return GetPingTimesRegKey(KEY_READ
, &key
) &&
180 key
.ReadInt64(product_name
.c_str(), time
) == ERROR_SUCCESS
;
183 bool RlzValueStoreRegistry::ClearPingTime(Product product
) {
184 base::win::RegKey key
;
185 GetPingTimesRegKey(KEY_WRITE
, &key
);
187 std::wstring product_name
= GetWideProductName(product
);
188 key
.DeleteValue(product_name
.c_str());
192 DWORD size
= sizeof(value
);
194 product_name
.c_str(), &value
, &size
, NULL
) == ERROR_SUCCESS
) {
195 ASSERT_STRING("RlzValueStoreRegistry::ClearPingTime: Failed to delete.");
202 bool RlzValueStoreRegistry::WriteAccessPointRlz(AccessPoint access_point
,
203 const char* new_rlz
) {
204 const char* access_point_name
= GetAccessPointName(access_point
);
205 if (!access_point_name
)
208 std::wstring
access_point_name_wide(ASCIIToWide(access_point_name
));
209 base::win::RegKey key
;
210 GetAccessPointRlzsRegKey(KEY_WRITE
, &key
);
212 if (!RegKeyWriteValue(key
, access_point_name_wide
.c_str(), new_rlz
)) {
213 ASSERT_STRING("SetAccessPointRlz: Could not write the new RLZ value");
219 bool RlzValueStoreRegistry::ReadAccessPointRlz(AccessPoint access_point
,
222 const char* access_point_name
= GetAccessPointName(access_point
);
223 if (!access_point_name
)
226 size_t size
= rlz_size
;
227 base::win::RegKey key
;
228 GetAccessPointRlzsRegKey(KEY_READ
, &key
);
229 if (!RegKeyReadValue(key
, ASCIIToWide(access_point_name
).c_str(),
232 if (size
> rlz_size
) {
233 ASSERT_STRING("GetAccessPointRlz: Insufficient buffer size");
240 bool RlzValueStoreRegistry::ClearAccessPointRlz(AccessPoint access_point
) {
241 const char* access_point_name
= GetAccessPointName(access_point
);
242 if (!access_point_name
)
245 std::wstring
access_point_name_wide(ASCIIToWide(access_point_name
));
246 base::win::RegKey key
;
247 GetAccessPointRlzsRegKey(KEY_WRITE
, &key
);
249 key
.DeleteValue(access_point_name_wide
.c_str());
253 if (key
.ReadValueDW(access_point_name_wide
.c_str(), &value
) ==
255 ASSERT_STRING("SetAccessPointRlz: Could not clear the RLZ value.");
261 bool RlzValueStoreRegistry::AddProductEvent(Product product
,
262 const char* event_rlz
) {
263 std::wstring
event_rlz_wide(ASCIIToWide(event_rlz
));
264 base::win::RegKey reg_key
;
265 GetEventsRegKey(kEventsSubkeyName
, &product
, KEY_WRITE
, ®_key
);
266 if (reg_key
.WriteValue(event_rlz_wide
.c_str(), 1) != ERROR_SUCCESS
) {
267 ASSERT_STRING("AddProductEvent: Could not write the new event value");
274 bool RlzValueStoreRegistry::ReadProductEvents(Product product
,
275 std::vector
<std::string
>* events
) {
276 // Open the events key.
277 base::win::RegKey events_key
;
278 GetEventsRegKey(kEventsSubkeyName
, &product
, KEY_READ
, &events_key
);
279 if (!events_key
.Valid())
282 // Append the events to the buffer.
284 LONG result
= ERROR_SUCCESS
;
285 for (num_values
= 0; result
== ERROR_SUCCESS
; ++num_values
) {
286 // Max 32767 bytes according to MSDN, but we never use that much.
287 const size_t kMaxValueNameLength
= 2048;
288 char buffer
[kMaxValueNameLength
];
289 DWORD size
= arraysize(buffer
);
291 result
= RegEnumValueA(events_key
.Handle(), num_values
, buffer
, &size
,
292 NULL
, NULL
, NULL
, NULL
);
293 if (result
== ERROR_SUCCESS
)
294 events
->push_back(std::string(buffer
));
297 return result
== ERROR_NO_MORE_ITEMS
;
300 bool RlzValueStoreRegistry::ClearProductEvent(Product product
,
301 const char* event_rlz
) {
302 std::wstring
event_rlz_wide(ASCIIToWide(event_rlz
));
303 base::win::RegKey key
;
304 GetEventsRegKey(kEventsSubkeyName
, &product
, KEY_WRITE
, &key
);
305 key
.DeleteValue(event_rlz_wide
.c_str());
309 if (key
.ReadValueDW(event_rlz_wide
.c_str(), &value
) == ERROR_SUCCESS
) {
310 ASSERT_STRING("ClearProductEvent: Could not delete the event value.");
317 bool RlzValueStoreRegistry::ClearAllProductEvents(Product product
) {
318 return ClearAllProductEventValues(product
, kEventsSubkeyName
);
321 bool RlzValueStoreRegistry::AddStatefulEvent(Product product
,
322 const char* event_rlz
) {
323 base::win::RegKey key
;
324 std::wstring
event_rlz_wide(ASCIIToWide(event_rlz
));
325 if (!GetEventsRegKey(kStatefulEventsSubkeyName
, &product
, KEY_WRITE
, &key
) ||
326 key
.WriteValue(event_rlz_wide
.c_str(), 1) != ERROR_SUCCESS
) {
328 "AddStatefulEvent: Could not write the new stateful event");
335 bool RlzValueStoreRegistry::IsStatefulEvent(Product product
,
336 const char* event_rlz
) {
338 base::win::RegKey key
;
339 GetEventsRegKey(kStatefulEventsSubkeyName
, &product
, KEY_READ
, &key
);
340 std::wstring
event_rlz_wide(ASCIIToWide(event_rlz
));
341 return key
.ReadValueDW(event_rlz_wide
.c_str(), &value
) == ERROR_SUCCESS
;
344 bool RlzValueStoreRegistry::ClearAllStatefulEvents(Product product
) {
345 return ClearAllProductEventValues(product
, kStatefulEventsSubkeyName
);
348 void RlzValueStoreRegistry::CollectGarbage() {
349 // Delete each of the known subkeys if empty.
350 const char* subkeys
[] = {
353 kStatefulEventsSubkeyName
,
357 for (int i
= 0; i
< arraysize(subkeys
); i
++) {
358 std::string subkey_name
;
359 base::StringAppendF(&subkey_name
, "%s\\%s", kLibKeyName
, subkeys
[i
]);
360 AppendBrandToString(&subkey_name
);
362 VERIFY(DeleteKeyIfEmpty(HKEY_CURRENT_USER
,
363 ASCIIToWide(subkey_name
).c_str()));
366 // Delete the library key and its parents too now if empty.
367 VERIFY(DeleteKeyIfEmpty(HKEY_CURRENT_USER
, GetWideLibKeyName().c_str()));
368 VERIFY(DeleteKeyIfEmpty(HKEY_CURRENT_USER
, kGoogleCommonKeyName
));
369 VERIFY(DeleteKeyIfEmpty(HKEY_CURRENT_USER
, kGoogleKeyName
));
372 ScopedRlzValueStoreLock::ScopedRlzValueStoreLock() {
374 store_
.reset(new RlzValueStoreRegistry
);
377 ScopedRlzValueStoreLock::~ScopedRlzValueStoreLock() {
380 RlzValueStore
* ScopedRlzValueStoreLock::GetStore() {
384 } // namespace rlz_lib