Split uri_beacons target from bluetooth target.
[chromium-blink-merge.git] / cloud_print / common / win / cloud_print_utils.cc
blobee3096410f636f11218c2c2af11f6f1e80d7b616
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
3 // found in the LICENSE file.
5 #include "cloud_print/common/win/cloud_print_utils.h"
7 #include <windows.h>
9 #include "base/win/registry.h"
11 namespace cloud_print {
13 namespace {
15 // Google Update related constants.
16 const wchar_t kClientStateKey[] = L"SOFTWARE\\Google\\Update\\ClientState\\";
17 const wchar_t* kUsageKey = L"dr";
19 } // namespace
21 HRESULT GetLastHResult() {
22 DWORD error_code = GetLastError();
23 return error_code ? HRESULT_FROM_WIN32(error_code) : E_FAIL;
26 base::string16 LoadLocalString(DWORD id) {
27 static wchar_t dummy = L'\0';
28 HMODULE module = NULL;
29 ::GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT |
30 GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, &dummy, &module);
31 LPCWSTR buffer = NULL;
32 // If the last parameter is 0, LoadString assume that 3rd parameter type is
33 // LPCWSTR* and assign pointer to read-only memory with resource.
34 int count = ::LoadString(module, id, reinterpret_cast<LPWSTR>(&buffer), 0);
35 if (!buffer)
36 return base::string16();
37 return base::string16(buffer, buffer + count);
40 base::string16 GetErrorMessage(HRESULT hr) {
41 LPWSTR buffer = NULL;
42 ::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS |
43 FORMAT_MESSAGE_ALLOCATE_BUFFER,
44 0, hr, 0, reinterpret_cast<LPWSTR>(&buffer), 0, NULL);
45 base::string16 result(buffer);
46 ::LocalFree(buffer);
47 return result;
50 void SetGoogleUpdateUsage(const base::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";
61 } // namespace cloud_print