Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(496)

Side by Side Diff: components/user_prefs/tracked/device_id_win.cc

Issue 2782803002: Move tracked prefs into services/preferences/tracked. (Closed)
Patch Set: rebase Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "components/user_prefs/tracked/device_id.h"
6
7 #include <windows.h>
8 #include <sddl.h> // For ConvertSidToStringSidA.
9
10 #include <memory>
11
12 #include "base/logging.h"
13 #include "base/macros.h"
14
15 MachineIdStatus GetDeterministicMachineSpecificId(std::string* machine_id) {
16 DCHECK(machine_id);
17
18 wchar_t computer_name[MAX_COMPUTERNAME_LENGTH + 1] = {};
19 DWORD computer_name_size = arraysize(computer_name);
20
21 if (!::GetComputerNameW(computer_name, &computer_name_size))
22 return MachineIdStatus::FAILURE;
23
24 DWORD sid_size = SECURITY_MAX_SID_SIZE;
25 char sid_buffer[SECURITY_MAX_SID_SIZE];
26 SID* sid = reinterpret_cast<SID*>(sid_buffer);
27 DWORD domain_size = 128; // Will expand below if needed.
28 std::unique_ptr<wchar_t[]> domain_buffer(new wchar_t[domain_size]);
29 SID_NAME_USE sid_name_use;
30
31 // Although the fifth argument to |LookupAccountNameW()|,
32 // |ReferencedDomainName|, is annotated as |_Out_opt_|, if a null
33 // value is passed in, zero is returned and |GetLastError()| will
34 // return |ERROR_INSUFFICIENT_BUFFER| (assuming that nothing else went
35 // wrong). In order to ensure that the call to |LookupAccountNameW()|
36 // has succeeded, it is necessary to include the following logic and
37 // obtain the domain name.
38 if (!::LookupAccountNameW(nullptr, computer_name, sid, &sid_size,
39 domain_buffer.get(), &domain_size, &sid_name_use)) {
40 // If the initial size of |domain_buffer| was too small, the
41 // required size is now found in |domain_size|. Resize and try
42 // again.
43 if (::GetLastError() != ERROR_INSUFFICIENT_BUFFER)
44 return MachineIdStatus::FAILURE;
45
46 domain_buffer.reset(new wchar_t[domain_size]);
47 if (!::LookupAccountNameW(nullptr, computer_name, sid, &sid_size,
48 domain_buffer.get(), &domain_size,
49 &sid_name_use)) {
50 return MachineIdStatus::FAILURE;
51 }
52 }
53
54 // Ensure that the correct type of SID was obtained. The
55 // |LookupAccountNameW()| function seems to always return
56 // |SidTypeDomain| instead of |SidTypeComputer| when the computer name
57 // is passed in as its second argument and therefore both enum values
58 // will be considered acceptable. If the computer name and user name
59 // coincide, |LookupAccountNameW()| seems to always return the machine
60 // SID and set the returned enum to |SidTypeDomain|.
61 DCHECK(sid_name_use == SID_NAME_USE::SidTypeComputer ||
62 sid_name_use == SID_NAME_USE::SidTypeDomain);
63
64 char* sid_string = nullptr;
65 if (!::ConvertSidToStringSidA(sid, &sid_string))
66 return MachineIdStatus::FAILURE;
67
68 *machine_id = sid_string;
69 ::LocalFree(sid_string);
70
71 return MachineIdStatus::SUCCESS;
72 }
OLDNEW
« no previous file with comments | « components/user_prefs/tracked/device_id_unittest.cc ('k') | components/user_prefs/tracked/dictionary_hash_store_contents.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698