This source file includes following definitions.
- ReadUsageStatsValue
- GetUsageStatsConsent
- IsUsageStatsAllowed
- SetUsageStatsConsent
#include "remoting/host/usage_stats_consent.h"
#include <windows.h>
#include <string>
#include "base/logging.h"
#include "base/strings/stringprintf.h"
#include "base/win/registry.h"
#include "remoting/host/win/omaha.h"
namespace {
const wchar_t kOmahaClientStateKeyFormat[] =
L"Software\\Google\\Update\\%ls\\%ls";
const wchar_t kOmahaClientState[] = L"ClientState";
const wchar_t kOmahaClientStateMedium[] = L"ClientStateMedium";
const wchar_t kOmahaUsagestatsValue[] = L"usagestats";
LONG ReadUsageStatsValue(const wchar_t* state_key, DWORD* usagestats_out) {
std::wstring client_state = base::StringPrintf(kOmahaClientStateKeyFormat,
state_key,
remoting::kHostOmahaAppid);
base::win::RegKey key;
LONG result = key.Open(HKEY_LOCAL_MACHINE, client_state.c_str(), KEY_READ);
if (result != ERROR_SUCCESS) {
return result;
}
return key.ReadValueDW(kOmahaUsagestatsValue, usagestats_out);
}
}
namespace remoting {
bool GetUsageStatsConsent(bool* allowed, bool* set_by_policy) {
*set_by_policy = false;
DWORD value = 0;
if (ReadUsageStatsValue(kOmahaClientStateMedium, &value) == ERROR_SUCCESS) {
*allowed = value != 0;
return true;
}
if (ReadUsageStatsValue(kOmahaClientState, &value) == ERROR_SUCCESS) {
*allowed = value != 0;
return true;
}
return false;
}
bool IsUsageStatsAllowed() {
bool allowed;
bool set_by_policy;
return GetUsageStatsConsent(&allowed, &set_by_policy) && allowed;
}
bool SetUsageStatsConsent(bool allowed) {
DWORD value = allowed;
std::wstring client_state = base::StringPrintf(kOmahaClientStateKeyFormat,
kOmahaClientStateMedium,
kHostOmahaAppid);
base::win::RegKey key;
LONG result = key.Create(HKEY_LOCAL_MACHINE, client_state.c_str(),
KEY_SET_VALUE);
if (result == ERROR_SUCCESS) {
result = key.WriteValue(kOmahaUsagestatsValue, value);
if (result == ERROR_SUCCESS) {
return true;
}
}
LOG_GETLASTERROR(ERROR)
<< "Failed to record the user's consent to crash dump reporting";
return false;
}
}