Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / components / proximity_auth / bluetooth_connection.h
1 // Copyright 2014 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.
4
5 #ifndef COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_CONNECTION_H
6 #define COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_CONNECTION_H
7
8 #include <string>
9
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/weak_ptr.h"
12 #include "components/proximity_auth/connection.h"
13 #include "device/bluetooth/bluetooth_adapter.h"
14 #include "device/bluetooth/bluetooth_device.h"
15 #include "device/bluetooth/bluetooth_socket.h"
16 #include "device/bluetooth/bluetooth_uuid.h"
17
18 namespace net {
19 class IOBuffer;
20 }
21
22 namespace proximity_auth {
23
24 struct RemoteDevice;
25
26 // Represents a Bluetooth connection with a remote device. The connection is a
27 // persistent bidirectional channel for sending and receiving wire messages.
28 class BluetoothConnection : public Connection,
29                             public device::BluetoothAdapter::Observer {
30  public:
31   // Constructs a Bluetooth connection to the service with |uuid| on the
32   // |remote_device|. The |remote_device| must already be known to the system
33   // Bluetooth daemon.
34   BluetoothConnection(const RemoteDevice& remote_device,
35                       const device::BluetoothUUID& uuid);
36   ~BluetoothConnection() override;
37
38  protected:
39   // Connection:
40   void Connect() override;
41   void Disconnect() override;
42   void SendMessageImpl(scoped_ptr<WireMessage> message) override;
43
44   // BluetoothAdapter::Observer:
45   void DeviceRemoved(device::BluetoothAdapter* adapter,
46                      device::BluetoothDevice* device) override;
47
48  private:
49   // Registers receive callbacks with the backing |socket_|.
50   void StartReceive();
51
52   // Callbacks for asynchronous Bluetooth operations.
53   void OnAdapterInitialized(scoped_refptr<device::BluetoothAdapter> adapter);
54   void OnConnected(scoped_refptr<device::BluetoothSocket> socket);
55   void OnConnectionError(const std::string& error_message);
56   void OnSend(int bytes_sent);
57   void OnSendError(const std::string& error_message);
58   void OnReceive(int bytes_received, scoped_refptr<net::IOBuffer> buffer);
59   void OnReceiveError(device::BluetoothSocket::ErrorReason error_reason,
60                       const std::string& error_message);
61
62   // The UUID (universally unique identifier) of the Bluetooth service on the
63   // remote device that |this| connection should connect to.
64   const device::BluetoothUUID uuid_;
65
66   // The Bluetooth adapter over which this connection is made. Non-null iff
67   // |this| connection is registered as an observer of the |adapter_|.
68   scoped_refptr<device::BluetoothAdapter> adapter_;
69
70   // The Bluetooth socket that backs this connection. NULL iff the connection is
71   // not in a connected state.
72   scoped_refptr<device::BluetoothSocket> socket_;
73
74   // The message that was sent over the backing |socket_|. NULL iff there is no
75   // send operation in progress.
76   scoped_ptr<WireMessage> pending_message_;
77
78   base::WeakPtrFactory<BluetoothConnection> weak_ptr_factory_;
79
80   DISALLOW_COPY_AND_ASSIGN(BluetoothConnection);
81 };
82
83 }  // namespace proximity_auth
84
85 #endif  // COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_CONNECTION_H