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

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

Issue 180163009: chrome.bluetooth API improvements. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix ChromeOS Full build. Created 6 years, 8 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 #include <string> 7 #include <string>
8 8
9 #include "base/lazy_instance.h" 9 #include "base/lazy_instance.h"
10 #include "base/memory/ref_counted.h" 10 #include "base/memory/ref_counted.h"
11 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h" 11 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h"
12 #include "chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h" 12 #include "chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h"
13 #include "chrome/browser/extensions/api/bluetooth/bluetooth_socket_event_dispatc her.h"
14 #include "chrome/browser/profiles/profile_manager.h"
13 #include "chrome/common/extensions/api/bluetooth.h" 15 #include "chrome/common/extensions/api/bluetooth.h"
14 #include "chrome/common/extensions/api/bluetooth/bluetooth_manifest_data.h" 16 #include "chrome/common/extensions/api/bluetooth/bluetooth_manifest_data.h"
15 #include "content/public/browser/browser_thread.h" 17 #include "content/public/browser/browser_thread.h"
16 #include "device/bluetooth/bluetooth_adapter.h" 18 #include "device/bluetooth/bluetooth_adapter.h"
17 #include "device/bluetooth/bluetooth_device.h" 19 #include "device/bluetooth/bluetooth_device.h"
18 #include "device/bluetooth/bluetooth_out_of_band_pairing_data.h" 20 #include "device/bluetooth/bluetooth_out_of_band_pairing_data.h"
19 #include "device/bluetooth/bluetooth_profile.h" 21 #include "device/bluetooth/bluetooth_profile.h"
20 #include "device/bluetooth/bluetooth_service_record.h" 22 #include "device/bluetooth/bluetooth_service_record.h"
21 #include "device/bluetooth/bluetooth_socket.h" 23 #include "device/bluetooth/bluetooth_socket.h"
22 #include "extensions/browser/event_router.h" 24 #include "extensions/browser/event_router.h"
23 #include "extensions/browser/extension_system.h" 25 #include "extensions/browser/extension_system.h"
24 #include "extensions/common/permissions/permissions_data.h" 26 #include "extensions/common/permissions/permissions_data.h"
25 #include "net/base/io_buffer.h" 27 #include "net/base/io_buffer.h"
26 28
27 using content::BrowserContext; 29 using content::BrowserContext;
30 using content::BrowserThread;
31
28 using device::BluetoothAdapter; 32 using device::BluetoothAdapter;
29 using device::BluetoothDevice; 33 using device::BluetoothDevice;
30 using device::BluetoothProfile; 34 using device::BluetoothProfile;
31 using device::BluetoothServiceRecord; 35 using device::BluetoothServiceRecord;
32 using device::BluetoothSocket; 36 using device::BluetoothSocket;
33 37
34 namespace { 38 using extensions::BluetoothApiSocket;
35 39
36 extensions::BluetoothEventRouter* GetEventRouter(BrowserContext* context) { 40 namespace AddProfile = extensions::api::bluetooth::AddProfile;
37 return extensions::BluetoothAPI::Get(context)->bluetooth_event_router(); 41 namespace bluetooth = extensions::api::bluetooth;
38 } 42 namespace Connect = extensions::api::bluetooth::Connect;
39 43 namespace Disconnect = extensions::api::bluetooth::Disconnect;
40 } // namespace 44 namespace GetDevice = extensions::api::bluetooth::GetDevice;
45 namespace GetDevices = extensions::api::bluetooth::GetDevices;
46 namespace RemoveProfile = extensions::api::bluetooth::RemoveProfile;
47 namespace SetOutOfBandPairingData =
48 extensions::api::bluetooth::SetOutOfBandPairingData;
49 namespace Send = extensions::api::bluetooth::Send;
41 50
42 namespace { 51 namespace {
43 52
44 const char kCouldNotGetLocalOutOfBandPairingData[] = 53 const char kCouldNotGetLocalOutOfBandPairingData[] =
45 "Could not get local Out Of Band Pairing Data"; 54 "Could not get local Out Of Band Pairing Data";
46 const char kCouldNotSetOutOfBandPairingData[] = 55 const char kCouldNotSetOutOfBandPairingData[] =
47 "Could not set Out Of Band Pairing Data"; 56 "Could not set Out Of Band Pairing Data";
48 const char kFailedToConnect[] = "Connection failed";
49 const char kInvalidDevice[] = "Invalid device"; 57 const char kInvalidDevice[] = "Invalid device";
50 const char kInvalidUuid[] = "Invalid UUID"; 58 const char kInvalidUuid[] = "Invalid UUID";
51 const char kPermissionDenied[] = "Permission to add profile denied."; 59 const char kPermissionDenied[] = "Permission to add profile denied.";
52 const char kProfileAlreadyRegistered[] = 60 const char kProfileAlreadyRegistered[] =
53 "This profile has already been registered"; 61 "This profile has already been registered";
54 const char kProfileNotFound[] = "Profile not found: invalid uuid"; 62 const char kProfileNotFound[] = "Profile not found: invalid uuid";
55 const char kProfileRegistrationFailed[] = "Profile registration failed"; 63 const char kProfileRegistrationFailed[] = "Profile registration failed";
56 const char kSocketNotFoundError[] = "Socket not found: invalid socket id"; 64 const char kSocketNotFoundError[] = "Socket not found: invalid socket id";
57 const char kStartDiscoveryFailed[] = "Starting discovery failed"; 65 const char kStartDiscoveryFailed[] = "Starting discovery failed";
58 const char kStopDiscoveryFailed[] = "Failed to stop discovery"; 66 const char kStopDiscoveryFailed[] = "Failed to stop discovery";
59 67
68 extensions::BluetoothEventRouter* GetEventRouter(BrowserContext* context) {
69 // Note: |context| is valid on UI thread only.
70 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
71 return extensions::BluetoothAPI::Get(context)->event_router();
72 }
73
74 linked_ptr<bluetooth::Socket> CreateSocketInfo(int socket_id,
75 BluetoothApiSocket* socket) {
76 DCHECK(BrowserThread::CurrentlyOn(BluetoothApiSocket::kThreadId));
77 linked_ptr<bluetooth::Socket> socket_info(new bluetooth::Socket());
78 // This represents what we know about the socket, and does not call through
79 // to the system.
80 socket_info->id = socket_id;
81 if (!socket->name().empty()) {
82 socket_info->name.reset(new std::string(socket->name()));
83 }
84 socket_info->persistent = socket->persistent();
85 if (socket->buffer_size() > 0) {
86 socket_info->buffer_size.reset(new int(socket->buffer_size()));
87 }
88 socket_info->paused = socket->paused();
89 socket_info->device.address = socket->device_address();
90 socket_info->uuid = socket->uuid().canonical_value();
91
92 return socket_info;
93 }
94
95 void SetSocketProperties(extensions::BluetoothApiSocket* socket,
96 bluetooth::SocketProperties* properties) {
97 if (properties->name.get()) {
98 socket->set_name(*properties->name.get());
99 }
100 if (properties->persistent.get()) {
101 socket->set_persistent(*properties->persistent.get());
102 }
103 if (properties->buffer_size.get()) {
104 // buffer size is validated when issuing the actual Recv operation
105 // on the socket.
106 socket->set_buffer_size(*properties->buffer_size.get());
107 }
108 }
109
110 static void DispatchConnectionEventWorker(
111 void* browser_context_id,
112 const std::string& extension_id,
113 const device::BluetoothUUID& profile_uuid,
114 const device::BluetoothDevice* device,
115 scoped_refptr<device::BluetoothSocket> socket) {
116 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
117
118 content::BrowserContext* context =
119 reinterpret_cast<content::BrowserContext*>(browser_context_id);
120 if (!extensions::ExtensionsBrowserClient::Get()->IsValidContext(context))
121 return;
122
123 extensions::BluetoothAPI* bluetooth_api =
124 extensions::BluetoothAPI::Get(context);
125 if (!bluetooth_api)
126 return;
127
128 bluetooth_api->DispatchConnectionEvent(
129 extension_id, profile_uuid, device, socket);
130 }
131
60 } // namespace 132 } // namespace
61 133
62 namespace AddProfile = extensions::api::bluetooth::AddProfile;
63 namespace bluetooth = extensions::api::bluetooth;
64 namespace Connect = extensions::api::bluetooth::Connect;
65 namespace Disconnect = extensions::api::bluetooth::Disconnect;
66 namespace GetDevice = extensions::api::bluetooth::GetDevice;
67 namespace GetDevices = extensions::api::bluetooth::GetDevices;
68 namespace Read = extensions::api::bluetooth::Read;
69 namespace RemoveProfile = extensions::api::bluetooth::RemoveProfile;
70 namespace SetOutOfBandPairingData =
71 extensions::api::bluetooth::SetOutOfBandPairingData;
72 namespace Write = extensions::api::bluetooth::Write;
73
74 namespace extensions { 134 namespace extensions {
75 135
76 static base::LazyInstance<BrowserContextKeyedAPIFactory<BluetoothAPI> > 136 static base::LazyInstance<BrowserContextKeyedAPIFactory<BluetoothAPI> >
77 g_factory = LAZY_INSTANCE_INITIALIZER; 137 g_factory = LAZY_INSTANCE_INITIALIZER;
78 138
79 // static 139 // static
80 BrowserContextKeyedAPIFactory<BluetoothAPI>* 140 BrowserContextKeyedAPIFactory<BluetoothAPI>*
81 BluetoothAPI::GetFactoryInstance() { 141 BluetoothAPI::GetFactoryInstance() {
82 return g_factory.Pointer(); 142 return g_factory.Pointer();
83 } 143 }
84 144
85 // static 145 // static
86 BluetoothAPI* BluetoothAPI::Get(BrowserContext* context) { 146 BluetoothAPI* BluetoothAPI::Get(BrowserContext* context) {
147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
87 return GetFactoryInstance()->Get(context); 148 return GetFactoryInstance()->Get(context);
88 } 149 }
89 150
90 BluetoothAPI::BluetoothAPI(BrowserContext* context) 151 BluetoothAPI::ConnectionParams::ConnectionParams() {}
152
153 BluetoothAPI::ConnectionParams::~ConnectionParams() {}
154
155 BluetoothAPI::BluetoothAPI(content::BrowserContext* context)
91 : browser_context_(context) { 156 : browser_context_(context) {
157 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
92 ExtensionSystem::Get(browser_context_)->event_router()->RegisterObserver( 158 ExtensionSystem::Get(browser_context_)->event_router()->RegisterObserver(
93 this, bluetooth::OnAdapterStateChanged::kEventName); 159 this, bluetooth::OnAdapterStateChanged::kEventName);
94 ExtensionSystem::Get(browser_context_)->event_router()->RegisterObserver( 160 ExtensionSystem::Get(browser_context_)->event_router()->RegisterObserver(
95 this, bluetooth::OnDeviceAdded::kEventName); 161 this, bluetooth::OnDeviceAdded::kEventName);
96 ExtensionSystem::Get(browser_context_)->event_router()->RegisterObserver( 162 ExtensionSystem::Get(browser_context_)->event_router()->RegisterObserver(
97 this, bluetooth::OnDeviceChanged::kEventName); 163 this, bluetooth::OnDeviceChanged::kEventName);
98 ExtensionSystem::Get(browser_context_)->event_router()->RegisterObserver( 164 ExtensionSystem::Get(browser_context_)->event_router()->RegisterObserver(
99 this, bluetooth::OnDeviceRemoved::kEventName); 165 this, bluetooth::OnDeviceRemoved::kEventName);
100 } 166 }
101 167
102 BluetoothAPI::~BluetoothAPI() { 168 BluetoothAPI::~BluetoothAPI() {}
103 } 169
104 170 BluetoothEventRouter* BluetoothAPI::event_router() {
105 BluetoothEventRouter* BluetoothAPI::bluetooth_event_router() { 171 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
106 if (!bluetooth_event_router_) 172 if (!event_router_) {
107 bluetooth_event_router_.reset(new BluetoothEventRouter(browser_context_)); 173 event_router_.reset(new BluetoothEventRouter(browser_context_));
108 174 }
109 return bluetooth_event_router_.get(); 175 return event_router_.get();
176 }
177
178 scoped_refptr<BluetoothAPI::SocketData> BluetoothAPI::socket_data() {
179 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
180 if (!socket_data_) {
181 ApiResourceManager<BluetoothApiSocket>* socket_manager =
182 ApiResourceManager<BluetoothApiSocket>::Get(browser_context_);
183 DCHECK(socket_manager)
184 << "There is no socket manager. "
185 "If this assertion is failing during a test, then it is likely that "
186 "TestExtensionSystem is failing to provide an instance of "
187 "ApiResourceManager<BluetoothApiSocket>.";
188
189 socket_data_ = socket_manager->data_;
190 }
191 return socket_data_;
192 }
193
194 scoped_refptr<api::BluetoothSocketEventDispatcher>
195 BluetoothAPI::socket_event_dispatcher() {
196 if (!socket_event_dispatcher_) {
197 socket_event_dispatcher_ = new api::BluetoothSocketEventDispatcher(
198 browser_context_, socket_data());
199 }
200 return socket_event_dispatcher_;
110 } 201 }
111 202
112 void BluetoothAPI::Shutdown() { 203 void BluetoothAPI::Shutdown() {
204 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
113 ExtensionSystem::Get(browser_context_)->event_router()->UnregisterObserver( 205 ExtensionSystem::Get(browser_context_)->event_router()->UnregisterObserver(
114 this); 206 this);
115 } 207 }
116 208
117 void BluetoothAPI::OnListenerAdded(const EventListenerInfo& details) { 209 void BluetoothAPI::OnListenerAdded(const EventListenerInfo& details) {
118 if (bluetooth_event_router()->IsBluetoothSupported()) 210 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
119 bluetooth_event_router()->OnListenerAdded(); 211 if (event_router()->IsBluetoothSupported())
212 event_router()->OnListenerAdded();
120 } 213 }
121 214
122 void BluetoothAPI::OnListenerRemoved(const EventListenerInfo& details) { 215 void BluetoothAPI::OnListenerRemoved(const EventListenerInfo& details) {
123 if (bluetooth_event_router()->IsBluetoothSupported()) 216 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
124 bluetooth_event_router()->OnListenerRemoved(); 217 if (event_router()->IsBluetoothSupported())
218 event_router()->OnListenerRemoved();
219 }
220
221 void BluetoothAPI::DispatchConnectionEvent(
222 const std::string& extension_id,
223 const device::BluetoothUUID& uuid,
224 const device::BluetoothDevice* device,
225 scoped_refptr<device::BluetoothSocket> socket) {
226 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
227
228 if (!event_router()->HasProfile(uuid))
229 return;
230
231 extensions::BluetoothAPI::ConnectionParams params;
232 params.browser_context_id = browser_context_;
233 params.thread_id = BluetoothApiSocket::kThreadId;
234 params.extension_id = extension_id;
235 params.uuid = uuid;
236 params.device_address = device->GetAddress();
237 params.socket = socket;
238 params.socket_data = socket_data();
239 BrowserThread::PostTask(
240 params.thread_id, FROM_HERE, base::Bind(&RegisterSocket, params));
241 }
242
243 // static
244 void BluetoothAPI::RegisterSocket(
245 const BluetoothAPI::ConnectionParams& params) {
246 DCHECK(BrowserThread::CurrentlyOn(params.thread_id));
247
248 BluetoothApiSocket* api_socket = new BluetoothApiSocket(
249 params.extension_id, params.socket, params.device_address, params.uuid);
250 int socket_id = params.socket_data->Add(api_socket);
251
252 BrowserThread::PostTask(BrowserThread::UI,
253 FROM_HERE,
254 base::Bind(&RegisterSocketUI, params, socket_id));
255 }
256
257 // static
258 void BluetoothAPI::RegisterSocketUI(const ConnectionParams& params,
259 int socket_id) {
260 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
261
262 content::BrowserContext* context =
263 reinterpret_cast<content::BrowserContext*>(params.browser_context_id);
264 if (!extensions::ExtensionsBrowserClient::Get()->IsValidContext(context))
265 return;
266
267 BluetoothAPI::Get(context)->event_router()->GetAdapter(
268 base::Bind(&RegisterSocketWithAdapterUI, params, socket_id));
269 }
270
271 void BluetoothAPI::RegisterSocketWithAdapterUI(
272 const ConnectionParams& params,
273 int socket_id,
274 scoped_refptr<device::BluetoothAdapter> adapter) {
275 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
276
277 content::BrowserContext* context =
278 reinterpret_cast<content::BrowserContext*>(params.browser_context_id);
279 if (!extensions::ExtensionsBrowserClient::Get()->IsValidContext(context))
280 return;
281
282 BluetoothDevice* device = adapter->GetDevice(params.device_address);
283 if (!device)
284 return;
285
286 api::bluetooth::Socket result_socket;
287 bluetooth::BluetoothDeviceToApiDevice(*device, &result_socket.device);
288 result_socket.uuid = params.uuid.canonical_value();
289 result_socket.id = socket_id;
290
291 scoped_ptr<base::ListValue> args =
292 bluetooth::OnConnection::Create(result_socket);
293 scoped_ptr<Event> event(
294 new Event(bluetooth::OnConnection::kEventName, args.Pass()));
295
296 EventRouter* router = ExtensionSystem::Get(context)->event_router();
297 if (router)
298 router->DispatchEventToExtension(params.extension_id, event.Pass());
125 } 299 }
126 300
127 namespace api { 301 namespace api {
128 302
129 BluetoothAddProfileFunction::BluetoothAddProfileFunction() { 303 BluetoothSocketApiFunction::BluetoothSocketApiFunction() {}
130 } 304
305 BluetoothSocketApiFunction::~BluetoothSocketApiFunction() {}
306
307 bool BluetoothSocketApiFunction::RunImpl() {
308 if (!PrePrepare() || !Prepare()) {
309 return false;
310 }
311 AsyncWorkStart();
312 return true;
313 }
314
315 bool BluetoothSocketApiFunction::PrePrepare() {
316 socket_data_ = BluetoothAPI::Get(browser_context())->socket_data();
317 socket_event_dispatcher_ =
318 BluetoothAPI::Get(browser_context())->socket_event_dispatcher();
319 return socket_data_ && socket_event_dispatcher_;
320 }
321
322 void BluetoothSocketApiFunction::AsyncWorkStart() {
323 Work();
324 AsyncWorkCompleted();
325 }
326
327 void BluetoothSocketApiFunction::Work() {}
328
329 void BluetoothSocketApiFunction::AsyncWorkCompleted() {
330 SendResponse(Respond());
331 }
332
333 bool BluetoothSocketApiFunction::Respond() { return error_.empty(); }
334
335 BluetoothGetAdapterStateFunction::~BluetoothGetAdapterStateFunction() {}
336
337 bool BluetoothGetAdapterStateFunction::DoWork(
338 scoped_refptr<BluetoothAdapter> adapter) {
339 bluetooth::AdapterState state;
340 PopulateAdapterState(*adapter.get(), &state);
341 results_ = bluetooth::GetAdapterState::Results::Create(state);
342 SendResponse(true);
343 return true;
344 }
345
346 BluetoothGetDevicesFunction::~BluetoothGetDevicesFunction() {}
347
348 bool BluetoothGetDevicesFunction::DoWork(
349 scoped_refptr<BluetoothAdapter> adapter) {
350 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
351
352 base::ListValue* device_list = new base::ListValue;
353 SetResult(device_list);
354
355 BluetoothAdapter::DeviceList devices = adapter->GetDevices();
356 for (BluetoothAdapter::DeviceList::const_iterator iter = devices.begin();
357 iter != devices.end();
358 ++iter) {
359 const BluetoothDevice* device = *iter;
360 DCHECK(device);
361
362 bluetooth::Device extension_device;
363 bluetooth::BluetoothDeviceToApiDevice(*device, &extension_device);
364
365 device_list->Append(extension_device.ToValue().release());
366 }
367
368 SendResponse(true);
369
370 return true;
371 }
372
373 BluetoothGetDeviceFunction::~BluetoothGetDeviceFunction() {}
374
375 bool BluetoothGetDeviceFunction::DoWork(
376 scoped_refptr<BluetoothAdapter> adapter) {
377 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
378
379 scoped_ptr<GetDevice::Params> params(GetDevice::Params::Create(*args_));
380 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL);
381 const std::string& device_address = params->device_address;
382
383 BluetoothDevice* device = adapter->GetDevice(device_address);
384 if (device) {
385 bluetooth::Device extension_device;
386 bluetooth::BluetoothDeviceToApiDevice(*device, &extension_device);
387 SetResult(extension_device.ToValue().release());
388 SendResponse(true);
389 } else {
390 SetError(kInvalidDevice);
391 SendResponse(false);
392 }
393
394 return false;
395 }
396
397 BluetoothAddProfileFunction::BluetoothAddProfileFunction() {}
398
399 BluetoothAddProfileFunction::~BluetoothAddProfileFunction() {}
131 400
132 bool BluetoothAddProfileFunction::RunImpl() { 401 bool BluetoothAddProfileFunction::RunImpl() {
402 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
133 scoped_ptr<AddProfile::Params> params(AddProfile::Params::Create(*args_)); 403 scoped_ptr<AddProfile::Params> params(AddProfile::Params::Create(*args_));
134 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); 404 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL);
135 405
136 device::BluetoothUUID uuid(params->profile.uuid); 406 device::BluetoothUUID uuid(params->profile.uuid);
137 407
138 if (!uuid.IsValid()) { 408 if (!uuid.IsValid()) {
139 SetError(kInvalidUuid); 409 SetError(kInvalidUuid);
140 return false; 410 return false;
141 } 411 }
142 412
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 RegisterProfile( 448 RegisterProfile(
179 options, 449 options,
180 base::Bind(&BluetoothAddProfileFunction::OnProfileRegistered, this)); 450 base::Bind(&BluetoothAddProfileFunction::OnProfileRegistered, this));
181 451
182 return true; 452 return true;
183 } 453 }
184 454
185 void BluetoothAddProfileFunction::RegisterProfile( 455 void BluetoothAddProfileFunction::RegisterProfile(
186 const BluetoothProfile::Options& options, 456 const BluetoothProfile::Options& options,
187 const BluetoothProfile::ProfileCallback& callback) { 457 const BluetoothProfile::ProfileCallback& callback) {
458 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
188 BluetoothProfile::Register(uuid_, options, callback); 459 BluetoothProfile::Register(uuid_, options, callback);
189 } 460 }
190 461
191 void BluetoothAddProfileFunction::OnProfileRegistered( 462 void BluetoothAddProfileFunction::OnProfileRegistered(
192 BluetoothProfile* bluetooth_profile) { 463 BluetoothProfile* bluetooth_profile) {
464 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
193 if (!bluetooth_profile) { 465 if (!bluetooth_profile) {
194 SetError(kProfileRegistrationFailed); 466 SetError(kProfileRegistrationFailed);
195 SendResponse(false); 467 SendResponse(false);
196 return; 468 return;
197 } 469 }
198 470
199 if (GetEventRouter(browser_context())->HasProfile(uuid_)) { 471 if (GetEventRouter(browser_context())->HasProfile(uuid_)) {
200 bluetooth_profile->Unregister(); 472 bluetooth_profile->Unregister();
201 SetError(kProfileAlreadyRegistered); 473 SetError(kProfileAlreadyRegistered);
202 SendResponse(false); 474 SendResponse(false);
203 return; 475 return;
204 } 476 }
205 477
206 bluetooth_profile->SetConnectionCallback( 478 bluetooth_profile->SetConnectionCallback(
207 base::Bind(&BluetoothEventRouter::DispatchConnectionEvent, 479 base::Bind(&DispatchConnectionEventWorker,
208 base::Unretained(GetEventRouter(browser_context())), 480 browser_context(),
209 extension_id(), 481 extension_id(),
210 uuid_)); 482 uuid_));
211 GetEventRouter(browser_context()) 483 GetEventRouter(browser_context())
212 ->AddProfile(uuid_, extension_id(), bluetooth_profile); 484 ->AddProfile(uuid_, extension_id(), bluetooth_profile);
213 SendResponse(true); 485 SendResponse(true);
214 } 486 }
215 487
488 BluetoothRemoveProfileFunction::~BluetoothRemoveProfileFunction() {}
489
216 bool BluetoothRemoveProfileFunction::RunImpl() { 490 bool BluetoothRemoveProfileFunction::RunImpl() {
491 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
217 scoped_ptr<RemoveProfile::Params> params( 492 scoped_ptr<RemoveProfile::Params> params(
218 RemoveProfile::Params::Create(*args_)); 493 RemoveProfile::Params::Create(*args_));
219 494
220 device::BluetoothUUID uuid(params->profile.uuid); 495 device::BluetoothUUID uuid(params->profile.uuid);
221 496
222 if (!uuid.IsValid()) { 497 if (!uuid.IsValid()) {
223 SetError(kInvalidUuid); 498 SetError(kInvalidUuid);
224 return false; 499 return false;
225 } 500 }
226 501
227 if (!GetEventRouter(browser_context())->HasProfile(uuid)) { 502 if (!GetEventRouter(browser_context())->HasProfile(uuid)) {
228 SetError(kProfileNotFound); 503 SetError(kProfileNotFound);
229 return false; 504 return false;
230 } 505 }
231 506
232 GetEventRouter(browser_context())->RemoveProfile(uuid); 507 GetEventRouter(browser_context())->RemoveProfile(uuid);
233 return true; 508 return true;
234 } 509 }
235 510
236 bool BluetoothGetAdapterStateFunction::DoWork( 511 BluetoothConnectFunction::~BluetoothConnectFunction() {}
237 scoped_refptr<BluetoothAdapter> adapter) {
238 bluetooth::AdapterState state;
239 PopulateAdapterState(*adapter.get(), &state);
240 SetResult(state.ToValue().release());
241 SendResponse(true);
242 return true;
243 }
244
245 bool BluetoothGetDevicesFunction::DoWork(
246 scoped_refptr<BluetoothAdapter> adapter) {
247 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
248
249 base::ListValue* device_list = new base::ListValue;
250 SetResult(device_list);
251
252 BluetoothAdapter::DeviceList devices = adapter->GetDevices();
253 for (BluetoothAdapter::DeviceList::const_iterator iter = devices.begin();
254 iter != devices.end();
255 ++iter) {
256 const BluetoothDevice* device = *iter;
257 DCHECK(device);
258
259 bluetooth::Device extension_device;
260 bluetooth::BluetoothDeviceToApiDevice(*device, &extension_device);
261
262 device_list->Append(extension_device.ToValue().release());
263 }
264
265 SendResponse(true);
266
267 return true;
268 }
269
270 bool BluetoothGetDeviceFunction::DoWork(
271 scoped_refptr<BluetoothAdapter> adapter) {
272 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
273
274 scoped_ptr<GetDevice::Params> params(GetDevice::Params::Create(*args_));
275 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL);
276 const std::string& device_address = params->device_address;
277
278 BluetoothDevice* device = adapter->GetDevice(device_address);
279 if (device) {
280 bluetooth::Device extension_device;
281 bluetooth::BluetoothDeviceToApiDevice(*device, &extension_device);
282 SetResult(extension_device.ToValue().release());
283 SendResponse(true);
284 } else {
285 SetError(kInvalidDevice);
286 SendResponse(false);
287 }
288
289 return false;
290 }
291
292 void BluetoothConnectFunction::OnSuccessCallback() {
293 SendResponse(true);
294 }
295
296 void BluetoothConnectFunction::OnErrorCallback() {
297 SetError(kFailedToConnect);
298 SendResponse(false);
299 }
300 512
301 bool BluetoothConnectFunction::DoWork(scoped_refptr<BluetoothAdapter> adapter) { 513 bool BluetoothConnectFunction::DoWork(scoped_refptr<BluetoothAdapter> adapter) {
302 scoped_ptr<Connect::Params> params(Connect::Params::Create(*args_)); 514 scoped_ptr<Connect::Params> params(Connect::Params::Create(*args_));
303 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); 515 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL);
304 const bluetooth::ConnectOptions& options = params->options; 516 const bluetooth::ConnectOptions& options = params->options;
305 517
306 device::BluetoothUUID uuid(options.profile.uuid); 518 device::BluetoothUUID uuid(options.profile.uuid);
307 519
308 if (!uuid.IsValid()) { 520 if (!uuid.IsValid()) {
309 SetError(kInvalidUuid); 521 SetError(kInvalidUuid);
(...skipping 17 matching lines...) Expand all
327 } 539 }
328 540
329 device->ConnectToProfile( 541 device->ConnectToProfile(
330 bluetooth_profile, 542 bluetooth_profile,
331 base::Bind(&BluetoothConnectFunction::OnSuccessCallback, this), 543 base::Bind(&BluetoothConnectFunction::OnSuccessCallback, this),
332 base::Bind(&BluetoothConnectFunction::OnErrorCallback, this)); 544 base::Bind(&BluetoothConnectFunction::OnErrorCallback, this));
333 545
334 return true; 546 return true;
335 } 547 }
336 548
337 bool BluetoothDisconnectFunction::RunImpl() { 549 void BluetoothConnectFunction::OnSuccessCallback() {
338 scoped_ptr<Disconnect::Params> params(Disconnect::Params::Create(*args_)); 550 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
339 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); 551 SendResponse(true);
340 const bluetooth::DisconnectOptions& options = params->options;
341 return GetEventRouter(browser_context())->ReleaseSocket(options.socket.id);
342 } 552 }
343 553
344 BluetoothReadFunction::BluetoothReadFunction() : success_(false) {} 554 void BluetoothConnectFunction::OnErrorCallback(const std::string& error) {
345 BluetoothReadFunction::~BluetoothReadFunction() {} 555 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
556 SetError(error);
557 SendResponse(false);
558 }
346 559
347 bool BluetoothReadFunction::Prepare() { 560 BluetoothDisconnectFunction::BluetoothDisconnectFunction() {}
348 scoped_ptr<Read::Params> params(Read::Params::Create(*args_));
349 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL);
350 const bluetooth::ReadOptions& options = params->options;
351 561
352 socket_ = GetEventRouter(browser_context())->GetSocket(options.socket.id); 562 BluetoothDisconnectFunction::~BluetoothDisconnectFunction() {}
353 if (socket_.get() == NULL) {
354 SetError(kSocketNotFoundError);
355 return false;
356 }
357 563
358 success_ = false; 564 bool BluetoothDisconnectFunction::Prepare() {
565 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
566 params_ = Disconnect::Params::Create(*args_);
567 EXTENSION_FUNCTION_VALIDATE(params_.get() != NULL);
359 return true; 568 return true;
360 } 569 }
361 570
362 void BluetoothReadFunction::Work() { 571 void BluetoothDisconnectFunction::AsyncWorkStart() {
363 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 572 DCHECK(BrowserThread::CurrentlyOn(work_thread_id()));
364 573 BluetoothApiSocket* socket =
365 if (!socket_.get()) 574 socket_data_->Get(extension_id(), params_->options.socket_id);
575 if (!socket) {
576 error_ = kSocketNotFoundError;
366 return; 577 return;
367 578 }
368 scoped_refptr<net::GrowableIOBuffer> buffer(new net::GrowableIOBuffer); 579 socket->Disconnect(base::Bind(&BluetoothDisconnectFunction::OnSuccess, this));
369 success_ = socket_->Receive(buffer.get());
370 if (success_)
371 SetResult(base::BinaryValue::CreateWithCopiedBuffer(buffer->StartOfBuffer(),
372 buffer->offset()));
373 else
374 SetError(socket_->GetLastErrorMessage());
375 } 580 }
376 581
377 bool BluetoothReadFunction::Respond() { 582 void BluetoothDisconnectFunction::OnSuccess() {
378 return success_; 583 DCHECK(BrowserThread::CurrentlyOn(work_thread_id()));
584 socket_data_->Remove(extension_id(), params_->options.socket_id);
585 results_ = bluetooth::Disconnect::Results::Create();
586 AsyncWorkCompleted();
379 } 587 }
380 588
381 BluetoothWriteFunction::BluetoothWriteFunction() 589 BluetoothSendFunction::BluetoothSendFunction() : io_buffer_size_(0) {}
382 : success_(false), 590
383 data_to_write_(NULL) { 591 BluetoothSendFunction::~BluetoothSendFunction() {}
592
593 bool BluetoothSendFunction::Prepare() {
594 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
595 params_ = Send::Params::Create(*args_);
596 EXTENSION_FUNCTION_VALIDATE(params_.get() != NULL);
597 io_buffer_size_ = params_->data.size();
598 io_buffer_ = new net::WrappedIOBuffer(params_->data.data());
599 return true;
384 } 600 }
385 601
386 BluetoothWriteFunction::~BluetoothWriteFunction() {} 602 void BluetoothSendFunction::AsyncWorkStart() {
603 DCHECK(BrowserThread::CurrentlyOn(work_thread_id()));
604 BluetoothApiSocket* socket =
605 socket_data_->Get(extension_id(), params_->socket_id);
606 if (!socket) {
607 error_ = kSocketNotFoundError;
608 return;
609 }
610 socket->Send(io_buffer_,
611 io_buffer_size_,
612 base::Bind(&BluetoothSendFunction::OnSendSuccess, this),
613 base::Bind(&BluetoothSendFunction::OnSendError, this));
614 }
387 615
388 bool BluetoothWriteFunction::Prepare() { 616 void BluetoothSendFunction::OnSendSuccess(int bytes_sent) {
389 // TODO(bryeung): update to new-style parameter passing when ArrayBuffer 617 DCHECK(BrowserThread::CurrentlyOn(work_thread_id()));
390 // support is added 618 results_ = Send::Results::Create(bytes_sent);
391 base::DictionaryValue* options; 619 AsyncWorkCompleted();
392 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &options)); 620 }
393 621
394 base::DictionaryValue* socket; 622 void BluetoothSendFunction::OnSendError(const std::string& message) {
395 EXTENSION_FUNCTION_VALIDATE(options->GetDictionary("socket", &socket)); 623 DCHECK(BrowserThread::CurrentlyOn(work_thread_id()));
624 error_ = message;
625 AsyncWorkCompleted();
626 }
396 627
397 int socket_id; 628 BluetoothUpdateSocketFunction::BluetoothUpdateSocketFunction() {}
398 EXTENSION_FUNCTION_VALIDATE(socket->GetInteger("id", &socket_id));
399 629
400 socket_ = GetEventRouter(browser_context())->GetSocket(socket_id); 630 BluetoothUpdateSocketFunction::~BluetoothUpdateSocketFunction() {}
401 if (socket_.get() == NULL) { 631
402 SetError(kSocketNotFoundError); 632 bool BluetoothUpdateSocketFunction::Prepare() {
403 return false; 633 params_ = bluetooth::UpdateSocket::Params::Create(*args_);
634 EXTENSION_FUNCTION_VALIDATE(params_.get());
635 return true;
636 }
637
638 void BluetoothUpdateSocketFunction::Work() {
639 BluetoothApiSocket* socket =
640 socket_data_->Get(extension_id(), params_->socket_id);
641 if (!socket) {
642 error_ = kSocketNotFoundError;
643 return;
404 } 644 }
405 645
406 base::BinaryValue* tmp_data; 646 SetSocketProperties(socket, &params_.get()->properties);
407 EXTENSION_FUNCTION_VALIDATE(options->GetBinary("data", &tmp_data)); 647 results_ = bluetooth::UpdateSocket::Results::Create();
408 data_to_write_ = tmp_data;
409
410 success_ = false;
411 return socket_.get() != NULL;
412 } 648 }
413 649
414 void BluetoothWriteFunction::Work() { 650 BluetoothSetSocketPausedFunction::BluetoothSetSocketPausedFunction() {}
415 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
416 651
417 if (socket_.get() == NULL) 652 BluetoothSetSocketPausedFunction::~BluetoothSetSocketPausedFunction() {}
418 return;
419 653
420 scoped_refptr<net::WrappedIOBuffer> wrapped_io_buffer( 654 bool BluetoothSetSocketPausedFunction::Prepare() {
421 new net::WrappedIOBuffer(data_to_write_->GetBuffer())); 655 params_ = bluetooth::SetSocketPaused::Params::Create(*args_);
422 scoped_refptr<net::DrainableIOBuffer> drainable_io_buffer( 656 EXTENSION_FUNCTION_VALIDATE(params_.get());
423 new net::DrainableIOBuffer(wrapped_io_buffer.get(), 657 return true;
424 data_to_write_->GetSize()));
425 success_ = socket_->Send(drainable_io_buffer.get());
426 if (success_) {
427 if (drainable_io_buffer->BytesConsumed() > 0)
428 SetResult(new base::FundamentalValue(
429 drainable_io_buffer->BytesConsumed()));
430 else
431 results_.reset();
432 } else {
433 SetError(socket_->GetLastErrorMessage());
434 }
435 } 658 }
436 659
437 bool BluetoothWriteFunction::Respond() { 660 void BluetoothSetSocketPausedFunction::Work() {
438 return success_; 661 BluetoothApiSocket* socket =
662 socket_data_->Get(extension_id(), params_->socket_id);
663 if (!socket) {
664 error_ = kSocketNotFoundError;
665 return;
666 }
667
668 if (socket->paused() != params_->paused) {
669 socket->set_paused(params_->paused);
670 if (!params_->paused) {
671 socket_event_dispatcher_->OnSocketResume(extension_->id(),
672 params_->socket_id);
673 }
674 }
675
676 results_ = bluetooth::SetSocketPaused::Results::Create();
677 }
678
679 BluetoothGetSocketFunction::BluetoothGetSocketFunction() {}
680
681 BluetoothGetSocketFunction::~BluetoothGetSocketFunction() {}
682
683 bool BluetoothGetSocketFunction::Prepare() {
684 params_ = bluetooth::GetSocket::Params::Create(*args_);
685 EXTENSION_FUNCTION_VALIDATE(params_.get());
686 return true;
687 }
688
689 void BluetoothGetSocketFunction::Work() {
690 BluetoothApiSocket* socket =
691 socket_data_->Get(extension_id(), params_->socket_id);
692 if (!socket) {
693 error_ = kSocketNotFoundError;
694 return;
695 }
696
697 linked_ptr<bluetooth::Socket> socket_info =
698 CreateSocketInfo(params_->socket_id, socket);
699 results_ = bluetooth::GetSocket::Results::Create(*socket_info);
700 }
701
702 BluetoothGetSocketsFunction::BluetoothGetSocketsFunction() {}
703
704 BluetoothGetSocketsFunction::~BluetoothGetSocketsFunction() {}
705
706 bool BluetoothGetSocketsFunction::Prepare() { return true; }
707
708 void BluetoothGetSocketsFunction::Work() {
709 std::vector<linked_ptr<bluetooth::Socket> > socket_infos;
710 base::hash_set<int>* resource_ids =
711 socket_data_->GetResourceIds(extension_id());
712 if (resource_ids != NULL) {
713 for (base::hash_set<int>::iterator it = resource_ids->begin();
714 it != resource_ids->end();
715 ++it) {
716 int socket_id = *it;
717 BluetoothApiSocket* socket = socket_data_->Get(extension_id(), socket_id);
718 if (socket) {
719 socket_infos.push_back(CreateSocketInfo(socket_id, socket));
720 }
721 }
722 }
723 results_ = bluetooth::GetSockets::Results::Create(socket_infos);
439 } 724 }
440 725
441 void BluetoothSetOutOfBandPairingDataFunction::OnSuccessCallback() { 726 void BluetoothSetOutOfBandPairingDataFunction::OnSuccessCallback() {
442 SendResponse(true); 727 SendResponse(true);
443 } 728 }
444 729
445 void BluetoothSetOutOfBandPairingDataFunction::OnErrorCallback() { 730 void BluetoothSetOutOfBandPairingDataFunction::OnErrorCallback() {
446 SetError(kCouldNotSetOutOfBandPairingData); 731 SetError(kCouldNotSetOutOfBandPairingData);
447 SendResponse(false); 732 SendResponse(false);
448 } 733 }
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
572 adapter, 857 adapter,
573 extension_id(), 858 extension_id(),
574 base::Bind(&BluetoothStopDiscoveryFunction::OnSuccessCallback, this), 859 base::Bind(&BluetoothStopDiscoveryFunction::OnSuccessCallback, this),
575 base::Bind(&BluetoothStopDiscoveryFunction::OnErrorCallback, this)); 860 base::Bind(&BluetoothStopDiscoveryFunction::OnErrorCallback, this));
576 861
577 return true; 862 return true;
578 } 863 }
579 864
580 } // namespace api 865 } // namespace api
581 } // namespace extensions 866 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698