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