Bumping manifests a=b2g-bump
[gecko.git] / dom / bluetooth2 / BluetoothPairingHandle.cpp
blob7412c554af0866355a3181a6421023506d7e5404
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "BluetoothCommon.h"
8 #include "BluetoothDevice.h"
9 #include "BluetoothPairingHandle.h"
10 #include "BluetoothReplyRunnable.h"
11 #include "BluetoothService.h"
13 #include "mozilla/dom/BluetoothPairingHandleBinding.h"
14 #include "mozilla/dom/Promise.h"
16 using namespace mozilla;
17 using namespace dom;
19 USING_BLUETOOTH_NAMESPACE
21 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(BluetoothPairingHandle, mOwner)
22 NS_IMPL_CYCLE_COLLECTING_ADDREF(BluetoothPairingHandle)
23 NS_IMPL_CYCLE_COLLECTING_RELEASE(BluetoothPairingHandle)
24 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(BluetoothPairingHandle)
25 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
26 NS_INTERFACE_MAP_ENTRY(nsISupports)
27 NS_INTERFACE_MAP_END
29 BluetoothPairingHandle::BluetoothPairingHandle(nsPIDOMWindow* aOwner,
30 const nsAString& aDeviceAddress,
31 const nsAString& aType,
32 const nsAString& aPasskey)
33 : mOwner(aOwner)
34 , mDeviceAddress(aDeviceAddress)
35 , mType(aType)
36 , mPasskey(aPasskey)
38 MOZ_ASSERT(aOwner && !aDeviceAddress.IsEmpty() && !aType.IsEmpty());
40 if (aType.EqualsLiteral(PAIRING_REQ_TYPE_DISPLAYPASSKEY) ||
41 aType.EqualsLiteral(PAIRING_REQ_TYPE_CONFIRMATION)) {
42 MOZ_ASSERT(!aPasskey.IsEmpty());
43 } else {
44 MOZ_ASSERT(aPasskey.IsEmpty());
47 SetIsDOMBinding();
50 BluetoothPairingHandle::~BluetoothPairingHandle()
54 already_AddRefed<BluetoothPairingHandle>
55 BluetoothPairingHandle::Create(nsPIDOMWindow* aOwner,
56 const nsAString& aDeviceAddress,
57 const nsAString& aType,
58 const nsAString& aPasskey)
60 MOZ_ASSERT(aOwner && !aDeviceAddress.IsEmpty() && !aType.IsEmpty());
62 nsRefPtr<BluetoothPairingHandle> handle =
63 new BluetoothPairingHandle(aOwner, aDeviceAddress, aType, aPasskey);
65 return handle.forget();
68 already_AddRefed<Promise>
69 BluetoothPairingHandle::SetPinCode(const nsAString& aPinCode, ErrorResult& aRv)
71 nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetParentObject());
72 if (!global) {
73 aRv.Throw(NS_ERROR_FAILURE);
74 return nullptr;
77 nsRefPtr<Promise> promise = Promise::Create(global, aRv);
78 NS_ENSURE_TRUE(!aRv.Failed(), nullptr);
80 BT_ENSURE_TRUE_REJECT(mType.EqualsLiteral("enterpincodereq"),
81 NS_ERROR_DOM_INVALID_STATE_ERR);
83 BluetoothService* bs = BluetoothService::Get();
84 BT_ENSURE_TRUE_REJECT(bs, NS_ERROR_NOT_AVAILABLE);
86 nsRefPtr<BluetoothReplyRunnable> result =
87 new BluetoothVoidReplyRunnable(nullptr /* DOMRequest */,
88 promise,
89 NS_LITERAL_STRING("SetPinCode"));
90 bs->SetPinCodeInternal(mDeviceAddress, aPinCode, result);
92 return promise.forget();
95 already_AddRefed<Promise>
96 BluetoothPairingHandle::SetPairingConfirmation(bool aConfirm, ErrorResult& aRv)
98 nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetParentObject());
99 if (!global) {
100 aRv.Throw(NS_ERROR_FAILURE);
101 return nullptr;
104 nsRefPtr<Promise> promise = Promise::Create(global, aRv);
105 NS_ENSURE_TRUE(!aRv.Failed(), nullptr);
107 BT_ENSURE_TRUE_REJECT(mType.EqualsLiteral("pairingconfirmationreq"),
108 NS_ERROR_DOM_INVALID_STATE_ERR);
110 BluetoothService* bs = BluetoothService::Get();
111 BT_ENSURE_TRUE_REJECT(bs, NS_ERROR_NOT_AVAILABLE);
113 nsRefPtr<BluetoothReplyRunnable> result =
114 new BluetoothVoidReplyRunnable(nullptr /* DOMRequest */,
115 promise,
116 NS_LITERAL_STRING(
117 "SetPairingConfirmation"));
119 bs->SetPairingConfirmationInternal(mDeviceAddress,
120 aConfirm,
121 result);
122 return promise.forget();
125 JSObject*
126 BluetoothPairingHandle::WrapObject(JSContext* aCx)
128 return BluetoothPairingHandleBinding::Wrap(aCx, this);