OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_SCHEDULER_H_ | 5 #ifndef MEDIA_MIDI_MIDI_SCHEDULER_H_ |
6 #define MEDIA_MIDI_MIDI_SCHEDULER_H_ | 6 #define MEDIA_MIDI_MIDI_SCHEDULER_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 13 #include "base/single_thread_task_runner.h" |
| 14 #include "base/threading/thread_checker.h" |
13 #include "media/midi/midi_export.h" | 15 #include "media/midi/midi_export.h" |
14 | 16 |
15 namespace media { | 17 namespace media { |
16 namespace midi { | 18 namespace midi { |
17 | 19 |
18 class MidiManager; | 20 class MidiManager; |
19 class MidiManagerClient; | 21 class MidiManagerClient; |
20 | 22 |
21 // TODO(crbug.com/467442): Make tasks cancelable per client. | 23 // TODO(crbug.com/467442): Make tasks cancelable per client. |
22 class MIDI_EXPORT MidiScheduler final { | 24 class MIDI_EXPORT MidiScheduler final { |
23 public: | 25 public: |
| 26 // Both constructor and destructor should be run on the same thread. The |
| 27 // instance is bound to the TaskRunner of the constructing thread, on which |
| 28 // InvokeClosure() is run. |
24 explicit MidiScheduler(MidiManager* manager); | 29 explicit MidiScheduler(MidiManager* manager); |
25 ~MidiScheduler(); | 30 ~MidiScheduler(); |
26 | 31 |
27 // Post |closure| to the current message loop safely. The |closure| will not | 32 // Post |closure| to |task_runner_| safely. The |closure| will not be invoked |
28 // be invoked after MidiScheduler is deleted. AccumulateMidiBytesSent() of | 33 // after MidiScheduler is deleted. AccumulateMidiBytesSent() of |client| is |
29 // |client| is called internally. | 34 // called internally. May be called on any thread, but the user should ensure |
| 35 // this method is not called on other threads during/after MidiScheduler |
| 36 // destruction. |
30 void PostSendDataTask(MidiManagerClient* client, | 37 void PostSendDataTask(MidiManagerClient* client, |
31 size_t length, | 38 size_t length, |
32 double timestamp, | 39 double timestamp, |
33 const base::Closure& closure); | 40 const base::Closure& closure); |
34 | 41 |
35 private: | 42 private: |
36 void InvokeClosure(MidiManagerClient* client, | 43 void InvokeClosure(MidiManagerClient* client, |
37 size_t length, | 44 size_t length, |
38 const base::Closure& closure); | 45 const base::Closure& closure); |
39 | 46 |
40 // MidiManager should own the MidiScheduler and be alive longer. | 47 // MidiManager should own the MidiScheduler and be alive longer. |
41 MidiManager* manager_; | 48 MidiManager* manager_; |
| 49 |
| 50 // The TaskRunner of the thread on which the instance is constructed. |
| 51 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 52 |
| 53 // Ensures |weak_factory_| is destructed, and WeakPtrs are dereferenced on the |
| 54 // same thread. |
| 55 base::ThreadChecker thread_checker_; |
| 56 |
42 base::WeakPtrFactory<MidiScheduler> weak_factory_; | 57 base::WeakPtrFactory<MidiScheduler> weak_factory_; |
43 | 58 |
44 DISALLOW_COPY_AND_ASSIGN(MidiScheduler); | 59 DISALLOW_COPY_AND_ASSIGN(MidiScheduler); |
45 }; | 60 }; |
46 | 61 |
47 } // namespace midi | 62 } // namespace midi |
48 } // namespace media | 63 } // namespace media |
49 | 64 |
50 #endif // MEDIA_MIDI_MIDI_SCHEDULER_H_ | 65 #endif // MEDIA_MIDI_MIDI_SCHEDULER_H_ |
OLD | NEW |