Add in an Owned() wrapper to base::Bind().

This allows expression of ownership of a pointer by a callback. It's basically a "scoped_ptr<>" for Callback where the scope tied to the callback object.

BUG=96118
TEST=new unittests.

Review URL: http://codereview.chromium.org/8209001

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@105622 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/base/bind_unittest.cc b/base/bind_unittest.cc
index acaf562..0fdd2411 100644
--- a/base/bind_unittest.cc
+++ b/base/bind_unittest.cc
@@ -133,6 +133,22 @@
   int* assigns_;
 };
 
+class DeleteCounter {
+ public:
+  explicit DeleteCounter(int* deletes)
+      : deletes_(deletes) {
+  }
+
+  ~DeleteCounter() {
+    (*deletes_)++;
+  }
+
+  void VoidMethod0() {}
+
+ private:
+  int* deletes_;
+};
+
 // Some test functions that we can Bind to.
 template <typename T>
 T PolymorphicIdentity(T t) {
@@ -187,11 +203,6 @@
   return n;
 }
 
-// Only useful in no-compile tests.
-int UnwrapNoRefParentRef(Parent& p) {
-  return p.value;
-}
-
 class BindTest : public ::testing::Test {
  public:
   BindTest() {
@@ -595,6 +606,31 @@
   EXPECT_EQ(0, assigns);
 }
 
+// Test Owned() support.
+TEST_F(BindTest, Owned) {
+  int deletes = 0;
+  DeleteCounter* counter = new DeleteCounter(&deletes);
+
+  // If we don't capture, delete happens on Callback destruction/reset.
+  // return the same value.
+  Callback<DeleteCounter*(void)> no_capture_cb =
+      Bind(&PolymorphicIdentity<DeleteCounter*>, Owned(counter));
+  EXPECT_EQ(counter, no_capture_cb.Run());
+  EXPECT_EQ(counter, no_capture_cb.Run());
+  EXPECT_EQ(0, deletes);
+  no_capture_cb.Reset();  // This should trigger a delete.
+  EXPECT_EQ(1, deletes);
+
+  deletes = 0;
+  counter = new DeleteCounter(&deletes);
+  base::Closure own_object_cb =
+      Bind(&DeleteCounter::VoidMethod0, Owned(counter));
+  own_object_cb.Run();
+  EXPECT_EQ(0, deletes);
+  own_object_cb.Reset();
+  EXPECT_EQ(1, deletes);
+}
+
 // Argument Copy-constructor usage for non-reference parameters.
 //   - Bound arguments are only copied once.
 //   - Forwarded arguments are only copied once.