OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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.h" | 5 #include "media/midi/midi_manager.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/debug/trace_event.h" | 8 #include "base/debug/trace_event.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/message_loop/message_loop_proxy.h" | 10 #include "base/message_loop/message_loop_proxy.h" |
11 | 11 |
12 namespace media { | 12 namespace media { |
13 | 13 |
14 #if !defined(OS_MACOSX) && !defined(OS_WIN) && !defined(USE_ALSA) && \ | |
15 !defined(OS_ANDROID) && !defined(OS_CHROMEOS) | |
16 MidiManager* MidiManager::Create() { | |
17 return new MidiManager; | |
18 } | |
19 #endif | |
20 | |
21 MidiManager::MidiManager() | 14 MidiManager::MidiManager() |
22 : initialized_(false), | 15 : initialized_(false), |
23 result_(MIDI_NOT_SUPPORTED) { | 16 result_(MIDI_NOT_SUPPORTED) { |
24 } | 17 } |
25 | 18 |
26 MidiManager::~MidiManager() { | 19 MidiManager::~MidiManager() { |
27 } | 20 } |
28 | 21 |
| 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; |
| 26 } |
| 27 #endif |
| 28 |
29 void MidiManager::StartSession(MidiManagerClient* client, int client_id) { | 29 void MidiManager::StartSession(MidiManagerClient* client, int client_id) { |
30 bool session_is_ready; | 30 bool session_is_ready; |
31 bool session_needs_initialization = false; | 31 bool session_needs_initialization = false; |
| 32 bool too_many_pending_clients_exist = false; |
32 | 33 |
33 { | 34 { |
34 base::AutoLock auto_lock(lock_); | 35 base::AutoLock auto_lock(lock_); |
35 session_is_ready = initialized_; | 36 session_is_ready = initialized_; |
36 if (!session_is_ready) { | 37 if (!session_is_ready) { |
37 // Call StartInitialization() only for the first request. | 38 // Do not accept a new request if the pending client list contains too |
38 session_needs_initialization = pending_clients_.empty(); | 39 // many clients. |
39 pending_clients_.insert( | 40 too_many_pending_clients_exist = |
40 std::pair<int, MidiManagerClient*>(client_id, client)); | 41 pending_clients_.size() >= kMaxPendingClientCount; |
| 42 |
| 43 if (!too_many_pending_clients_exist) { |
| 44 // Call StartInitialization() only for the first request. |
| 45 session_needs_initialization = pending_clients_.empty(); |
| 46 pending_clients_.insert( |
| 47 std::pair<int, MidiManagerClient*>(client_id, client)); |
| 48 } |
41 } | 49 } |
42 } | 50 } |
43 | 51 |
44 // Lazily initialize the MIDI back-end. | 52 // Lazily initialize the MIDI back-end. |
45 if (!session_is_ready) { | 53 if (!session_is_ready) { |
46 if (session_needs_initialization) { | 54 if (session_needs_initialization) { |
47 TRACE_EVENT0("midi", "MidiManager::StartInitialization"); | 55 TRACE_EVENT0("midi", "MidiManager::StartInitialization"); |
48 session_thread_runner_ = | 56 session_thread_runner_ = |
49 base::MessageLoop::current()->message_loop_proxy(); | 57 base::MessageLoop::current()->message_loop_proxy(); |
50 StartInitialization(); | 58 StartInitialization(); |
51 } | 59 } |
| 60 if (too_many_pending_clients_exist) { |
| 61 // Return an error immediately if there are too many requests. |
| 62 client->CompleteStartSession(client_id, MIDI_INITIALIZATION_ERROR); |
| 63 return; |
| 64 } |
52 // CompleteInitialization() will be called asynchronously when platform | 65 // CompleteInitialization() will be called asynchronously when platform |
53 // dependent initialization is finished. | 66 // dependent initialization is finished. |
54 return; | 67 return; |
55 } | 68 } |
56 | 69 |
57 // Platform dependent initialization was already finished for previously | 70 // Platform dependent initialization was already finished for previously |
58 // initialized clients. | 71 // initialized clients. |
59 MidiResult result; | 72 MidiResult result; |
60 { | 73 { |
61 base::AutoLock auto_lock(lock_); | 74 base::AutoLock auto_lock(lock_); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 it != pending_clients_.end(); | 141 it != pending_clients_.end(); |
129 ++it) { | 142 ++it) { |
130 if (result_ == MIDI_OK) | 143 if (result_ == MIDI_OK) |
131 clients_.insert(it->second); | 144 clients_.insert(it->second); |
132 it->second->CompleteStartSession(it->first, result_); | 145 it->second->CompleteStartSession(it->first, result_); |
133 } | 146 } |
134 pending_clients_.clear(); | 147 pending_clients_.clear(); |
135 } | 148 } |
136 | 149 |
137 } // namespace media | 150 } // namespace media |
OLD | NEW |