michael@0: /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ michael@0: /* vim: set ts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "base/basictypes.h" michael@0: #include "BluetoothHfpManager.h" michael@0: #include "BluetoothProfileController.h" michael@0: #include "mozilla/Services.h" michael@0: #include "mozilla/StaticPtr.h" michael@0: #include "nsIObserverService.h" michael@0: #include "nsThreadUtils.h" michael@0: michael@0: using namespace mozilla; michael@0: USING_BLUETOOTH_NAMESPACE michael@0: michael@0: namespace { michael@0: StaticRefPtr sBluetoothHfpManager; michael@0: bool sInShutdown = false; michael@0: } // anonymous namespace michael@0: michael@0: /** michael@0: * nsIObserver function michael@0: */ michael@0: NS_IMETHODIMP michael@0: BluetoothHfpManager::Observe(nsISupports* aSubject, michael@0: const char* aTopic, michael@0: const char16_t* aData) michael@0: { michael@0: if (!strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) { michael@0: HandleShutdown(); michael@0: } else { michael@0: MOZ_ASSERT(false, "BluetoothHfpManager got unexpected topic!"); michael@0: return NS_ERROR_UNEXPECTED; michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: /** michael@0: * BluetoothProfileManagerBase functions michael@0: */ michael@0: void michael@0: BluetoothHfpManager::Connect(const nsAString& aDeviceAddress, michael@0: BluetoothProfileController* aController) michael@0: { michael@0: MOZ_ASSERT(aController); michael@0: michael@0: aController->NotifyCompletion(NS_LITERAL_STRING(ERR_NO_AVAILABLE_RESOURCE)); michael@0: } michael@0: michael@0: void michael@0: BluetoothHfpManager::Disconnect(BluetoothProfileController* aController) michael@0: { michael@0: MOZ_ASSERT(aController); michael@0: michael@0: aController->NotifyCompletion(NS_LITERAL_STRING(ERR_NO_AVAILABLE_RESOURCE)); michael@0: } michael@0: michael@0: bool michael@0: BluetoothHfpManager::IsConnected() michael@0: { michael@0: return false; michael@0: } michael@0: michael@0: void michael@0: BluetoothHfpManager::OnConnect(const nsAString& aErrorStr) michael@0: { michael@0: MOZ_ASSERT(false); michael@0: } michael@0: michael@0: void michael@0: BluetoothHfpManager::OnDisconnect(const nsAString& aErrorStr) michael@0: { michael@0: MOZ_ASSERT(false); michael@0: } michael@0: michael@0: void michael@0: BluetoothHfpManager::GetAddress(nsAString& aDeviceAddress) michael@0: { michael@0: aDeviceAddress.AssignLiteral(BLUETOOTH_ADDRESS_NONE); michael@0: } michael@0: michael@0: void michael@0: BluetoothHfpManager::OnGetServiceChannel(const nsAString& aDeviceAddress, michael@0: const nsAString& aServiceUuid, michael@0: int aChannel) michael@0: { michael@0: MOZ_ASSERT(false); michael@0: } michael@0: michael@0: void michael@0: BluetoothHfpManager::OnUpdateSdpRecords(const nsAString& aDeviceAddress) michael@0: { michael@0: MOZ_ASSERT(false); michael@0: } michael@0: michael@0: /** michael@0: * BluetoothHfpManagerBase function michael@0: */ michael@0: bool michael@0: BluetoothHfpManager::IsScoConnected() michael@0: { michael@0: return false; michael@0: } michael@0: michael@0: /** michael@0: * Non-inherited functions michael@0: */ michael@0: // static michael@0: BluetoothHfpManager* michael@0: BluetoothHfpManager::Get() michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread()); michael@0: michael@0: // If sBluetoothHfpManager already exists, exit early michael@0: if (sBluetoothHfpManager) { michael@0: return sBluetoothHfpManager; michael@0: } michael@0: michael@0: // If we're in shutdown, don't create a new instance michael@0: NS_ENSURE_FALSE(sInShutdown, nullptr); michael@0: michael@0: // Create a new instance and return michael@0: BluetoothHfpManager* manager = new BluetoothHfpManager(); michael@0: NS_ENSURE_TRUE(manager->Init(), nullptr); michael@0: michael@0: sBluetoothHfpManager = manager; michael@0: return sBluetoothHfpManager; michael@0: } michael@0: michael@0: bool michael@0: BluetoothHfpManager::Init() michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread()); michael@0: michael@0: nsCOMPtr obs = services::GetObserverService(); michael@0: NS_ENSURE_TRUE(obs, false); michael@0: michael@0: if (NS_FAILED(obs->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false))) { michael@0: BT_WARNING("Failed to add observers!"); michael@0: return false; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: void michael@0: BluetoothHfpManager::HandleShutdown() michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread()); michael@0: sInShutdown = true; michael@0: sBluetoothHfpManager = nullptr; michael@0: } michael@0: michael@0: bool michael@0: BluetoothHfpManager::ConnectSco() michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread()); michael@0: michael@0: /** michael@0: * TODO: michael@0: * Implement ConnectSco() for applications that want to create SCO link michael@0: * without a HFP connection (e.g., VoIP). michael@0: */ michael@0: return false; michael@0: } michael@0: michael@0: bool michael@0: BluetoothHfpManager::DisconnectSco() michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread()); michael@0: michael@0: /** michael@0: * TODO: michael@0: * Implement DisconnectSco() for applications that want to destroy SCO link michael@0: * without a HFP connection (e.g., VoIP). michael@0: */ michael@0: return false; michael@0: } michael@0: michael@0: void michael@0: BluetoothHfpManager::Reset() michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread()); michael@0: } michael@0: michael@0: NS_IMPL_ISUPPORTS(BluetoothHfpManager, nsIObserver)