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_LOOP_OBSERVER_H_
6 #define MEDIA_AUDIO_SCOPED_LOOP_OBSERVER_H_
8 #include "base/memory/ref_counted.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/message_loop/message_loop_proxy.h"
18 // A common base class for AudioOutputDevice and AudioInputDevice that manages
19 // a message loop pointer and monitors it for destruction. If the object goes
20 // out of scope before the message loop, the object will automatically remove
21 // itself from the message loop'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 ScopedLoopObserver
25 : public base::MessageLoop::DestructionObserver
{
27 explicit ScopedLoopObserver(
28 const scoped_refptr
<base::MessageLoopProxy
>& message_loop
);
31 virtual ~ScopedLoopObserver();
33 // Accessor to the loop that's used by the derived class.
34 const scoped_refptr
<base::MessageLoopProxy
>& message_loop() { return loop_
; }
37 // Call to add or remove ourselves from the list of destruction observers for
39 void ObserveLoopDestruction(bool enable
, base::WaitableEvent
* done
);
41 // A pointer to the message loop's proxy. In case the loop gets destroyed
42 // before this object goes out of scope, PostTask etc will fail but not crash.
43 scoped_refptr
<base::MessageLoopProxy
> loop_
;
45 DISALLOW_COPY_AND_ASSIGN(ScopedLoopObserver
);
50 #endif // MEDIA_AUDIO_SCOPED_LOOP_OBSERVER_H_