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..db31381c5c1a0f2da680fb64db9fee7bbcc2994d |
--- /dev/null |
+++ b/chrome/browser/prefs/tracked/device_id_win.cc |
@@ -0,0 +1,58 @@ |
+// 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 <cstddef> |
+#include <cwchar> |
gab
2015/01/20 15:34:12
I don't think you need these two headers.
alito
2015/01/26 16:32:48
<cstddef> was for NULL. But probably also defined
gab
2015/01/26 19:40:46
I've never seen anyone include <cwchar>, from what
alito
2015/01/26 21:01:10
I've removed <cwchar>. It is not normally included
grt (UTC plus 2)
2015/01/27 17:06:22
New code should use nullptr.
gab
2015/01/27 18:27:40
Yes, but in this case it's using Windows APIs whos
grt (UTC plus 2)
2015/01/27 19:48:59
Yup, use nullptr rather than NULL when calling a W
alito
2015/01/27 20:56:55
Done.
|
+ |
+#include <intSafe.h> // For DWORD. |
gab
2015/01/20 15:34:12
DWORD implicitly comes with windows.h AFAIK (I've
alito
2015/01/26 16:32:48
Done.
|
+#include <sddl.h> // For ConvertSidToStringSidW. |
gab
2015/01/20 15:34:12
s/ConvertSidToStringSidW/ConvertSidToStringSidA
alito
2015/01/26 16:32:48
Done.
|
+#include <windows.h> |
gab
2015/01/20 15:34:12
We always put windows.h first (this is more or les
alito
2015/01/26 16:32:48
Done.
|
+ |
+#include "base/macros.h" |
+#include "base/memory/scoped_ptr.h" |
+ |
+bool GetDeterministicMachineSpecificId(std::string* machine_id) { |
gab
2015/01/20 15:34:13
Looks like there is a GetUserSidString() method in
alito
2015/01/26 16:32:48
As we discussed offline, we will wait with this re
|
+ if (!machine_id) |
+ return false; |
gab
2015/01/20 15:34:13
Instead do:
DCHECK(machine_id);
to enforce the con
alito
2015/01/26 16:32:48
Done.
|
+ |
+ wchar_t computer_name[MAX_COMPUTERNAME_LENGTH + 1] = {0}; |
gab
2015/01/20 15:34:12
{0} is the same as {} except that you explicitly s
alito
2015/01/26 16:32:48
Done.
grt (UTC plus 2)
2015/01/27 17:06:22
It's still my preference.
alito
2015/01/27 20:56:55
Acknowledged.
|
+ DWORD computer_name_size = arraysize(computer_name); |
+ |
+ if (!::GetComputerNameW(computer_name, &computer_name_size)) |
+ return false; |
+ |
+ 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. |
+ 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 |
+ // again. |
+ if (::GetLastError() != ERROR_INSUFFICIENT_BUFFER) |
+ return false; |
+ |
+ domain_buffer.reset(new wchar_t[domain_size]); |
+ if (!::LookupAccountNameW(NULL, computer_name, sid, &sid_size, |
+ domain_buffer.get(), &domain_size, |
gab
2015/01/20 15:34:12
|domain_buffer| is optional and we don't use its v
alito
2015/01/26 16:32:48
The documentation does not mention this explicitly
gab
2015/01/26 19:40:45
I see, please add comments to that effect to avoid
alito
2015/01/26 21:01:10
Done.
|
+ &sid_name_use)) |
+ return false; |
gab
2015/01/20 15:34:12
Wrap in {}
(always wrap in {} unless both the con
alito
2015/01/26 16:32:48
Done.
|
+ } |
+ |
gab
2015/01/20 15:34:12
DCHECK_EQ(SID_NAME_USE::SidTypeComputer, sid_name_
alito
2015/01/26 16:32:48
As we discussed offline, this actually fails consi
|
+ char* sid_string = NULL; |
+ if (!::ConvertSidToStringSidA(sid, &sid_string)) |
+ return false; |
+ |
+ *machine_id = sid_string; |
+ ::LocalFree(sid_string); |
+ |
+ return true; |
+} |