summaryrefslogtreecommitdiffstats
path: root/components/metrics/machine_id_provider_win.cc
blob: 41079a7dc2cfc54fba5b669b7eae0757785e84aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2014 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 "components/metrics/machine_id_provider.h"

#include <windows.h>
#include <stdint.h>
#include <winioctl.h>

#include "base/base_paths.h"
#include "base/files/file_path.h"
#include "base/path_service.h"
#include "base/threading/thread_restrictions.h"
#include "base/win/scoped_handle.h"

namespace metrics {

MachineIdProvider::MachineIdProvider() {
}

MachineIdProvider::~MachineIdProvider() {
}

// On windows, the machine id is based on the serial number of the drive Chrome
// is running from.
std::string MachineIdProvider::GetMachineId() {
  base::ThreadRestrictions::AssertIOAllowed();

  // Use the program's path to get the drive used for the machine id. This means
  // that whenever the underlying drive changes, it's considered a new machine.
  // This is fine as we do not support migrating Chrome installs to new drives.
  base::FilePath executable_path;

  if (!PathService::Get(base::FILE_EXE, &executable_path)) {
    NOTREACHED();
    return std::string();
  }

  std::vector<base::FilePath::StringType> path_components;
  executable_path.GetComponents(&path_components);
  if (path_components.empty()) {
    NOTREACHED();
    return std::string();
  }
  base::FilePath::StringType drive_name = L"\\\\.\\" + path_components[0];

  base::win::ScopedHandle drive_handle(
      CreateFile(drive_name.c_str(),
                 0,
                 FILE_SHARE_READ | FILE_SHARE_WRITE,
                 NULL,
                 OPEN_EXISTING,
                 0,
                 NULL));

  STORAGE_PROPERTY_QUERY query = {};
  query.PropertyId = StorageDeviceProperty;
  query.QueryType = PropertyStandardQuery;

  // Perform an initial query to get the number of bytes being returned.
  DWORD bytes_returned;
  STORAGE_DESCRIPTOR_HEADER header = {};
  BOOL status = DeviceIoControl(drive_handle.Get(),
                                IOCTL_STORAGE_QUERY_PROPERTY,
                                &query,
                                sizeof(STORAGE_PROPERTY_QUERY),
                                &header,
                                sizeof(STORAGE_DESCRIPTOR_HEADER),
                                &bytes_returned,
                                NULL);

  if (!status)
    return std::string();

  // Query for the actual serial number.
  std::vector<int8_t> output_buf(header.Size);
  status = DeviceIoControl(drive_handle.Get(),
                           IOCTL_STORAGE_QUERY_PROPERTY,
                           &query,
                           sizeof(STORAGE_PROPERTY_QUERY),
                           &output_buf[0],
                           output_buf.size(),
                           &bytes_returned,
                           NULL);

  if (!status)
    return std::string();

  const STORAGE_DEVICE_DESCRIPTOR* device_descriptor =
      reinterpret_cast<STORAGE_DEVICE_DESCRIPTOR*>(&output_buf[0]);

  // The serial number is stored in the |output_buf| as a null-terminated
  // string starting at the specified offset.
  const DWORD offset = device_descriptor->SerialNumberOffset;
  if (offset >= output_buf.size())
    return std::string();

  // Make sure that the null-terminator exists.
  const std::vector<int8_t>::iterator serial_number_begin =
      output_buf.begin() + offset;
  const std::vector<int8_t>::iterator null_location =
      std::find(serial_number_begin, output_buf.end(), '\0');
  if (null_location == output_buf.end())
    return std::string();

  const char* serial_number =
      reinterpret_cast<const char*>(&output_buf[offset]);

  return std::string(serial_number);
}

// static
MachineIdProvider* MachineIdProvider::CreateInstance() {
  return new MachineIdProvider();
}

}  //  namespace metrics