Relanding addProfile/removeProfile patch with heapchecker fix.

This is a relanding of r197554 with heapchecker fix.

Some profile-dependent services depend on UI thread to clean up, so I had to add
UI message loop in bluetooth_event_router_unittest.cc to make sure that all the
objects are released at the end of test.

I verified with valgrind that this patch does not introduce any new memory leak.

BUG=229636

Review URL: https://chromiumcodereview.appspot.com/14569007

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@197741 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/device/bluetooth/bluetooth_profile.h b/device/bluetooth/bluetooth_profile.h
index 466bdf2a..46200b3 100644
--- a/device/bluetooth/bluetooth_profile.h
+++ b/device/bluetooth/bluetooth_profile.h
@@ -12,8 +12,10 @@
 
 namespace device {
 
+class BluetoothDevice;
 class BluetoothProfileMac;
 class BluetoothSocket;
+class MockBluetoothProfile;
 
 // BluetoothProfile represents an implementation of either a client or server
 // of a particular specified profile (aka service or protocol in other
@@ -92,11 +94,14 @@
   // The socket will be closed when all references are released; none of the
   // BluetoothProfile, or BluetoothAdapter or BluetoothDevice objects are
   // guaranteed to hold a reference so this may outlive all of them.
-  typedef base::Callback<void(scoped_refptr<BluetoothSocket>)> SocketCallback;
-  virtual void SetConnectionCallback(const SocketCallback& callback) = 0;
+  typedef base::Callback<void(
+      const BluetoothDevice*,
+      scoped_refptr<BluetoothSocket>)> ConnectionCallback;
+  virtual void SetConnectionCallback(const ConnectionCallback& callback) = 0;
 
  private:
   friend class BluetoothProfileMac;
+  friend class MockBluetoothProfile;
 
   BluetoothProfile();
   virtual ~BluetoothProfile();
diff --git a/device/bluetooth/bluetooth_profile_mac.h b/device/bluetooth/bluetooth_profile_mac.h
index 34b6b34..0085efde 100644
--- a/device/bluetooth/bluetooth_profile_mac.h
+++ b/device/bluetooth/bluetooth_profile_mac.h
@@ -23,7 +23,8 @@
  public:
   // BluetoothProfile override.
   virtual void Unregister() OVERRIDE;
-  virtual void SetConnectionCallback(const SocketCallback& callback) OVERRIDE;
+  virtual void SetConnectionCallback(
+      const ConnectionCallback& callback) OVERRIDE;
 
   // Makes an outgoing connection to |device|.
   // This method runs |socket_callback_| with the socket and returns true if the
@@ -38,7 +39,7 @@
 
   const std::string uuid_;
   const std::string name_;
-  SocketCallback socket_callback_;
+  ConnectionCallback connection_callback_;
 };
 
 }  // namespace device
diff --git a/device/bluetooth/bluetooth_profile_mac.mm b/device/bluetooth/bluetooth_profile_mac.mm
index ca50f06..c0658a9 100644
--- a/device/bluetooth/bluetooth_profile_mac.mm
+++ b/device/bluetooth/bluetooth_profile_mac.mm
@@ -59,8 +59,8 @@
 }
 
 void BluetoothProfileMac::SetConnectionCallback(
-    const SocketCallback& callback) {
-  socket_callback_ = callback;
+    const ConnectionCallback& callback) {
+  connection_callback_ = callback;
 }
 
 bool BluetoothProfileMac::Connect(IOBluetoothDevice* device) {
@@ -70,7 +70,8 @@
     scoped_refptr<BluetoothSocket> socket(
         BluetoothSocketMac::CreateBluetoothSocket(record));
     if (socket.get() != NULL) {
-      socket_callback_.Run(socket);
+      BluetoothDeviceMac device_mac(device);
+      connection_callback_.Run(&device_mac, socket);
       return true;
     }
   }
diff --git a/device/bluetooth/test/mock_bluetooth_profile.cc b/device/bluetooth/test/mock_bluetooth_profile.cc
new file mode 100644
index 0000000..bf2af214
--- /dev/null
+++ b/device/bluetooth/test/mock_bluetooth_profile.cc
@@ -0,0 +1,15 @@
+// Copyright 2013 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 "device/bluetooth/test/mock_bluetooth_profile.h"
+
+namespace device {
+
+MockBluetoothProfile::MockBluetoothProfile() {
+}
+
+MockBluetoothProfile::~MockBluetoothProfile() {
+}
+
+}  // namespace device
diff --git a/device/bluetooth/test/mock_bluetooth_profile.h b/device/bluetooth/test/mock_bluetooth_profile.h
new file mode 100644
index 0000000..92b7505
--- /dev/null
+++ b/device/bluetooth/test/mock_bluetooth_profile.h
@@ -0,0 +1,25 @@
+// Copyright 2013 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 DEVICE_BLUETOOTH_TEST_MOCK_BLUETOOTH_PROFILE_H_
+#define DEVICE_BLUETOOTH_TEST_MOCK_BLUETOOTH_PROFILE_H_
+
+#include "device/bluetooth/bluetooth_profile.h"
+#include "testing/gmock/include/gmock/gmock.h"
+
+namespace device {
+
+class MockBluetoothProfile : public BluetoothProfile {
+ public:
+  MockBluetoothProfile();
+  virtual ~MockBluetoothProfile();
+
+  MOCK_METHOD0(Unregister, void());
+  MOCK_METHOD1(SetConnectionCallback,
+               void(const BluetoothProfile::ConnectionCallback&));
+};
+
+}  // namespace device
+
+#endif  // DEVICE_BLUETOOTH_TEST_MOCK_BLUETOOTH_PROFILE_H_
diff --git a/device/device.gyp b/device/device.gyp
index 3f3d010c..6afb8a0 100644
--- a/device/device.gyp
+++ b/device/device.gyp
@@ -110,6 +110,8 @@
         'bluetooth/test/mock_bluetooth_adapter.h',
         'bluetooth/test/mock_bluetooth_device.cc',
         'bluetooth/test/mock_bluetooth_device.h',
+        'bluetooth/test/mock_bluetooth_profile.cc',
+        'bluetooth/test/mock_bluetooth_profile.h',
         'bluetooth/test/mock_bluetooth_socket.cc',
         'bluetooth/test/mock_bluetooth_socket.h',
       ],