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 EXTENSIONS_BROWSER_API_BLUETOOTH_SOCKET_BLUETOOTH_API_SOCKET_H_ 6#define EXTENSIONS_BROWSER_API_BLUETOOTH_SOCKET_BLUETOOTH_API_SOCKET_H_ 7 8#include <string> 9 10#include "device/bluetooth/bluetooth_device.h" 11#include "device/bluetooth/bluetooth_socket.h" 12#include "device/bluetooth/bluetooth_uuid.h" 13#include "extensions/browser/api/api_resource.h" 14#include "extensions/browser/api/api_resource_manager.h" 15 16namespace net { 17class IOBuffer; 18} // namespace net 19 20namespace extensions { 21 22// Representation of socket instances from the "bluetooth" namespace, 23// abstracting away platform differences from the underlying BluetoothSocketXxx 24// class. All methods must be called on the |kThreadId| thread. 25class BluetoothApiSocket : public ApiResource { 26 public: 27 enum ErrorReason { kSystemError, kNotConnected, kNotListening, kIOPending, 28 kDisconnected }; 29 30 typedef base::Callback<void(int)> SendCompletionCallback; 31 typedef base::Callback<void(int, scoped_refptr<net::IOBuffer> io_buffer)> 32 ReceiveCompletionCallback; 33 typedef base::Callback<void(const device::BluetoothDevice* device, 34 scoped_refptr<device::BluetoothSocket>)> 35 AcceptCompletionCallback; 36 typedef base::Callback<void(ErrorReason, const std::string& error_message)> 37 ErrorCompletionCallback; 38 39 explicit BluetoothApiSocket(const std::string& owner_extension_id); 40 BluetoothApiSocket(const std::string& owner_extension_id, 41 scoped_refptr<device::BluetoothSocket> socket, 42 const std::string& device_address, 43 const device::BluetoothUUID& uuid); 44 virtual ~BluetoothApiSocket(); 45 46 // Adopts a socket |socket| connected to a device with address 47 // |device_address| using the service with UUID |uuid|. 48 virtual void AdoptConnectedSocket( 49 scoped_refptr<device::BluetoothSocket> socket, 50 const std::string& device_address, 51 const device::BluetoothUUID& uuid); 52 53 // Adopts a socket |socket| listening on a service advertised with UUID 54 // |uuid|. 55 virtual void AdoptListeningSocket( 56 scoped_refptr<device::BluetoothSocket> socket, 57 const device::BluetoothUUID& uuid); 58 59 // Closes the underlying connection. This is a best effort, and never fails. 60 virtual void Disconnect(const base::Closure& callback); 61 62 // Receives data from the socket and calls |success_callback| when data is 63 // available. |count| is maximum amount of bytes received. If an error occurs, 64 // calls |error_callback| with a reason and a message. In particular, if a 65 // |Receive| operation is still pending, |error_callback| will be called with 66 // |kIOPending| error. 67 virtual void Receive(int count, 68 const ReceiveCompletionCallback& success_callback, 69 const ErrorCompletionCallback& error_callback); 70 71 // Sends |buffer| to the socket and calls |success_callback| when data has 72 // been successfully sent. |buffer_size| is the numberof bytes contained in 73 // |buffer|. If an error occurs, calls |error_callback| with a reason and a 74 // message. Calling |Send| multiple times without waiting for the callbacks to 75 // be called is a valid usage, as |buffer| instances are buffered until the 76 // underlying communication channel is available for sending data. 77 virtual void Send(scoped_refptr<net::IOBuffer> buffer, 78 int buffer_size, 79 const SendCompletionCallback& success_callback, 80 const ErrorCompletionCallback& error_callback); 81 82 // Accepts a client connection from the socket and calls |success_callback| 83 // when one has connected. If an error occurs, calls |error_callback| with a 84 // reason and a message. 85 virtual void Accept(const AcceptCompletionCallback& success_callback, 86 const ErrorCompletionCallback& error_callback); 87 88 const std::string& device_address() const { return device_address_; } 89 const device::BluetoothUUID& uuid() const { return uuid_; } 90 91 // Overriden from extensions::ApiResource. 92 virtual bool IsPersistent() const OVERRIDE; 93 94 const std::string* name() const { return name_.get(); } 95 void set_name(const std::string& name) { name_.reset(new std::string(name)); } 96 97 bool persistent() const { return persistent_; } 98 void set_persistent(bool persistent) { persistent_ = persistent; } 99 100 int buffer_size() const { return buffer_size_; } 101 void set_buffer_size(int buffer_size) { buffer_size_ = buffer_size; } 102 103 bool paused() const { return paused_; } 104 void set_paused(bool paused) { paused_ = paused; } 105 106 bool IsConnected() const { return connected_; } 107 108 // Platform specific implementations of |BluetoothSocket| require being called 109 // on the UI thread. 110 static const content::BrowserThread::ID kThreadId = 111 content::BrowserThread::UI; 112 113 private: 114 friend class ApiResourceManager<BluetoothApiSocket>; 115 static const char* service_name() { return "BluetoothApiSocketManager"; } 116 117 static void OnSocketReceiveError( 118 const ErrorCompletionCallback& error_callback, 119 device::BluetoothSocket::ErrorReason reason, 120 const std::string& message); 121 122 static void OnSocketSendError( 123 const ErrorCompletionCallback& error_callback, 124 const std::string& message); 125 126 static void OnSocketAcceptError( 127 const ErrorCompletionCallback& error_callback, 128 const std::string& message); 129 130 // The underlying device socket instance. 131 scoped_refptr<device::BluetoothSocket> socket_; 132 133 // The address of the device this socket is connected to. 134 std::string device_address_; 135 136 // The uuid of the service this socket is connected to. 137 device::BluetoothUUID uuid_; 138 139 // Application-defined string - see bluetooth.idl. 140 scoped_ptr<std::string> name_; 141 142 // Flag indicating whether the socket is left open when the application is 143 // suspended - see bluetooth.idl. 144 bool persistent_; 145 146 // The size of the buffer used to receive data - see bluetooth.idl. 147 int buffer_size_; 148 149 // Flag indicating whether a connected socket blocks its peer from sending 150 // more data - see bluetooth.idl. 151 bool paused_; 152 153 // Flag indicating whether a socket is connected. 154 bool connected_; 155 156 DISALLOW_COPY_AND_ASSIGN(BluetoothApiSocket); 157}; 158 159} // namespace extensions 160 161#endif // EXTENSIONS_BROWSER_API_BLUETOOTH_SOCKET_BLUETOOTH_API_SOCKET_H_ 162