Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ |
michael@0 | 2 | /* vim: set ts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 5 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "base/basictypes.h" |
michael@0 | 8 | |
michael@0 | 9 | #include "BluetoothHidManager.h" |
michael@0 | 10 | |
michael@0 | 11 | #include "BluetoothCommon.h" |
michael@0 | 12 | #include "BluetoothService.h" |
michael@0 | 13 | #include "BluetoothUtils.h" |
michael@0 | 14 | |
michael@0 | 15 | #include "mozilla/dom/bluetooth/BluetoothTypes.h" |
michael@0 | 16 | #include "mozilla/Services.h" |
michael@0 | 17 | #include "mozilla/StaticPtr.h" |
michael@0 | 18 | #include "nsIObserverService.h" |
michael@0 | 19 | #include "MainThreadUtils.h" |
michael@0 | 20 | |
michael@0 | 21 | using namespace mozilla; |
michael@0 | 22 | USING_BLUETOOTH_NAMESPACE |
michael@0 | 23 | |
michael@0 | 24 | namespace { |
michael@0 | 25 | StaticRefPtr<BluetoothHidManager> sBluetoothHidManager; |
michael@0 | 26 | bool sInShutdown = false; |
michael@0 | 27 | } // anonymous namespace |
michael@0 | 28 | |
michael@0 | 29 | NS_IMETHODIMP |
michael@0 | 30 | BluetoothHidManager::Observe(nsISupports* aSubject, |
michael@0 | 31 | const char* aTopic, |
michael@0 | 32 | const char16_t* aData) |
michael@0 | 33 | { |
michael@0 | 34 | MOZ_ASSERT(sBluetoothHidManager); |
michael@0 | 35 | |
michael@0 | 36 | if (!strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) { |
michael@0 | 37 | HandleShutdown(); |
michael@0 | 38 | return NS_OK; |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | MOZ_ASSERT(false, "BluetoothHidManager got unexpected topic!"); |
michael@0 | 42 | return NS_ERROR_UNEXPECTED; |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | BluetoothHidManager::BluetoothHidManager() |
michael@0 | 46 | { |
michael@0 | 47 | Reset(); |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | void |
michael@0 | 51 | BluetoothHidManager::Reset() |
michael@0 | 52 | { |
michael@0 | 53 | mConnected = false; |
michael@0 | 54 | mController = nullptr; |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | bool |
michael@0 | 58 | BluetoothHidManager::Init() |
michael@0 | 59 | { |
michael@0 | 60 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 61 | |
michael@0 | 62 | nsCOMPtr<nsIObserverService> obs = services::GetObserverService(); |
michael@0 | 63 | NS_ENSURE_TRUE(obs, false); |
michael@0 | 64 | if (NS_FAILED(obs->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false))) { |
michael@0 | 65 | BT_WARNING("Failed to add shutdown observer!"); |
michael@0 | 66 | return false; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | return true; |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | BluetoothHidManager::~BluetoothHidManager() |
michael@0 | 73 | { |
michael@0 | 74 | nsCOMPtr<nsIObserverService> obs = services::GetObserverService(); |
michael@0 | 75 | NS_ENSURE_TRUE_VOID(obs); |
michael@0 | 76 | if (NS_FAILED(obs->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID))) { |
michael@0 | 77 | BT_WARNING("Failed to remove shutdown observer!"); |
michael@0 | 78 | } |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | //static |
michael@0 | 82 | BluetoothHidManager* |
michael@0 | 83 | BluetoothHidManager::Get() |
michael@0 | 84 | { |
michael@0 | 85 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 86 | |
michael@0 | 87 | // If we already exist, exit early |
michael@0 | 88 | if (sBluetoothHidManager) { |
michael@0 | 89 | return sBluetoothHidManager; |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | // If we're in shutdown, don't create a new instance |
michael@0 | 93 | NS_ENSURE_FALSE(sInShutdown, nullptr); |
michael@0 | 94 | |
michael@0 | 95 | // Create a new instance, register, and return |
michael@0 | 96 | BluetoothHidManager* manager = new BluetoothHidManager(); |
michael@0 | 97 | NS_ENSURE_TRUE(manager->Init(), nullptr); |
michael@0 | 98 | |
michael@0 | 99 | sBluetoothHidManager = manager; |
michael@0 | 100 | return sBluetoothHidManager; |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | void |
michael@0 | 104 | BluetoothHidManager::HandleShutdown() |
michael@0 | 105 | { |
michael@0 | 106 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 107 | sInShutdown = true; |
michael@0 | 108 | Disconnect(nullptr); |
michael@0 | 109 | sBluetoothHidManager = nullptr; |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | void |
michael@0 | 113 | BluetoothHidManager::Connect(const nsAString& aDeviceAddress, |
michael@0 | 114 | BluetoothProfileController* aController) |
michael@0 | 115 | { |
michael@0 | 116 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 117 | MOZ_ASSERT(!aDeviceAddress.IsEmpty()); |
michael@0 | 118 | MOZ_ASSERT(aController && !mController); |
michael@0 | 119 | |
michael@0 | 120 | BluetoothService* bs = BluetoothService::Get(); |
michael@0 | 121 | if (!bs || sInShutdown) { |
michael@0 | 122 | aController->NotifyCompletion(NS_LITERAL_STRING(ERR_NO_AVAILABLE_RESOURCE)); |
michael@0 | 123 | return; |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | if (mConnected) { |
michael@0 | 127 | aController->NotifyCompletion(NS_LITERAL_STRING(ERR_ALREADY_CONNECTED)); |
michael@0 | 128 | return; |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | mDeviceAddress = aDeviceAddress; |
michael@0 | 132 | mController = aController; |
michael@0 | 133 | |
michael@0 | 134 | if (NS_FAILED(bs->SendInputMessage(aDeviceAddress, |
michael@0 | 135 | NS_LITERAL_STRING("Connect")))) { |
michael@0 | 136 | aController->NotifyCompletion(NS_LITERAL_STRING(ERR_NO_AVAILABLE_RESOURCE)); |
michael@0 | 137 | return; |
michael@0 | 138 | } |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | void |
michael@0 | 142 | BluetoothHidManager::Disconnect(BluetoothProfileController* aController) |
michael@0 | 143 | { |
michael@0 | 144 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 145 | |
michael@0 | 146 | BluetoothService* bs = BluetoothService::Get(); |
michael@0 | 147 | if (!bs) { |
michael@0 | 148 | if (aController) { |
michael@0 | 149 | aController->NotifyCompletion(NS_LITERAL_STRING(ERR_NO_AVAILABLE_RESOURCE)); |
michael@0 | 150 | } |
michael@0 | 151 | return; |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | if (!mConnected) { |
michael@0 | 155 | if (aController) { |
michael@0 | 156 | aController->NotifyCompletion(NS_LITERAL_STRING(ERR_ALREADY_DISCONNECTED)); |
michael@0 | 157 | } |
michael@0 | 158 | return; |
michael@0 | 159 | } |
michael@0 | 160 | |
michael@0 | 161 | MOZ_ASSERT(!mDeviceAddress.IsEmpty()); |
michael@0 | 162 | MOZ_ASSERT(!mController); |
michael@0 | 163 | |
michael@0 | 164 | mController = aController; |
michael@0 | 165 | |
michael@0 | 166 | if (NS_FAILED(bs->SendInputMessage(mDeviceAddress, |
michael@0 | 167 | NS_LITERAL_STRING("Disconnect")))) { |
michael@0 | 168 | aController->NotifyCompletion(NS_LITERAL_STRING(ERR_NO_AVAILABLE_RESOURCE)); |
michael@0 | 169 | return; |
michael@0 | 170 | } |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | void |
michael@0 | 174 | BluetoothHidManager::OnConnect(const nsAString& aErrorStr) |
michael@0 | 175 | { |
michael@0 | 176 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 177 | |
michael@0 | 178 | /** |
michael@0 | 179 | * On the one hand, notify the controller that we've done for outbound |
michael@0 | 180 | * connections. On the other hand, we do nothing for inbound connections. |
michael@0 | 181 | */ |
michael@0 | 182 | NS_ENSURE_TRUE_VOID(mController); |
michael@0 | 183 | |
michael@0 | 184 | nsRefPtr<BluetoothProfileController> controller = mController.forget(); |
michael@0 | 185 | controller->NotifyCompletion(aErrorStr); |
michael@0 | 186 | } |
michael@0 | 187 | |
michael@0 | 188 | void |
michael@0 | 189 | BluetoothHidManager::OnDisconnect(const nsAString& aErrorStr) |
michael@0 | 190 | { |
michael@0 | 191 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 192 | |
michael@0 | 193 | /** |
michael@0 | 194 | * On the one hand, notify the controller that we've done for outbound |
michael@0 | 195 | * connections. On the other hand, we do nothing for inbound connections. |
michael@0 | 196 | */ |
michael@0 | 197 | NS_ENSURE_TRUE_VOID(mController); |
michael@0 | 198 | |
michael@0 | 199 | nsRefPtr<BluetoothProfileController> controller = mController.forget(); |
michael@0 | 200 | controller->NotifyCompletion(aErrorStr); |
michael@0 | 201 | } |
michael@0 | 202 | |
michael@0 | 203 | bool |
michael@0 | 204 | BluetoothHidManager::IsConnected() |
michael@0 | 205 | { |
michael@0 | 206 | return mConnected; |
michael@0 | 207 | } |
michael@0 | 208 | |
michael@0 | 209 | void |
michael@0 | 210 | BluetoothHidManager::HandleInputPropertyChanged(const BluetoothSignal& aSignal) |
michael@0 | 211 | { |
michael@0 | 212 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 213 | MOZ_ASSERT(aSignal.value().type() == BluetoothValue::TArrayOfBluetoothNamedValue); |
michael@0 | 214 | |
michael@0 | 215 | const InfallibleTArray<BluetoothNamedValue>& arr = |
michael@0 | 216 | aSignal.value().get_ArrayOfBluetoothNamedValue(); |
michael@0 | 217 | MOZ_ASSERT(arr.Length() == 1); |
michael@0 | 218 | |
michael@0 | 219 | const nsString& name = arr[0].name(); |
michael@0 | 220 | const BluetoothValue& value = arr[0].value(); |
michael@0 | 221 | |
michael@0 | 222 | if (name.EqualsLiteral("Connected")) { |
michael@0 | 223 | MOZ_ASSERT(value.type() == BluetoothValue::Tbool); |
michael@0 | 224 | MOZ_ASSERT(mConnected != value.get_bool()); |
michael@0 | 225 | |
michael@0 | 226 | mConnected = value.get_bool(); |
michael@0 | 227 | NotifyStatusChanged(); |
michael@0 | 228 | if (mConnected) { |
michael@0 | 229 | OnConnect(EmptyString()); |
michael@0 | 230 | } else { |
michael@0 | 231 | OnDisconnect(EmptyString()); |
michael@0 | 232 | } |
michael@0 | 233 | } |
michael@0 | 234 | } |
michael@0 | 235 | |
michael@0 | 236 | void |
michael@0 | 237 | BluetoothHidManager::NotifyStatusChanged() |
michael@0 | 238 | { |
michael@0 | 239 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 240 | |
michael@0 | 241 | NS_NAMED_LITERAL_STRING(type, BLUETOOTH_HID_STATUS_CHANGED_ID); |
michael@0 | 242 | InfallibleTArray<BluetoothNamedValue> parameters; |
michael@0 | 243 | |
michael@0 | 244 | BT_APPEND_NAMED_VALUE(parameters, "connected", mConnected); |
michael@0 | 245 | BT_APPEND_NAMED_VALUE(parameters, "address", mDeviceAddress); |
michael@0 | 246 | |
michael@0 | 247 | BT_ENSURE_TRUE_VOID_BROADCAST_SYSMSG(type, parameters); |
michael@0 | 248 | } |
michael@0 | 249 | |
michael@0 | 250 | void |
michael@0 | 251 | BluetoothHidManager::OnGetServiceChannel(const nsAString& aDeviceAddress, |
michael@0 | 252 | const nsAString& aServiceUuid, |
michael@0 | 253 | int aChannel) |
michael@0 | 254 | { |
michael@0 | 255 | // Do nothing here as bluez acquires service channel and connects for us |
michael@0 | 256 | } |
michael@0 | 257 | |
michael@0 | 258 | void |
michael@0 | 259 | BluetoothHidManager::OnUpdateSdpRecords(const nsAString& aDeviceAddress) |
michael@0 | 260 | { |
michael@0 | 261 | // Do nothing here as bluez acquires service channel and connects for us |
michael@0 | 262 | } |
michael@0 | 263 | |
michael@0 | 264 | void |
michael@0 | 265 | BluetoothHidManager::GetAddress(nsAString& aDeviceAddress) |
michael@0 | 266 | { |
michael@0 | 267 | aDeviceAddress = mDeviceAddress; |
michael@0 | 268 | } |
michael@0 | 269 | |
michael@0 | 270 | NS_IMPL_ISUPPORTS(BluetoothHidManager, nsIObserver) |