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

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

Issue 177113013: Bluetooth: add Device events, and cleanup JS API (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@arman-patch
Patch Set: Created 6 years, 9 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"
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 88
89 // static 89 // static
90 BluetoothAPI* BluetoothAPI::Get(BrowserContext* context) { 90 BluetoothAPI* BluetoothAPI::Get(BrowserContext* context) {
91 return GetFactoryInstance()->GetForProfile(context); 91 return GetFactoryInstance()->GetForProfile(context);
92 } 92 }
93 93
94 BluetoothAPI::BluetoothAPI(BrowserContext* context) 94 BluetoothAPI::BluetoothAPI(BrowserContext* context)
95 : browser_context_(context) { 95 : browser_context_(context) {
96 ExtensionSystem::Get(browser_context_)->event_router()->RegisterObserver( 96 ExtensionSystem::Get(browser_context_)->event_router()->RegisterObserver(
97 this, bluetooth::OnAdapterStateChanged::kEventName); 97 this, bluetooth::OnAdapterStateChanged::kEventName);
98 ExtensionSystem::Get(browser_context_)->event_router()->RegisterObserver(
99 this, bluetooth::OnDeviceAdded::kEventName);
100 ExtensionSystem::Get(browser_context_)->event_router()->RegisterObserver(
101 this, bluetooth::OnDeviceChanged::kEventName);
102 ExtensionSystem::Get(browser_context_)->event_router()->RegisterObserver(
103 this, bluetooth::OnDeviceRemoved::kEventName);
98 } 104 }
99 105
100 BluetoothAPI::~BluetoothAPI() { 106 BluetoothAPI::~BluetoothAPI() {
101 } 107 }
102 108
103 ExtensionBluetoothEventRouter* BluetoothAPI::bluetooth_event_router() { 109 ExtensionBluetoothEventRouter* BluetoothAPI::bluetooth_event_router() {
104 if (!bluetooth_event_router_) 110 if (!bluetooth_event_router_)
105 bluetooth_event_router_.reset( 111 bluetooth_event_router_.reset(
106 new ExtensionBluetoothEventRouter(browser_context_)); 112 new ExtensionBluetoothEventRouter(browser_context_));
107 113
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
264 270
265 bool BluetoothGetAdapterStateFunction::DoWork( 271 bool BluetoothGetAdapterStateFunction::DoWork(
266 scoped_refptr<BluetoothAdapter> adapter) { 272 scoped_refptr<BluetoothAdapter> adapter) {
267 bluetooth::AdapterState state; 273 bluetooth::AdapterState state;
268 PopulateAdapterState(*adapter.get(), &state); 274 PopulateAdapterState(*adapter.get(), &state);
269 SetResult(state.ToValue().release()); 275 SetResult(state.ToValue().release());
270 SendResponse(true); 276 SendResponse(true);
271 return true; 277 return true;
272 } 278 }
273 279
274 BluetoothGetDevicesFunction::BluetoothGetDevicesFunction()
275 : device_events_sent_(0) {}
276
277 void BluetoothGetDevicesFunction::DispatchDeviceSearchResult(
278 const BluetoothDevice& device) {
279 bluetooth::Device extension_device;
280 bluetooth::BluetoothDeviceToApiDevice(device, &extension_device);
281 GetEventRouter(browser_context())->DispatchDeviceEvent(
282 extensions::event_names::kBluetoothOnDeviceSearchResult,
283 extension_device);
284
285 device_events_sent_++;
286 }
287
288 void BluetoothGetDevicesFunction::FinishDeviceSearch() {
289 scoped_ptr<base::ListValue> args(new base::ListValue());
290 scoped_ptr<base::DictionaryValue> info(new base::DictionaryValue());
291 info->SetInteger("expectedEventCount", device_events_sent_);
292 args->Append(info.release());
293
294 scoped_ptr<extensions::Event> event(new extensions::Event(
295 extensions::event_names::kBluetoothOnDeviceSearchFinished, args.Pass()));
296 extensions::ExtensionSystem::Get(browser_context())
297 ->event_router()
298 ->BroadcastEvent(event.Pass());
299
300 SendResponse(true);
301 }
302
303 bool BluetoothGetDevicesFunction::DoWork( 280 bool BluetoothGetDevicesFunction::DoWork(
304 scoped_refptr<BluetoothAdapter> adapter) { 281 scoped_refptr<BluetoothAdapter> adapter) {
305 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 282 base::ListValue* device_list = new base::ListValue;
armansito 2014/03/05 21:44:15 Keep this assertion?
keybuk 2014/03/06 21:33:44 Done.
306 283 SetResult(device_list);
307 scoped_ptr<GetDevices::Params> params(GetDevices::Params::Create(*args_));
308 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL);
309 const bluetooth::GetDevicesOptions& options = params->options;
310
311 std::string uuid;
312 if (options.profile.get() != NULL) {
313 uuid = options.profile->uuid;
314 if (!BluetoothDevice::IsUUIDValid(uuid)) {
315 SetError(kInvalidUuid);
316 SendResponse(false);
317 return false;
318 }
319 }
320 284
321 BluetoothAdapter::DeviceList devices = adapter->GetDevices(); 285 BluetoothAdapter::DeviceList devices = adapter->GetDevices();
322 for (BluetoothAdapter::DeviceList::const_iterator iter = devices.begin(); 286 for (BluetoothAdapter::DeviceList::const_iterator iter = devices.begin();
323 iter != devices.end(); 287 iter != devices.end();
324 ++iter) { 288 ++iter) {
325 const BluetoothDevice* device = *iter; 289 const BluetoothDevice* device = *iter;
326 DCHECK(device); 290 DCHECK(device);
327 if (uuid.empty() || device->ProvidesServiceWithUUID(uuid)) 291
328 DispatchDeviceSearchResult(*device); 292 bluetooth::Device extension_device;
293 bluetooth::BluetoothDeviceToApiDevice(*device, &extension_device);
294
295 device_list->Append(extension_device.ToValue().release());
329 } 296 }
330 297
331 FinishDeviceSearch(); 298 SendResponse(true);
332 299
333 return true; 300 return true;
334 } 301 }
335 302
336 void BluetoothGetServicesFunction::GetServiceRecordsCallback( 303 void BluetoothGetServicesFunction::GetServiceRecordsCallback(
337 base::ListValue* services, 304 base::ListValue* services,
338 const BluetoothDevice::ServiceRecordList& records) { 305 const BluetoothDevice::ServiceRecordList& records) {
339 for (BluetoothDevice::ServiceRecordList::const_iterator i = records.begin(); 306 for (BluetoothDevice::ServiceRecordList::const_iterator i = records.begin();
340 i != records.end(); ++i) { 307 i != records.end(); ++i) {
341 const BluetoothServiceRecord& record = **i; 308 const BluetoothServiceRecord& record = **i;
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after
629 return true; 596 return true;
630 } 597 }
631 598
632 void BluetoothStartDiscoveryFunction::OnSuccessCallback() { 599 void BluetoothStartDiscoveryFunction::OnSuccessCallback() {
633 SendResponse(true); 600 SendResponse(true);
634 } 601 }
635 602
636 void BluetoothStartDiscoveryFunction::OnErrorCallback() { 603 void BluetoothStartDiscoveryFunction::OnErrorCallback() {
637 SetError(kStartDiscoveryFailed); 604 SetError(kStartDiscoveryFailed);
638 SendResponse(false); 605 SendResponse(false);
639 GetEventRouter(browser_context())->OnListenerRemoved();
640 } 606 }
641 607
642 bool BluetoothStartDiscoveryFunction::DoWork( 608 bool BluetoothStartDiscoveryFunction::DoWork(
643 scoped_refptr<BluetoothAdapter> adapter) { 609 scoped_refptr<BluetoothAdapter> adapter) {
644 GetEventRouter(browser_context())->OnListenerAdded();
645 GetEventRouter(browser_context())->StartDiscoverySession( 610 GetEventRouter(browser_context())->StartDiscoverySession(
646 adapter, 611 adapter,
647 extension_id(), 612 extension_id(),
648 base::Bind(&BluetoothStartDiscoveryFunction::OnSuccessCallback, this), 613 base::Bind(&BluetoothStartDiscoveryFunction::OnSuccessCallback, this),
649 base::Bind(&BluetoothStartDiscoveryFunction::OnErrorCallback, this)); 614 base::Bind(&BluetoothStartDiscoveryFunction::OnErrorCallback, this));
650 615
651 return true; 616 return true;
652 } 617 }
653 618
654 void BluetoothStopDiscoveryFunction::OnSuccessCallback() { 619 void BluetoothStopDiscoveryFunction::OnSuccessCallback() {
655 SendResponse(true); 620 SendResponse(true);
656 GetEventRouter(browser_context())->OnListenerRemoved();
657 } 621 }
658 622
659 void BluetoothStopDiscoveryFunction::OnErrorCallback() { 623 void BluetoothStopDiscoveryFunction::OnErrorCallback() {
660 SetError(kStopDiscoveryFailed); 624 SetError(kStopDiscoveryFailed);
661 SendResponse(false); 625 SendResponse(false);
662 GetEventRouter(browser_context())->OnListenerRemoved();
663 } 626 }
664 627
665 bool BluetoothStopDiscoveryFunction::DoWork( 628 bool BluetoothStopDiscoveryFunction::DoWork(
666 scoped_refptr<BluetoothAdapter> adapter) { 629 scoped_refptr<BluetoothAdapter> adapter) {
667 GetEventRouter(browser_context())->StopDiscoverySession( 630 GetEventRouter(browser_context())->StopDiscoverySession(
668 adapter, 631 adapter,
669 extension_id(), 632 extension_id(),
670 base::Bind(&BluetoothStopDiscoveryFunction::OnSuccessCallback, this), 633 base::Bind(&BluetoothStopDiscoveryFunction::OnSuccessCallback, this),
671 base::Bind(&BluetoothStopDiscoveryFunction::OnErrorCallback, this)); 634 base::Bind(&BluetoothStopDiscoveryFunction::OnErrorCallback, this));
672 635
673 return true; 636 return true;
674 } 637 }
675 638
676 } // namespace api 639 } // namespace api
677 } // namespace extensions 640 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698