• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The Chromium Authors
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/metrics/machine_id_provider.h"
6 
7 #include <windows.h>
8 
9 #include <stdint.h>
10 #include <winioctl.h>
11 
12 #include "base/base_paths.h"
13 #include "base/files/file_path.h"
14 #include "base/notreached.h"
15 #include "base/path_service.h"
16 #include "base/threading/scoped_blocking_call.h"
17 #include "base/win/scoped_handle.h"
18 
19 namespace metrics {
20 
21 // static
HasId()22 bool MachineIdProvider::HasId() {
23   return true;
24 }
25 
26 // On windows, the machine id is based on the serial number of the drive Chrome
27 // is running from.
28 // static
GetMachineId()29 std::string MachineIdProvider::GetMachineId() {
30   base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
31                                                 base::BlockingType::MAY_BLOCK);
32 
33   // Use the program's path to get the drive used for the machine id. This means
34   // that whenever the underlying drive changes, it's considered a new machine.
35   // This is fine as we do not support migrating Chrome installs to new drives.
36   base::FilePath executable_path;
37 
38   CHECK(base::PathService::Get(base::FILE_EXE, &executable_path));
39 
40   std::vector<base::FilePath::StringType> path_components =
41       executable_path.GetComponents();
42   CHECK(!path_components.empty());
43   base::FilePath::StringType drive_name = L"\\\\.\\" + path_components[0];
44 
45   base::win::ScopedHandle drive_handle(
46       CreateFile(drive_name.c_str(), 0, FILE_SHARE_READ | FILE_SHARE_WRITE,
47                  nullptr, OPEN_EXISTING, 0, nullptr));
48 
49   STORAGE_PROPERTY_QUERY query = {};
50   query.PropertyId = StorageDeviceProperty;
51   query.QueryType = PropertyStandardQuery;
52 
53   // Perform an initial query to get the number of bytes being returned.
54   DWORD bytes_returned;
55   STORAGE_DESCRIPTOR_HEADER header = {};
56   BOOL status = DeviceIoControl(
57       drive_handle.Get(), IOCTL_STORAGE_QUERY_PROPERTY, &query,
58       sizeof(STORAGE_PROPERTY_QUERY), &header,
59       sizeof(STORAGE_DESCRIPTOR_HEADER), &bytes_returned, nullptr);
60 
61   if (!status)
62     return std::string();
63 
64   // Query for the actual serial number.
65   std::vector<int8_t> output_buf(header.Size);
66   status =
67       DeviceIoControl(drive_handle.Get(), IOCTL_STORAGE_QUERY_PROPERTY, &query,
68                       sizeof(STORAGE_PROPERTY_QUERY), &output_buf[0],
69                       output_buf.size(), &bytes_returned, nullptr);
70 
71   if (!status)
72     return std::string();
73 
74   const STORAGE_DEVICE_DESCRIPTOR* device_descriptor =
75       reinterpret_cast<STORAGE_DEVICE_DESCRIPTOR*>(&output_buf[0]);
76 
77   // The serial number is stored in the |output_buf| as a null-terminated
78   // string starting at the specified offset.
79   const DWORD offset = device_descriptor->SerialNumberOffset;
80   if (offset >= output_buf.size())
81     return std::string();
82 
83   // Make sure that the null-terminator exists.
84   const std::vector<int8_t>::iterator serial_number_begin =
85       output_buf.begin() + offset;
86   const std::vector<int8_t>::iterator null_location =
87       std::find(serial_number_begin, output_buf.end(), '\0');
88   if (null_location == output_buf.end())
89     return std::string();
90 
91   const char* serial_number =
92       reinterpret_cast<const char*>(&output_buf[offset]);
93 
94   return std::string(serial_number);
95 }
96 }  //  namespace metrics
97