1 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "base/basictypes.h"
8 #include "BluetoothManager.h"
9 #include "BluetoothCommon.h"
10 #include "BluetoothAdapter.h"
11 #include "BluetoothService.h"
12 #include "BluetoothReplyRunnable.h"
14 #include "DOMRequest.h"
15 #include "nsContentUtils.h"
16 #include "nsDOMClassInfo.h"
17 #include "nsIPermissionManager.h"
18 #include "nsThreadUtils.h"
19 #include "mozilla/dom/bluetooth/BluetoothTypes.h"
20 #include "mozilla/dom/BluetoothManagerBinding.h"
21 #include "mozilla/dom/ScriptSettings.h"
22 #include "mozilla/Services.h"
24 using namespace mozilla
;
26 USING_BLUETOOTH_NAMESPACE
28 // QueryInterface implementation for BluetoothManager
29 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(BluetoothManager
)
30 NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper
)
32 NS_IMPL_ADDREF_INHERITED(BluetoothManager
, DOMEventTargetHelper
)
33 NS_IMPL_RELEASE_INHERITED(BluetoothManager
, DOMEventTargetHelper
)
35 class GetAdapterTask
: public BluetoothReplyRunnable
38 GetAdapterTask(BluetoothManager
* aManager
,
39 nsIDOMDOMRequest
* aReq
) :
40 BluetoothReplyRunnable(aReq
),
46 ParseSuccessfulReply(JS::MutableHandle
<JS::Value
> aValue
)
48 aValue
.setUndefined();
50 const BluetoothValue
& v
= mReply
->get_BluetoothReplySuccess().value();
51 if (v
.type() != BluetoothValue::TArrayOfBluetoothNamedValue
) {
52 BT_WARNING("Not a BluetoothNamedValue array!");
53 SetError(NS_LITERAL_STRING("BluetoothReplyTypeError"));
57 if (!mManagerPtr
->GetOwner()) {
58 BT_WARNING("Bluetooth manager was disconnected from owner window.");
60 // Stop to create adapter since owner window of Bluetooth manager was
61 // gone. These is no need to create a DOMEvent target which has no owner
66 const InfallibleTArray
<BluetoothNamedValue
>& values
=
67 v
.get_ArrayOfBluetoothNamedValue();
68 nsRefPtr
<BluetoothAdapter
> adapter
=
69 BluetoothAdapter::Create(mManagerPtr
->GetOwner(), values
);
72 if (!jsapi
.Init(mManagerPtr
->GetOwner())) {
73 BT_WARNING("Failed to initialise AutoJSAPI!");
74 SetError(NS_LITERAL_STRING("BluetoothAutoJSAPIInitError"));
77 JSContext
* cx
= jsapi
.cx();
78 if (NS_FAILED(nsContentUtils::WrapNative(cx
, adapter
, aValue
))) {
79 BT_WARNING("Cannot create native object!");
80 SetError(NS_LITERAL_STRING("BluetoothNativeObjectError"));
90 BluetoothReplyRunnable::ReleaseMembers();
91 mManagerPtr
= nullptr;
95 nsRefPtr
<BluetoothManager
> mManagerPtr
;
98 BluetoothManager::BluetoothManager(nsPIDOMWindow
*aWindow
)
99 : DOMEventTargetHelper(aWindow
)
100 , BluetoothPropertyContainer(BluetoothObjectType::TYPE_MANAGER
)
103 MOZ_ASSERT(IsDOMBinding());
107 BluetoothService
* bs
= BluetoothService::Get();
108 NS_ENSURE_TRUE_VOID(bs
);
109 bs
->RegisterBluetoothSignalHandler(NS_LITERAL_STRING(KEY_MANAGER
), this);
112 BluetoothManager::~BluetoothManager()
114 BluetoothService
* bs
= BluetoothService::Get();
115 NS_ENSURE_TRUE_VOID(bs
);
116 bs
->UnregisterBluetoothSignalHandler(NS_LITERAL_STRING(KEY_MANAGER
), this);
120 BluetoothManager::DisconnectFromOwner()
122 DOMEventTargetHelper::DisconnectFromOwner();
124 BluetoothService
* bs
= BluetoothService::Get();
125 NS_ENSURE_TRUE_VOID(bs
);
126 bs
->UnregisterBluetoothSignalHandler(NS_LITERAL_STRING(KEY_MANAGER
), this);
130 BluetoothManager::SetPropertyByValue(const BluetoothNamedValue
& aValue
)
133 const nsString
& name
= aValue
.name();
134 nsCString warningMsg
;
135 warningMsg
.AssignLiteral("Not handling manager property: ");
136 warningMsg
.Append(NS_ConvertUTF16toUTF8(name
));
137 BT_WARNING(warningMsg
.get());
142 BluetoothManager::GetEnabled(ErrorResult
& aRv
)
144 BluetoothService
* bs
= BluetoothService::Get();
146 aRv
.Throw(NS_ERROR_FAILURE
);
150 return bs
->IsEnabled();
153 already_AddRefed
<dom::DOMRequest
>
154 BluetoothManager::GetDefaultAdapter(ErrorResult
& aRv
)
156 nsCOMPtr
<nsPIDOMWindow
> win
= GetOwner();
158 aRv
.Throw(NS_ERROR_FAILURE
);
162 nsRefPtr
<DOMRequest
> request
= new DOMRequest(win
);
163 nsRefPtr
<BluetoothReplyRunnable
> results
=
164 new GetAdapterTask(this, request
);
166 BluetoothService
* bs
= BluetoothService::Get();
168 aRv
.Throw(NS_ERROR_FAILURE
);
172 nsresult rv
= bs
->GetDefaultAdapterPathInternal(results
);
178 return request
.forget();
182 already_AddRefed
<BluetoothManager
>
183 BluetoothManager::Create(nsPIDOMWindow
* aWindow
)
185 MOZ_ASSERT(NS_IsMainThread());
188 nsRefPtr
<BluetoothManager
> manager
= new BluetoothManager(aWindow
);
189 return manager
.forget();
193 BluetoothManager::Notify(const BluetoothSignal
& aData
)
195 BT_LOGD("[M] %s: %s", __FUNCTION__
, NS_ConvertUTF16toUTF8(aData
.name()).get());
197 if (aData
.name().EqualsLiteral("AdapterAdded")) {
198 DispatchTrustedEvent(NS_LITERAL_STRING("adapteradded"));
199 } else if (aData
.name().EqualsLiteral("Enabled")) {
200 DispatchTrustedEvent(NS_LITERAL_STRING("enabled"));
201 } else if (aData
.name().EqualsLiteral("Disabled")) {
202 DispatchTrustedEvent(NS_LITERAL_STRING("disabled"));
205 nsCString warningMsg
;
206 warningMsg
.AssignLiteral("Not handling manager signal: ");
207 warningMsg
.Append(NS_ConvertUTF16toUTF8(aData
.name()));
208 BT_WARNING(warningMsg
.get());
214 BluetoothManager::WrapObject(JSContext
* aCx
)
216 return BluetoothManagerBinding::Wrap(aCx
, this);