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