1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef MEDIA_AUDIO_SCOPED_TASK_RUNNER_OBSERVER_H_
6 #define MEDIA_AUDIO_SCOPED_TASK_RUNNER_OBSERVER_H_
8 #include "base/memory/ref_counted.h"
9 #include "base/message_loop/message_loop.h"
12 class SingleThreadTaskRunner
;
18 // A common base class for AudioOutputDevice and AudioInputDevice that manages
19 // a task runner and monitors it for destruction. If the object goes out of
20 // scope before the task runner, the object will automatically remove itself
21 // from the task runner's list of destruction observers.
22 // NOTE: The class that inherits from this class must implement the
23 // WillDestroyCurrentMessageLoop virtual method from DestructionObserver.
24 class ScopedTaskRunnerObserver
25 : public base::MessageLoop::DestructionObserver
{
27 explicit ScopedTaskRunnerObserver(
28 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
);
31 ~ScopedTaskRunnerObserver() override
;
33 // Accessor to the loop that's used by the derived class.
34 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner() {
39 // Call to add or remove ourselves from the list of destruction observers for
41 void ObserveLoopDestruction(bool enable
, base::WaitableEvent
* done
);
43 // A pointer to the task runner. In case it gets destroyed before this object
44 // goes out of scope, PostTask() etc will fail but not crash.
45 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
47 DISALLOW_COPY_AND_ASSIGN(ScopedTaskRunnerObserver
);
52 #endif // MEDIA_AUDIO_SCOPED_TASK_RUNNER_OBSERVER_H_