Bumping manifests a=b2g-bump
[gecko.git] / dom / bluetooth / BluetoothManager.cpp
blob798e463902158e4a1336b1342e5c01c3964cb566
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
37 public:
38 GetAdapterTask(BluetoothManager* aManager,
39 nsIDOMDOMRequest* aReq) :
40 BluetoothReplyRunnable(aReq),
41 mManagerPtr(aManager)
45 bool
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"));
54 return false;
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
62 // to reply to.
63 return false;
66 const InfallibleTArray<BluetoothNamedValue>& values =
67 v.get_ArrayOfBluetoothNamedValue();
68 nsRefPtr<BluetoothAdapter> adapter =
69 BluetoothAdapter::Create(mManagerPtr->GetOwner(), values);
71 dom::AutoJSAPI jsapi;
72 if (!jsapi.Init(mManagerPtr->GetOwner())) {
73 BT_WARNING("Failed to initialise AutoJSAPI!");
74 SetError(NS_LITERAL_STRING("BluetoothAutoJSAPIInitError"));
75 return false;
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"));
81 return false;
84 return true;
87 void
88 ReleaseMembers()
90 BluetoothReplyRunnable::ReleaseMembers();
91 mManagerPtr = nullptr;
94 private:
95 nsRefPtr<BluetoothManager> mManagerPtr;
98 BluetoothManager::BluetoothManager(nsPIDOMWindow *aWindow)
99 : DOMEventTargetHelper(aWindow)
100 , BluetoothPropertyContainer(BluetoothObjectType::TYPE_MANAGER)
102 MOZ_ASSERT(aWindow);
103 MOZ_ASSERT(IsDOMBinding());
105 mPath.Assign('/');
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);
119 void
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);
129 void
130 BluetoothManager::SetPropertyByValue(const BluetoothNamedValue& aValue)
132 #ifdef DEBUG
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());
138 #endif
141 bool
142 BluetoothManager::GetEnabled(ErrorResult& aRv)
144 BluetoothService* bs = BluetoothService::Get();
145 if (!bs) {
146 aRv.Throw(NS_ERROR_FAILURE);
147 return false;
150 return bs->IsEnabled();
153 already_AddRefed<dom::DOMRequest>
154 BluetoothManager::GetDefaultAdapter(ErrorResult& aRv)
156 nsCOMPtr<nsPIDOMWindow> win = GetOwner();
157 if (!win) {
158 aRv.Throw(NS_ERROR_FAILURE);
159 return nullptr;
162 nsRefPtr<DOMRequest> request = new DOMRequest(win);
163 nsRefPtr<BluetoothReplyRunnable> results =
164 new GetAdapterTask(this, request);
166 BluetoothService* bs = BluetoothService::Get();
167 if (!bs) {
168 aRv.Throw(NS_ERROR_FAILURE);
169 return nullptr;
172 nsresult rv = bs->GetDefaultAdapterPathInternal(results);
173 if (NS_FAILED(rv)) {
174 aRv.Throw(rv);
175 return nullptr;
178 return request.forget();
181 // static
182 already_AddRefed<BluetoothManager>
183 BluetoothManager::Create(nsPIDOMWindow* aWindow)
185 MOZ_ASSERT(NS_IsMainThread());
186 MOZ_ASSERT(aWindow);
188 nsRefPtr<BluetoothManager> manager = new BluetoothManager(aWindow);
189 return manager.forget();
192 void
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"));
203 } else {
204 #ifdef DEBUG
205 nsCString warningMsg;
206 warningMsg.AssignLiteral("Not handling manager signal: ");
207 warningMsg.Append(NS_ConvertUTF16toUTF8(aData.name()));
208 BT_WARNING(warningMsg.get());
209 #endif
213 JSObject*
214 BluetoothManager::WrapObject(JSContext* aCx)
216 return BluetoothManagerBinding::Wrap(aCx, this);