Home | History | Annotate | Download | only in audio
      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.
      4 
      5 #ifndef MEDIA_AUDIO_SCOPED_LOOP_OBSERVER_H_
      6 #define MEDIA_AUDIO_SCOPED_LOOP_OBSERVER_H_
      7 
      8 #include "base/memory/ref_counted.h"
      9 #include "base/message_loop/message_loop.h"
     10 #include "base/message_loop/message_loop_proxy.h"
     11 
     12 namespace base {
     13 class WaitableEvent;
     14 }
     15 
     16 namespace media {
     17 
     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);
     29 
     30  protected:
     31   virtual ~ScopedLoopObserver();
     32 
     33   // Accessor to the loop that's used by the derived class.
     34   const scoped_refptr<base::MessageLoopProxy>& message_loop() { return loop_; }
     35 
     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);
     40 
     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_;
     44 
     45   DISALLOW_COPY_AND_ASSIGN(ScopedLoopObserver);
     46 };
     47 
     48 }  // namespace media.
     49 
     50 #endif  // MEDIA_AUDIO_SCOPED_LOOP_OBSERVER_H_
     51