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 3 // found in the LICENSE file. 4 5 #include "chrome/browser/extensions/api/bluetooth/bluetooth_extension_function.h" 6 7 #include "base/memory/ref_counted.h" 8 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api.h" 9 #include "chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h" 10 #include "content/public/browser/browser_thread.h" 11 #include "device/bluetooth/bluetooth_adapter.h" 12 #include "device/bluetooth/bluetooth_adapter_factory.h" 13 14 using content::BrowserThread; 15 16 namespace { 17 18 const char kPlatformNotSupported[] = 19 "This operation is not supported on your platform"; 20 GetEventRouter(content::BrowserContext * context)21extensions::BluetoothEventRouter* GetEventRouter( 22 content::BrowserContext* context) { 23 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 24 return extensions::BluetoothAPI::Get(context)->event_router(); 25 } 26 IsBluetoothSupported(content::BrowserContext * context)27bool IsBluetoothSupported(content::BrowserContext* context) { 28 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 29 return GetEventRouter(context)->IsBluetoothSupported(); 30 } 31 GetAdapter(const device::BluetoothAdapterFactory::AdapterCallback callback,content::BrowserContext * context)32void GetAdapter(const device::BluetoothAdapterFactory::AdapterCallback callback, 33 content::BrowserContext* context) { 34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 35 GetEventRouter(context)->GetAdapter(callback); 36 } 37 38 } // namespace 39 40 namespace extensions { 41 namespace api { 42 BluetoothExtensionFunction()43BluetoothExtensionFunction::BluetoothExtensionFunction() { 44 } 45 ~BluetoothExtensionFunction()46BluetoothExtensionFunction::~BluetoothExtensionFunction() { 47 } 48 RunAsync()49bool BluetoothExtensionFunction::RunAsync() { 50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 51 52 if (!IsBluetoothSupported(browser_context())) { 53 SetError(kPlatformNotSupported); 54 return false; 55 } 56 GetAdapter(base::Bind(&BluetoothExtensionFunction::RunOnAdapterReady, this), 57 browser_context()); 58 59 return true; 60 } 61 RunOnAdapterReady(scoped_refptr<device::BluetoothAdapter> adapter)62void BluetoothExtensionFunction::RunOnAdapterReady( 63 scoped_refptr<device::BluetoothAdapter> adapter) { 64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 65 DoWork(adapter); 66 } 67 68 } // namespace api 69 } // namespace extensions 70