OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cloud_print/common/win/cloud_print_utils.h" | 5 #include "cloud_print/common/win/cloud_print_utils.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 | 8 |
| 9 #include "base/win/registry.h" |
| 10 |
9 namespace cloud_print { | 11 namespace cloud_print { |
10 | 12 |
| 13 namespace { |
| 14 |
| 15 // Google Update related constants. |
| 16 const wchar_t kClientStateKey[] = L"SOFTWARE\\Google\\Update\\ClientState\\"; |
| 17 const wchar_t* kUsageKey = L"dr"; |
| 18 |
| 19 } // namespace |
| 20 |
11 HRESULT GetLastHResult() { | 21 HRESULT GetLastHResult() { |
12 DWORD error_code = GetLastError(); | 22 DWORD error_code = GetLastError(); |
13 return error_code ? HRESULT_FROM_WIN32(error_code) : E_FAIL; | 23 return error_code ? HRESULT_FROM_WIN32(error_code) : E_FAIL; |
14 } | 24 } |
15 | 25 |
16 string16 LoadLocalString(DWORD id) { | 26 string16 LoadLocalString(DWORD id) { |
17 static wchar_t dummy = L'\0'; | 27 static wchar_t dummy = L'\0'; |
18 HMODULE module = NULL; | 28 HMODULE module = NULL; |
19 ::GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT | | 29 ::GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT | |
20 GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, &dummy, &module); | 30 GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, &dummy, &module); |
21 LPCWSTR buffer = NULL; | 31 LPCWSTR buffer = NULL; |
22 // If the last parameter is 0, LoadString assume that 3rd parameter type is | 32 // If the last parameter is 0, LoadString assume that 3rd parameter type is |
23 // LPCWSTR* and assign pointer to read-only memory with resource. | 33 // LPCWSTR* and assign pointer to read-only memory with resource. |
24 int count = ::LoadString(module, id, reinterpret_cast<LPWSTR>(&buffer), 0); | 34 int count = ::LoadString(module, id, reinterpret_cast<LPWSTR>(&buffer), 0); |
25 if (!buffer) | 35 if (!buffer) |
26 return string16(); | 36 return string16(); |
27 return string16(buffer, buffer + count); | 37 return string16(buffer, buffer + count); |
28 } | 38 } |
29 | 39 |
30 string16 GetErrorMessage(HRESULT hr) { | 40 string16 GetErrorMessage(HRESULT hr) { |
31 LPWSTR buffer = NULL; | 41 LPWSTR buffer = NULL; |
32 ::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | | 42 ::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | |
33 FORMAT_MESSAGE_ALLOCATE_BUFFER, | 43 FORMAT_MESSAGE_ALLOCATE_BUFFER, |
34 0, hr, 0, reinterpret_cast<LPWSTR>(&buffer), 0, NULL); | 44 0, hr, 0, reinterpret_cast<LPWSTR>(&buffer), 0, NULL); |
35 string16 result(buffer); | 45 string16 result(buffer); |
36 ::LocalFree(buffer); | 46 ::LocalFree(buffer); |
37 return result; | 47 return result; |
38 } | 48 } |
39 | 49 |
| 50 void SetGoogleUpdateUsage(const string16& product_id) { |
| 51 // Set appropriate key to 1 to let Omaha record usage. |
| 52 base::win::RegKey key; |
| 53 if (key.Create(HKEY_CURRENT_USER, |
| 54 (kClientStateKey + product_id).c_str(), |
| 55 KEY_SET_VALUE) != ERROR_SUCCESS || |
| 56 key.WriteValue(kUsageKey, L"1") != ERROR_SUCCESS) { |
| 57 LOG(ERROR) << "Unable to set usage key"; |
| 58 } |
| 59 } |
| 60 |
40 } // namespace cloud_print | 61 } // namespace cloud_print |
OLD | NEW |