Index: chrome/browser/prefs/tracked/device_id_win.cc |
diff --git a/chrome/browser/prefs/tracked/device_id_win.cc b/chrome/browser/prefs/tracked/device_id_win.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..3801f7b80638b35649a8015b1626bef70ff6c8cc |
--- /dev/null |
+++ b/chrome/browser/prefs/tracked/device_id_win.cc |
@@ -0,0 +1,73 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/prefs/tracked/device_id.h" |
+ |
+#include <windows.h> |
+#include <sddl.h> // For ConvertSidToStringSidA. |
+ |
+#include <cwchar> |
+ |
+#include "base/macros.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/logging.h" |
+ |
+namespace tracked_prefs { |
+ |
+MachineIdStatus GetDeterministicMachineSpecificId(std::string* machine_id) { |
+ DCHECK(machine_id); |
+ |
+ wchar_t computer_name[MAX_COMPUTERNAME_LENGTH + 1] = {}; |
+ DWORD computer_name_size = arraysize(computer_name); |
+ |
+ if (!::GetComputerNameW(computer_name, &computer_name_size)) |
+ return FAILURE; |
+ |
+ DWORD sid_size = SECURITY_MAX_SID_SIZE; |
+ char sid_buffer[SECURITY_MAX_SID_SIZE]; |
+ SID* sid = reinterpret_cast<SID*>(sid_buffer); |
+ 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.
|
+ scoped_ptr<wchar_t[]> domain_buffer(new wchar_t[domain_size]); |
+ SID_NAME_USE sid_name_use; |
+ |
+ if (!::LookupAccountNameW(NULL, computer_name, sid, &sid_size, |
+ domain_buffer.get(), &domain_size, |
+ &sid_name_use)) { |
+ // If the initial size of domain_buffer was too small, the |
+ // 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.
|
+ // again. |
+ if (::GetLastError() != ERROR_INSUFFICIENT_BUFFER) |
+ return FAILURE; |
+ |
+ domain_buffer.reset(new wchar_t[domain_size]); |
+ if (!::LookupAccountNameW(NULL, computer_name, sid, &sid_size, |
+ domain_buffer.get(), &domain_size, |
+ &sid_name_use)) { |
+ return FAILURE; |
+ } |
+ } |
+ |
+ // 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.
|
+ // |LookupAccountNameW()| function seems to always return |
+ // |SidTypeDomain| instead of |SidTypeComputer| when the computer name |
+ // 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.
|
+ // either of the two enum values. If the computer name and user name |
+ // coincide, |LookupAccountNameW()| seems to always return the machine |
+ // SID and set the returned enum to |SidTypeDomain|. |
+ 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.
|
+ sid_name_use != SID_NAME_USE::SidTypeDomain) { |
+ return FAILURE; |
+ } |
+ |
+ char* sid_string = NULL; |
+ if (!::ConvertSidToStringSidA(sid, &sid_string)) |
+ return FAILURE; |
+ |
+ *machine_id = sid_string; |
+ ::LocalFree(sid_string); |
+ |
+ return SUCCESS; |
+} |
+ |
+} // namespace tracked_prefs |