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

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

Issue 10694106: Added support for multiple parameters to Extension API callbacks. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Review fixes. Created 8 years, 5 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
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
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 namespace SetOutOfBandPairingData = 66 namespace SetOutOfBandPairingData =
67 extensions::api::experimental_bluetooth::SetOutOfBandPairingData; 67 extensions::api::experimental_bluetooth::SetOutOfBandPairingData;
68 namespace Write = extensions::api::experimental_bluetooth::Write; 68 namespace Write = extensions::api::experimental_bluetooth::Write;
69 69
70 namespace extensions { 70 namespace extensions {
71 namespace api { 71 namespace api {
72 72
73 #if defined(OS_CHROMEOS) 73 #if defined(OS_CHROMEOS)
74 74
75 bool BluetoothIsAvailableFunction::RunImpl() { 75 bool BluetoothIsAvailableFunction::RunImpl() {
76 result_.reset(Value::CreateBooleanValue(GetAdapter(profile())->IsPresent())); 76 SetSingleResult(
77 Value::CreateBooleanValue(GetAdapter(profile())->IsPresent()));
77 return true; 78 return true;
78 } 79 }
79 80
80 bool BluetoothIsPoweredFunction::RunImpl() { 81 bool BluetoothIsPoweredFunction::RunImpl() {
81 result_.reset(Value::CreateBooleanValue(GetAdapter(profile())->IsPowered())); 82 SetSingleResult(
83 Value::CreateBooleanValue(GetAdapter(profile())->IsPowered()));
82 return true; 84 return true;
83 } 85 }
84 86
85 bool BluetoothGetAddressFunction::RunImpl() { 87 bool BluetoothGetAddressFunction::RunImpl() {
86 result_.reset(Value::CreateStringValue(GetAdapter(profile())->address())); 88 SetSingleResult(Value::CreateStringValue(GetAdapter(profile())->address()));
87 return true; 89 return true;
88 } 90 }
89 91
90 BluetoothGetDevicesFunction::BluetoothGetDevicesFunction() 92 BluetoothGetDevicesFunction::BluetoothGetDevicesFunction()
91 : callbacks_pending_(0) {} 93 : callbacks_pending_(0) {}
92 94
93 void BluetoothGetDevicesFunction::AddDeviceIfTrueCallback( 95 void BluetoothGetDevicesFunction::AddDeviceIfTrueCallback(
94 ListValue* list, 96 ListValue* list,
95 const chromeos::BluetoothDevice* device, 97 const chromeos::BluetoothDevice* device,
96 bool shouldAdd) { 98 bool shouldAdd) {
97 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 99 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
98 100
99 if (shouldAdd) 101 if (shouldAdd)
100 list->Append(experimental_bluetooth::BluetoothDeviceToValue(*device)); 102 list->Append(experimental_bluetooth::BluetoothDeviceToValue(*device));
101 103
102 callbacks_pending_--; 104 callbacks_pending_--;
103 if (callbacks_pending_ == -1) 105 if (callbacks_pending_ == -1)
104 SendResponse(true); 106 SendResponse(true);
105 } 107 }
106 108
107 bool BluetoothGetDevicesFunction::RunImpl() { 109 bool BluetoothGetDevicesFunction::RunImpl() {
108 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 110 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
109 111
110 scoped_ptr<GetDevices::Params> params(GetDevices::Params::Create(*args_)); 112 scoped_ptr<GetDevices::Params> params(GetDevices::Params::Create(*args_));
111 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); 113 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL);
112 const experimental_bluetooth::GetDevicesOptions& options = params->options; 114 const experimental_bluetooth::GetDevicesOptions& options = params->options;
113 115
114 ListValue* matches = new ListValue; 116 ListValue* matches = new ListValue;
115 result_.reset(matches); 117 SetSingleResult(matches);
116 118
117 CHECK_EQ(0, callbacks_pending_); 119 CHECK_EQ(0, callbacks_pending_);
118 120
119 chromeos::BluetoothAdapter::DeviceList devices = 121 chromeos::BluetoothAdapter::DeviceList devices =
120 GetMutableAdapter(profile())->GetDevices(); 122 GetMutableAdapter(profile())->GetDevices();
121 for (chromeos::BluetoothAdapter::DeviceList::iterator i = devices.begin(); 123 for (chromeos::BluetoothAdapter::DeviceList::iterator i = devices.begin();
122 i != devices.end(); ++i) { 124 i != devices.end(); ++i) {
123 chromeos::BluetoothDevice* device = *i; 125 chromeos::BluetoothDevice* device = *i;
124 126
125 if (options.uuid.get() != NULL && 127 if (options.uuid.get() != NULL &&
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 180
179 chromeos::BluetoothDevice* device = 181 chromeos::BluetoothDevice* device =
180 GetMutableAdapter(profile())->GetDevice(options.device_address); 182 GetMutableAdapter(profile())->GetDevice(options.device_address);
181 if (!device) { 183 if (!device) {
182 SendResponse(false); 184 SendResponse(false);
183 SetError(kInvalidDevice); 185 SetError(kInvalidDevice);
184 return false; 186 return false;
185 } 187 }
186 188
187 ListValue* services = new ListValue; 189 ListValue* services = new ListValue;
188 result_.reset(services); 190 SetSingleResult(services);
189 191
190 device->GetServiceRecords( 192 device->GetServiceRecords(
191 base::Bind(&BluetoothGetServicesFunction::GetServiceRecordsCallback, 193 base::Bind(&BluetoothGetServicesFunction::GetServiceRecordsCallback,
192 this, 194 this,
193 services), 195 services),
194 base::Bind(&BluetoothGetServicesFunction::OnErrorCallback, 196 base::Bind(&BluetoothGetServicesFunction::OnErrorCallback,
195 this)); 197 this));
196 198
197 return true; 199 return true;
198 } 200 }
199 201
200 void BluetoothConnectFunction::ConnectToServiceCallback( 202 void BluetoothConnectFunction::ConnectToServiceCallback(
201 const chromeos::BluetoothDevice* device, 203 const chromeos::BluetoothDevice* device,
202 const std::string& service_uuid, 204 const std::string& service_uuid,
203 scoped_refptr<chromeos::BluetoothSocket> socket) { 205 scoped_refptr<chromeos::BluetoothSocket> socket) {
204 if (socket.get()) { 206 if (socket.get()) {
205 int socket_id = GetEventRouter(profile())->RegisterSocket(socket); 207 int socket_id = GetEventRouter(profile())->RegisterSocket(socket);
206 208
207 experimental_bluetooth::Socket result_socket; 209 experimental_bluetooth::Socket result_socket;
208 experimental_bluetooth::BluetoothDeviceToApiDevice( 210 experimental_bluetooth::BluetoothDeviceToApiDevice(
209 *device, &result_socket.device); 211 *device, &result_socket.device);
210 result_socket.service_uuid = service_uuid; 212 result_socket.service_uuid = service_uuid;
211 result_socket.id = socket_id; 213 result_socket.id = socket_id;
212 result_.reset(result_socket.ToValue().release()); 214 SetSingleResult(result_socket.ToValue().release());
213 SendResponse(true); 215 SendResponse(true);
214 } else { 216 } else {
215 SetError(kFailedToConnect); 217 SetError(kFailedToConnect);
216 SendResponse(false); 218 SendResponse(false);
217 } 219 }
218 } 220 }
219 221
220 bool BluetoothConnectFunction::RunImpl() { 222 bool BluetoothConnectFunction::RunImpl() {
221 scoped_ptr<Connect::Params> params(Connect::Params::Create(*args_)); 223 scoped_ptr<Connect::Params> params(Connect::Params::Create(*args_));
222 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); 224 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL);
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
278 buffer_size - total_bytes_read); 280 buffer_size - total_bytes_read);
279 errsv = errno; 281 errsv = errno;
280 if (bytes_read <= 0) 282 if (bytes_read <= 0)
281 break; 283 break;
282 284
283 total_bytes_read += bytes_read; 285 total_bytes_read += bytes_read;
284 } 286 }
285 287
286 if (total_bytes_read > 0) { 288 if (total_bytes_read > 0) {
287 success_ = true; 289 success_ = true;
288 result_.reset(base::BinaryValue::Create(all_bytes, total_bytes_read)); 290 SetSingleResult(base::BinaryValue::Create(all_bytes, total_bytes_read));
289 } else { 291 } else {
290 success_ = (errsv == EAGAIN || errsv == EWOULDBLOCK); 292 success_ = (errsv == EAGAIN || errsv == EWOULDBLOCK);
291 free(all_bytes); 293 free(all_bytes);
292 } 294 }
293 295
294 if (!success_) 296 if (!success_)
295 SetError(safe_strerror(errsv)); 297 SetError(safe_strerror(errsv));
296 } 298 }
297 299
298 bool BluetoothReadFunction::Respond() { 300 bool BluetoothReadFunction::Respond() {
(...skipping 26 matching lines...) Expand all
325 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); 327 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
326 328
327 if (socket_.get() == NULL) 329 if (socket_.get() == NULL)
328 return; 330 return;
329 331
330 ssize_t bytes_written = write(socket_->fd(), 332 ssize_t bytes_written = write(socket_->fd(),
331 data_to_write_->GetBuffer(), data_to_write_->GetSize()); 333 data_to_write_->GetBuffer(), data_to_write_->GetSize());
332 int errsv = errno; 334 int errsv = errno;
333 335
334 if (bytes_written > 0) { 336 if (bytes_written > 0) {
335 result_.reset(Value::CreateIntegerValue(bytes_written)); 337 SetSingleResult(Value::CreateIntegerValue(bytes_written));
336 success_ = true; 338 success_ = true;
337 } else { 339 } else {
338 result_.reset(0); 340 SetSingleResult(0);
339 success_ = (errsv == EAGAIN || errsv == EWOULDBLOCK); 341 success_ = (errsv == EAGAIN || errsv == EWOULDBLOCK);
340 } 342 }
341 343
342 if (!success_) 344 if (!success_)
343 SetError(safe_strerror(errsv)); 345 SetError(safe_strerror(errsv));
344 } 346 }
345 347
346 bool BluetoothWriteFunction::Respond() { 348 bool BluetoothWriteFunction::Respond() {
347 return success_; 349 return success_;
348 } 350 }
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
418 base::BinaryValue* randomizer = base::BinaryValue::CreateWithCopiedBuffer( 420 base::BinaryValue* randomizer = base::BinaryValue::CreateWithCopiedBuffer(
419 reinterpret_cast<const char*>(data.randomizer), 421 reinterpret_cast<const char*>(data.randomizer),
420 chromeos::kBluetoothOutOfBandPairingDataSize); 422 chromeos::kBluetoothOutOfBandPairingDataSize);
421 423
422 // TODO(bryeung): convert to experimental_bluetooth::OutOfBandPairingData 424 // TODO(bryeung): convert to experimental_bluetooth::OutOfBandPairingData
423 // when ArrayBuffer support within objects is completed. 425 // when ArrayBuffer support within objects is completed.
424 DictionaryValue* result = new DictionaryValue(); 426 DictionaryValue* result = new DictionaryValue();
425 result->Set("hash", hash); 427 result->Set("hash", hash);
426 result->Set("randomizer", randomizer); 428 result->Set("randomizer", randomizer);
427 429
428 result_.reset(result); 430 SetSingleResult(result);
429 431
430 SendResponse(true); 432 SendResponse(true);
431 } 433 }
432 434
433 void BluetoothGetLocalOutOfBandPairingDataFunction::ErrorCallback() { 435 void BluetoothGetLocalOutOfBandPairingDataFunction::ErrorCallback() {
434 SetError(kCouldNotGetLocalOutOfBandPairingData); 436 SetError(kCouldNotGetLocalOutOfBandPairingData);
435 SendResponse(false); 437 SendResponse(false);
436 } 438 }
437 439
438 bool BluetoothGetLocalOutOfBandPairingDataFunction::RunImpl() { 440 bool BluetoothGetLocalOutOfBandPairingDataFunction::RunImpl() {
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
573 #endif 575 #endif
574 576
575 BluetoothReadFunction::BluetoothReadFunction() {} 577 BluetoothReadFunction::BluetoothReadFunction() {}
576 BluetoothReadFunction::~BluetoothReadFunction() {} 578 BluetoothReadFunction::~BluetoothReadFunction() {}
577 579
578 BluetoothWriteFunction::BluetoothWriteFunction() {} 580 BluetoothWriteFunction::BluetoothWriteFunction() {}
579 BluetoothWriteFunction::~BluetoothWriteFunction() {} 581 BluetoothWriteFunction::~BluetoothWriteFunction() {}
580 582
581 } // namespace api 583 } // namespace api
582 } // namespace extensions 584 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698