Revert 285173 "Removed InProcessBrowserTest::CleanUpOnMainThread()"
[chromium-blink-merge.git] / chrome / browser / extensions / api / bluetooth_socket / bluetooth_api_socket.cc
blobda0c5e014c3ce71493dfe88780019afa1714b990
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_socket/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";
13 const char kSocketNotListeningError[] = "Socket not listening";
15 } // namespace
17 namespace extensions {
19 // static
20 static base::LazyInstance<
21 BrowserContextKeyedAPIFactory<ApiResourceManager<BluetoothApiSocket> > >
22 g_server_factory = LAZY_INSTANCE_INITIALIZER;
24 // static
25 template <>
26 BrowserContextKeyedAPIFactory<ApiResourceManager<BluetoothApiSocket> >*
27 ApiResourceManager<BluetoothApiSocket>::GetFactoryInstance() {
28 return g_server_factory.Pointer();
31 BluetoothApiSocket::BluetoothApiSocket(const std::string& owner_extension_id)
32 : ApiResource(owner_extension_id),
33 persistent_(false),
34 buffer_size_(0),
35 paused_(false),
36 connected_(false) {
37 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
40 BluetoothApiSocket::BluetoothApiSocket(
41 const std::string& owner_extension_id,
42 scoped_refptr<device::BluetoothSocket> socket,
43 const std::string& device_address,
44 const device::BluetoothUUID& uuid)
45 : ApiResource(owner_extension_id),
46 socket_(socket),
47 device_address_(device_address),
48 uuid_(uuid),
49 persistent_(false),
50 buffer_size_(0),
51 paused_(true),
52 connected_(true) {
53 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
56 BluetoothApiSocket::~BluetoothApiSocket() {
57 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
58 if (socket_.get())
59 socket_->Close();
62 void BluetoothApiSocket::AdoptConnectedSocket(
63 scoped_refptr<device::BluetoothSocket> socket,
64 const std::string& device_address,
65 const device::BluetoothUUID& uuid) {
66 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
68 if (socket_.get())
69 socket_->Close();
71 socket_ = socket;
72 device_address_ = device_address;
73 uuid_ = uuid;
74 connected_ = true;
77 void BluetoothApiSocket::AdoptListeningSocket(
78 scoped_refptr<device::BluetoothSocket> socket,
79 const device::BluetoothUUID& uuid) {
80 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
82 if (socket_.get())
83 socket_->Close();
85 socket_ = socket;
86 device_address_ = "";
87 uuid_ = uuid;
88 connected_ = false;
91 void BluetoothApiSocket::Disconnect(const base::Closure& callback) {
92 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
94 if (!socket_.get()) {
95 callback.Run();
96 return;
99 connected_ = false;
100 socket_->Disconnect(callback);
103 bool BluetoothApiSocket::IsPersistent() const {
104 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
105 return persistent_;
108 void BluetoothApiSocket::Receive(
109 int count,
110 const ReceiveCompletionCallback& success_callback,
111 const ErrorCompletionCallback& error_callback) {
112 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
114 if (!socket_.get() || !IsConnected()) {
115 error_callback.Run(BluetoothApiSocket::kNotConnected,
116 kSocketNotConnectedError);
117 return;
120 socket_->Receive(count,
121 success_callback,
122 base::Bind(&OnSocketReceiveError, error_callback));
125 // static
126 void BluetoothApiSocket::OnSocketReceiveError(
127 const ErrorCompletionCallback& error_callback,
128 device::BluetoothSocket::ErrorReason reason,
129 const std::string& message) {
130 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
131 BluetoothApiSocket::ErrorReason error_reason;
132 switch (reason) {
133 case device::BluetoothSocket::kIOPending:
134 error_reason = BluetoothApiSocket::kIOPending;
135 break;
136 case device::BluetoothSocket::kDisconnected:
137 error_reason = BluetoothApiSocket::kDisconnected;
138 break;
139 case device::BluetoothSocket::kSystemError:
140 error_reason = BluetoothApiSocket::kSystemError;
141 break;
142 default:
143 NOTREACHED();
145 error_callback.Run(error_reason, message);
148 void BluetoothApiSocket::Send(scoped_refptr<net::IOBuffer> buffer,
149 int buffer_size,
150 const SendCompletionCallback& success_callback,
151 const ErrorCompletionCallback& error_callback) {
152 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
154 if (!socket_.get() || !IsConnected()) {
155 error_callback.Run(BluetoothApiSocket::kNotConnected,
156 kSocketNotConnectedError);
157 return;
160 socket_->Send(buffer,
161 buffer_size,
162 success_callback,
163 base::Bind(&OnSocketSendError, error_callback));
166 // static
167 void BluetoothApiSocket::OnSocketSendError(
168 const ErrorCompletionCallback& error_callback,
169 const std::string& message) {
170 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
171 error_callback.Run(BluetoothApiSocket::kSystemError, message);
174 void BluetoothApiSocket::Accept(
175 const AcceptCompletionCallback& success_callback,
176 const ErrorCompletionCallback& error_callback) {
177 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
179 if (!socket_.get() || IsConnected()) {
180 error_callback.Run(BluetoothApiSocket::kNotListening,
181 kSocketNotListeningError);
182 return;
185 socket_->Accept(success_callback,
186 base::Bind(&OnSocketAcceptError, error_callback));
189 // static
190 void BluetoothApiSocket::OnSocketAcceptError(
191 const ErrorCompletionCallback& error_callback,
192 const std::string& message) {
193 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId));
194 error_callback.Run(BluetoothApiSocket::kSystemError, message);
197 } // namespace extensions