diff --git a/dom/bluetooth2/bluedroid/BluetoothInterface.cpp b/dom/bluetooth2/bluedroid/BluetoothInterface.cpp index d17359f3e115..619555ac6bed 100644 --- a/dom/bluetooth2/bluedroid/BluetoothInterface.cpp +++ b/dom/bluetooth2/bluedroid/BluetoothInterface.cpp @@ -5,6 +5,8 @@ * You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "BluetoothInterface.h" +#include "nsAutoPtr.h" +#include "nsThreadUtils.h" BEGIN_BLUETOOTH_NAMESPACE @@ -12,6 +14,61 @@ template struct interface_traits { }; +// +// Result handling +// + +template +class BluetoothInterfaceRunnable0 : public nsRunnable +{ +public: + BluetoothInterfaceRunnable0(Obj* aObj, Res (Obj::*aMethod)()) + : mObj(aObj) + , mMethod(aMethod) + { + MOZ_ASSERT(mObj); + MOZ_ASSERT(mMethod); + } + + NS_METHOD + Run() MOZ_OVERRIDE + { + ((*mObj).*mMethod)(); + return NS_OK; + } + +private: + nsRefPtr mObj; + void (Obj::*mMethod)(); +}; + +template +class BluetoothInterfaceRunnable1 : public nsRunnable +{ +public: + BluetoothInterfaceRunnable1(Obj* aObj, Res (Obj::*aMethod)(Arg1), + const Arg1& aArg1) + : mObj(aObj) + , mMethod(aMethod) + , mArg1(aArg1) + { + MOZ_ASSERT(mObj); + MOZ_ASSERT(mMethod); + } + + NS_METHOD + Run() MOZ_OVERRIDE + { + ((*mObj).*mMethod)(mArg1); + return NS_OK; + } + +private: + nsRefPtr mObj; + void (Obj::*mMethod)(Arg1); + Arg1 mArg1; +}; + // // Socket Interface // @@ -371,6 +428,36 @@ BluetoothAvrcpInterface::SetVolume(uint8_t aVolume) // Bluetooth Core Interface // +typedef + BluetoothInterfaceRunnable0 + BluetoothResultRunnable; + +typedef + BluetoothInterfaceRunnable1 + BluetoothErrorRunnable; + +static nsresult +DispatchBluetoothResult(BluetoothResultHandler* aRes, + void (BluetoothResultHandler::*aMethod)(), + int aStatus) +{ + MOZ_ASSERT(aRes); + + nsRunnable* runnable; + + if (aStatus == BT_STATUS_SUCCESS) { + runnable = new BluetoothResultRunnable(aRes, aMethod); + } else { + runnable = new + BluetoothErrorRunnable(aRes, &BluetoothResultHandler::OnError, aStatus); + } + nsresult rv = NS_DispatchToMainThread(runnable); + if (NS_FAILED(rv)) { + BT_LOGR("NS_DispatchToMainThread failed: %X", rv); + } + return rv; +} + /* returns the container structure of a variable; _t is the container's * type, _v the name of the variable, and _m is _v's field within _t */ diff --git a/dom/bluetooth2/bluedroid/BluetoothInterface.h b/dom/bluetooth2/bluedroid/BluetoothInterface.h index 91c93ca92ae9..a9673a2c06f7 100644 --- a/dom/bluetooth2/bluedroid/BluetoothInterface.h +++ b/dom/bluetooth2/bluedroid/BluetoothInterface.h @@ -181,6 +181,50 @@ private: // Bluetooth Core Interface // +class BluetoothResultHandler +{ +public: + NS_INLINE_DECL_THREADSAFE_REFCOUNTING(BluetoothResultHandler) + + virtual ~BluetoothResultHandler() { } + + virtual void OnError(int aStatus) + { + BT_LOGR("Received error code %d", aStatus); + } + + virtual void Init() { } + virtual void Cleanup() { } + virtual void Enable() { } + virtual void Disable() { } + + virtual void GetAdapterProperties() { } + virtual void GetAdapterProperty() { } + virtual void SetAdapterProperty() { } + + virtual void GetRemoteDeviceProperties() { } + virtual void GetRemoteDeviceProperty() { } + virtual void SetRemoteDeviceProperty() { } + + virtual void GetRemoteServiceRecord() { } + virtual void GetRemoteServices() { } + + virtual void StartDiscovery() { } + virtual void CancelDiscovery() { } + + virtual void CreateBond() { } + virtual void RemoveBond() { } + virtual void CancelBond() { } + + virtual void PinReply() { } + virtual void SspReply() { } + + virtual void DutModeConfigure() { } + virtual void DutModeSend() { } + + virtual void LeTestMode() { } +}; + class BluetoothInterface { public: