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

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

Powered by Google App Engine
This is Rietveld 408576698