Enable Enterprise enrollment on desktop builds.
[chromium-blink-merge.git] / chrome / browser / extensions / api / bluetooth / bluetooth_api_socket.cc
blob2be7f3e7244c69fcd8f9ada849074c8af3c4636e
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.
5 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_socket.h"
7 #include "device/bluetooth/bluetooth_socket.h"
8 #include "net/base/io_buffer.h"
10 namespace {
12 const char kSocketNotConnectedError[] = "Socket not connected";
14 } // namespace
16 namespace extensions {
18 // static
19 static base::LazyInstance<
20 BrowserContextKeyedAPIFactory<ApiResourceManager<BluetoothApiSocket> > >
21 g_server_factory = LAZY_INSTANCE_INITIALIZER;
23 // static
24 template <>
25 BrowserContextKeyedAPIFactory<ApiResourceManager<BluetoothApiSocket> >*
26 ApiResourceManager<BluetoothApiSocket>::GetFactoryInstance() {
27 return g_server_factory.Pointer();
30 BluetoothApiSocket::BluetoothApiSocket(const std::string& owner_extension_id)
31 : ApiResource(owner_extension_id),
32 persistent_(false),
33 buffer_size_(0),
34 paused_(false),
35 connected_(false) {
36 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
39 BluetoothApiSocket::BluetoothApiSocket(
40 const std::string& owner_extension_id,
41 scoped_refptr<device::BluetoothSocket> socket,
42 const std::string& device_address,
43 const device::BluetoothUUID& uuid)
44 : ApiResource(owner_extension_id),
45 socket_(socket),
46 device_address_(device_address),
47 uuid_(uuid),
48 persistent_(false),
49 buffer_size_(0),
50 paused_(true),
51 connected_(true) {
52 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
55 BluetoothApiSocket::~BluetoothApiSocket() {
56 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
57 if (socket_.get())
58 socket_->Close();
61 void BluetoothApiSocket::Disconnect(const base::Closure& success_callback) {
62 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
64 if (!socket_.get() || !IsConnected()) {
65 success_callback.Run();
66 return;
69 socket_->Disconnect(success_callback);
72 bool BluetoothApiSocket::IsPersistent() const {
73 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
74 return persistent_;
77 void BluetoothApiSocket::Receive(
78 int count,
79 const ReceiveCompletionCallback& success_callback,
80 const ErrorCompletionCallback& error_callback) {
81 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
83 if (!socket_.get() || !IsConnected()) {
84 error_callback.Run(BluetoothApiSocket::kNotConnected,
85 kSocketNotConnectedError);
86 return;
89 socket_->Receive(count,
90 success_callback,
91 base::Bind(&OnSocketReceiveError, error_callback));
94 // static
95 void BluetoothApiSocket::OnSocketReceiveError(
96 const ErrorCompletionCallback& error_callback,
97 device::BluetoothSocket::ErrorReason reason,
98 const std::string& message) {
99 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
100 BluetoothApiSocket::ErrorReason error_reason;
101 switch (reason) {
102 case device::BluetoothSocket::kIOPending:
103 error_reason = BluetoothApiSocket::kIOPending;
104 break;
105 case device::BluetoothSocket::kDisconnected:
106 error_reason = BluetoothApiSocket::kDisconnected;
107 break;
108 case device::BluetoothSocket::kSystemError:
109 error_reason = BluetoothApiSocket::kSystemError;
110 break;
111 default:
112 NOTREACHED();
114 error_callback.Run(error_reason, message);
117 void BluetoothApiSocket::Send(scoped_refptr<net::IOBuffer> buffer,
118 int buffer_size,
119 const SendCompletionCallback& success_callback,
120 const ErrorCompletionCallback& error_callback) {
121 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
123 if (!socket_.get() || !IsConnected()) {
124 error_callback.Run(BluetoothApiSocket::kNotConnected,
125 kSocketNotConnectedError);
126 return;
129 socket_->Send(buffer,
130 buffer_size,
131 success_callback,
132 base::Bind(&OnSocketSendError, error_callback));
135 // static
136 void BluetoothApiSocket::OnSocketSendError(
137 const ErrorCompletionCallback& error_callback,
138 const std::string& message) {
139 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
140 error_callback.Run(BluetoothApiSocket::kSystemError, message);
144 } // namespace extensions