OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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.h" | 5 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api.h" |
6 | 6 |
| 7 #include "base/memory/weak_ptr.h" |
7 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
8 #include "chrome/browser/extensions/extension_service.h" | 9 #include "chrome/browser/extensions/extension_service.h" |
9 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
10 #include "chrome/common/extensions/api/experimental.bluetooth.h" | 11 #include "chrome/common/extensions/api/experimental.bluetooth.h" |
11 | 12 |
12 #if defined(OS_CHROMEOS) | 13 #if defined(OS_CHROMEOS) |
| 14 #include "base/memory/ref_counted.h" |
13 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
14 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h" | 16 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h" |
15 #include "chrome/browser/chromeos/bluetooth/bluetooth_device.h" | 17 #include "chrome/browser/chromeos/bluetooth/bluetooth_device.h" |
| 18 #include "chrome/browser/chromeos/bluetooth/bluetooth_socket.h" |
16 #include "chrome/browser/chromeos/extensions/bluetooth_event_router.h" | 19 #include "chrome/browser/chromeos/extensions/bluetooth_event_router.h" |
17 | 20 |
18 using chromeos::BluetoothAdapter; | 21 using chromeos::BluetoothAdapter; |
19 using chromeos::BluetoothDevice; | 22 using chromeos::BluetoothDevice; |
20 | 23 |
21 #endif | 24 #endif |
22 | 25 |
23 namespace GetDevicesWithServiceUUID = | 26 namespace GetDevicesWithServiceUUID = |
24 extensions::api::experimental_bluetooth::GetDevicesWithServiceUUID; | 27 extensions::api::experimental_bluetooth::GetDevicesWithServiceUUID; |
25 namespace GetDevicesWithServiceName = | 28 namespace GetDevicesWithServiceName = |
26 extensions::api::experimental_bluetooth::GetDevicesWithServiceName; | 29 extensions::api::experimental_bluetooth::GetDevicesWithServiceName; |
| 30 namespace Connect = extensions::api::experimental_bluetooth::Connect; |
| 31 namespace Disconnect = extensions::api::experimental_bluetooth::Disconnect; |
27 | 32 |
28 namespace extensions { | 33 namespace extensions { |
29 namespace api { | 34 namespace api { |
30 | 35 |
31 #if defined(OS_CHROMEOS) | 36 #if defined(OS_CHROMEOS) |
32 | 37 |
33 const chromeos::BluetoothAdapter* BluetoothExtensionFunction::adapter() const { | 38 const chromeos::BluetoothAdapter* BluetoothExtensionFunction::adapter() const { |
34 return profile()->GetExtensionService()->bluetooth_event_router()->adapter(); | 39 const chromeos::ExtensionBluetoothEventRouter* bluetooth_event_router = |
| 40 profile()->GetExtensionService()->bluetooth_event_router(); |
| 41 return bluetooth_event_router->adapter(); |
35 } | 42 } |
36 | 43 |
37 chromeos::BluetoothAdapter* BluetoothExtensionFunction::GetMutableAdapter() { | 44 chromeos::BluetoothAdapter* BluetoothExtensionFunction::GetMutableAdapter() { |
38 return profile()->GetExtensionService()->bluetooth_event_router()-> | 45 chromeos::ExtensionBluetoothEventRouter* bluetooth_event_router = |
39 GetMutableAdapter(); | 46 profile()->GetExtensionService()->bluetooth_event_router(); |
| 47 return bluetooth_event_router->GetMutableAdapter(); |
40 } | 48 } |
41 | 49 |
42 const chromeos::BluetoothAdapter* | 50 const chromeos::BluetoothAdapter* |
43 AsyncBluetoothExtensionFunction::adapter() const { | 51 AsyncBluetoothExtensionFunction::adapter() const { |
44 return profile()->GetExtensionService()->bluetooth_event_router()->adapter(); | 52 return profile()->GetExtensionService()->bluetooth_event_router()->adapter(); |
45 } | 53 } |
46 | 54 |
47 chromeos::BluetoothAdapter* | 55 chromeos::BluetoothAdapter* |
48 AsyncBluetoothExtensionFunction::GetMutableAdapter() { | 56 AsyncBluetoothExtensionFunction::GetMutableAdapter() { |
49 return profile()->GetExtensionService()->bluetooth_event_router()-> | 57 return profile()->GetExtensionService()->bluetooth_event_router()-> |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 callbacks_pending_++; | 136 callbacks_pending_++; |
129 } | 137 } |
130 | 138 |
131 if (callbacks_pending_) { | 139 if (callbacks_pending_) { |
132 AddRef(); // Released in AddDeviceIfTrue when callbacks_pending_ == 0 | 140 AddRef(); // Released in AddDeviceIfTrue when callbacks_pending_ == 0 |
133 } | 141 } |
134 | 142 |
135 return true; | 143 return true; |
136 } | 144 } |
137 | 145 |
| 146 void BluetoothConnectFunction::ConnectToServiceCallback( |
| 147 const chromeos::BluetoothDevice* device, |
| 148 const std::string& service_uuid, |
| 149 scoped_refptr<chromeos::BluetoothSocket> socket) { |
| 150 if (socket.get()) { |
| 151 // TODO(bryeung): hold on to this socket somewhere... |
| 152 |
| 153 experimental_bluetooth::Socket result_socket; |
| 154 result_socket.device.address = device->address(); |
| 155 result_socket.device.name = UTF16ToUTF8(device->GetName()); |
| 156 result_socket.service_uuid = service_uuid; |
| 157 result_socket.id = socket->fd(); |
| 158 result_.reset(result_socket.ToValue().get()); |
| 159 SendResponse(true); |
| 160 } |
| 161 SendResponse(false); |
| 162 } |
| 163 |
| 164 bool BluetoothConnectFunction::RunImpl() { |
| 165 scoped_ptr<Connect::Params> params(Connect::Params::Create(*args_)); |
| 166 |
| 167 chromeos::BluetoothDevice* device = |
| 168 GetMutableAdapter()->GetDevice(params->device.address); |
| 169 if (!device) { |
| 170 SendResponse(false); |
| 171 return false; |
| 172 } |
| 173 |
| 174 device->ConnectToService(params->service, |
| 175 base::Bind(&BluetoothConnectFunction::ConnectToServiceCallback, |
| 176 this, |
| 177 device, |
| 178 params->service)); |
| 179 return true; |
| 180 } |
| 181 |
| 182 bool BluetoothDisconnectFunction::RunImpl() { |
| 183 scoped_ptr<Disconnect::Params> params(Disconnect::Params::Create(*args_)); |
| 184 |
| 185 chromeos::BluetoothDevice* device = |
| 186 GetMutableAdapter()->GetDevice(params->socket.device.address); |
| 187 if (!device) |
| 188 return false; |
| 189 |
| 190 // TODO(bryeung): implement this... |
| 191 |
| 192 return true; |
| 193 } |
| 194 |
138 #else | 195 #else |
139 | 196 |
140 // ----------------------------------------------------------------------------- | 197 // ----------------------------------------------------------------------------- |
141 // NIY stubs | 198 // NIY stubs |
142 // ----------------------------------------------------------------------------- | 199 // ----------------------------------------------------------------------------- |
143 bool BluetoothIsAvailableFunction::RunImpl() { | 200 bool BluetoothIsAvailableFunction::RunImpl() { |
144 NOTREACHED() << "Not implemented yet"; | 201 NOTREACHED() << "Not implemented yet"; |
145 return false; | 202 return false; |
146 } | 203 } |
147 | 204 |
(...skipping 10 matching lines...) Expand all Loading... |
158 bool BluetoothGetDevicesWithServiceUUIDFunction::RunImpl() { | 215 bool BluetoothGetDevicesWithServiceUUIDFunction::RunImpl() { |
159 NOTREACHED() << "Not implemented yet"; | 216 NOTREACHED() << "Not implemented yet"; |
160 return false; | 217 return false; |
161 } | 218 } |
162 | 219 |
163 bool BluetoothGetDevicesWithServiceNameFunction::RunImpl() { | 220 bool BluetoothGetDevicesWithServiceNameFunction::RunImpl() { |
164 NOTREACHED() << "Not implemented yet"; | 221 NOTREACHED() << "Not implemented yet"; |
165 return false; | 222 return false; |
166 } | 223 } |
167 | 224 |
168 #endif | 225 bool BluetoothConnectFunction::RunImpl() { |
| 226 NOTREACHED() << "Not implemented yet"; |
| 227 return false; |
| 228 } |
169 | 229 |
170 bool BluetoothDisconnectFunction::RunImpl() { | 230 bool BluetoothDisconnectFunction::RunImpl() { |
171 NOTREACHED() << "Not implemented yet"; | 231 NOTREACHED() << "Not implemented yet"; |
172 return false; | 232 return false; |
173 } | 233 } |
174 | 234 |
| 235 #endif |
| 236 |
175 bool BluetoothReadFunction::RunImpl() { | 237 bool BluetoothReadFunction::RunImpl() { |
176 NOTREACHED() << "Not implemented yet"; | 238 NOTREACHED() << "Not implemented yet"; |
177 return false; | 239 return false; |
178 } | 240 } |
179 | 241 |
180 bool BluetoothSetOutOfBandPairingDataFunction::RunImpl() { | 242 bool BluetoothSetOutOfBandPairingDataFunction::RunImpl() { |
181 NOTREACHED() << "Not implemented yet"; | 243 NOTREACHED() << "Not implemented yet"; |
182 return false; | 244 return false; |
183 } | 245 } |
184 | 246 |
185 bool BluetoothGetOutOfBandPairingDataFunction::RunImpl() { | 247 bool BluetoothGetOutOfBandPairingDataFunction::RunImpl() { |
186 NOTREACHED() << "Not implemented yet"; | 248 NOTREACHED() << "Not implemented yet"; |
187 return false; | 249 return false; |
188 } | 250 } |
189 | 251 |
190 bool BluetoothWriteFunction::RunImpl() { | 252 bool BluetoothWriteFunction::RunImpl() { |
191 NOTREACHED() << "Not implemented yet"; | 253 NOTREACHED() << "Not implemented yet"; |
192 return false; | 254 return false; |
193 } | 255 } |
194 | 256 |
195 bool BluetoothConnectFunction::RunImpl() { | |
196 NOTREACHED() << "Not implemented yet"; | |
197 return false; | |
198 } | |
199 | |
200 } // namespace api | 257 } // namespace api |
201 } // namespace extensions | 258 } // namespace extensions |
OLD | NEW |