OLD | NEW |
(Empty) | |
| 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 #include "content/renderer/media/scoped_loop_observer.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/synchronization/waitable_event.h" |
| 9 |
| 10 ScopedLoopObserver::ScopedLoopObserver(MessageLoop* loop) |
| 11 : loop_(loop->message_loop_proxy()) { |
| 12 ObserveLoopDestruction(true, NULL); |
| 13 } |
| 14 |
| 15 ScopedLoopObserver::~ScopedLoopObserver() { |
| 16 ObserveLoopDestruction(false, NULL); |
| 17 } |
| 18 |
| 19 void ScopedLoopObserver::ObserveLoopDestruction(bool enable, |
| 20 base::WaitableEvent* done) { |
| 21 // Note: |done| may be NULL. |
| 22 if (loop_->BelongsToCurrentThread()) { |
| 23 MessageLoop* loop = MessageLoop::current(); |
| 24 if (enable) { |
| 25 loop->AddDestructionObserver(this); |
| 26 } else { |
| 27 loop->RemoveDestructionObserver(this); |
| 28 } |
| 29 } else { |
| 30 base::WaitableEvent event(false, false); |
| 31 if (loop_->PostTask(FROM_HERE, |
| 32 base::Bind(&ScopedLoopObserver::ObserveLoopDestruction, |
| 33 base::Unretained(this), enable, &event))) { |
| 34 event.Wait(); |
| 35 } else { |
| 36 // The message loop's thread has already terminated, so no need to wait. |
| 37 } |
| 38 } |
| 39 |
| 40 if (done) |
| 41 done->Signal(); |
| 42 } |
OLD | NEW |