OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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. | |
4 | |
5 #include "chrome/browser/prefs/tracked/device_id.h" | |
6 | |
7 #include <windows.h> | |
8 #include <sddl.h> // For ConvertSidToStringSidA. | |
9 | |
10 #include <cwchar> | |
11 | |
12 #include "base/macros.h" | |
13 #include "base/memory/scoped_ptr.h" | |
14 #include "base/logging.h" | |
15 | |
16 namespace tracked_prefs { | |
17 | |
18 MachineIdStatus GetDeterministicMachineSpecificId(std::string* machine_id) { | |
19 DCHECK(machine_id); | |
20 | |
21 wchar_t computer_name[MAX_COMPUTERNAME_LENGTH + 1] = {}; | |
22 DWORD computer_name_size = arraysize(computer_name); | |
23 | |
24 if (!::GetComputerNameW(computer_name, &computer_name_size)) | |
25 return FAILURE; | |
26 | |
27 DWORD sid_size = SECURITY_MAX_SID_SIZE; | |
28 char sid_buffer[SECURITY_MAX_SID_SIZE]; | |
29 SID* sid = reinterpret_cast<SID*>(sid_buffer); | |
30 DWORD domain_size = 128; // Will expand if needed. | |
gab
2015/01/26 19:40:46
s/Will expand if needed/Will expand below if neede
alito
2015/01/26 21:01:11
Done.
| |
31 scoped_ptr<wchar_t[]> domain_buffer(new wchar_t[domain_size]); | |
32 SID_NAME_USE sid_name_use; | |
33 | |
34 if (!::LookupAccountNameW(NULL, computer_name, sid, &sid_size, | |
35 domain_buffer.get(), &domain_size, | |
36 &sid_name_use)) { | |
37 // If the initial size of domain_buffer was too small, the | |
38 // required size is now found in domain_size. Resize and try | |
gab
2015/01/26 19:40:46
s/domain_buffer/|domain_buffer|
same for domain_s
alito
2015/01/26 21:01:11
Done.
| |
39 // again. | |
40 if (::GetLastError() != ERROR_INSUFFICIENT_BUFFER) | |
41 return FAILURE; | |
42 | |
43 domain_buffer.reset(new wchar_t[domain_size]); | |
44 if (!::LookupAccountNameW(NULL, computer_name, sid, &sid_size, | |
45 domain_buffer.get(), &domain_size, | |
46 &sid_name_use)) { | |
47 return FAILURE; | |
48 } | |
49 } | |
50 | |
51 // We want to ensure that the correct type of SID was obtained. The | |
gab
2015/01/26 19:40:46
Pronouns in comments are frowned upon in Chromium.
alito
2015/01/26 21:01:11
Done.
| |
52 // |LookupAccountNameW()| function seems to always return | |
53 // |SidTypeDomain| instead of |SidTypeComputer| when the computer name | |
54 // is passed in as its second argument and therefore we will accept | |
gab
2015/01/26 19:40:46
s/second argument and therefore we will accept eit
alito
2015/01/26 21:01:11
Done.
| |
55 // either of the two enum values. If the computer name and user name | |
56 // coincide, |LookupAccountNameW()| seems to always return the machine | |
57 // SID and set the returned enum to |SidTypeDomain|. | |
58 if (sid_name_use != SID_NAME_USE::SidTypeComputer && | |
gab
2015/01/26 19:40:46
Let's just assume this is correct and not handle f
alito
2015/01/26 21:01:11
Done.
| |
59 sid_name_use != SID_NAME_USE::SidTypeDomain) { | |
60 return FAILURE; | |
61 } | |
62 | |
63 char* sid_string = NULL; | |
64 if (!::ConvertSidToStringSidA(sid, &sid_string)) | |
65 return FAILURE; | |
66 | |
67 *machine_id = sid_string; | |
68 ::LocalFree(sid_string); | |
69 | |
70 return SUCCESS; | |
71 } | |
72 | |
73 } // namespace tracked_prefs | |
OLD | NEW |