OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_socket.h" | 5 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_socket.h" |
6 | 6 |
7 #include "device/bluetooth/bluetooth_socket.h" | 7 #include "device/bluetooth/bluetooth_socket.h" |
8 #include "net/base/io_buffer.h" | 8 #include "net/base/io_buffer.h" |
9 | 9 |
10 namespace { | 10 namespace { |
11 | 11 |
12 const char kSocketNotConnectedError[] = "Socket not connected"; | 12 const char kSocketNotConnectedError[] = "Socket not connected"; |
| 13 const char kSocketNotListeningError[] = "Socket not listening"; |
13 | 14 |
14 } // namespace | 15 } // namespace |
15 | 16 |
16 namespace extensions { | 17 namespace extensions { |
17 | 18 |
18 // static | 19 // static |
19 static base::LazyInstance< | 20 static base::LazyInstance< |
20 BrowserContextKeyedAPIFactory<ApiResourceManager<BluetoothApiSocket> > > | 21 BrowserContextKeyedAPIFactory<ApiResourceManager<BluetoothApiSocket> > > |
21 g_server_factory = LAZY_INSTANCE_INITIALIZER; | 22 g_server_factory = LAZY_INSTANCE_INITIALIZER; |
22 | 23 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 | 67 |
67 if (socket_.get()) | 68 if (socket_.get()) |
68 socket_->Close(); | 69 socket_->Close(); |
69 | 70 |
70 socket_ = socket; | 71 socket_ = socket; |
71 device_address_ = device_address; | 72 device_address_ = device_address; |
72 uuid_ = uuid; | 73 uuid_ = uuid; |
73 connected_ = true; | 74 connected_ = true; |
74 } | 75 } |
75 | 76 |
76 void BluetoothApiSocket::Disconnect(const base::Closure& success_callback) { | 77 void BluetoothApiSocket::AdoptListeningSocket( |
| 78 scoped_refptr<device::BluetoothSocket> socket, |
| 79 const device::BluetoothUUID& uuid) { |
77 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId)); | 80 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId)); |
78 | 81 |
79 if (!socket_.get() || !IsConnected()) { | 82 if (socket_.get()) |
80 success_callback.Run(); | 83 socket_->Close(); |
| 84 |
| 85 socket_ = socket; |
| 86 device_address_ = ""; |
| 87 uuid_ = uuid; |
| 88 connected_ = false; |
| 89 } |
| 90 |
| 91 void BluetoothApiSocket::Disconnect(const base::Closure& callback) { |
| 92 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId)); |
| 93 |
| 94 if (!socket_.get()) { |
| 95 callback.Run(); |
81 return; | 96 return; |
82 } | 97 } |
83 | 98 |
84 socket_->Disconnect(success_callback); | 99 connected_ = false; |
| 100 socket_->Disconnect(callback); |
85 } | 101 } |
86 | 102 |
87 bool BluetoothApiSocket::IsPersistent() const { | 103 bool BluetoothApiSocket::IsPersistent() const { |
88 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId)); | 104 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId)); |
89 return persistent_; | 105 return persistent_; |
90 } | 106 } |
91 | 107 |
92 void BluetoothApiSocket::Receive( | 108 void BluetoothApiSocket::Receive( |
93 int count, | 109 int count, |
94 const ReceiveCompletionCallback& success_callback, | 110 const ReceiveCompletionCallback& success_callback, |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 success_callback, | 162 success_callback, |
147 base::Bind(&OnSocketSendError, error_callback)); | 163 base::Bind(&OnSocketSendError, error_callback)); |
148 } | 164 } |
149 | 165 |
150 // static | 166 // static |
151 void BluetoothApiSocket::OnSocketSendError( | 167 void BluetoothApiSocket::OnSocketSendError( |
152 const ErrorCompletionCallback& error_callback, | 168 const ErrorCompletionCallback& error_callback, |
153 const std::string& message) { | 169 const std::string& message) { |
154 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId)); | 170 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId)); |
155 error_callback.Run(BluetoothApiSocket::kSystemError, message); | 171 error_callback.Run(BluetoothApiSocket::kSystemError, message); |
| 172 } |
156 | 173 |
| 174 void BluetoothApiSocket::Accept( |
| 175 const AcceptCompletionCallback& success_callback, |
| 176 const ErrorCompletionCallback& error_callback) { |
| 177 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId)); |
| 178 |
| 179 if (!socket_.get() || IsConnected()) { |
| 180 error_callback.Run(BluetoothApiSocket::kNotListening, |
| 181 kSocketNotListeningError); |
| 182 return; |
| 183 } |
| 184 |
| 185 socket_->Accept(success_callback, |
| 186 base::Bind(&OnSocketAcceptError, error_callback)); |
| 187 } |
| 188 |
| 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); |
157 } | 195 } |
158 | 196 |
159 } // namespace extensions | 197 } // namespace extensions |
OLD | NEW |