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

Side by Side Diff: media/midi/midi_manager_winrt.cc

Issue 2309253002: Retrieve driver information in WinRT WebMIDI backend (Closed)
Patch Set: rebase Created 4 years, 3 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 unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "media/midi/midi_manager_winrt.h" 5 #include "media/midi/midi_manager_winrt.h"
6 6
7 // TODO(shaochuan): Remove this once clang supports uuid syntax in <robuffer.h>. 7 // TODO(shaochuan): Remove this once clang supports uuid syntax in <robuffer.h>.
8 // https://reviews.llvm.org/D23895 8 // https://reviews.llvm.org/D23895
9 namespace Windows { 9 namespace Windows {
10 namespace Storage { 10 namespace Storage {
11 namespace Streams { 11 namespace Streams {
12 #pragma warning(disable : 4467) 12 #pragma warning(disable : 4467)
13 struct __declspec(uuid("905a0fef-bc53-11df-8c49-001e4fc686da")) 13 struct __declspec(uuid("905a0fef-bc53-11df-8c49-001e4fc686da"))
14 IBufferByteAccess; 14 IBufferByteAccess;
15 } 15 }
16 } 16 }
17 } 17 }
18 18
19 #include <comdef.h> 19 #include <comdef.h>
20 #include <robuffer.h> 20 #include <robuffer.h>
21 #include <setupapi.h>
21 #include <windows.devices.enumeration.h> 22 #include <windows.devices.enumeration.h>
22 #include <windows.devices.midi.h> 23 #include <windows.devices.midi.h>
23 #include <wrl/event.h> 24 #include <wrl/event.h>
24 25
25 #include <iomanip> 26 #include <iomanip>
26 #include <unordered_map> 27 #include <unordered_map>
27 #include <unordered_set> 28 #include <unordered_set>
28 29
29 #include "base/bind.h" 30 #include "base/bind.h"
30 #include "base/lazy_instance.h" 31 #include "base/lazy_instance.h"
31 #include "base/scoped_generic.h" 32 #include "base/scoped_generic.h"
33 #include "base/strings/string_util.h"
32 #include "base/strings/utf_string_conversions.h" 34 #include "base/strings/utf_string_conversions.h"
33 #include "base/threading/thread_checker.h" 35 #include "base/threading/thread_checker.h"
34 #include "base/threading/thread_task_runner_handle.h" 36 #include "base/threading/thread_task_runner_handle.h"
35 #include "base/timer/timer.h" 37 #include "base/timer/timer.h"
36 #include "base/win/scoped_comptr.h" 38 #include "base/win/scoped_comptr.h"
37 #include "media/midi/midi_scheduler.h" 39 #include "media/midi/midi_scheduler.h"
38 40
39 namespace media { 41 namespace media {
40 namespace midi { 42 namespace midi {
41 namespace { 43 namespace {
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 bool IsMicrosoftSynthesizer(IDeviceInformation* info) { 256 bool IsMicrosoftSynthesizer(IDeviceInformation* info) {
255 auto midi_synthesizer_statics = 257 auto midi_synthesizer_statics =
256 WrlStaticsFactory<IMidiSynthesizerStatics, 258 WrlStaticsFactory<IMidiSynthesizerStatics,
257 RuntimeClass_Windows_Devices_Midi_MidiSynthesizer>(); 259 RuntimeClass_Windows_Devices_Midi_MidiSynthesizer>();
258 boolean result = FALSE; 260 boolean result = FALSE;
259 HRESULT hr = midi_synthesizer_statics->IsSynthesizer(info, &result); 261 HRESULT hr = midi_synthesizer_statics->IsSynthesizer(info, &result);
260 VLOG_IF(1, FAILED(hr)) << "IsSynthesizer failed: " << PrintHr(hr); 262 VLOG_IF(1, FAILED(hr)) << "IsSynthesizer failed: " << PrintHr(hr);
261 return result != FALSE; 263 return result != FALSE;
262 } 264 }
263 265
266 class ScopedDeviceInfoListTraits {
267 public:
268 static HDEVINFO InvalidValue() { return INVALID_HANDLE_VALUE; }
269
270 static void Free(HDEVINFO devinfo_set) {
271 SetupDiDestroyDeviceInfoList(devinfo_set);
272 }
273 };
274
275 using ScopedDeviceInfoList =
276 base::ScopedGeneric<HDEVINFO, ScopedDeviceInfoListTraits>;
277
278 void GetDriverInfoFromDeviceId(const std::string& dev_id,
Takashi Toyoshima 2016/09/06 05:45:57 Can you leave a comment for this function, that sh
Shao-Chuan Lee 2016/09/06 06:57:16 Done.
279 std::string* out_manufacturer,
280 std::string* out_driver_version) {
281 // Extract device instance ID from device (interface) ID provided by WinRT
282 // APIs.
283 base::string16 dev_instance_id =
284 base::UTF8ToWide(dev_id.substr(4, dev_id.size() - 43));
285 base::ReplaceChars(dev_instance_id, L"#", L"\\", &dev_instance_id);
286
287 ScopedDeviceInfoList devinfo_list(
288 SetupDiCreateDeviceInfoList(nullptr, nullptr));
289 if (!devinfo_list.is_valid()) {
290 VLOG(1) << "SetupDiCreateDeviceInfoList failed: "
291 << PrintHr(HRESULT_FROM_WIN32(GetLastError()));
292 return;
293 }
294
295 SP_DEVINFO_DATA devinfo_data = {0};
296 devinfo_data.cbSize = sizeof(SP_DEVINFO_DATA);
297 SP_DRVINFO_DATA drvinfo_data = {0};
298 drvinfo_data.cbSize = sizeof(SP_DRVINFO_DATA);
299
300 if (!SetupDiOpenDeviceInfo(devinfo_list.get(), dev_instance_id.c_str(),
301 nullptr, 0, &devinfo_data)) {
302 VLOG(1) << "SetupDiOpenDeviceInfo failed: "
303 << PrintHr(HRESULT_FROM_WIN32(GetLastError()));
304 return;
305 }
306
307 if (!SetupDiBuildDriverInfoList(devinfo_list.get(), &devinfo_data,
308 SPDIT_COMPATDRIVER)) {
309 VLOG(1) << "SetupDiBuildDriverInfoList failed: "
310 << PrintHr(HRESULT_FROM_WIN32(GetLastError()));
311 return;
312 }
313
314 // Assume only one entry in driver info list.
315 if (SetupDiEnumDriverInfo(devinfo_list.get(), &devinfo_data,
316 SPDIT_COMPATDRIVER, 0, &drvinfo_data)) {
317 *out_manufacturer = base::WideToUTF8(drvinfo_data.ProviderName);
318
319 std::stringstream ss;
320 ss << (drvinfo_data.DriverVersion >> 48) << "."
321 << (drvinfo_data.DriverVersion >> 32 & 0xffff) << "."
322 << (drvinfo_data.DriverVersion >> 16 & 0xffff) << "."
323 << (drvinfo_data.DriverVersion & 0xffff);
324 *out_driver_version = ss.str();
325 }
326
327 if (!SetupDiDestroyDriverInfoList(devinfo_list.get(), &devinfo_data,
328 SPDIT_COMPATDRIVER))
329 VLOG(1) << "SetupDiDestroyDriverInfoList failed: "
330 << PrintHr(HRESULT_FROM_WIN32(GetLastError()));
331 }
332
264 // Tokens with value = 0 are considered invalid (as in <wrl/event.h>). 333 // Tokens with value = 0 are considered invalid (as in <wrl/event.h>).
265 const int64_t kInvalidTokenValue = 0; 334 const int64_t kInvalidTokenValue = 0;
266 335
267 template <typename InterfaceType> 336 template <typename InterfaceType>
268 struct MidiPort { 337 struct MidiPort {
269 MidiPort() = default; 338 MidiPort() = default;
270 339
271 uint32_t index; 340 uint32_t index;
272 ScopedComPtr<InterfaceType> handle; 341 ScopedComPtr<InterfaceType> handle;
273 EventRegistrationToken token_MessageReceived; 342 EventRegistrationToken token_MessageReceived;
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after
587 656
588 EventRegistrationToken token = {kInvalidTokenValue}; 657 EventRegistrationToken token = {kInvalidTokenValue};
589 if (!RegisterOnMessageReceived(handle, &token)) 658 if (!RegisterOnMessageReceived(handle, &token))
590 return; 659 return;
591 660
592 std::string dev_id = GetDeviceIdString(handle); 661 std::string dev_id = GetDeviceIdString(handle);
593 662
594 MidiPort<InterfaceType>* port = GetPortByDeviceId(dev_id); 663 MidiPort<InterfaceType>* port = GetPortByDeviceId(dev_id);
595 664
596 if (port == nullptr) { 665 if (port == nullptr) {
597 // TODO(crbug.com/642604): Fill in manufacturer and driver version. 666 std::string manufacturer = "Unknown", driver_version = "Unknown";
598 AddPort(MidiPortInfo(dev_id, std::string("Manufacturer"), 667 GetDriverInfoFromDeviceId(dev_id, &manufacturer, &driver_version);
599 port_names_[dev_id], std::string("DriverVersion"), 668
600 MIDI_PORT_OPENED)); 669 AddPort(MidiPortInfo(dev_id, manufacturer, port_names_[dev_id],
670 driver_version, MIDI_PORT_OPENED));
601 671
602 port = new MidiPort<InterfaceType>; 672 port = new MidiPort<InterfaceType>;
603 port->index = static_cast<uint32_t>(port_ids_.size()); 673 port->index = static_cast<uint32_t>(port_ids_.size());
604 674
605 ports_[dev_id].reset(port); 675 ports_[dev_id].reset(port);
606 port_ids_.push_back(dev_id); 676 port_ids_.push_back(dev_id);
607 } else { 677 } else {
608 SetPortState(port->index, MIDI_PORT_CONNECTED); 678 SetPortState(port->index, MIDI_PORT_CONNECTED);
609 } 679 }
610 680
(...skipping 341 matching lines...) Expand 10 before | Expand all | Expand 10 after
952 void MidiManagerWinrt::OnPortManagerReady() { 1022 void MidiManagerWinrt::OnPortManagerReady() {
953 DCHECK(com_thread_checker_->CalledOnValidThread()); 1023 DCHECK(com_thread_checker_->CalledOnValidThread());
954 DCHECK(port_manager_ready_count_ < 2); 1024 DCHECK(port_manager_ready_count_ < 2);
955 1025
956 if (++port_manager_ready_count_ == 2) 1026 if (++port_manager_ready_count_ == 2)
957 CompleteInitialization(Result::OK); 1027 CompleteInitialization(Result::OK);
958 } 1028 }
959 1029
960 } // namespace midi 1030 } // namespace midi
961 } // namespace media 1031 } // namespace media
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698