Fix potential crash in CreateHBITMAPFromSkBitmap().
[chromium-blink-merge.git] / media / audio / scoped_loop_observer.h
blob7aaab542225314f1638bdea7b84df8f053cf1243
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"
12 namespace base {
13 class WaitableEvent;
16 namespace media {
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 {
26 public:
27 explicit ScopedLoopObserver(
28 const scoped_refptr<base::MessageLoopProxy>& message_loop);
30 protected:
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_; }
36 private:
37 // Call to add or remove ourselves from the list of destruction observers for
38 // the message loop.
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);
48 } // namespace media.
50 #endif // MEDIA_AUDIO_SCOPED_LOOP_OBSERVER_H_