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

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

Powered by Google App Engine
This is Rietveld 408576698