summaryrefslogtreecommitdiffstats
path: root/remoting/base
diff options
context:
space:
mode:
authorsergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-09-29 20:53:06 +0000
committersergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-09-29 20:53:06 +0000
commit45214d2a1d9e942946a21ae1ff0a1d7b7cc848f1 (patch)
tree0333d94301d42dc4820ef15d5ccaa35bcbef2f73 /remoting/base
parentcff62a0b8d82241c276f7aa733742539329a1160 (diff)
downloadchromium_src-45214d2a1d9e942946a21ae1ff0a1d7b7cc848f1.zip
chromium_src-45214d2a1d9e942946a21ae1ff0a1d7b7cc848f1.tar.gz
chromium_src-45214d2a1d9e942946a21ae1ff0a1d7b7cc848f1.tar.bz2
Replace TaskThreadProxy with non-refcounted ScopedThreadProxy.
BUG=None TEST=Unittests. Review URL: http://codereview.chromium.org/7906020 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@103343 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'remoting/base')
-rw-r--r--remoting/base/scoped_thread_proxy.cc84
-rw-r--r--remoting/base/scoped_thread_proxy.h73
-rw-r--r--remoting/base/task_thread_proxy.cc34
-rw-r--r--remoting/base/task_thread_proxy.h50
4 files changed, 157 insertions, 84 deletions
diff --git a/remoting/base/scoped_thread_proxy.cc b/remoting/base/scoped_thread_proxy.cc
new file mode 100644
index 0000000..b526d6af
--- /dev/null
+++ b/remoting/base/scoped_thread_proxy.cc
@@ -0,0 +1,84 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "remoting/base/scoped_thread_proxy.h"
+
+namespace remoting {
+
+class ScopedThreadProxy::Core : public base::RefCountedThreadSafe<Core> {
+ public:
+ Core(base::MessageLoopProxy* message_loop)
+ : message_loop_(message_loop),
+ canceled_(false) {
+ }
+
+ void PostTask(const tracked_objects::Location& from_here,
+ const base::Closure& closure) {
+ if (!canceled_)
+ message_loop_->PostTask(from_here, Wrap(closure));
+ }
+
+ void PostDelayedTask(
+ const tracked_objects::Location& from_here,
+ const base::Closure& closure,
+ int64 delay_ms) {
+ if (!canceled_) {
+ message_loop_->PostDelayedTask(from_here, Wrap(closure), delay_ms);
+ }
+ }
+
+ void Detach() {
+ DCHECK(message_loop_->BelongsToCurrentThread());
+ canceled_ = true;
+ }
+
+ private:
+ friend class base::RefCountedThreadSafe<Core>;
+
+ ~Core() {
+ DCHECK(canceled_);
+ }
+
+ base::Closure Wrap(const base::Closure& closure) {
+ return base::Bind(&Core::CallClosure, this, closure);
+ }
+
+ void CallClosure(const base::Closure& closure) {
+ DCHECK(message_loop_->BelongsToCurrentThread());
+
+ if (!canceled_)
+ closure.Run();
+ }
+
+ scoped_refptr<base::MessageLoopProxy> message_loop_;
+ bool canceled_;
+
+ DISALLOW_COPY_AND_ASSIGN(Core);
+};
+
+ScopedThreadProxy::ScopedThreadProxy(base::MessageLoopProxy* message_loop)
+ : core_(new Core(message_loop)) {
+}
+
+ScopedThreadProxy::~ScopedThreadProxy() {
+ Detach();
+}
+
+void ScopedThreadProxy::PostTask(const tracked_objects::Location& from_here,
+ const base::Closure& closure) {
+ core_->PostTask(from_here, closure);
+}
+
+void ScopedThreadProxy::PostDelayedTask(
+ const tracked_objects::Location& from_here,
+ const base::Closure& closure,
+ int64 delay_ms) {
+ core_->PostDelayedTask(from_here, closure, delay_ms);
+}
+
+void ScopedThreadProxy::Detach() {
+ core_->Detach();
+}
+
+} // namespace remoting
diff --git a/remoting/base/scoped_thread_proxy.h b/remoting/base/scoped_thread_proxy.h
new file mode 100644
index 0000000..63dcaf8
--- /dev/null
+++ b/remoting/base/scoped_thread_proxy.h
@@ -0,0 +1,73 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef REMOTING_BASE_SCOPED_THREAD_PROXY_H_
+#define REMOTING_BASE_SCOPED_THREAD_PROXY_H_
+
+#include "base/bind.h"
+#include "base/message_loop_proxy.h"
+
+namespace remoting {
+
+// ScopedThreadProxy is proxy for message loops that cancels all
+// pending tasks when it is destroyed. It can be used to post tasks
+// for a non-refcounted object. Must be deleted on the thread it
+// belongs to.
+//
+// The main difference from WeakPtr<> and ScopedRunnableMethodFactory<>
+// is that this class can be used safely to post tasks from different
+// threads.
+// It is similar to WeakHandle<> used in sync: the main difference is
+// that WeakHandle<> creates closures itself, while ScopedThreadProxy
+// accepts base::Closure instances which caller needs to create using
+// base::Bind().
+//
+// TODO(sergeyu): Potentially we could use WeakHandle<> instead of
+// this class. Consider migrating to WeakHandle<> when it is moved to
+// src/base and support for delayed tasks is implemented.
+//
+// Usage:
+// class MyClass {
+// public:
+// MyClass()
+// : thread_proxy_(base::MessageLoopProxy::current()) {}
+//
+// // Always called on the thread on which this object was created.
+// void NonThreadSafeMethod() {}
+//
+// // Can be called on any thread.
+// void ThreadSafeMethod() {
+// thread_proxy_.PostTask(FROM_HERE, base::Bind(
+// &MyClass::NonThreadSafeMethod, base::Unretained(this)));
+// }
+//
+// private:
+// ScopedThreadProxy thread_proxy_;
+// };
+class ScopedThreadProxy {
+ public:
+ ScopedThreadProxy(base::MessageLoopProxy* message_loop);
+ ~ScopedThreadProxy();
+
+ void PostTask(const tracked_objects::Location& from_here,
+ const base::Closure& closure);
+ void PostDelayedTask(const tracked_objects::Location& from_here,
+ const base::Closure& closure,
+ int64 delay_ms);
+
+ // Cancels all tasks posted via this proxy. Must be called on the
+ // thread this object belongs to.
+ void Detach();
+
+ private:
+ class Core;
+
+ scoped_refptr<Core> core_;
+
+ DISALLOW_COPY_AND_ASSIGN(ScopedThreadProxy);
+};
+
+} // namespace remoting
+
+#endif // REMOTING_BASE_SCOPED_THREAD_PROXY_H_
diff --git a/remoting/base/task_thread_proxy.cc b/remoting/base/task_thread_proxy.cc
deleted file mode 100644
index d379620..0000000
--- a/remoting/base/task_thread_proxy.cc
+++ /dev/null
@@ -1,34 +0,0 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "remoting/base/task_thread_proxy.h"
-
-#include "base/bind.h"
-
-namespace remoting {
-
-TaskThreadProxy::TaskThreadProxy(MessageLoop* loop)
- : message_loop_(loop) {
-}
-
-TaskThreadProxy::~TaskThreadProxy() {
-}
-
-void TaskThreadProxy::Detach() {
- message_loop_ = NULL;
-}
-
-void TaskThreadProxy::Call(const base::Closure& closure) {
- if (message_loop_) {
- message_loop_->PostTask(FROM_HERE, NewRunnableMethod(
- this, &TaskThreadProxy::CallClosure, closure));
- }
-}
-
-void TaskThreadProxy::CallClosure(const base::Closure& closure) {
- if (message_loop_)
- closure.Run();
-}
-
-} // namespace remoting
diff --git a/remoting/base/task_thread_proxy.h b/remoting/base/task_thread_proxy.h
deleted file mode 100644
index 67ccc2d..0000000
--- a/remoting/base/task_thread_proxy.h
+++ /dev/null
@@ -1,50 +0,0 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef REMOTING_BASE_TASK_THREAD_PROXY_H_
-#define REMOTING_BASE_TASK_THREAD_PROXY_H_
-
-#include "base/message_loop.h"
-
-namespace remoting {
-
-// This is a refcounted class that is used to switch to the appropriate thread
-// before running a task on a target object. It should be used whenever you
-// need to post to an object, but:
-// (1) You don't know when the object might be deleted, and
-// (2) You cannot subclass the target from RefCountedThreadSafe.
-//
-// Example usage:
-// Instead of:
-// MyClass* obj;
-// obj->Method(param);
-// Use:
-// proxy->Call(base::Bind(&MyClass::Method,
-// base::Unretained(obj),
-// param);
-class TaskThreadProxy : public base::RefCountedThreadSafe<TaskThreadProxy> {
- public:
- TaskThreadProxy(MessageLoop* loop);
-
- // Detach should be called when the target of the posted task is being
- // destroyed.
- void Detach();
-
- void Call(const base::Closure& closure);
-
- private:
- friend class base::RefCountedThreadSafe<TaskThreadProxy>;
-
- virtual ~TaskThreadProxy();
-
- void CallClosure(const base::Closure& closure);
-
- MessageLoop* message_loop_;
-
- DISALLOW_COPY_AND_ASSIGN(TaskThreadProxy);
-};
-
-} // namespace remoting
-
-#endif // REMOTING_BASE_TASK_THREAD_PROXY_H_