Bumping manifests a=b2g-bump
[gecko.git] / dom / bluetooth / BluetoothManager.cpp
blob97dcc98fae2dd803157194bf925624b97ef92502
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);
104 mPath.Assign('/');
106 BluetoothService* bs = BluetoothService::Get();
107 NS_ENSURE_TRUE_VOID(bs);
108 bs->RegisterBluetoothSignalHandler(NS_LITERAL_STRING(KEY_MANAGER), this);
111 BluetoothManager::~BluetoothManager()
113 BluetoothService* bs = BluetoothService::Get();
114 NS_ENSURE_TRUE_VOID(bs);
115 bs->UnregisterBluetoothSignalHandler(NS_LITERAL_STRING(KEY_MANAGER), this);
118 void
119 BluetoothManager::DisconnectFromOwner()
121 DOMEventTargetHelper::DisconnectFromOwner();
123 BluetoothService* bs = BluetoothService::Get();
124 NS_ENSURE_TRUE_VOID(bs);
125 bs->UnregisterBluetoothSignalHandler(NS_LITERAL_STRING(KEY_MANAGER), this);
128 void
129 BluetoothManager::SetPropertyByValue(const BluetoothNamedValue& aValue)
131 #ifdef DEBUG
132 const nsString& name = aValue.name();
133 nsCString warningMsg;
134 warningMsg.AssignLiteral("Not handling manager property: ");
135 warningMsg.Append(NS_ConvertUTF16toUTF8(name));
136 BT_WARNING(warningMsg.get());
137 #endif
140 bool
141 BluetoothManager::GetEnabled(ErrorResult& aRv)
143 BluetoothService* bs = BluetoothService::Get();
144 if (!bs) {
145 aRv.Throw(NS_ERROR_FAILURE);
146 return false;
149 return bs->IsEnabled();
152 already_AddRefed<dom::DOMRequest>
153 BluetoothManager::GetDefaultAdapter(ErrorResult& aRv)
155 nsCOMPtr<nsPIDOMWindow> win = GetOwner();
156 if (!win) {
157 aRv.Throw(NS_ERROR_FAILURE);
158 return nullptr;
161 nsRefPtr<DOMRequest> request = new DOMRequest(win);
162 nsRefPtr<BluetoothReplyRunnable> results =
163 new GetAdapterTask(this, request);
165 BluetoothService* bs = BluetoothService::Get();
166 if (!bs) {
167 aRv.Throw(NS_ERROR_FAILURE);
168 return nullptr;
171 nsresult rv = bs->GetDefaultAdapterPathInternal(results);
172 if (NS_FAILED(rv)) {
173 aRv.Throw(rv);
174 return nullptr;
177 return request.forget();
180 // static
181 already_AddRefed<BluetoothManager>
182 BluetoothManager::Create(nsPIDOMWindow* aWindow)
184 MOZ_ASSERT(NS_IsMainThread());
185 MOZ_ASSERT(aWindow);
187 nsRefPtr<BluetoothManager> manager = new BluetoothManager(aWindow);
188 return manager.forget();
191 void
192 BluetoothManager::Notify(const BluetoothSignal& aData)
194 BT_LOGD("[M] %s: %s", __FUNCTION__, NS_ConvertUTF16toUTF8(aData.name()).get());
196 if (aData.name().EqualsLiteral("AdapterAdded")) {
197 DispatchTrustedEvent(NS_LITERAL_STRING("adapteradded"));
198 } else if (aData.name().EqualsLiteral("Enabled")) {
199 DispatchTrustedEvent(NS_LITERAL_STRING("enabled"));
200 } else if (aData.name().EqualsLiteral("Disabled")) {
201 DispatchTrustedEvent(NS_LITERAL_STRING("disabled"));
202 } else {
203 #ifdef DEBUG
204 nsCString warningMsg;
205 warningMsg.AssignLiteral("Not handling manager signal: ");
206 warningMsg.Append(NS_ConvertUTF16toUTF8(aData.name()));
207 BT_WARNING(warningMsg.get());
208 #endif
212 JSObject*
213 BluetoothManager::WrapObject(JSContext* aCx)
215 return BluetoothManagerBinding::Wrap(aCx, this);