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

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

Issue 1315793008: Web MIDI: introduce MidiManager::Shutdown to shutdown gracefully (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: more checks in unit tests Created 5 years, 3 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 #ifndef MEDIA_MIDI_MIDI_MANAGER_H_ 5 #ifndef MEDIA_MIDI_MIDI_MANAGER_H_
6 #define MEDIA_MIDI_MIDI_MANAGER_H_ 6 #define MEDIA_MIDI_MIDI_MANAGER_H_
7 7
8 #include <set> 8 #include <set>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 // |timestamp| is the time the data was received, in seconds. 53 // |timestamp| is the time the data was received, in seconds.
54 virtual void ReceiveMidiData(uint32 port_index, 54 virtual void ReceiveMidiData(uint32 port_index,
55 const uint8* data, 55 const uint8* data,
56 size_t length, 56 size_t length,
57 double timestamp) = 0; 57 double timestamp) = 0;
58 58
59 // AccumulateMidiBytesSent() is called to acknowledge when bytes have 59 // AccumulateMidiBytesSent() is called to acknowledge when bytes have
60 // successfully been sent to the hardware. 60 // successfully been sent to the hardware.
61 // This happens as a result of the client having previously called 61 // This happens as a result of the client having previously called
62 // MidiManager::DispatchSendMidiData(). 62 // MidiManager::DispatchSendMidiData().
63 virtual void AccumulateMidiBytesSent(size_t n) = 0; 63 virtual void AccumulateMidiBytesSent(size_t n) = 0;
yhirano 2015/09/11 15:59:47 Should we have Detach() which will be called in Fi
Takashi Toyoshima 2015/09/17 07:30:49 Do you already have any real concerns in client si
yhirano 2015/09/28 07:50:00 0: A MidiManager m has a client c. 1: m->Shutdown
Takashi Toyoshima 2015/10/02 15:19:24 I see. Let me fix this.
64 }; 64 };
65 65
66 // Manages access to all MIDI hardware. 66 // Manages access to all MIDI hardware.
67 class MIDI_EXPORT MidiManager { 67 class MIDI_EXPORT MidiManager {
68 public: 68 public:
69 static const size_t kMaxPendingClientCount = 128; 69 static const size_t kMaxPendingClientCount = 128;
70 70
71 MidiManager(); 71 MidiManager();
72 virtual ~MidiManager(); 72 virtual ~MidiManager();
73 73
74 // The constructor and the destructor will be called on the CrBrowserMain 74 // The constructor and the destructor will be called on the CrBrowserMain
75 // thread. 75 // thread.
76 static MidiManager* Create(); 76 static MidiManager* Create();
77 77
78 // Called on the CrBrowserMain thread to notify the Chrome_IOThread will stop
79 // and the instance will be destructed on the CrBrowserMain thread soon.
80 void Shutdown();
81
78 // A client calls StartSession() to receive and send MIDI data. 82 // A client calls StartSession() to receive and send MIDI data.
79 // If the session is ready to start, the MIDI system is lazily initialized 83 // If the session is ready to start, the MIDI system is lazily initialized
80 // and the client is registered to receive MIDI data. 84 // and the client is registered to receive MIDI data.
81 // CompleteStartSession() is called with Result::OK if the session is started. 85 // CompleteStartSession() is called with Result::OK if the session is started.
82 // Otherwise CompleteStartSession() is called with proper Result code. 86 // Otherwise CompleteStartSession() is called with proper Result code.
83 // StartSession() and EndSession() can be called on the Chrome_IOThread. 87 // StartSession() and EndSession() can be called on the Chrome_IOThread.
84 // CompleteStartSession() will be invoked on the same Chrome_IOThread. 88 // CompleteStartSession() will be invoked on the same Chrome_IOThread.
85 void StartSession(MidiManagerClient* client); 89 void StartSession(MidiManagerClient* client);
86 90
87 // A client calls EndSession() to stop receiving MIDI data. 91 // A client calls EndSession() to stop receiving MIDI data.
(...skipping 24 matching lines...) Expand all
112 // default implementation that synchronously calls CompleteInitialization() 116 // default implementation that synchronously calls CompleteInitialization()
113 // with Result::NOT_SUPPORTED on the caller thread. A derived class for a 117 // with Result::NOT_SUPPORTED on the caller thread. A derived class for a
114 // specific platform should override this method correctly. 118 // specific platform should override this method correctly.
115 // This method is called on Chrome_IOThread thread inside StartSession(). 119 // This method is called on Chrome_IOThread thread inside StartSession().
116 // Platform dependent initialization can be processed synchronously or 120 // Platform dependent initialization can be processed synchronously or
117 // asynchronously. When the initialization is completed, 121 // asynchronously. When the initialization is completed,
118 // CompleteInitialization() should be called with |result|. 122 // CompleteInitialization() should be called with |result|.
119 // |result| should be Result::OK on success, otherwise a proper Result. 123 // |result| should be Result::OK on success, otherwise a proper Result.
120 virtual void StartInitialization(); 124 virtual void StartInitialization();
121 125
126 // Finalizes the platform dependent MIDI system. Called on Chrome_IOThread
127 // thread and the thread will stop immediately after this call.
128 // Platform dependent resources that were allocated on the Chrome_IOThread
129 // should be disposed inside this method.
130 virtual void Finalize() {}
131
122 // Called from a platform dependent implementation of StartInitialization(). 132 // Called from a platform dependent implementation of StartInitialization().
123 // It invokes CompleteInitializationInternal() on the thread that calls 133 // It invokes CompleteInitializationInternal() on the thread that calls
124 // StartSession() and distributes |result| to MIDIManagerClient objects in 134 // StartSession() and distributes |result| to MIDIManagerClient objects in
125 // |pending_clients_|. 135 // |pending_clients_|.
126 void CompleteInitialization(Result result); 136 void CompleteInitialization(Result result);
127 137
128 void AddInputPort(const MidiPortInfo& info); 138 void AddInputPort(const MidiPortInfo& info);
129 void AddOutputPort(const MidiPortInfo& info); 139 void AddOutputPort(const MidiPortInfo& info);
130 void SetInputPortState(uint32 port_index, MidiPortState state); 140 void SetInputPortState(uint32 port_index, MidiPortState state);
131 void SetOutputPortState(uint32 port_index, MidiPortState state); 141 void SetOutputPortState(uint32 port_index, MidiPortState state);
(...skipping 15 matching lines...) Expand all
147 } 157 }
148 158
149 size_t clients_size_for_testing() const { return clients_.size(); } 159 size_t clients_size_for_testing() const { return clients_.size(); }
150 size_t pending_clients_size_for_testing() const { 160 size_t pending_clients_size_for_testing() const {
151 return pending_clients_.size(); 161 return pending_clients_.size();
152 } 162 }
153 163
154 private: 164 private:
155 void CompleteInitializationInternal(Result result); 165 void CompleteInitializationInternal(Result result);
156 void AddInitialPorts(MidiManagerClient* client); 166 void AddInitialPorts(MidiManagerClient* client);
167 void ShutdownOnSessionThread();
157 168
158 // Keeps track of all clients who wish to receive MIDI data. 169 // Keeps track of all clients who wish to receive MIDI data.
159 typedef std::set<MidiManagerClient*> ClientSet; 170 typedef std::set<MidiManagerClient*> ClientSet;
160 ClientSet clients_; 171 ClientSet clients_;
161 172
162 // Keeps track of all clients who are waiting for CompleteStartSession(). 173 // Keeps track of all clients who are waiting for CompleteStartSession().
163 ClientSet pending_clients_; 174 ClientSet pending_clients_;
164 175
165 // Keeps a SingleThreadTaskRunner of the thread that calls StartSession in 176 // Keeps a SingleThreadTaskRunner of the thread that calls StartSession in
166 // order to invoke CompleteStartSession() on the thread. 177 // order to invoke CompleteStartSession() on the thread.
167 scoped_refptr<base::SingleThreadTaskRunner> session_thread_runner_; 178 scoped_refptr<base::SingleThreadTaskRunner> session_thread_runner_;
168 179
169 // Keeps true if platform dependent initialization is already completed. 180 // Keeps true if platform dependent initialization is already completed.
170 bool initialized_; 181 bool initialized_;
171 182
183 // Keeps false until Finalize() is called.
184 bool finalized_;
185
172 // Keeps the platform dependent initialization result if initialization is 186 // Keeps the platform dependent initialization result if initialization is
173 // completed. Otherwise keeps Result::NOT_INITIALIZED. 187 // completed. Otherwise keeps Result::NOT_INITIALIZED.
174 Result result_; 188 Result result_;
175 189
176 // Keeps all MidiPortInfo. 190 // Keeps all MidiPortInfo.
177 MidiPortInfoList input_ports_; 191 MidiPortInfoList input_ports_;
178 MidiPortInfoList output_ports_; 192 MidiPortInfoList output_ports_;
179 193
180 // Protects access to |clients_|, |pending_clients_|, |initialized_|, 194 // Protects access to |clients_|, |pending_clients_|,
181 // |result_|, |input_ports_| and |output_ports_|. 195 // |session_thread_runner_|, |initialized_|, |finalize_|, |result_|,
196 // |input_ports_| and |output_ports_|.
182 base::Lock lock_; 197 base::Lock lock_;
183 198
184 DISALLOW_COPY_AND_ASSIGN(MidiManager); 199 DISALLOW_COPY_AND_ASSIGN(MidiManager);
185 }; 200 };
186 201
187 } // namespace midi 202 } // namespace midi
188 } // namespace media 203 } // namespace media
189 204
190 #endif // MEDIA_MIDI_MIDI_MANAGER_H_ 205 #endif // MEDIA_MIDI_MIDI_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698