Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(63)

Side by Side Diff: chrome/browser/extensions/api/bluetooth/bluetooth_api.cc

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

Powered by Google App Engine
This is Rietveld 408576698