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

Unified Diff: chrome/browser/prefs/tracked/device_id_win.cc

Issue 836703005: Add a new function for getting a deterministic machine ID (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkgr
Patch Set: Added link to bug report in comment and fixed include order Created 5 years, 11 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/browser/prefs/tracked/device_id_unittest.cc ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..05e70b07bd809f97e75659d8f7b378b6cd443342
--- /dev/null
+++ b/chrome/browser/prefs/tracked/device_id_win.cc
@@ -0,0 +1,71 @@
+// 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 "base/logging.h"
+#include "base/macros.h"
+#include "base/memory/scoped_ptr.h"
+
+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 MachineIdStatus::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 below if needed.
+ scoped_ptr<wchar_t[]> domain_buffer(new wchar_t[domain_size]);
+ SID_NAME_USE sid_name_use;
+
+ // Although the fifth argument to |LookupAccountNameW()|,
+ // |ReferencedDomainName|, is annotated as |_Out_opt_|, if a null
+ // value is passed in, zero is returned and |GetLastError()| will
+ // return |ERROR_INSUFFICIENT_BUFFER| (assuming that nothing else went
+ // wrong). In order to ensure that the call to |LookupAccountNameW()|
+ // has succeeded, it is necessary to include the following logic and
+ // obtain the domain name.
+ if (!::LookupAccountNameW(nullptr, 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 MachineIdStatus::FAILURE;
+
+ domain_buffer.reset(new wchar_t[domain_size]);
+ if (!::LookupAccountNameW(nullptr, computer_name, sid, &sid_size,
+ domain_buffer.get(), &domain_size,
+ &sid_name_use)) {
+ return MachineIdStatus::FAILURE;
+ }
+ }
+
+ // Ensure that the correct type of SID was obtained. The
+ // |LookupAccountNameW()| function seems to always return
+ // |SidTypeDomain| instead of |SidTypeComputer| when the computer name
+ // is passed in as its second argument and therefore both enum values
+ // will be considered acceptable. If the computer name and user name
+ // coincide, |LookupAccountNameW()| seems to always return the machine
+ // SID and set the returned enum to |SidTypeDomain|.
+ DCHECK(sid_name_use == SID_NAME_USE::SidTypeComputer ||
+ sid_name_use == SID_NAME_USE::SidTypeDomain);
+
+ char* sid_string = nullptr;
+ if (!::ConvertSidToStringSidA(sid, &sid_string))
+ return MachineIdStatus::FAILURE;
+
+ *machine_id = sid_string;
+ ::LocalFree(sid_string);
+
+ return MachineIdStatus::SUCCESS;
+}
« no previous file with comments | « chrome/browser/prefs/tracked/device_id_unittest.cc ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698