Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(483)

Unified Diff: base/message_loop_proxy.h

Issue 7210053: Implementation of PostTaskAndReply() in MessageLoopProxy and BrowserThread. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: copyright Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/message_loop.h ('k') | base/message_loop_proxy.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/message_loop_proxy.h
diff --git a/base/message_loop_proxy.h b/base/message_loop_proxy.h
index 9a856bf65ae3c5cecfb8c824344515aec340e2b6..decaa38f86bfe454d0a7f8857f5599e6ba2fbf87 100644
--- a/base/message_loop_proxy.h
+++ b/base/message_loop_proxy.h
@@ -70,6 +70,51 @@ class BASE_EXPORT MessageLoopProxy
// this proxy represents.
virtual bool BelongsToCurrentThread() = 0;
+ // Executes |task| on the given MessageLoopProxy. On completion, |reply|
+ // is passed back to the MessageLoopProxy for the thread that called
+ // PostTaskAndReply(). Both |task| and |reply| are guaranteed to be deleted
+ // on the thread from which PostTaskAndReply() is invoked. This allows
+ // objects that must be deleted on the originating thread to be bound into the
+ // |task| and |reply| Closures. In particular, it can be useful to use
+ // WeakPtr<> in the |reply| Closure so that the reply operation can be
+ // canceled. See the following pseudo-code:
+ //
+ // class DataBuffer : public RefCountedThreadSafe<DataBuffer> {
+ // public:
+ // // Called to add data into a buffer.
+ // void AddData(void* buf, size_t length);
+ // ...
+ // };
+ //
+ //
+ // class DataLoader : public SupportsWeakPtr<ReadToBuffer> {
+ // public:
+ // void GetData() {
+ // scoped_refptr<DataBuffer> buffer = new DataBuffer();
+ // target_thread_.message_loop_proxy()->PostTaskAndReply(
+ // FROM_HERE,
+ // base::Bind(&DataBuffer::AddData, buffer),
+ // base::Bind(&DataLoader::OnDataReceived, AsWeakPtr(), buffer));
+ // }
+ //
+ // private:
+ // void OnDataReceived(scoped_refptr<DataBuffer> buffer) {
+ // // Do something with buffer.
+ // }
+ // };
+ //
+ //
+ // Things to notice:
+ // * Results of |task| are shared with |reply| by binding a shared argument
+ // (a DataBuffer instance).
+ // * The DataLoader object has no special thread safety.
+ // * The DataLoader object can be deleted while |task| is still running,
+ // and the reply will cancel itself safely because it is bound to a
+ // WeakPtr<>.
+ bool PostTaskAndReply(const tracked_objects::Location& from_here,
+ const Closure& task,
+ const Closure& reply);
+
template <class T>
bool DeleteSoon(const tracked_objects::Location& from_here,
T* object) {
« no previous file with comments | « base/message_loop.h ('k') | base/message_loop_proxy.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698