[Android] Open up API in jni bridge to fetch images from tab
[chromium-blink-merge.git] / media / midi / midi_manager.cc
blob9e1f1e270d38f81ae85b5caf03e6675420749694
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::ReceiveMidiData(
134 uint32 port_index,
135 const uint8* data,
136 size_t length,
137 double timestamp) {
138 base::AutoLock auto_lock(lock_);
140 for (auto client : clients_)
141 client->ReceiveMidiData(port_index, data, length, timestamp);
144 void MidiManager::CompleteInitializationInternal(MidiResult result) {
145 TRACE_EVENT0("midi", "MidiManager::CompleteInitialization");
147 base::AutoLock auto_lock(lock_);
148 DCHECK(clients_.empty());
149 DCHECK(!initialized_);
150 initialized_ = true;
151 result_ = result;
153 for (auto client : pending_clients_) {
154 if (result_ == MIDI_OK) {
155 AddInitialPorts(client);
156 clients_.insert(client);
158 client->CompleteStartSession(result_);
160 pending_clients_.clear();
163 void MidiManager::AddInitialPorts(MidiManagerClient* client) {
164 lock_.AssertAcquired();
166 for (const auto& info : input_ports_)
167 client->AddInputPort(info);
168 for (const auto& info : output_ports_)
169 client->AddOutputPort(info);
172 } // namespace media