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

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: address review comments at #7 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
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 on renderer being hijacked.
palmer 2014/10/21 21:20:12 Nit: "But just in case the renderer is compromised
Takashi Toyoshima 2014/10/22 05:57:40 Done.
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) {
82 base::AutoLock auto_lock(lock_); 88 base::AutoLock auto_lock(lock_);
yhirano 2014/10/21 13:21:20 Please add a comment that |client| can be in the d
Takashi Toyoshima 2014/10/22 05:57:40 Done.
83 clients_.erase(client); 89 clients_.erase(client);
84 pending_clients_.erase(client); 90 pending_clients_.erase(client);
85 } 91 }
86 92
87 void MidiManager::DispatchSendMidiData(MidiManagerClient* client, 93 void MidiManager::DispatchSendMidiData(MidiManagerClient* client,
88 uint32 port_index, 94 uint32 port_index,
89 const std::vector<uint8>& data, 95 const std::vector<uint8>& data,
90 double timestamp) { 96 double timestamp) {
91 NOTREACHED(); 97 NOTREACHED();
92 } 98 }
(...skipping 21 matching lines...) Expand all
114 output_ports_.push_back(info); 120 output_ports_.push_back(info);
115 } 121 }
116 122
117 void MidiManager::ReceiveMidiData( 123 void MidiManager::ReceiveMidiData(
118 uint32 port_index, 124 uint32 port_index,
119 const uint8* data, 125 const uint8* data,
120 size_t length, 126 size_t length,
121 double timestamp) { 127 double timestamp) {
122 base::AutoLock auto_lock(lock_); 128 base::AutoLock auto_lock(lock_);
123 129
124 for (ClientList::iterator i = clients_.begin(); i != clients_.end(); ++i) 130 for (MidiManagerClient* client : clients_)
125 (*i)->ReceiveMidiData(port_index, data, length, timestamp); 131 client->ReceiveMidiData(port_index, data, length, timestamp);
126 } 132 }
127 133
128 void MidiManager::CompleteInitializationInternal(MidiResult result) { 134 void MidiManager::CompleteInitializationInternal(MidiResult result) {
129 TRACE_EVENT0("midi", "MidiManager::CompleteInitialization"); 135 TRACE_EVENT0("midi", "MidiManager::CompleteInitialization");
130 136
131 base::AutoLock auto_lock(lock_); 137 base::AutoLock auto_lock(lock_);
132 DCHECK(clients_.empty()); 138 DCHECK(clients_.empty());
133 DCHECK(!initialized_); 139 DCHECK(!initialized_);
134 initialized_ = true; 140 initialized_ = true;
135 result_ = result; 141 result_ = result;
136 142
137 for (PendingClientMap::iterator it = pending_clients_.begin(); 143 for (MidiManagerClient* client : pending_clients_) {
138 it != pending_clients_.end();
139 ++it) {
140 if (result_ == MIDI_OK) 144 if (result_ == MIDI_OK)
141 clients_.insert(it->first); 145 clients_.insert(client);
142 it->first->CompleteStartSession(it->second, result_); 146 client->CompleteStartSession(result_);
143 } 147 }
144 pending_clients_.clear(); 148 pending_clients_.clear();
145 } 149 }
146 150
147 } // namespace media 151 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698