| 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 #ifndef REMOTING_BASE_SCOPED_THREAD_PROXY_H_ | |
| 6 #define REMOTING_BASE_SCOPED_THREAD_PROXY_H_ | |
| 7 | |
| 8 #include "base/callback_forward.h" | |
| 9 #include "base/message_loop_proxy.h" | |
| 10 | |
| 11 namespace remoting { | |
| 12 | |
| 13 // ScopedThreadProxy is proxy for message loops that cancels all | |
| 14 // pending tasks when it is destroyed. It can be used to post tasks | |
| 15 // for a non-refcounted object. Must be deleted on the thread it | |
| 16 // belongs to. | |
| 17 // | |
| 18 // | |
| 19 // The main difference from WeakPtr<> is that this class can be used safely to | |
| 20 // post tasks from different threads. | |
| 21 // It is similar to WeakHandle<> used in sync: the main difference is | |
| 22 // that WeakHandle<> creates closures itself, while ScopedThreadProxy | |
| 23 // accepts base::Closure instances which caller needs to create using | |
| 24 // base::Bind(). | |
| 25 // | |
| 26 // TODO(sergeyu): Potentially we could use WeakHandle<> instead of | |
| 27 // this class. Consider migrating to WeakHandle<> when it is moved to | |
| 28 // src/base and support for delayed tasks is implemented. | |
| 29 // | |
| 30 // Usage: | |
| 31 // class MyClass { | |
| 32 // public: | |
| 33 // MyClass() | |
| 34 // : thread_proxy_(base::MessageLoopProxy::current()) {} | |
| 35 // | |
| 36 // // Always called on the thread on which this object was created. | |
| 37 // void NonThreadSafeMethod() {} | |
| 38 // | |
| 39 // // Can be called on any thread. | |
| 40 // void ThreadSafeMethod() { | |
| 41 // thread_proxy_.PostTask(FROM_HERE, base::Bind( | |
| 42 // &MyClass::NonThreadSafeMethod, base::Unretained(this))); | |
| 43 // } | |
| 44 // | |
| 45 // private: | |
| 46 // ScopedThreadProxy thread_proxy_; | |
| 47 // }; | |
| 48 class ScopedThreadProxy { | |
| 49 public: | |
| 50 ScopedThreadProxy(base::MessageLoopProxy* message_loop); | |
| 51 ~ScopedThreadProxy(); | |
| 52 | |
| 53 void PostTask(const tracked_objects::Location& from_here, | |
| 54 const base::Closure& closure); | |
| 55 void PostDelayedTask(const tracked_objects::Location& from_here, | |
| 56 const base::Closure& closure, | |
| 57 int64 delay_ms); | |
| 58 | |
| 59 // Cancels all tasks posted via this proxy. Must be called on the | |
| 60 // thread this object belongs to. | |
| 61 void Detach(); | |
| 62 | |
| 63 private: | |
| 64 class Core; | |
| 65 | |
| 66 scoped_refptr<Core> core_; | |
| 67 | |
| 68 DISALLOW_COPY_AND_ASSIGN(ScopedThreadProxy); | |
| 69 }; | |
| 70 | |
| 71 } // namespace remoting | |
| 72 | |
| 73 #endif // REMOTING_BASE_SCOPED_THREAD_PROXY_H_ | |
| OLD | NEW |