Remove no longer needed toolbar layer method.
[chromium-blink-merge.git] / media / midi / midi_manager.cc
blobf7de36f2ac50d2a73c8d63a65129806b6d17ba23
1 // Copyright (c) 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.
5 #include "media/midi/midi_manager.h"
7 #include "base/bind.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/message_loop/message_loop_proxy.h"
10 #include "base/trace_event/trace_event.h"
12 namespace media {
14 MidiManager::MidiManager()
15 : initialized_(false),
16 result_(MIDI_NOT_SUPPORTED) {
19 MidiManager::~MidiManager() {
22 #if !defined(OS_MACOSX) && !defined(OS_WIN) && !defined(USE_ALSA) && \
23 !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
24 MidiManager* MidiManager::Create() {
25 return new MidiManager;
27 #endif
29 void MidiManager::StartSession(MidiManagerClient* client) {
30 bool session_is_ready;
31 bool session_needs_initialization = false;
32 bool too_many_pending_clients_exist = false;
35 base::AutoLock auto_lock(lock_);
36 session_is_ready = initialized_;
37 if (clients_.find(client) != clients_.end() ||
38 pending_clients_.find(client) != pending_clients_.end()) {
39 // Should not happen. But just in case the renderer is compromised.
40 NOTREACHED();
41 return;
43 if (!session_is_ready) {
44 // Do not accept a new request if the pending client list contains too
45 // many clients.
46 too_many_pending_clients_exist =
47 pending_clients_.size() >= kMaxPendingClientCount;
49 if (!too_many_pending_clients_exist) {
50 // Call StartInitialization() only for the first request.
51 session_needs_initialization = pending_clients_.empty();
52 pending_clients_.insert(client);
57 // Lazily initialize the MIDI back-end.
58 if (!session_is_ready) {
59 if (session_needs_initialization) {
60 TRACE_EVENT0("midi", "MidiManager::StartInitialization");
61 session_thread_runner_ =
62 base::MessageLoop::current()->message_loop_proxy();
63 StartInitialization();
65 if (too_many_pending_clients_exist) {
66 // Return an error immediately if there are too many requests.
67 client->CompleteStartSession(MIDI_INITIALIZATION_ERROR);
68 return;
70 // CompleteInitialization() will be called asynchronously when platform
71 // dependent initialization is finished.
72 return;
75 // Platform dependent initialization was already finished for previously
76 // initialized clients.
77 MidiResult result;
79 base::AutoLock auto_lock(lock_);
80 if (result_ == MIDI_OK) {
81 AddInitialPorts(client);
82 clients_.insert(client);
84 result = result_;
86 client->CompleteStartSession(result);
89 void MidiManager::EndSession(MidiManagerClient* client) {
90 // At this point, |client| can be in the destruction process, and calling
91 // any method of |client| is dangerous.
92 base::AutoLock auto_lock(lock_);
93 clients_.erase(client);
94 pending_clients_.erase(client);
97 void MidiManager::DispatchSendMidiData(MidiManagerClient* client,
98 uint32 port_index,
99 const std::vector<uint8>& data,
100 double timestamp) {
101 NOTREACHED();
104 void MidiManager::StartInitialization() {
105 CompleteInitialization(MIDI_NOT_SUPPORTED);
108 void MidiManager::CompleteInitialization(MidiResult result) {
109 DCHECK(session_thread_runner_.get());
110 // It is safe to post a task to the IO thread from here because the IO thread
111 // should have stopped if the MidiManager is going to be destructed.
112 session_thread_runner_->PostTask(
113 FROM_HERE,
114 base::Bind(&MidiManager::CompleteInitializationInternal,
115 base::Unretained(this),
116 result));
119 void MidiManager::AddInputPort(const MidiPortInfo& info) {
120 base::AutoLock auto_lock(lock_);
121 input_ports_.push_back(info);
122 for (auto client : clients_)
123 client->AddInputPort(info);
126 void MidiManager::AddOutputPort(const MidiPortInfo& info) {
127 base::AutoLock auto_lock(lock_);
128 output_ports_.push_back(info);
129 for (auto client : clients_)
130 client->AddOutputPort(info);
133 void MidiManager::SetInputPortState(uint32 port_index, MidiPortState state) {
134 base::AutoLock auto_lock(lock_);
135 DCHECK_LT(port_index, input_ports_.size());
136 input_ports_[port_index].state = state;
137 for (auto client : clients_)
138 client->SetInputPortState(port_index, state);
141 void MidiManager::SetOutputPortState(uint32 port_index, MidiPortState state) {
142 base::AutoLock auto_lock(lock_);
143 DCHECK_LT(port_index, output_ports_.size());
144 output_ports_[port_index].state = state;
145 for (auto client : clients_)
146 client->SetOutputPortState(port_index, state);
149 void MidiManager::ReceiveMidiData(
150 uint32 port_index,
151 const uint8* data,
152 size_t length,
153 double timestamp) {
154 base::AutoLock auto_lock(lock_);
156 for (auto client : clients_)
157 client->ReceiveMidiData(port_index, data, length, timestamp);
160 void MidiManager::CompleteInitializationInternal(MidiResult result) {
161 TRACE_EVENT0("midi", "MidiManager::CompleteInitialization");
163 base::AutoLock auto_lock(lock_);
164 DCHECK(clients_.empty());
165 DCHECK(!initialized_);
166 initialized_ = true;
167 result_ = result;
169 for (auto client : pending_clients_) {
170 if (result_ == MIDI_OK) {
171 AddInitialPorts(client);
172 clients_.insert(client);
174 client->CompleteStartSession(result_);
176 pending_clients_.clear();
179 void MidiManager::AddInitialPorts(MidiManagerClient* client) {
180 lock_.AssertAcquired();
182 for (const auto& info : input_ports_)
183 client->AddInputPort(info);
184 for (const auto& info : output_ports_)
185 client->AddOutputPort(info);
188 } // namespace media