Delete historical data usage from store when user clears data usage.
[chromium-blink-merge.git] / rlz / win / lib / machine_id_win.cc
blobe5425c9f3506b5b18dbbdeac48098c2d4a71c85e
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 <windows.h>
6 #include <Sddl.h> // For ConvertSidToStringSidW.
7 #include <string>
9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/string16.h"
11 #include "rlz/lib/assert.h"
13 namespace rlz_lib {
15 namespace {
17 bool GetSystemVolumeSerialNumber(int* number) {
18 if (!number)
19 return false;
21 *number = 0;
23 // Find the system root path (e.g: C:\).
24 wchar_t system_path[MAX_PATH + 1];
25 if (!GetSystemDirectoryW(system_path, MAX_PATH))
26 return false;
28 wchar_t* first_slash = wcspbrk(system_path, L"\\/");
29 if (first_slash != NULL)
30 *(first_slash + 1) = 0;
32 DWORD number_local = 0;
33 if (!GetVolumeInformationW(system_path, NULL, 0, &number_local, NULL, NULL,
34 NULL, 0))
35 return false;
37 *number = number_local;
38 return true;
41 bool GetComputerSid(const wchar_t* account_name, SID* sid, DWORD sid_size) {
42 static const DWORD kStartDomainLength = 128; // reasonable to start with
44 scoped_ptr<wchar_t[]> domain_buffer(new wchar_t[kStartDomainLength]);
45 DWORD domain_size = kStartDomainLength;
46 DWORD sid_dword_size = sid_size;
47 SID_NAME_USE sid_name_use;
49 BOOL success = ::LookupAccountNameW(NULL, account_name, sid,
50 &sid_dword_size, domain_buffer.get(),
51 &domain_size, &sid_name_use);
52 if (!success && ::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
53 // We could have gotten the insufficient buffer error because
54 // one or both of sid and szDomain was too small. Check for that
55 // here.
56 if (sid_dword_size > sid_size)
57 return false;
59 if (domain_size > kStartDomainLength)
60 domain_buffer.reset(new wchar_t[domain_size]);
62 success = ::LookupAccountNameW(NULL, account_name, sid, &sid_dword_size,
63 domain_buffer.get(), &domain_size,
64 &sid_name_use);
67 return success != FALSE;
70 std::wstring ConvertSidToString(SID* sid) {
71 std::wstring sid_string;
72 wchar_t* sid_buffer = NULL;
73 if (ConvertSidToStringSidW(sid, &sid_buffer)) {
74 sid_string = sid_buffer;
75 LocalFree(sid_buffer);
77 return sid_string;
80 } // namespace
82 bool GetRawMachineId(base::string16* sid_string, int* volume_id) {
83 // Calculate the Windows SID.
85 wchar_t computer_name[MAX_COMPUTERNAME_LENGTH + 1] = {0};
86 DWORD size = arraysize(computer_name);
88 if (GetComputerNameW(computer_name, &size)) {
89 char sid_buffer[SECURITY_MAX_SID_SIZE];
90 SID* sid = reinterpret_cast<SID*>(sid_buffer);
91 if (GetComputerSid(computer_name, sid, SECURITY_MAX_SID_SIZE)) {
92 *sid_string = ConvertSidToString(sid);
96 // Get the system drive volume serial number.
97 *volume_id = 0;
98 if (!GetSystemVolumeSerialNumber(volume_id)) {
99 ASSERT_STRING("GetMachineId: Failed to retrieve volume serial number");
100 *volume_id = 0;
103 return true;
106 } // namespace rlz_lib