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

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

Issue 2350683003: Check if null pointer is provided in DeviceWatcher callbacks (Closed)
Patch Set: 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 #pragma warning(disable : 4467) 7 #pragma warning(disable : 4467)
8 8
9 #include <initguid.h> // Required by <devpkey.h> 9 #include <initguid.h> // Required by <devpkey.h>
10 10
(...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after
395 // thread. |weak_ptr| and |task_runner| are captured by lambda callbacks for 395 // thread. |weak_ptr| and |task_runner| are captured by lambda callbacks for
396 // posting jobs. Note that WinRT callback arguments should not be passed 396 // posting jobs. Note that WinRT callback arguments should not be passed
397 // outside the callback since the pointers may be unavailable afterwards. 397 // outside the callback since the pointers may be unavailable afterwards.
398 base::WeakPtr<MidiPortManager> weak_ptr = GetWeakPtrFromFactory(); 398 base::WeakPtr<MidiPortManager> weak_ptr = GetWeakPtrFromFactory();
399 scoped_refptr<base::SingleThreadTaskRunner> task_runner = task_runner_; 399 scoped_refptr<base::SingleThreadTaskRunner> task_runner = task_runner_;
400 400
401 hr = watcher_->add_Added( 401 hr = watcher_->add_Added(
402 WRL::Callback<ITypedEventHandler<DeviceWatcher*, DeviceInformation*>>( 402 WRL::Callback<ITypedEventHandler<DeviceWatcher*, DeviceInformation*>>(
403 [weak_ptr, task_runner](IDeviceWatcher* watcher, 403 [weak_ptr, task_runner](IDeviceWatcher* watcher,
404 IDeviceInformation* info) { 404 IDeviceInformation* info) {
405 if (!info) {
406 VLOG(1) << "DeviceWatcher.Added callback provides null "
407 "pointer, ignoring";
408 return S_OK;
409 }
410
405 // Disable Microsoft GS Wavetable Synth due to security reasons. 411 // Disable Microsoft GS Wavetable Synth due to security reasons.
406 // http://crbug.com/499279 412 // http://crbug.com/499279
407 if (IsMicrosoftSynthesizer(info)) 413 if (IsMicrosoftSynthesizer(info))
408 return S_OK; 414 return S_OK;
409 415
410 std::string dev_id = GetIdString(info), 416 std::string dev_id = GetIdString(info),
411 dev_name = GetNameString(info); 417 dev_name = GetNameString(info);
412 418
413 task_runner->PostTask( 419 task_runner->PostTask(
414 FROM_HERE, base::Bind(&MidiPortManager::OnAdded, weak_ptr, 420 FROM_HERE, base::Bind(&MidiPortManager::OnAdded, weak_ptr,
(...skipping 24 matching lines...) Expand all
439 if (FAILED(hr)) { 445 if (FAILED(hr)) {
440 VLOG(1) << "add_EnumerationCompleted failed: " << PrintHr(hr); 446 VLOG(1) << "add_EnumerationCompleted failed: " << PrintHr(hr);
441 return false; 447 return false;
442 } 448 }
443 449
444 hr = watcher_->add_Removed( 450 hr = watcher_->add_Removed(
445 WRL::Callback< 451 WRL::Callback<
446 ITypedEventHandler<DeviceWatcher*, DeviceInformationUpdate*>>( 452 ITypedEventHandler<DeviceWatcher*, DeviceInformationUpdate*>>(
447 [weak_ptr, task_runner](IDeviceWatcher* watcher, 453 [weak_ptr, task_runner](IDeviceWatcher* watcher,
448 IDeviceInformationUpdate* update) { 454 IDeviceInformationUpdate* update) {
455 if (!update) {
456 VLOG(1) << "DeviceWatcher.Removed callback provides null "
457 "pointer, ignoring";
458 return S_OK;
459 }
460
449 std::string dev_id = GetIdString(update); 461 std::string dev_id = GetIdString(update);
450 462
451 task_runner->PostTask( 463 task_runner->PostTask(
452 FROM_HERE, 464 FROM_HERE,
453 base::Bind(&MidiPortManager::OnRemoved, weak_ptr, dev_id)); 465 base::Bind(&MidiPortManager::OnRemoved, weak_ptr, dev_id));
454 466
455 return S_OK; 467 return S_OK;
456 }) 468 })
457 .Get(), 469 .Get(),
458 &token_Removed_); 470 &token_Removed_);
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
589 return; 601 return;
590 } 602 }
591 603
592 base::WeakPtr<MidiPortManager> weak_ptr = GetWeakPtrFromFactory(); 604 base::WeakPtr<MidiPortManager> weak_ptr = GetWeakPtrFromFactory();
593 scoped_refptr<base::SingleThreadTaskRunner> task_runner = task_runner_; 605 scoped_refptr<base::SingleThreadTaskRunner> task_runner = task_runner_;
594 606
595 hr = async_op->put_Completed( 607 hr = async_op->put_Completed(
596 WRL::Callback<IAsyncOperationCompletedHandler<RuntimeType*>>( 608 WRL::Callback<IAsyncOperationCompletedHandler<RuntimeType*>>(
597 [weak_ptr, task_runner](IAsyncOperation<RuntimeType*>* async_op, 609 [weak_ptr, task_runner](IAsyncOperation<RuntimeType*>* async_op,
598 AsyncStatus status) { 610 AsyncStatus status) {
599 InterfaceType* handle;
600 HRESULT hr = async_op->GetResults(&handle);
601 if (FAILED(hr)) {
602 VLOG(1) << "GetResults failed: " << PrintHr(hr);
603 return hr;
604 }
605
606 // A reference to |async_op| is kept in |async_ops_|, safe to pass 611 // A reference to |async_op| is kept in |async_ops_|, safe to pass
607 // outside. 612 // outside.
608 task_runner->PostTask( 613 task_runner->PostTask(
609 FROM_HERE, 614 FROM_HERE,
610 base::Bind(&MidiPortManager::OnCompletedGetPortFromIdAsync, 615 base::Bind(&MidiPortManager::OnCompletedGetPortFromIdAsync,
611 weak_ptr, handle, async_op)); 616 weak_ptr, async_op));
612 617
613 return S_OK; 618 return S_OK;
614 }) 619 })
615 .Get()); 620 .Get());
616 if (FAILED(hr)) { 621 if (FAILED(hr)) {
617 VLOG(1) << "put_Completed failed: " << PrintHr(hr); 622 VLOG(1) << "put_Completed failed: " << PrintHr(hr);
618 return; 623 return;
619 } 624 }
620 625
621 // Keep a reference to incompleted |async_op| for releasing later. 626 // Keep a reference to incompleted |async_op| for releasing later.
(...skipping 21 matching lines...) Expand all
643 VLOG(1) << "Removing non-existent port " << dev_id; 648 VLOG(1) << "Removing non-existent port " << dev_id;
644 return; 649 return;
645 } 650 }
646 651
647 SetPortState(port->index, MIDI_PORT_DISCONNECTED); 652 SetPortState(port->index, MIDI_PORT_DISCONNECTED);
648 653
649 RemovePortEventHandlers(port); 654 RemovePortEventHandlers(port);
650 port->handle = nullptr; 655 port->handle = nullptr;
651 } 656 }
652 657
653 void OnCompletedGetPortFromIdAsync(InterfaceType* handle, 658 void OnCompletedGetPortFromIdAsync(IAsyncOperation<RuntimeType*>* async_op) {
654 IAsyncOperation<RuntimeType*>* async_op) {
655 DCHECK(thread_checker_.CalledOnValidThread()); 659 DCHECK(thread_checker_.CalledOnValidThread());
656 CHECK(is_initialized_); 660 CHECK(is_initialized_);
657 661
662 InterfaceType* handle = nullptr;
663 HRESULT hr = async_op->GetResults(&handle);
664 if (FAILED(hr)) {
665 VLOG(1) << "GetResults failed: " << PrintHr(hr);
666 return;
667 }
668
669 // Manually release COM interface to completed |async_op|.
670 auto it = async_ops_.find(async_op);
671 CHECK(it != async_ops_.end());
672 (*it)->Release();
673 async_ops_.erase(it);
674
675 if (!handle) {
676 VLOG(1) << "Midi{In,Out}Port.FromIdAsync callback provides null pointer, "
677 "ignoring";
678 return;
679 }
680
658 EventRegistrationToken token = {kInvalidTokenValue}; 681 EventRegistrationToken token = {kInvalidTokenValue};
659 if (!RegisterOnMessageReceived(handle, &token)) 682 if (!RegisterOnMessageReceived(handle, &token))
660 return; 683 return;
661 684
662 std::string dev_id = GetDeviceIdString(handle); 685 std::string dev_id = GetDeviceIdString(handle);
663 686
664 MidiPort<InterfaceType>* port = GetPortByDeviceId(dev_id); 687 MidiPort<InterfaceType>* port = GetPortByDeviceId(dev_id);
665 688
666 if (port == nullptr) { 689 if (port == nullptr) {
667 std::string manufacturer = "Unknown", driver_version = "Unknown"; 690 std::string manufacturer = "Unknown", driver_version = "Unknown";
668 GetDriverInfoFromDeviceId(dev_id, &manufacturer, &driver_version); 691 GetDriverInfoFromDeviceId(dev_id, &manufacturer, &driver_version);
669 692
670 AddPort(MidiPortInfo(dev_id, manufacturer, port_names_[dev_id], 693 AddPort(MidiPortInfo(dev_id, manufacturer, port_names_[dev_id],
671 driver_version, MIDI_PORT_OPENED)); 694 driver_version, MIDI_PORT_OPENED));
672 695
673 port = new MidiPort<InterfaceType>; 696 port = new MidiPort<InterfaceType>;
674 port->index = static_cast<uint32_t>(port_ids_.size()); 697 port->index = static_cast<uint32_t>(port_ids_.size());
675 698
676 ports_[dev_id].reset(port); 699 ports_[dev_id].reset(port);
677 port_ids_.push_back(dev_id); 700 port_ids_.push_back(dev_id);
678 } else { 701 } else {
679 SetPortState(port->index, MIDI_PORT_CONNECTED); 702 SetPortState(port->index, MIDI_PORT_CONNECTED);
680 } 703 }
681 704
682 port->handle = handle; 705 port->handle = handle;
683 port->token_MessageReceived = token; 706 port->token_MessageReceived = token;
684 707
685 // Manually release COM interface to completed |async_op|.
686 auto it = async_ops_.find(async_op);
687 CHECK(it != async_ops_.end());
688 (*it)->Release();
689 async_ops_.erase(it);
690
691 if (enumeration_completed_not_ready_ && async_ops_.empty()) { 708 if (enumeration_completed_not_ready_ && async_ops_.empty()) {
692 midi_manager_->OnPortManagerReady(); 709 midi_manager_->OnPortManagerReady();
693 enumeration_completed_not_ready_ = false; 710 enumeration_completed_not_ready_ = false;
694 } 711 }
695 } 712 }
696 713
697 // Overrided by MidiInPortManager to listen to input ports. 714 // Overrided by MidiInPortManager to listen to input ports.
698 virtual bool RegisterOnMessageReceived(InterfaceType* handle, 715 virtual bool RegisterOnMessageReceived(InterfaceType* handle,
699 EventRegistrationToken* p_token) { 716 EventRegistrationToken* p_token) {
700 return true; 717 return true;
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after
1023 void MidiManagerWinrt::OnPortManagerReady() { 1040 void MidiManagerWinrt::OnPortManagerReady() {
1024 DCHECK(com_thread_checker_->CalledOnValidThread()); 1041 DCHECK(com_thread_checker_->CalledOnValidThread());
1025 DCHECK(port_manager_ready_count_ < 2); 1042 DCHECK(port_manager_ready_count_ < 2);
1026 1043
1027 if (++port_manager_ready_count_ == 2) 1044 if (++port_manager_ready_count_ == 2)
1028 CompleteInitialization(Result::OK); 1045 CompleteInitialization(Result::OK);
1029 } 1046 }
1030 1047
1031 } // namespace midi 1048 } // namespace midi
1032 } // namespace media 1049 } // 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