OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 "remoting/base/scoped_thread_proxy.h" | |
6 | |
7 #include "base/bind.h" | |
8 | |
9 namespace remoting { | |
10 | |
11 class ScopedThreadProxy::Core : public base::RefCountedThreadSafe<Core> { | |
12 public: | |
13 Core(base::MessageLoopProxy* message_loop) | |
14 : message_loop_(message_loop), | |
15 canceled_(false) { | |
16 } | |
17 | |
18 void PostTask(const tracked_objects::Location& from_here, | |
19 const base::Closure& closure) { | |
20 if (!canceled_) | |
21 message_loop_->PostTask(from_here, Wrap(closure)); | |
22 } | |
23 | |
24 void PostDelayedTask( | |
25 const tracked_objects::Location& from_here, | |
26 const base::Closure& closure, | |
27 int64 delay_ms) { | |
28 if (!canceled_) { | |
29 message_loop_->PostDelayedTask(from_here, Wrap(closure), delay_ms); | |
30 } | |
31 } | |
32 | |
33 void Detach() { | |
34 DCHECK(message_loop_->BelongsToCurrentThread()); | |
35 canceled_ = true; | |
36 } | |
37 | |
38 private: | |
39 friend class base::RefCountedThreadSafe<Core>; | |
40 | |
41 ~Core() { | |
42 DCHECK(canceled_); | |
43 } | |
44 | |
45 base::Closure Wrap(const base::Closure& closure) { | |
46 return base::Bind(&Core::CallClosure, this, closure); | |
47 } | |
48 | |
49 void CallClosure(const base::Closure& closure) { | |
50 DCHECK(message_loop_->BelongsToCurrentThread()); | |
51 | |
52 if (!canceled_) | |
53 closure.Run(); | |
54 } | |
55 | |
56 scoped_refptr<base::MessageLoopProxy> message_loop_; | |
57 bool canceled_; | |
58 | |
59 DISALLOW_COPY_AND_ASSIGN(Core); | |
60 }; | |
61 | |
62 ScopedThreadProxy::ScopedThreadProxy(base::MessageLoopProxy* message_loop) | |
63 : core_(new Core(message_loop)) { | |
64 } | |
65 | |
66 ScopedThreadProxy::~ScopedThreadProxy() { | |
67 Detach(); | |
68 } | |
69 | |
70 void ScopedThreadProxy::PostTask(const tracked_objects::Location& from_here, | |
71 const base::Closure& closure) { | |
72 core_->PostTask(from_here, closure); | |
73 } | |
74 | |
75 void ScopedThreadProxy::PostDelayedTask( | |
76 const tracked_objects::Location& from_here, | |
77 const base::Closure& closure, | |
78 int64 delay_ms) { | |
79 core_->PostDelayedTask(from_here, closure, delay_ms); | |
80 } | |
81 | |
82 void ScopedThreadProxy::Detach() { | |
83 core_->Detach(); | |
84 } | |
85 | |
86 } // namespace remoting | |
OLD | NEW |