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 #if defined(OS_CHROMEOS) | 7 #if defined(OS_CHROMEOS) |
8 #include <errno.h> | 8 #include <errno.h> |
9 #endif | 9 #endif |
10 | 10 |
11 #include <string> | 11 #include <string> |
12 | 12 |
13 #include "base/memory/ref_counted.h" | |
13 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h" | 14 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h" |
15 #include "chrome/browser/extensions/bluetooth_event_router.h" | |
14 #include "chrome/browser/extensions/event_names.h" | 16 #include "chrome/browser/extensions/event_names.h" |
15 #include "chrome/browser/extensions/extension_service.h" | 17 #include "chrome/browser/extensions/extension_service.h" |
16 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
17 #include "chrome/common/extensions/api/experimental_bluetooth.h" | 19 #include "chrome/common/extensions/api/experimental_bluetooth.h" |
18 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
21 #include "device/bluetooth/bluetooth_adapter.h" | |
22 #include "device/bluetooth/bluetooth_device.h" | |
23 #include "device/bluetooth/bluetooth_out_of_band_pairing_data.h" | |
24 #include "device/bluetooth/bluetooth_service_record.h" | |
25 #include "device/bluetooth/bluetooth_socket.h" | |
26 #include "device/bluetooth/bluetooth_utils.h" | |
19 | 27 |
20 #if defined(OS_CHROMEOS) | 28 #if defined(OS_CHROMEOS) |
21 #include "base/memory/ref_counted.h" | |
22 #include "base/safe_strerror_posix.h" | 29 #include "base/safe_strerror_posix.h" |
23 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h" | 30 #endif |
24 #include "chrome/browser/chromeos/bluetooth/bluetooth_device.h" | 31 |
25 #include "chrome/browser/chromeos/bluetooth/bluetooth_service_record.h" | 32 using bluetooth::BluetoothAdapter; |
26 #include "chrome/browser/chromeos/bluetooth/bluetooth_socket.h" | 33 using bluetooth::BluetoothDevice; |
27 #include "chrome/browser/chromeos/bluetooth/bluetooth_utils.h" | |
28 #include "chrome/browser/chromeos/extensions/bluetooth_event_router.h" | |
29 #include "chromeos/dbus/bluetooth_out_of_band_client.h" | |
30 #include "chromeos/dbus/bluetooth_out_of_band_pairing_data.h" | |
31 | 34 |
32 namespace { | 35 namespace { |
33 | 36 |
34 chromeos::ExtensionBluetoothEventRouter* GetEventRouter(Profile* profile) { | 37 extensions::ExtensionBluetoothEventRouter* GetEventRouter(Profile* profile) { |
35 return profile->GetExtensionService()->bluetooth_event_router(); | 38 return profile->GetExtensionService()->bluetooth_event_router(); |
36 } | 39 } |
37 | 40 |
38 const chromeos::BluetoothAdapter& GetAdapter(Profile* profile) { | 41 const BluetoothAdapter* GetAdapter(Profile* profile) { |
39 return GetEventRouter(profile)->adapter(); | 42 return GetEventRouter(profile)->adapter(); |
40 } | 43 } |
41 | 44 |
42 chromeos::BluetoothAdapter* GetMutableAdapter(Profile* profile) { | 45 BluetoothAdapter* GetMutableAdapter(Profile* profile) { |
43 chromeos::BluetoothAdapter* adapter = | 46 BluetoothAdapter* adapter = GetEventRouter(profile)->GetMutableAdapter(); |
44 GetEventRouter(profile)->GetMutableAdapter(); | |
45 CHECK(adapter); | |
46 return adapter; | 47 return adapter; |
47 } | 48 } |
48 | 49 |
50 bool IsBluetoothSupported(Profile* profile) { | |
51 return GetAdapter(profile) != NULL; | |
52 } | |
53 | |
49 } // namespace | 54 } // namespace |
50 #endif | |
51 | 55 |
52 namespace { | 56 namespace { |
53 | 57 |
54 const char kCouldNotGetLocalOutOfBandPairingData[] = | 58 const char kCouldNotGetLocalOutOfBandPairingData[] = |
55 "Could not get local Out Of Band Pairing Data"; | 59 "Could not get local Out Of Band Pairing Data"; |
56 const char kCouldNotSetOutOfBandPairingData[] = | 60 const char kCouldNotSetOutOfBandPairingData[] = |
57 "Could not set Out Of Band Pairing Data"; | 61 "Could not set Out Of Band Pairing Data"; |
58 const char kFailedToConnect[] = "Connection failed"; | 62 const char kFailedToConnect[] = "Connection failed"; |
59 const char kInvalidDevice[] = "Invalid device"; | 63 const char kInvalidDevice[] = "Invalid device"; |
60 const char kInvalidUuid[] = "Invalid UUID"; | 64 const char kInvalidUuid[] = "Invalid UUID"; |
61 const char kServiceDiscoveryFailed[] = "Service discovery failed"; | 65 const char kServiceDiscoveryFailed[] = "Service discovery failed"; |
62 const char kSocketNotFoundError[] = "Socket not found: invalid socket id"; | 66 const char kSocketNotFoundError[] = "Socket not found: invalid socket id"; |
63 const char kStartDiscoveryFailed[] = "Starting discovery failed"; | 67 const char kStartDiscoveryFailed[] = "Starting discovery failed"; |
64 const char kStopDiscoveryFailed[] = "Failed to stop discovery"; | 68 const char kStopDiscoveryFailed[] = "Failed to stop discovery"; |
65 | 69 |
66 } // namespace | 70 } // namespace |
67 | 71 |
68 namespace Connect = extensions::api::experimental_bluetooth::Connect; | 72 namespace Connect = extensions::api::experimental_bluetooth::Connect; |
69 namespace Disconnect = extensions::api::experimental_bluetooth::Disconnect; | 73 namespace Disconnect = extensions::api::experimental_bluetooth::Disconnect; |
70 namespace GetDevices = extensions::api::experimental_bluetooth::GetDevices; | 74 namespace GetDevices = extensions::api::experimental_bluetooth::GetDevices; |
71 namespace GetServices = extensions::api::experimental_bluetooth::GetServices; | 75 namespace GetServices = extensions::api::experimental_bluetooth::GetServices; |
72 namespace Read = extensions::api::experimental_bluetooth::Read; | 76 namespace Read = extensions::api::experimental_bluetooth::Read; |
73 namespace SetOutOfBandPairingData = | 77 namespace SetOutOfBandPairingData = |
74 extensions::api::experimental_bluetooth::SetOutOfBandPairingData; | 78 extensions::api::experimental_bluetooth::SetOutOfBandPairingData; |
75 namespace Write = extensions::api::experimental_bluetooth::Write; | 79 namespace Write = extensions::api::experimental_bluetooth::Write; |
76 | 80 |
77 namespace extensions { | 81 namespace extensions { |
78 namespace api { | 82 namespace api { |
79 | 83 |
80 #if defined(OS_CHROMEOS) | 84 bool BluetoothIsAvailableFunction::RunImpl() { |
85 if (!IsBluetoothSupported(profile())) | |
bryeung
2012/10/11 15:15:39
These should be calling SetError as well, with an
youngki
2012/10/11 16:21:18
Done.
| |
86 return false; | |
81 | 87 |
82 bool BluetoothIsAvailableFunction::RunImpl() { | 88 SetResult(Value::CreateBooleanValue(GetAdapter(profile())->IsPresent())); |
83 SetResult(Value::CreateBooleanValue(GetAdapter(profile()).IsPresent())); | |
84 return true; | 89 return true; |
85 } | 90 } |
86 | 91 |
87 bool BluetoothIsPoweredFunction::RunImpl() { | 92 bool BluetoothIsPoweredFunction::RunImpl() { |
88 SetResult(Value::CreateBooleanValue(GetAdapter(profile()).IsPowered())); | 93 if (!IsBluetoothSupported(profile())) |
94 return false; | |
95 | |
96 SetResult(Value::CreateBooleanValue(GetAdapter(profile())->IsPowered())); | |
89 return true; | 97 return true; |
90 } | 98 } |
91 | 99 |
92 bool BluetoothGetAddressFunction::RunImpl() { | 100 bool BluetoothGetAddressFunction::RunImpl() { |
93 SetResult(Value::CreateStringValue(GetAdapter(profile()).address())); | 101 if (!IsBluetoothSupported(profile())) |
102 return false; | |
103 | |
104 SetResult(Value::CreateStringValue(GetAdapter(profile())->address())); | |
94 return true; | 105 return true; |
95 } | 106 } |
96 | 107 |
97 bool BluetoothGetNameFunction::RunImpl() { | 108 bool BluetoothGetNameFunction::RunImpl() { |
98 SetResult(Value::CreateStringValue(GetAdapter(profile()).name())); | 109 if (!IsBluetoothSupported(profile())) |
110 return false; | |
111 | |
112 SetResult(Value::CreateStringValue(GetAdapter(profile())->name())); | |
99 return true; | 113 return true; |
100 } | 114 } |
101 | 115 |
102 BluetoothGetDevicesFunction::BluetoothGetDevicesFunction() | 116 BluetoothGetDevicesFunction::BluetoothGetDevicesFunction() |
103 : callbacks_pending_(0) {} | 117 : callbacks_pending_(0) {} |
104 | 118 |
105 void BluetoothGetDevicesFunction::DispatchDeviceSearchResult( | 119 void BluetoothGetDevicesFunction::DispatchDeviceSearchResult( |
106 const chromeos::BluetoothDevice& device) { | 120 const BluetoothDevice& device) { |
107 experimental_bluetooth::Device extension_device; | 121 experimental_bluetooth::Device extension_device; |
108 experimental_bluetooth::BluetoothDeviceToApiDevice(device, &extension_device); | 122 experimental_bluetooth::BluetoothDeviceToApiDevice(device, &extension_device); |
109 GetEventRouter(profile())->DispatchDeviceEvent( | 123 GetEventRouter(profile())->DispatchDeviceEvent( |
110 extensions::event_names::kBluetoothOnDeviceSearchResult, | 124 extensions::event_names::kBluetoothOnDeviceSearchResult, |
111 extension_device); | 125 extension_device); |
112 } | 126 } |
113 | 127 |
114 void BluetoothGetDevicesFunction::ProvidesServiceCallback( | 128 void BluetoothGetDevicesFunction::ProvidesServiceCallback( |
115 const chromeos::BluetoothDevice* device, | 129 const BluetoothDevice* device, bool providesService) { |
116 bool providesService) { | |
117 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 130 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
118 | 131 |
119 CHECK(device); | 132 CHECK(device); |
120 if (providesService) | 133 if (providesService) |
121 DispatchDeviceSearchResult(*device); | 134 DispatchDeviceSearchResult(*device); |
122 | 135 |
123 callbacks_pending_--; | 136 callbacks_pending_--; |
124 if (callbacks_pending_ == -1) | 137 if (callbacks_pending_ == -1) |
125 SendResponse(true); | 138 SendResponse(true); |
126 } | 139 } |
127 | 140 |
128 bool BluetoothGetDevicesFunction::RunImpl() { | 141 bool BluetoothGetDevicesFunction::RunImpl() { |
142 if (!IsBluetoothSupported(profile())) | |
143 return false; | |
144 | |
129 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 145 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
130 | 146 |
131 scoped_ptr<GetDevices::Params> params(GetDevices::Params::Create(*args_)); | 147 scoped_ptr<GetDevices::Params> params(GetDevices::Params::Create(*args_)); |
132 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 148 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
133 const experimental_bluetooth::GetDevicesOptions& options = params->options; | 149 const experimental_bluetooth::GetDevicesOptions& options = params->options; |
134 | 150 |
135 std::string uuid; | 151 std::string uuid; |
136 if (options.uuid.get() != NULL) { | 152 if (options.uuid.get() != NULL) { |
137 uuid = chromeos::bluetooth_utils::CanonicalUuid(*options.uuid.get()); | 153 uuid = bluetooth::utils::CanonicalUuid(*options.uuid.get()); |
138 if (uuid.empty()) { | 154 if (uuid.empty()) { |
139 SetError(kInvalidUuid); | 155 SetError(kInvalidUuid); |
140 return false; | 156 return false; |
141 } | 157 } |
142 } | 158 } |
143 | 159 |
144 CHECK_EQ(0, callbacks_pending_); | 160 CHECK_EQ(0, callbacks_pending_); |
145 | 161 |
146 chromeos::BluetoothAdapter::DeviceList devices = | 162 BluetoothAdapter::DeviceList devices = |
147 GetMutableAdapter(profile())->GetDevices(); | 163 GetMutableAdapter(profile())->GetDevices(); |
148 for (chromeos::BluetoothAdapter::DeviceList::iterator i = devices.begin(); | 164 for (BluetoothAdapter::DeviceList::iterator i = devices.begin(); |
149 i != devices.end(); ++i) { | 165 i != devices.end(); ++i) { |
150 chromeos::BluetoothDevice* device = *i; | 166 BluetoothDevice* device = *i; |
151 CHECK(device); | 167 CHECK(device); |
152 | 168 |
153 if (!uuid.empty() && !(device->ProvidesServiceWithUUID(uuid))) | 169 if (!uuid.empty() && !(device->ProvidesServiceWithUUID(uuid))) |
154 continue; | 170 continue; |
155 | 171 |
156 if (options.name.get() == NULL) { | 172 if (options.name.get() == NULL) { |
157 DispatchDeviceSearchResult(*device); | 173 DispatchDeviceSearchResult(*device); |
158 continue; | 174 continue; |
159 } | 175 } |
160 | 176 |
(...skipping 10 matching lines...) Expand all Loading... | |
171 // for-loop, which ensures that all requests have been made before | 187 // for-loop, which ensures that all requests have been made before |
172 // SendResponse happens. | 188 // SendResponse happens. |
173 if (callbacks_pending_ == -1) | 189 if (callbacks_pending_ == -1) |
174 SendResponse(true); | 190 SendResponse(true); |
175 | 191 |
176 return true; | 192 return true; |
177 } | 193 } |
178 | 194 |
179 void BluetoothGetServicesFunction::GetServiceRecordsCallback( | 195 void BluetoothGetServicesFunction::GetServiceRecordsCallback( |
180 base::ListValue* services, | 196 base::ListValue* services, |
181 const chromeos::BluetoothDevice::ServiceRecordList& records) { | 197 const BluetoothDevice::ServiceRecordList& records) { |
182 for (chromeos::BluetoothDevice::ServiceRecordList::const_iterator i = | 198 for (BluetoothDevice::ServiceRecordList::const_iterator i = records.begin(); |
183 records.begin(); i != records.end(); ++i) { | 199 i != records.end(); ++i) { |
184 const chromeos::BluetoothServiceRecord& record = **i; | 200 const bluetooth::BluetoothServiceRecord& record = **i; |
185 experimental_bluetooth::ServiceRecord api_record; | 201 experimental_bluetooth::ServiceRecord api_record; |
186 api_record.name = record.name(); | 202 api_record.name = record.name(); |
187 if (!record.uuid().empty()) | 203 if (!record.uuid().empty()) |
188 api_record.uuid.reset(new std::string(record.uuid())); | 204 api_record.uuid.reset(new std::string(record.uuid())); |
189 services->Append(api_record.ToValue().release()); | 205 services->Append(api_record.ToValue().release()); |
190 } | 206 } |
191 | 207 |
192 SendResponse(true); | 208 SendResponse(true); |
193 } | 209 } |
194 | 210 |
195 void BluetoothGetServicesFunction::OnErrorCallback() { | 211 void BluetoothGetServicesFunction::OnErrorCallback() { |
196 SetError(kServiceDiscoveryFailed); | 212 SetError(kServiceDiscoveryFailed); |
197 SendResponse(false); | 213 SendResponse(false); |
198 } | 214 } |
199 | 215 |
200 bool BluetoothGetServicesFunction::RunImpl() { | 216 bool BluetoothGetServicesFunction::RunImpl() { |
217 if (!IsBluetoothSupported(profile())) | |
218 return false; | |
219 | |
201 scoped_ptr<GetServices::Params> params(GetServices::Params::Create(*args_)); | 220 scoped_ptr<GetServices::Params> params(GetServices::Params::Create(*args_)); |
202 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 221 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
203 const experimental_bluetooth::GetServicesOptions& options = params->options; | 222 const experimental_bluetooth::GetServicesOptions& options = params->options; |
204 | 223 |
205 chromeos::BluetoothDevice* device = | 224 BluetoothDevice* device = |
206 GetMutableAdapter(profile())->GetDevice(options.device_address); | 225 GetMutableAdapter(profile())->GetDevice(options.device_address); |
207 if (!device) { | 226 if (!device) { |
208 SetError(kInvalidDevice); | 227 SetError(kInvalidDevice); |
209 return false; | 228 return false; |
210 } | 229 } |
211 | 230 |
212 ListValue* services = new ListValue; | 231 ListValue* services = new ListValue; |
213 SetResult(services); | 232 SetResult(services); |
214 | 233 |
215 device->GetServiceRecords( | 234 device->GetServiceRecords( |
216 base::Bind(&BluetoothGetServicesFunction::GetServiceRecordsCallback, | 235 base::Bind(&BluetoothGetServicesFunction::GetServiceRecordsCallback, |
217 this, | 236 this, |
218 services), | 237 services), |
219 base::Bind(&BluetoothGetServicesFunction::OnErrorCallback, | 238 base::Bind(&BluetoothGetServicesFunction::OnErrorCallback, |
220 this)); | 239 this)); |
221 | 240 |
222 return true; | 241 return true; |
223 } | 242 } |
224 | 243 |
225 void BluetoothConnectFunction::ConnectToServiceCallback( | 244 void BluetoothConnectFunction::ConnectToServiceCallback( |
226 const chromeos::BluetoothDevice* device, | 245 const BluetoothDevice* device, |
227 const std::string& service_uuid, | 246 const std::string& service_uuid, |
228 scoped_refptr<chromeos::BluetoothSocket> socket) { | 247 scoped_refptr<bluetooth::BluetoothSocket> socket) { |
bryeung
2012/10/11 15:15:39
Should be using bluetooth::BluetoothSocket in this
youngki
2012/10/11 16:21:18
Done.
| |
229 if (socket.get()) { | 248 if (socket.get()) { |
230 int socket_id = GetEventRouter(profile())->RegisterSocket(socket); | 249 int socket_id = GetEventRouter(profile())->RegisterSocket(socket); |
231 | 250 |
232 experimental_bluetooth::Socket result_socket; | 251 experimental_bluetooth::Socket result_socket; |
233 experimental_bluetooth::BluetoothDeviceToApiDevice( | 252 experimental_bluetooth::BluetoothDeviceToApiDevice( |
234 *device, &result_socket.device); | 253 *device, &result_socket.device); |
235 result_socket.service_uuid = service_uuid; | 254 result_socket.service_uuid = service_uuid; |
236 result_socket.id = socket_id; | 255 result_socket.id = socket_id; |
237 SetResult(result_socket.ToValue().release()); | 256 SetResult(result_socket.ToValue().release()); |
238 SendResponse(true); | 257 SendResponse(true); |
239 } else { | 258 } else { |
240 SetError(kFailedToConnect); | 259 SetError(kFailedToConnect); |
241 SendResponse(false); | 260 SendResponse(false); |
242 } | 261 } |
243 } | 262 } |
244 | 263 |
245 bool BluetoothConnectFunction::RunImpl() { | 264 bool BluetoothConnectFunction::RunImpl() { |
265 if (!IsBluetoothSupported(profile())) | |
266 return false; | |
267 | |
246 scoped_ptr<Connect::Params> params(Connect::Params::Create(*args_)); | 268 scoped_ptr<Connect::Params> params(Connect::Params::Create(*args_)); |
247 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 269 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
248 const experimental_bluetooth::ConnectOptions& options = params->options; | 270 const experimental_bluetooth::ConnectOptions& options = params->options; |
249 | 271 |
250 std::string uuid = chromeos::bluetooth_utils::CanonicalUuid( | 272 std::string uuid = bluetooth::utils::CanonicalUuid( |
251 options.service_uuid); | 273 options.service_uuid); |
252 if (uuid.empty()) { | 274 if (uuid.empty()) { |
253 SetError(kInvalidUuid); | 275 SetError(kInvalidUuid); |
254 return false; | 276 return false; |
255 } | 277 } |
256 | 278 |
257 chromeos::BluetoothDevice* device = | 279 BluetoothDevice* device = |
258 GetMutableAdapter(profile())->GetDevice(options.device_address); | 280 GetMutableAdapter(profile())->GetDevice(options.device_address); |
259 if (!device) { | 281 if (!device) { |
260 SetError(kInvalidDevice); | 282 SetError(kInvalidDevice); |
261 return false; | 283 return false; |
262 } | 284 } |
263 | 285 |
264 device->ConnectToService(uuid, | 286 device->ConnectToService(uuid, |
265 base::Bind(&BluetoothConnectFunction::ConnectToServiceCallback, | 287 base::Bind(&BluetoothConnectFunction::ConnectToServiceCallback, |
266 this, | 288 this, |
267 device, | 289 device, |
268 uuid)); | 290 uuid)); |
269 return true; | 291 return true; |
270 } | 292 } |
271 | 293 |
272 bool BluetoothDisconnectFunction::RunImpl() { | 294 bool BluetoothDisconnectFunction::RunImpl() { |
273 scoped_ptr<Disconnect::Params> params(Disconnect::Params::Create(*args_)); | 295 scoped_ptr<Disconnect::Params> params(Disconnect::Params::Create(*args_)); |
274 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 296 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
275 const experimental_bluetooth::DisconnectOptions& options = params->options; | 297 const experimental_bluetooth::DisconnectOptions& options = params->options; |
276 return GetEventRouter(profile())->ReleaseSocket(options.socket_id); | 298 return GetEventRouter(profile())->ReleaseSocket(options.socket_id); |
277 } | 299 } |
278 | 300 |
301 BluetoothReadFunction::BluetoothReadFunction() {} | |
302 BluetoothReadFunction::~BluetoothReadFunction() {} | |
303 | |
279 bool BluetoothReadFunction::Prepare() { | 304 bool BluetoothReadFunction::Prepare() { |
280 scoped_ptr<Read::Params> params(Read::Params::Create(*args_)); | 305 scoped_ptr<Read::Params> params(Read::Params::Create(*args_)); |
281 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 306 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
282 const experimental_bluetooth::ReadOptions& options = params->options; | 307 const experimental_bluetooth::ReadOptions& options = params->options; |
283 | 308 |
284 socket_ = GetEventRouter(profile())->GetSocket(options.socket_id); | 309 socket_ = GetEventRouter(profile())->GetSocket(options.socket_id); |
285 if (socket_.get() == NULL) { | 310 if (socket_.get() == NULL) { |
286 SetError(kSocketNotFoundError); | 311 SetError(kSocketNotFoundError); |
287 return false; | 312 return false; |
288 } | 313 } |
289 | 314 |
290 success_ = false; | 315 success_ = false; |
291 return true; | 316 return true; |
292 } | 317 } |
293 | 318 |
294 void BluetoothReadFunction::Work() { | 319 void BluetoothReadFunction::Work() { |
320 if (!socket_.get()) | |
321 return; | |
322 | |
295 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 323 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
296 CHECK(socket_.get() != NULL); | |
297 | 324 |
298 char* all_bytes = NULL; | 325 char* all_bytes = NULL; |
299 ssize_t buffer_size = 0; | 326 ssize_t buffer_size = 0; |
300 ssize_t total_bytes_read = 0; | 327 ssize_t total_bytes_read = 0; |
301 int errsv; | 328 int errsv; |
302 while (true) { | 329 while (true) { |
303 buffer_size += 1024; | 330 buffer_size += 1024; |
304 all_bytes = static_cast<char*>(realloc(all_bytes, buffer_size)); | 331 all_bytes = static_cast<char*>(realloc(all_bytes, buffer_size)); |
305 CHECK(all_bytes) << "Failed to grow Bluetooth socket buffer"; | 332 CHECK(all_bytes) << "Failed to grow Bluetooth socket buffer"; |
306 | 333 |
307 // bluetooth sockets are non-blocking, so read until we hit an error | 334 // bluetooth sockets are non-blocking, so read until we hit an error |
308 ssize_t bytes_read = read(socket_->fd(), all_bytes + total_bytes_read, | 335 ssize_t bytes_read = read(socket_->fd(), all_bytes + total_bytes_read, |
309 buffer_size - total_bytes_read); | 336 buffer_size - total_bytes_read); |
310 errsv = errno; | 337 errsv = errno; |
311 if (bytes_read <= 0) | 338 if (bytes_read <= 0) |
312 break; | 339 break; |
313 | 340 |
314 total_bytes_read += bytes_read; | 341 total_bytes_read += bytes_read; |
315 } | 342 } |
316 | 343 |
317 if (total_bytes_read > 0) { | 344 if (total_bytes_read > 0) { |
318 success_ = true; | 345 success_ = true; |
319 SetResult(base::BinaryValue::Create(all_bytes, total_bytes_read)); | 346 SetResult(base::BinaryValue::Create(all_bytes, total_bytes_read)); |
320 } else { | 347 } else { |
321 success_ = (errsv == EAGAIN || errsv == EWOULDBLOCK); | 348 success_ = (errsv == EAGAIN || errsv == EWOULDBLOCK); |
322 free(all_bytes); | 349 free(all_bytes); |
323 } | 350 } |
324 | 351 |
352 #if defined(OS_CHROMEOS) | |
325 if (!success_) | 353 if (!success_) |
326 SetError(safe_strerror(errsv)); | 354 SetError(safe_strerror(errsv)); |
355 #endif | |
327 } | 356 } |
328 | 357 |
329 bool BluetoothReadFunction::Respond() { | 358 bool BluetoothReadFunction::Respond() { |
330 return success_; | 359 return success_; |
331 } | 360 } |
332 | 361 |
362 BluetoothWriteFunction::BluetoothWriteFunction() {} | |
363 BluetoothWriteFunction::~BluetoothWriteFunction() {} | |
364 | |
333 bool BluetoothWriteFunction::Prepare() { | 365 bool BluetoothWriteFunction::Prepare() { |
334 // TODO(bryeung): update to new-style parameter passing when ArrayBuffer | 366 // TODO(bryeung): update to new-style parameter passing when ArrayBuffer |
335 // support is added | 367 // support is added |
336 DictionaryValue* options; | 368 DictionaryValue* options; |
337 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &options)); | 369 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &options)); |
338 int socket_id; | 370 int socket_id; |
339 EXTENSION_FUNCTION_VALIDATE(options->GetInteger("socketId", &socket_id)); | 371 EXTENSION_FUNCTION_VALIDATE(options->GetInteger("socketId", &socket_id)); |
340 | 372 |
341 socket_ = GetEventRouter(profile())->GetSocket(socket_id); | 373 socket_ = GetEventRouter(profile())->GetSocket(socket_id); |
342 if (socket_.get() == NULL) { | 374 if (socket_.get() == NULL) { |
(...skipping 20 matching lines...) Expand all Loading... | |
363 int errsv = errno; | 395 int errsv = errno; |
364 | 396 |
365 if (bytes_written > 0) { | 397 if (bytes_written > 0) { |
366 SetResult(Value::CreateIntegerValue(bytes_written)); | 398 SetResult(Value::CreateIntegerValue(bytes_written)); |
367 success_ = true; | 399 success_ = true; |
368 } else { | 400 } else { |
369 results_.reset(); | 401 results_.reset(); |
370 success_ = (errsv == EAGAIN || errsv == EWOULDBLOCK); | 402 success_ = (errsv == EAGAIN || errsv == EWOULDBLOCK); |
371 } | 403 } |
372 | 404 |
405 #if defined(OS_CHROMEOS) | |
373 if (!success_) | 406 if (!success_) |
374 SetError(safe_strerror(errsv)); | 407 SetError(safe_strerror(errsv)); |
408 #endif | |
375 } | 409 } |
376 | 410 |
377 bool BluetoothWriteFunction::Respond() { | 411 bool BluetoothWriteFunction::Respond() { |
378 return success_; | 412 return success_; |
379 } | 413 } |
380 | 414 |
381 void BluetoothSetOutOfBandPairingDataFunction::OnSuccessCallback() { | 415 void BluetoothSetOutOfBandPairingDataFunction::OnSuccessCallback() { |
382 SendResponse(true); | 416 SendResponse(true); |
383 } | 417 } |
384 | 418 |
385 void BluetoothSetOutOfBandPairingDataFunction::OnErrorCallback() { | 419 void BluetoothSetOutOfBandPairingDataFunction::OnErrorCallback() { |
386 SetError(kCouldNotSetOutOfBandPairingData); | 420 SetError(kCouldNotSetOutOfBandPairingData); |
387 SendResponse(false); | 421 SendResponse(false); |
388 } | 422 } |
389 | 423 |
390 bool BluetoothSetOutOfBandPairingDataFunction::RunImpl() { | 424 bool BluetoothSetOutOfBandPairingDataFunction::RunImpl() { |
425 if (!IsBluetoothSupported(profile())) | |
426 return false; | |
427 | |
391 // TODO(bryeung): update to new-style parameter passing when ArrayBuffer | 428 // TODO(bryeung): update to new-style parameter passing when ArrayBuffer |
392 // support is added | 429 // support is added |
393 DictionaryValue* options; | 430 DictionaryValue* options; |
394 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &options)); | 431 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &options)); |
395 std::string address; | 432 std::string address; |
396 EXTENSION_FUNCTION_VALIDATE(options->GetString("deviceAddress", &address)); | 433 EXTENSION_FUNCTION_VALIDATE(options->GetString("deviceAddress", &address)); |
397 | 434 |
398 chromeos::BluetoothDevice* device = | 435 BluetoothDevice* device = GetMutableAdapter(profile())->GetDevice(address); |
399 GetMutableAdapter(profile())->GetDevice(address); | |
400 if (!device) { | 436 if (!device) { |
401 SetError(kInvalidDevice); | 437 SetError(kInvalidDevice); |
402 return false; | 438 return false; |
403 } | 439 } |
404 | 440 |
405 if (options->HasKey("data")) { | 441 if (options->HasKey("data")) { |
406 DictionaryValue* data_in; | 442 DictionaryValue* data_in; |
407 EXTENSION_FUNCTION_VALIDATE(options->GetDictionary("data", &data_in)); | 443 EXTENSION_FUNCTION_VALIDATE(options->GetDictionary("data", &data_in)); |
408 | 444 |
409 chromeos::BluetoothOutOfBandPairingData data_out; | 445 bluetooth::BluetoothOutOfBandPairingData data_out; |
410 | 446 |
411 base::BinaryValue* tmp_data; | 447 base::BinaryValue* tmp_data; |
412 EXTENSION_FUNCTION_VALIDATE(data_in->GetBinary("hash", &tmp_data)); | 448 EXTENSION_FUNCTION_VALIDATE(data_in->GetBinary("hash", &tmp_data)); |
413 EXTENSION_FUNCTION_VALIDATE( | 449 EXTENSION_FUNCTION_VALIDATE( |
414 tmp_data->GetSize() == chromeos::kBluetoothOutOfBandPairingDataSize); | 450 tmp_data->GetSize() == bluetooth::kBluetoothOutOfBandPairingDataSize); |
415 memcpy(data_out.hash, | 451 memcpy(data_out.hash, |
416 reinterpret_cast<uint8_t*>(tmp_data->GetBuffer()), | 452 reinterpret_cast<uint8_t*>(tmp_data->GetBuffer()), |
417 chromeos::kBluetoothOutOfBandPairingDataSize); | 453 bluetooth::kBluetoothOutOfBandPairingDataSize); |
418 | 454 |
419 EXTENSION_FUNCTION_VALIDATE(data_in->GetBinary("randomizer", &tmp_data)); | 455 EXTENSION_FUNCTION_VALIDATE(data_in->GetBinary("randomizer", &tmp_data)); |
420 EXTENSION_FUNCTION_VALIDATE( | 456 EXTENSION_FUNCTION_VALIDATE( |
421 tmp_data->GetSize() == chromeos::kBluetoothOutOfBandPairingDataSize); | 457 tmp_data->GetSize() == bluetooth::kBluetoothOutOfBandPairingDataSize); |
422 memcpy(data_out.randomizer, | 458 memcpy(data_out.randomizer, |
423 reinterpret_cast<uint8_t*>(tmp_data->GetBuffer()), | 459 reinterpret_cast<uint8_t*>(tmp_data->GetBuffer()), |
424 chromeos::kBluetoothOutOfBandPairingDataSize); | 460 bluetooth::kBluetoothOutOfBandPairingDataSize); |
425 | 461 |
426 device->SetOutOfBandPairingData( | 462 device->SetOutOfBandPairingData( |
427 data_out, | 463 data_out, |
428 base::Bind(&BluetoothSetOutOfBandPairingDataFunction::OnSuccessCallback, | 464 base::Bind(&BluetoothSetOutOfBandPairingDataFunction::OnSuccessCallback, |
429 this), | 465 this), |
430 base::Bind(&BluetoothSetOutOfBandPairingDataFunction::OnErrorCallback, | 466 base::Bind(&BluetoothSetOutOfBandPairingDataFunction::OnErrorCallback, |
431 this)); | 467 this)); |
432 } else { | 468 } else { |
433 device->ClearOutOfBandPairingData( | 469 device->ClearOutOfBandPairingData( |
434 base::Bind(&BluetoothSetOutOfBandPairingDataFunction::OnSuccessCallback, | 470 base::Bind(&BluetoothSetOutOfBandPairingDataFunction::OnSuccessCallback, |
435 this), | 471 this), |
436 base::Bind(&BluetoothSetOutOfBandPairingDataFunction::OnErrorCallback, | 472 base::Bind(&BluetoothSetOutOfBandPairingDataFunction::OnErrorCallback, |
437 this)); | 473 this)); |
438 } | 474 } |
439 | 475 |
440 return true; | 476 return true; |
441 } | 477 } |
442 | 478 |
443 void BluetoothGetLocalOutOfBandPairingDataFunction::ReadCallback( | 479 void BluetoothGetLocalOutOfBandPairingDataFunction::ReadCallback( |
444 const chromeos::BluetoothOutOfBandPairingData& data) { | 480 const bluetooth::BluetoothOutOfBandPairingData& data) { |
445 base::BinaryValue* hash = base::BinaryValue::CreateWithCopiedBuffer( | 481 base::BinaryValue* hash = base::BinaryValue::CreateWithCopiedBuffer( |
446 reinterpret_cast<const char*>(data.hash), | 482 reinterpret_cast<const char*>(data.hash), |
447 chromeos::kBluetoothOutOfBandPairingDataSize); | 483 bluetooth::kBluetoothOutOfBandPairingDataSize); |
448 base::BinaryValue* randomizer = base::BinaryValue::CreateWithCopiedBuffer( | 484 base::BinaryValue* randomizer = base::BinaryValue::CreateWithCopiedBuffer( |
449 reinterpret_cast<const char*>(data.randomizer), | 485 reinterpret_cast<const char*>(data.randomizer), |
450 chromeos::kBluetoothOutOfBandPairingDataSize); | 486 bluetooth::kBluetoothOutOfBandPairingDataSize); |
451 | 487 |
452 // TODO(bryeung): convert to experimental_bluetooth::OutOfBandPairingData | 488 // TODO(bryeung): convert to experimental_bluetooth::OutOfBandPairingData |
453 // when ArrayBuffer support within objects is completed. | 489 // when ArrayBuffer support within objects is completed. |
454 DictionaryValue* result = new DictionaryValue(); | 490 DictionaryValue* result = new DictionaryValue(); |
455 result->Set("hash", hash); | 491 result->Set("hash", hash); |
456 result->Set("randomizer", randomizer); | 492 result->Set("randomizer", randomizer); |
457 | 493 |
458 SetResult(result); | 494 SetResult(result); |
459 | 495 |
460 SendResponse(true); | 496 SendResponse(true); |
461 } | 497 } |
462 | 498 |
463 void BluetoothGetLocalOutOfBandPairingDataFunction::ErrorCallback() { | 499 void BluetoothGetLocalOutOfBandPairingDataFunction::ErrorCallback() { |
464 SetError(kCouldNotGetLocalOutOfBandPairingData); | 500 SetError(kCouldNotGetLocalOutOfBandPairingData); |
465 SendResponse(false); | 501 SendResponse(false); |
466 } | 502 } |
467 | 503 |
468 bool BluetoothGetLocalOutOfBandPairingDataFunction::RunImpl() { | 504 bool BluetoothGetLocalOutOfBandPairingDataFunction::RunImpl() { |
505 if (!IsBluetoothSupported(profile())) | |
506 return false; | |
507 | |
469 GetMutableAdapter(profile())->ReadLocalOutOfBandPairingData( | 508 GetMutableAdapter(profile())->ReadLocalOutOfBandPairingData( |
470 base::Bind(&BluetoothGetLocalOutOfBandPairingDataFunction::ReadCallback, | 509 base::Bind(&BluetoothGetLocalOutOfBandPairingDataFunction::ReadCallback, |
471 this), | 510 this), |
472 base::Bind(&BluetoothGetLocalOutOfBandPairingDataFunction::ErrorCallback, | 511 base::Bind(&BluetoothGetLocalOutOfBandPairingDataFunction::ErrorCallback, |
473 this)); | 512 this)); |
474 return true; | 513 return true; |
475 } | 514 } |
476 | 515 |
477 void BluetoothStartDiscoveryFunction::OnSuccessCallback() { | 516 void BluetoothStartDiscoveryFunction::OnSuccessCallback() { |
478 GetEventRouter(profile())->SetResponsibleForDiscovery(true); | 517 GetEventRouter(profile())->SetResponsibleForDiscovery(true); |
479 SendResponse(true); | 518 SendResponse(true); |
480 } | 519 } |
481 | 520 |
482 void BluetoothStartDiscoveryFunction::OnErrorCallback() { | 521 void BluetoothStartDiscoveryFunction::OnErrorCallback() { |
483 SetError(kStartDiscoveryFailed); | 522 SetError(kStartDiscoveryFailed); |
484 SendResponse(false); | 523 SendResponse(false); |
485 } | 524 } |
486 | 525 |
487 bool BluetoothStartDiscoveryFunction::RunImpl() { | 526 bool BluetoothStartDiscoveryFunction::RunImpl() { |
527 if (!IsBluetoothSupported(profile())) | |
528 return false; | |
529 | |
488 GetEventRouter(profile())->SetSendDiscoveryEvents(true); | 530 GetEventRouter(profile())->SetSendDiscoveryEvents(true); |
489 | 531 |
490 // If the adapter is already discovering, there is nothing else to do. | 532 // If the adapter is already discovering, there is nothing else to do. |
491 if (GetMutableAdapter(profile())->IsDiscovering()) { | 533 if (GetMutableAdapter(profile())->IsDiscovering()) { |
492 SendResponse(true); | 534 SendResponse(true); |
493 return true; | 535 return true; |
494 } | 536 } |
495 | 537 |
496 GetMutableAdapter(profile())->SetDiscovering(true, | 538 GetMutableAdapter(profile())->SetDiscovering(true, |
497 base::Bind(&BluetoothStartDiscoveryFunction::OnSuccessCallback, this), | 539 base::Bind(&BluetoothStartDiscoveryFunction::OnSuccessCallback, this), |
498 base::Bind(&BluetoothStartDiscoveryFunction::OnErrorCallback, this)); | 540 base::Bind(&BluetoothStartDiscoveryFunction::OnErrorCallback, this)); |
499 return true; | 541 return true; |
500 } | 542 } |
501 | 543 |
502 void BluetoothStopDiscoveryFunction::OnSuccessCallback() { | 544 void BluetoothStopDiscoveryFunction::OnSuccessCallback() { |
503 SendResponse(true); | 545 SendResponse(true); |
504 } | 546 } |
505 | 547 |
506 void BluetoothStopDiscoveryFunction::OnErrorCallback() { | 548 void BluetoothStopDiscoveryFunction::OnErrorCallback() { |
507 SetError(kStopDiscoveryFailed); | 549 SetError(kStopDiscoveryFailed); |
508 SendResponse(false); | 550 SendResponse(false); |
509 } | 551 } |
510 | 552 |
511 bool BluetoothStopDiscoveryFunction::RunImpl() { | 553 bool BluetoothStopDiscoveryFunction::RunImpl() { |
554 if (!IsBluetoothSupported(profile())) | |
555 return false; | |
556 | |
512 GetEventRouter(profile())->SetSendDiscoveryEvents(false); | 557 GetEventRouter(profile())->SetSendDiscoveryEvents(false); |
513 if (GetEventRouter(profile())->IsResponsibleForDiscovery()) { | 558 if (GetEventRouter(profile())->IsResponsibleForDiscovery()) { |
514 GetMutableAdapter(profile())->SetDiscovering(false, | 559 GetMutableAdapter(profile())->SetDiscovering(false, |
515 base::Bind(&BluetoothStopDiscoveryFunction::OnSuccessCallback, this), | 560 base::Bind(&BluetoothStopDiscoveryFunction::OnSuccessCallback, this), |
516 base::Bind(&BluetoothStopDiscoveryFunction::OnErrorCallback, this)); | 561 base::Bind(&BluetoothStopDiscoveryFunction::OnErrorCallback, this)); |
517 } | 562 } |
518 return true; | 563 return true; |
519 } | 564 } |
520 | 565 |
521 #else | |
522 | |
523 // ----------------------------------------------------------------------------- | |
524 // NIY stubs | |
525 // ----------------------------------------------------------------------------- | |
526 bool BluetoothIsAvailableFunction::RunImpl() { | |
527 NOTREACHED() << "Not implemented yet"; | |
528 return false; | |
529 } | |
530 | |
531 bool BluetoothIsPoweredFunction::RunImpl() { | |
532 NOTREACHED() << "Not implemented yet"; | |
533 return false; | |
534 } | |
535 | |
536 bool BluetoothGetAddressFunction::RunImpl() { | |
537 NOTREACHED() << "Not implemented yet"; | |
538 return false; | |
539 } | |
540 | |
541 bool BluetoothGetNameFunction::RunImpl() { | |
542 NOTREACHED() << "Not implemented yet"; | |
543 return false; | |
544 } | |
545 | |
546 bool BluetoothGetDevicesFunction::RunImpl() { | |
547 NOTREACHED() << "Not implemented yet"; | |
548 return false; | |
549 } | |
550 | |
551 bool BluetoothGetServicesFunction::RunImpl() { | |
552 NOTREACHED() << "Not implemented yet"; | |
553 return false; | |
554 } | |
555 | |
556 bool BluetoothConnectFunction::RunImpl() { | |
557 NOTREACHED() << "Not implemented yet"; | |
558 return false; | |
559 } | |
560 | |
561 bool BluetoothDisconnectFunction::RunImpl() { | |
562 NOTREACHED() << "Not implemented yet"; | |
563 return false; | |
564 } | |
565 | |
566 bool BluetoothReadFunction::Prepare() { | |
567 return true; | |
568 } | |
569 | |
570 void BluetoothReadFunction::Work() { | |
571 } | |
572 | |
573 bool BluetoothReadFunction::Respond() { | |
574 NOTREACHED() << "Not implemented yet"; | |
575 return false; | |
576 } | |
577 | |
578 bool BluetoothWriteFunction::Prepare() { | |
579 return true; | |
580 } | |
581 | |
582 void BluetoothWriteFunction::Work() { | |
583 } | |
584 | |
585 bool BluetoothWriteFunction::Respond() { | |
586 NOTREACHED() << "Not implemented yet"; | |
587 return false; | |
588 } | |
589 | |
590 bool BluetoothStartDiscoveryFunction::RunImpl() { | |
591 NOTREACHED() << "Not implemented yet"; | |
592 return false; | |
593 } | |
594 | |
595 bool BluetoothStopDiscoveryFunction::RunImpl() { | |
596 NOTREACHED() << "Not implemented yet"; | |
597 return false; | |
598 } | |
599 | |
600 bool BluetoothSetOutOfBandPairingDataFunction::RunImpl() { | |
601 NOTREACHED() << "Not implemented yet"; | |
602 return false; | |
603 } | |
604 | |
605 bool BluetoothGetLocalOutOfBandPairingDataFunction::RunImpl() { | |
606 NOTREACHED() << "Not implemented yet"; | |
607 return false; | |
608 } | |
609 | |
610 #endif | |
611 | |
612 BluetoothReadFunction::BluetoothReadFunction() {} | |
613 BluetoothReadFunction::~BluetoothReadFunction() {} | |
614 | |
615 BluetoothWriteFunction::BluetoothWriteFunction() {} | |
616 BluetoothWriteFunction::~BluetoothWriteFunction() {} | |
617 | |
618 } // namespace api | 566 } // namespace api |
619 } // namespace extensions | 567 } // namespace extensions |
OLD | NEW |