shill: Use WeakPtr in Connection::Bind to avoid use of bare pointers.

BUG=chromium-os:27467
TEST=unit tests

Change-Id: Id27bb26b9a9c1ae1e44ac029b7fa4998faff62bb
Reviewed-on: https://gerrit.chromium.org/gerrit/22463
Commit-Ready: Darin Petkov <petkov@chromium.org>
Reviewed-by: Darin Petkov <petkov@chromium.org>
Tested-by: Darin Petkov <petkov@chromium.org>
diff --git a/connection_unittest.cc b/connection_unittest.cc
index 5349223..4154373 100644
--- a/connection_unittest.cc
+++ b/connection_unittest.cc
@@ -566,7 +566,6 @@
     EXPECT_CALL(target, CallTarget()).Times(1);
     connection = NULL;
   }
-
   {
     // Circular binding of multiple connections should be safe.
     ConnectionRefPtr connection_a = GetNewConnection();
@@ -595,6 +594,23 @@
     AddDestructorExpectations();
     connection_a = NULL;
   }
+  {
+    // Test the weak pointer to the bound Connection. This is not a case that
+    // should occur but the weak pointer should handle it gracefully.
+    DisconnectCallbackTarget target;
+    Connection::Binder binder("test_weak", target.callback());
+    ConnectionRefPtr connection = GetNewConnection();
+    binder.Attach(connection);
+
+    // Make sure the connection doesn't notify the binder on destruction.
+    connection->binders_.clear();
+    AddDestructorExpectations();
+    EXPECT_CALL(target, CallTarget()).Times(0);
+    connection = NULL;
+
+    // Ensure no crash -- the weak pointer to connection should be NULL.
+    binder.Attach(NULL);
+  }
 }
 
 TEST_F(ConnectionTest, OnRouteQueryResponse) {