| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_SERVICE_H_ | 5 #ifndef MEDIA_MIDI_MIDI_SERVICE_H_ |
| 6 #define MEDIA_MIDI_MIDI_SERVICE_H_ | 6 #define MEDIA_MIDI_MIDI_SERVICE_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
| 16 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
| 17 #include "base/threading/thread.h" | 17 #include "base/threading/thread.h" |
| 18 #include "media/midi/midi_export.h" | 18 #include "media/midi/midi_export.h" |
| 19 #include "media/midi/midi_manager.h" | 19 #include "media/midi/midi_manager.h" |
| 20 | 20 |
| 21 namespace midi { | 21 namespace midi { |
| 22 | 22 |
| 23 class TaskService; |
| 24 |
| 23 // Manages MidiManager backends. This class expects to be constructed and | 25 // Manages MidiManager backends. This class expects to be constructed and |
| 24 // destructed on the browser main thread, but methods can be called on both | 26 // destructed on the browser main thread, but methods can be called on both |
| 25 // the main thread and the I/O thread. | 27 // the main thread and the I/O thread. |
| 26 class MIDI_EXPORT MidiService final { | 28 class MIDI_EXPORT MidiService final { |
| 27 public: | 29 public: |
| 28 // Use the first constructor for production code. | 30 // Use the first constructor for production code. |
| 29 MidiService(); | 31 MidiService(); |
| 30 // |MidiManager| can be explicitly specified in the constructor for testing. | 32 // |MidiManager| can be explicitly specified in the constructor for testing. |
| 31 explicit MidiService(std::unique_ptr<MidiManager> manager); | 33 explicit MidiService(std::unique_ptr<MidiManager> manager); |
| 32 ~MidiService(); | 34 ~MidiService(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 47 const std::vector<uint8_t>& data, | 49 const std::vector<uint8_t>& data, |
| 48 double timestamp); | 50 double timestamp); |
| 49 | 51 |
| 50 // Returns a SingleThreadTaskRunner reference to serve MidiManager. Each | 52 // Returns a SingleThreadTaskRunner reference to serve MidiManager. Each |
| 51 // TaskRunner will be constructed on demand. | 53 // TaskRunner will be constructed on demand. |
| 52 // MidiManager that supports the dynamic instantiation feature will use this | 54 // MidiManager that supports the dynamic instantiation feature will use this |
| 53 // method to post tasks that should not run on I/O. Since TaskRunners outlive | 55 // method to post tasks that should not run on I/O. Since TaskRunners outlive |
| 54 // MidiManager, each task should ensure that MidiManager that posted the task | 56 // MidiManager, each task should ensure that MidiManager that posted the task |
| 55 // is still alive while accessing |this|. TaskRunners will be reused when | 57 // is still alive while accessing |this|. TaskRunners will be reused when |
| 56 // another MidiManager is instantiated. | 58 // another MidiManager is instantiated. |
| 59 // TODO(toyoshim): Remove this interface. |
| 57 scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner(size_t runner_id); | 60 scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner(size_t runner_id); |
| 58 | 61 |
| 62 // Obtains a TaskService that lives with MidiService. |
| 63 TaskService* task_service() { return task_service_.get(); } |
| 64 |
| 59 private: | 65 private: |
| 60 // Holds MidiManager instance. If the dynamic instantiation feature is | 66 // Holds MidiManager instance. If the dynamic instantiation feature is |
| 61 // enabled, the MidiManager would be constructed and destructed on the I/O | 67 // enabled, the MidiManager would be constructed and destructed on the I/O |
| 62 // thread, and all MidiManager methods would be called on the I/O thread. | 68 // thread, and all MidiManager methods would be called on the I/O thread. |
| 63 std::unique_ptr<MidiManager> manager_; | 69 std::unique_ptr<MidiManager> manager_; |
| 64 | 70 |
| 71 // Holds TaskService instance. |
| 72 std::unique_ptr<TaskService> task_service_; |
| 73 |
| 65 // TaskRunner to destruct |manager_| on the right thread. | 74 // TaskRunner to destruct |manager_| on the right thread. |
| 66 scoped_refptr<base::SingleThreadTaskRunner> manager_destructor_runner_; | 75 scoped_refptr<base::SingleThreadTaskRunner> manager_destructor_runner_; |
| 67 | 76 |
| 68 // A flag to indicate if the dynamic instantiation feature is supported and | 77 // A flag to indicate if the dynamic instantiation feature is supported and |
| 69 // actually enabled. | 78 // actually enabled. |
| 70 const bool is_dynamic_instantiation_enabled_; | 79 const bool is_dynamic_instantiation_enabled_; |
| 71 | 80 |
| 72 // Counts active clients to manage dynamic MidiManager instantiation. | 81 // Counts active clients to manage dynamic MidiManager instantiation. |
| 73 size_t active_clients_; | 82 size_t active_clients_; |
| 74 | 83 |
| 75 // Protects all members above. | 84 // Protects all members above. |
| 76 base::Lock lock_; | 85 base::Lock lock_; |
| 77 | 86 |
| 78 // Threads to host SingleThreadTaskRunners. | 87 // Threads to host SingleThreadTaskRunners. |
| 79 std::vector<std::unique_ptr<base::Thread>> threads_; | 88 std::vector<std::unique_ptr<base::Thread>> threads_; |
| 80 | 89 |
| 81 // Protects |threads_|. | 90 // Protects |threads_|. |
| 82 base::Lock threads_lock_; | 91 base::Lock threads_lock_; |
| 83 | 92 |
| 84 DISALLOW_COPY_AND_ASSIGN(MidiService); | 93 DISALLOW_COPY_AND_ASSIGN(MidiService); |
| 85 }; | 94 }; |
| 86 | 95 |
| 87 } // namespace midi | 96 } // namespace midi |
| 88 | 97 |
| 89 #endif // MEDIA_MIDI_MIDI_SERVICE_H_ | 98 #endif // MEDIA_MIDI_MIDI_SERVICE_H_ |
| OLD | NEW |