Upstream version 8.37.180.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / image_writer_private / removable_storage_provider_win.cc
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // devguid requires Windows.h be imported first.
6 #include <windows.h>
7 #include <setupapi.h>
8 #include <winioctl.h>
9
10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/win/scoped_handle.h"
13 #include "chrome/browser/extensions/api/image_writer_private/removable_storage_provider.h"
14
15 namespace extensions {
16
17 namespace {
18
19 bool AddDeviceInfo(HANDLE interface_enumerator,
20                    SP_DEVICE_INTERFACE_DATA* interface_data,
21                    scoped_refptr<StorageDeviceList> device_list) {
22   // Get the required buffer size by calling with a null output buffer.
23   DWORD interface_detail_data_size;
24   BOOL status = SetupDiGetDeviceInterfaceDetail(
25       interface_enumerator,
26       interface_data,
27       NULL,                         // Output buffer.
28       0,                            // Output buffer size.
29       &interface_detail_data_size,  // Receives the buffer size.
30       NULL);                        // Optional DEVINFO_DATA.
31
32   if (status == FALSE && GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
33     PLOG(ERROR) << "SetupDiGetDeviceInterfaceDetail failed";
34     return false;
35   }
36
37
38   scoped_ptr<char[]> interface_detail_data_buffer(
39       new char[interface_detail_data_size]);
40
41   SP_DEVICE_INTERFACE_DETAIL_DATA* interface_detail_data =
42       reinterpret_cast<SP_DEVICE_INTERFACE_DETAIL_DATA*>(
43           interface_detail_data_buffer.get());
44
45   interface_detail_data->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
46
47   status = SetupDiGetDeviceInterfaceDetail(
48       interface_enumerator,
49       interface_data,
50       interface_detail_data, // Output struct.
51       interface_detail_data_size,  // Output struct size.
52       NULL,                        // Receives required size, unneeded.
53       NULL);                       // Optional DEVINFO_Data.
54
55   if (status == FALSE) {
56     PLOG(ERROR) << "SetupDiGetDeviceInterfaceDetail failed";
57     return false;
58   }
59
60   // Open a handle to the device to send DeviceIoControl messages.
61   base::win::ScopedHandle device_handle(CreateFile(
62       interface_detail_data->DevicePath,
63       // Desired access, which is none as we only need metadata.
64       0,
65       // Required to be read + write for devices.
66       FILE_SHARE_READ | FILE_SHARE_WRITE,
67       NULL,           // Optional security attributes.
68       OPEN_EXISTING,  // Devices already exist.
69       0,              // No optional flags.
70       NULL));          // No template file.
71
72   if (!device_handle) {
73     PLOG(ERROR) << "Opening device handle failed.";
74     return false;
75   }
76
77   DISK_GEOMETRY geometry;
78   DWORD bytes_returned;
79   status = DeviceIoControl(
80       device_handle,                 // Device handle.
81       IOCTL_DISK_GET_DRIVE_GEOMETRY, // Flag to request disk size.
82       NULL,                          // Optional additional parameters.
83       0,                             // Optional parameter size.
84       &geometry,                     // output buffer.
85       sizeof(DISK_GEOMETRY),         // output size.
86       &bytes_returned,               // Must be non-null. If overlapped is null,
87                                      // then value is meaningless.
88       NULL);                         // Optional unused overlapped parameter.
89
90   if (status == FALSE) {
91     PLOG(ERROR) << "DeviceIoControl";
92     return false;
93   }
94
95   ULONGLONG disk_capacity = geometry.Cylinders.QuadPart *
96     geometry.TracksPerCylinder *
97     geometry.SectorsPerTrack *
98     geometry.BytesPerSector;
99
100   STORAGE_PROPERTY_QUERY query = STORAGE_PROPERTY_QUERY();
101   query.PropertyId = StorageDeviceProperty;
102   query.QueryType = PropertyStandardQuery;
103
104   scoped_ptr<char[]> output_buf(new char[1024]);
105   status = DeviceIoControl(
106       device_handle,                  // Device handle.
107       IOCTL_STORAGE_QUERY_PROPERTY,   // Flag to request device properties.
108       &query,                         // Query parameters.
109       sizeof(STORAGE_PROPERTY_QUERY), // query parameters size.
110       output_buf.get(),               // output buffer.
111       1024,                           // Size of buffer.
112       &bytes_returned,                // Number of bytes returned.
113                                       // Must not be null.
114       NULL);                          // Optional unused overlapped perameter.
115
116   if (status == FALSE) {
117     PLOG(ERROR) << "Storage property query failed.";
118     return false;
119   }
120
121   STORAGE_DEVICE_DESCRIPTOR* device_descriptor =
122       reinterpret_cast<STORAGE_DEVICE_DESCRIPTOR*>(output_buf.get());
123
124   if (!device_descriptor->RemovableMedia) {
125     // Return true to indicate success but not add anything to the device list.
126     return true;
127   }
128
129   linked_ptr<api::image_writer_private::RemovableStorageDevice> device(
130     new api::image_writer_private::RemovableStorageDevice());
131   device->capacity = disk_capacity;
132
133   base::string16 device_path_16(interface_detail_data->DevicePath);
134   device->storage_unit_id = base::UTF16ToUTF8(device_path_16);
135
136   if (device_descriptor->VendorIdOffset &&
137       output_buf[device_descriptor->VendorIdOffset]) {
138     device->vendor.assign(output_buf.get() + device_descriptor->VendorIdOffset);
139   }
140
141   std::string product_id;
142   if (device_descriptor->ProductIdOffset &&
143       output_buf[device_descriptor->ProductIdOffset]) {
144     device->model.assign(output_buf.get() + device_descriptor->ProductIdOffset);
145   }
146
147   device_list->data.push_back(device);
148
149   return true;
150 }
151
152 }  // namespace
153
154 bool RemovableStorageProvider::PopulateDeviceList(
155     scoped_refptr<StorageDeviceList> device_list) {
156   HDEVINFO interface_enumerator = SetupDiGetClassDevs(
157       &DiskClassGuid,
158       NULL, // Enumerator.
159       NULL, // Parent window.
160       // Only devices present & interface class.
161       (DIGCF_PRESENT | DIGCF_INTERFACEDEVICE));
162
163   if (interface_enumerator == INVALID_HANDLE_VALUE) {
164     DPLOG(ERROR) << "SetupDiGetClassDevs failed.";
165     return false;
166   }
167
168   DWORD index = 0;
169   SP_DEVICE_INTERFACE_DATA interface_data;
170   interface_data.cbSize = sizeof(SP_INTERFACE_DEVICE_DATA);
171
172   while (SetupDiEnumDeviceInterfaces(
173       interface_enumerator,
174       NULL,                    // Device Info data.
175       &GUID_DEVINTERFACE_DISK, // Only disk devices.
176       index,
177       &interface_data)) {
178     AddDeviceInfo(interface_enumerator, &interface_data, device_list);
179     index++;
180   }
181
182   DWORD error_code = GetLastError();
183
184   if (error_code != ERROR_NO_MORE_ITEMS) {
185     PLOG(ERROR) << "SetupDiEnumDeviceInterfaces failed";
186     SetupDiDestroyDeviceInfoList(interface_enumerator);
187     return false;
188   }
189
190   SetupDiDestroyDeviceInfoList(interface_enumerator);
191   return true;
192 }
193
194 } // namespace extensions