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

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

Issue 662853003: Manage MIDI related objects and sessions' lifecycles correctly (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: one line bug fix Created 6 years, 2 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 | « media/midi/midi_manager.h ('k') | media/midi/midi_manager_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 MidiManager::MidiManager() 14 MidiManager::MidiManager()
15 : initialized_(false), 15 : initialized_(false),
16 result_(MIDI_NOT_SUPPORTED) { 16 result_(MIDI_NOT_SUPPORTED) {
17 } 17 }
18 18
19 MidiManager::~MidiManager() { 19 MidiManager::~MidiManager() {
20 } 20 }
21 21
22 #if !defined(OS_MACOSX) && !defined(OS_WIN) && !defined(USE_ALSA) && \ 22 #if !defined(OS_MACOSX) && !defined(OS_WIN) && !defined(USE_ALSA) && \
23 !defined(OS_ANDROID) && !defined(OS_CHROMEOS) 23 !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
24 MidiManager* MidiManager::Create() { 24 MidiManager* MidiManager::Create() {
25 return new MidiManager; 25 return new MidiManager;
26 } 26 }
27 #endif 27 #endif
28 28
29 void MidiManager::StartSession(MidiManagerClient* client, int client_id) { 29 void MidiManager::StartSession(MidiManagerClient* client) {
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 bool too_many_pending_clients_exist = false;
33 33
34 { 34 {
35 base::AutoLock auto_lock(lock_); 35 base::AutoLock auto_lock(lock_);
36 session_is_ready = initialized_; 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;
42 }
37 if (!session_is_ready) { 43 if (!session_is_ready) {
38 // Do not accept a new request if the pending client list contains too 44 // Do not accept a new request if the pending client list contains too
39 // many clients. 45 // many clients.
40 too_many_pending_clients_exist = 46 too_many_pending_clients_exist =
41 pending_clients_.size() >= kMaxPendingClientCount; 47 pending_clients_.size() >= kMaxPendingClientCount;
42 48
43 if (!too_many_pending_clients_exist) { 49 if (!too_many_pending_clients_exist) {
44 // Call StartInitialization() only for the first request. 50 // Call StartInitialization() only for the first request.
45 session_needs_initialization = pending_clients_.empty(); 51 session_needs_initialization = pending_clients_.empty();
46 pending_clients_.insert(std::make_pair(client, client_id)); 52 pending_clients_.insert(client);
47 } 53 }
48 } 54 }
49 } 55 }
50 56
51 // Lazily initialize the MIDI back-end. 57 // Lazily initialize the MIDI back-end.
52 if (!session_is_ready) { 58 if (!session_is_ready) {
53 if (session_needs_initialization) { 59 if (session_needs_initialization) {
54 TRACE_EVENT0("midi", "MidiManager::StartInitialization"); 60 TRACE_EVENT0("midi", "MidiManager::StartInitialization");
55 session_thread_runner_ = 61 session_thread_runner_ =
56 base::MessageLoop::current()->message_loop_proxy(); 62 base::MessageLoop::current()->message_loop_proxy();
57 StartInitialization(); 63 StartInitialization();
58 } 64 }
59 if (too_many_pending_clients_exist) { 65 if (too_many_pending_clients_exist) {
60 // Return an error immediately if there are too many requests. 66 // Return an error immediately if there are too many requests.
61 client->CompleteStartSession(client_id, MIDI_INITIALIZATION_ERROR); 67 client->CompleteStartSession(MIDI_INITIALIZATION_ERROR);
62 return; 68 return;
63 } 69 }
64 // CompleteInitialization() will be called asynchronously when platform 70 // CompleteInitialization() will be called asynchronously when platform
65 // dependent initialization is finished. 71 // dependent initialization is finished.
66 return; 72 return;
67 } 73 }
68 74
69 // Platform dependent initialization was already finished for previously 75 // Platform dependent initialization was already finished for previously
70 // initialized clients. 76 // initialized clients.
71 MidiResult result; 77 MidiResult result;
72 { 78 {
73 base::AutoLock auto_lock(lock_); 79 base::AutoLock auto_lock(lock_);
74 if (result_ == MIDI_OK) 80 if (result_ == MIDI_OK)
75 clients_.insert(client); 81 clients_.insert(client);
76 result = result_; 82 result = result_;
77 } 83 }
78 client->CompleteStartSession(client_id, result); 84 client->CompleteStartSession(result);
79 } 85 }
80 86
81 void MidiManager::EndSession(MidiManagerClient* client) { 87 void MidiManager::EndSession(MidiManagerClient* client) {
88 // At this point, |client| can be in the destruction process, and calling
89 // any method of |client| is dangerous.
82 base::AutoLock auto_lock(lock_); 90 base::AutoLock auto_lock(lock_);
83 clients_.erase(client); 91 clients_.erase(client);
84 pending_clients_.erase(client); 92 pending_clients_.erase(client);
85 } 93 }
86 94
87 void MidiManager::DispatchSendMidiData(MidiManagerClient* client, 95 void MidiManager::DispatchSendMidiData(MidiManagerClient* client,
88 uint32 port_index, 96 uint32 port_index,
89 const std::vector<uint8>& data, 97 const std::vector<uint8>& data,
90 double timestamp) { 98 double timestamp) {
91 NOTREACHED(); 99 NOTREACHED();
(...skipping 22 matching lines...) Expand all
114 output_ports_.push_back(info); 122 output_ports_.push_back(info);
115 } 123 }
116 124
117 void MidiManager::ReceiveMidiData( 125 void MidiManager::ReceiveMidiData(
118 uint32 port_index, 126 uint32 port_index,
119 const uint8* data, 127 const uint8* data,
120 size_t length, 128 size_t length,
121 double timestamp) { 129 double timestamp) {
122 base::AutoLock auto_lock(lock_); 130 base::AutoLock auto_lock(lock_);
123 131
124 for (ClientList::iterator i = clients_.begin(); i != clients_.end(); ++i) 132 for (MidiManagerClient* client : clients_)
125 (*i)->ReceiveMidiData(port_index, data, length, timestamp); 133 client->ReceiveMidiData(port_index, data, length, timestamp);
126 } 134 }
127 135
128 void MidiManager::CompleteInitializationInternal(MidiResult result) { 136 void MidiManager::CompleteInitializationInternal(MidiResult result) {
129 TRACE_EVENT0("midi", "MidiManager::CompleteInitialization"); 137 TRACE_EVENT0("midi", "MidiManager::CompleteInitialization");
130 138
131 base::AutoLock auto_lock(lock_); 139 base::AutoLock auto_lock(lock_);
132 DCHECK(clients_.empty()); 140 DCHECK(clients_.empty());
133 DCHECK(!initialized_); 141 DCHECK(!initialized_);
134 initialized_ = true; 142 initialized_ = true;
135 result_ = result; 143 result_ = result;
136 144
137 for (PendingClientMap::iterator it = pending_clients_.begin(); 145 for (MidiManagerClient* client : pending_clients_) {
138 it != pending_clients_.end();
139 ++it) {
140 if (result_ == MIDI_OK) 146 if (result_ == MIDI_OK)
141 clients_.insert(it->first); 147 clients_.insert(client);
142 it->first->CompleteStartSession(it->second, result_); 148 client->CompleteStartSession(result_);
143 } 149 }
144 pending_clients_.clear(); 150 pending_clients_.clear();
145 } 151 }
146 152
147 } // namespace media 153 } // namespace media
OLDNEW
« no previous file with comments | « media/midi/midi_manager.h ('k') | media/midi/midi_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698