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