Implement nacl_irt_memory for non-sfi mode.
[chromium-blink-merge.git] / device / bluetooth / bluetooth_profile_mac.mm
blob4956651eac6cd0fdae94863caed6478482fed589
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "device/bluetooth/bluetooth_profile_mac.h"
7 #import <IOBluetooth/objc/IOBluetoothDevice.h>
8 #import <IOBluetooth/objc/IOBluetoothSDPServiceRecord.h>
9 #import <IOBluetooth/objc/IOBluetoothSDPUUID.h>
11 #include <string>
12 #include <vector>
14 #include "base/basictypes.h"
15 #include "base/logging.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/strings/string_number_conversions.h"
18 #include "device/bluetooth/bluetooth_device_mac.h"
19 #include "device/bluetooth/bluetooth_socket_mac.h"
21 namespace {
23 // Converts |uuid| to a IOBluetoothSDPUUID instance.
25 // |uuid| must be in the format of XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX.
26 IOBluetoothSDPUUID* GetIOBluetoothSDPUUID(const std::string& uuid) {
27   DCHECK(uuid.size() == 36);
28   DCHECK(uuid[8] == '-');
29   DCHECK(uuid[13] == '-');
30   DCHECK(uuid[18] == '-');
31   DCHECK(uuid[23] == '-');
32   std::string numbers_only = uuid;
33   numbers_only.erase(23, 1);
34   numbers_only.erase(18, 1);
35   numbers_only.erase(13, 1);
36   numbers_only.erase(8, 1);
37   std::vector<uint8> uuid_bytes_vector;
38   base::HexStringToBytes(numbers_only, &uuid_bytes_vector);
39   DCHECK(uuid_bytes_vector.size() == 16);
41   return [IOBluetoothSDPUUID uuidWithBytes:&uuid_bytes_vector[0]
42                                     length:uuid_bytes_vector.size()];
45 }  // namespace
47 namespace device {
49 BluetoothProfileMac::BluetoothProfileMac(const std::string& uuid,
50                                          const std::string& name)
51     : BluetoothProfile(), uuid_(uuid), name_(name) {
54 BluetoothProfileMac::~BluetoothProfileMac() {
57 void BluetoothProfileMac::Unregister() {
58   delete this;
61 void BluetoothProfileMac::SetConnectionCallback(
62     const ConnectionCallback& callback) {
63   connection_callback_ = callback;
66 bool BluetoothProfileMac::Connect(IOBluetoothDevice* device) {
67   if (connection_callback_.is_null())
68     return false;
70   IOBluetoothSDPServiceRecord* record =
71       [device getServiceRecordForUUID:GetIOBluetoothSDPUUID(uuid_)];
72   if (record != nil) {
73     scoped_refptr<BluetoothSocket> socket(
74         BluetoothSocketMac::CreateBluetoothSocket(record));
75     if (socket.get() != NULL) {
76       BluetoothDeviceMac device_mac(device);
77       connection_callback_.Run(&device_mac, socket);
78       return true;
79     }
80   }
81   return false;
84 }  // namespace device