shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef MEDIA_MIDI_MIDI_MANAGER_WINRT_H_ |
| 6 | #define MEDIA_MIDI_MIDI_MANAGER_WINRT_H_ |
| 7 | |
| 8 | #include <memory> |
| 9 | |
| 10 | #include "base/strings/string16.h" |
| 11 | #include "base/threading/thread.h" |
| 12 | #include "media/midi/midi_manager.h" |
| 13 | |
| 14 | namespace base { |
| 15 | class ThreadChecker; |
| 16 | } |
| 17 | |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 18 | namespace midi { |
| 19 | |
| 20 | class MidiScheduler; |
toyoshim | f4d6152 | 2017-02-10 02:03:32 -0800 | [diff] [blame^] | 21 | class MidiService; |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 22 | |
| 23 | class MIDI_EXPORT MidiManagerWinrt final : public MidiManager { |
| 24 | public: |
toyoshim | f4d6152 | 2017-02-10 02:03:32 -0800 | [diff] [blame^] | 25 | explicit MidiManagerWinrt(MidiService* service); |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 26 | ~MidiManagerWinrt() override; |
| 27 | |
| 28 | // MidiManager overrides: |
| 29 | void StartInitialization() final; |
| 30 | void Finalize() final; |
| 31 | void DispatchSendMidiData(MidiManagerClient* client, |
| 32 | uint32_t port_index, |
| 33 | const std::vector<uint8_t>& data, |
| 34 | double timestamp) final; |
| 35 | |
| 36 | private: |
| 37 | // Callbacks on the COM thread. |
| 38 | void InitializeOnComThread(); |
| 39 | void FinalizeOnComThread(); |
| 40 | void SendOnComThread(uint32_t port_index, const std::vector<uint8_t>& data); |
| 41 | |
| 42 | // Callback from MidiPortManager::OnEnumerationComplete on the COM thread. |
| 43 | // Calls CompleteInitialization() when both MidiPortManagers are ready. |
| 44 | void OnPortManagerReady(); |
| 45 | |
| 46 | // Subclasses that access private/protected members of MidiManager. |
| 47 | template <typename InterfaceType, |
| 48 | typename RuntimeType, |
| 49 | typename StaticsInterfaceType, |
| 50 | base::char16 const* runtime_class_id> |
| 51 | class MidiPortManager; |
| 52 | class MidiInPortManager; |
| 53 | class MidiOutPortManager; |
| 54 | |
| 55 | // COM-initialized thread for calling WinRT methods. |
| 56 | base::Thread com_thread_; |
| 57 | |
| 58 | // Lock to ensure all smart pointers initialized in InitializeOnComThread() |
| 59 | // and destroyed in FinalizeOnComThread() will not be accidentally destructed |
| 60 | // twice in the destructor. |
| 61 | base::Lock lazy_init_member_lock_; |
| 62 | |
| 63 | // Should be instantiated on COM thread. |
| 64 | std::unique_ptr<base::ThreadChecker> |
| 65 | com_thread_checker_; // GUARDED_BY(lazy_init_member_lock_) |
| 66 | |
| 67 | // All operations to MidiPortManager should be done on COM thread. |
| 68 | std::unique_ptr<MidiInPortManager> |
| 69 | port_manager_in_; // GUARDED_BY(lazy_init_member_lock_) |
| 70 | std::unique_ptr<MidiOutPortManager> |
| 71 | port_manager_out_; // GUARDED_BY(lazy_init_member_lock_) |
| 72 | |
| 73 | // |scheduler_| is used by DispatchSendMidiData on Chrome_IOThread, should |
| 74 | // acquire lock before use. |
| 75 | std::unique_ptr<MidiScheduler> |
| 76 | scheduler_; // GUARDED_BY(lazy_init_member_lock_) |
| 77 | |
| 78 | // Incremented when a MidiPortManager is ready. |
| 79 | uint8_t port_manager_ready_count_ = 0; |
| 80 | |
| 81 | DISALLOW_COPY_AND_ASSIGN(MidiManagerWinrt); |
| 82 | }; |
| 83 | |
| 84 | } // namespace midi |
shaochuan | e58f9c7 | 2016-08-30 22:27:08 -0700 | [diff] [blame] | 85 | |
| 86 | #endif // MEDIA_MIDI_MIDI_MANAGER_WINRT_H_ |