Bug 830213 - Patch 3: Remove BluetoothScoManager, r=echou

This commit is contained in:
Gina Yeh 2013-05-10 19:03:25 +08:00
parent 8332be4c87
commit a8ebac2175
5 changed files with 7 additions and 428 deletions

View File

@ -9,7 +9,6 @@
#include "BluetoothHfpManager.h"
#include "BluetoothReplyRunnable.h"
#include "BluetoothScoManager.h"
#include "BluetoothService.h"
#include "BluetoothSocket.h"
#include "BluetoothUtils.h"
@ -334,35 +333,6 @@ private:
int mType;
};
void
OpenScoSocket(const nsAString& aDeviceAddress)
{
MOZ_ASSERT(NS_IsMainThread());
BluetoothScoManager* sco = BluetoothScoManager::Get();
if (!sco) {
NS_WARNING("BluetoothScoManager is not available!");
return;
}
if (!sco->Connect(aDeviceAddress)) {
NS_WARNING("Failed to create a sco socket!");
}
}
void
CloseScoSocket()
{
MOZ_ASSERT(NS_IsMainThread());
BluetoothScoManager* sco = BluetoothScoManager::Get();
if (!sco) {
NS_WARNING("BluetoothScoManager is not available!");
return;
}
sco->Disconnect();
}
static bool
IsValidDtmf(const char aChar) {
// Valid DTMF: [*#0-9ABCD]
@ -940,12 +910,6 @@ BluetoothHfpManager::ReceiveSocketData(BluetoothSocket* aSocket,
mCCWA = atCommandValues[0].EqualsLiteral("1");
} else if (msg.Find("AT+CKPD") != -1) {
BluetoothScoManager* sco = BluetoothScoManager::Get();
if (!sco) {
NS_WARNING("Couldn't get BluetoothScoManager instance");
goto respond_with_ok;
}
if (!sStopSendingRingFlag) {
// Bluetooth HSP spec 4.2.2
// There is an incoming call, notify Dialer to pick up the phone call
@ -953,12 +917,10 @@ BluetoothHfpManager::ReceiveSocketData(BluetoothSocket* aSocket,
// indicating the call is answered successfully.
NotifyDialer(NS_LITERAL_STRING("ATA"));
} else {
if (!sco->IsConnected()) {
if (!IsScoConnected()) {
// Bluetooth HSP spec 4.3
// If there's no SCO, set up a SCO link.
nsAutoString address;
mSocket->GetAddress(address);
sco->Connect(address);
ConnectSco();
} else if (!mFirstCKPD) {
// Bluetooth HSP spec 4.5
// There are two ways to release SCO: sending CHUP to dialer or closing
@ -967,7 +929,7 @@ BluetoothHfpManager::ReceiveSocketData(BluetoothSocket* aSocket,
if (mCurrentCallArray.Length() > 1) {
NotifyDialer(NS_LITERAL_STRING("CHUP"));
} else {
sco->Disconnect();
DisconnectSco();
}
} else {
// Three conditions have to be matched to come in here:
@ -1300,17 +1262,14 @@ BluetoothHfpManager::HandleCallStateChanged(uint32_t aCallIndex,
}
UpdateCIND(CINDType::CALLSETUP, CallSetupState::OUTGOING, aSend);
mSocket->GetAddress(address);
OpenScoSocket(address);
ConnectSco();
break;
case nsITelephonyProvider::CALL_STATE_ALERTING:
UpdateCIND(CINDType::CALLSETUP, CallSetupState::OUTGOING_ALERTING, aSend);
// If there's an ongoing call when the headset is just connected, we have
// to open a sco socket here.
mSocket->GetAddress(address);
OpenScoSocket(address);
ConnectSco();
break;
case nsITelephonyProvider::CALL_STATE_CONNECTED:
mCurrentCallIndex = aCallIndex;
@ -1319,9 +1278,7 @@ BluetoothHfpManager::HandleCallStateChanged(uint32_t aCallIndex,
case nsITelephonyProvider::CALL_STATE_DISCONNECTED:
// Incoming call, no break
sStopSendingRingFlag = true;
mSocket->GetAddress(address);
OpenScoSocket(address);
ConnectSco();
case nsITelephonyProvider::CALL_STATE_ALERTING:
// Outgoing call
UpdateCIND(CINDType::CALL, CallState::IN_PROGRESS, aSend);
@ -1391,7 +1348,7 @@ BluetoothHfpManager::HandleCallStateChanged(uint32_t aCallIndex,
// There is no call, close Sco and clear mCurrentCallArray
if (index == callArrayLength) {
CloseScoSocket();
DisconnectSco();
ResetCallArray();
}
}

View File

@ -1,293 +0,0 @@
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "base/basictypes.h"
#include "BluetoothScoManager.h"
#include "BluetoothReplyRunnable.h"
#include "BluetoothService.h"
#include "BluetoothSocket.h"
#include "BluetoothUtils.h"
#include "mozilla/Services.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/dom/bluetooth/BluetoothTypes.h"
#include "nsContentUtils.h"
#include "nsIAudioManager.h"
#include "nsIObserverService.h"
#define BLUETOOTH_SCO_STATUS_CHANGED "bluetooth-sco-status-changed"
using namespace mozilla;
using namespace mozilla::ipc;
USING_BLUETOOTH_NAMESPACE
class mozilla::dom::bluetooth::BluetoothScoManagerObserver : public nsIObserver
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIOBSERVER
BluetoothScoManagerObserver()
{
}
bool Init()
{
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
MOZ_ASSERT(obs);
if (NS_FAILED(obs->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false))) {
NS_WARNING("Failed to add shutdown observer!");
return false;
}
return true;
}
bool Shutdown()
{
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
if (!obs ||
(NS_FAILED(obs->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID)))) {
NS_WARNING("Can't unregister observers!");
return false;
}
return true;
}
~BluetoothScoManagerObserver()
{
Shutdown();
}
};
void
BluetoothScoManager::NotifyAudioManager(const nsAString& aAddress)
{
MOZ_ASSERT(NS_IsMainThread());
nsCOMPtr<nsIObserverService> obs =
do_GetService("@mozilla.org/observer-service;1");
NS_ENSURE_TRUE_VOID(obs);
const PRUnichar* addr =
aAddress.IsEmpty() ? nullptr : aAddress.BeginReading();
if (NS_FAILED(obs->NotifyObservers(nullptr,
BLUETOOTH_SCO_STATUS_CHANGED,
addr))) {
NS_WARNING("Failed to notify bluetooth-sco-status-changed observsers!");
return;
}
}
NS_IMPL_ISUPPORTS1(BluetoothScoManagerObserver, nsIObserver)
namespace {
StaticAutoPtr<BluetoothScoManager> gBluetoothScoManager;
StaticRefPtr<BluetoothScoManagerObserver> sScoObserver;
bool gInShutdown = false;
} // anonymous namespace
NS_IMETHODIMP
BluetoothScoManagerObserver::Observe(nsISupports* aSubject,
const char* aTopic,
const PRUnichar* aData)
{
MOZ_ASSERT(gBluetoothScoManager);
if (!strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) {
return gBluetoothScoManager->HandleShutdown();
}
MOZ_ASSERT(false, "BluetoothScoManager got unexpected topic!");
return NS_ERROR_UNEXPECTED;
}
BluetoothScoManager::BluetoothScoManager()
{
}
bool
BluetoothScoManager::Init()
{
mSocket = new BluetoothSocket(this,
BluetoothSocketType::SCO,
true,
false);
mPrevSocketStatus = mSocket->GetConnectionStatus();
sScoObserver = new BluetoothScoManagerObserver();
if (!sScoObserver->Init()) {
NS_WARNING("Cannot set up SCO observers!");
}
return true;
}
BluetoothScoManager::~BluetoothScoManager()
{
Cleanup();
}
void
BluetoothScoManager::Cleanup()
{
sScoObserver->Shutdown();
sScoObserver = nullptr;
}
//static
BluetoothScoManager*
BluetoothScoManager::Get()
{
MOZ_ASSERT(NS_IsMainThread());
// If we already exist, exit early
if (gBluetoothScoManager) {
return gBluetoothScoManager;
}
// If we're in shutdown, don't create a new instance
if (gInShutdown) {
NS_WARNING("BluetoothScoManager can't be created during shutdown");
return nullptr;
}
// Create new instance, register, return
BluetoothScoManager* manager = new BluetoothScoManager();
NS_ENSURE_TRUE(manager->Init(), nullptr);
gBluetoothScoManager = manager;
return gBluetoothScoManager;
}
// Virtual function of class SocketConsumer
void
BluetoothScoManager::ReceiveSocketData(BluetoothSocket* aSocket,
nsAutoPtr<UnixSocketRawData>& aMessage)
{
// SCO socket do nothing here
MOZ_NOT_REACHED("This should never be called!");
}
nsresult
BluetoothScoManager::HandleShutdown()
{
MOZ_ASSERT(NS_IsMainThread());
gInShutdown = true;
mSocket->Disconnect();
gBluetoothScoManager = nullptr;
return NS_OK;
}
bool
BluetoothScoManager::Connect(const nsAString& aDeviceAddress)
{
MOZ_ASSERT(NS_IsMainThread());
if (gInShutdown) {
MOZ_ASSERT(false, "Connect called while in shutdown!");
return false;
}
SocketConnectionStatus status = mSocket->GetConnectionStatus();
if (status == SocketConnectionStatus::SOCKET_CONNECTED ||
status == SocketConnectionStatus::SOCKET_CONNECTING) {
NS_WARNING("SCO connection exists or is being established");
return false;
}
mSocket->Disconnect();
BluetoothService* bs = BluetoothService::Get();
NS_ENSURE_TRUE(bs, false);
nsresult rv = bs->GetScoSocket(aDeviceAddress,
true,
false,
mSocket);
return NS_FAILED(rv) ? false : true;
}
bool
BluetoothScoManager::Listen()
{
MOZ_ASSERT(NS_IsMainThread());
if (gInShutdown) {
MOZ_ASSERT(false, "Listen called while in shutdown!");
return false;
}
if (mSocket->GetConnectionStatus() ==
SocketConnectionStatus::SOCKET_LISTENING) {
NS_WARNING("BluetoothScoManager has been already listening");
return true;
}
mSocket->Disconnect();
if (!mSocket->Listen(-1)) {
NS_WARNING("[SCO] Can't listen on socket!");
return false;
}
mPrevSocketStatus = mSocket->GetConnectionStatus();
return true;
}
void
BluetoothScoManager::Disconnect()
{
mSocket->Disconnect();
}
void
BluetoothScoManager::OnConnectSuccess(BluetoothSocket* aSocket)
{
MOZ_ASSERT(aSocket == mSocket);
nsString address;
mSocket->GetAddress(address);
NotifyAudioManager(address);
mPrevSocketStatus = mSocket->GetConnectionStatus();
}
void
BluetoothScoManager::OnConnectError(BluetoothSocket* aSocket)
{
MOZ_ASSERT(aSocket == mSocket);
mSocket->Disconnect();
mPrevSocketStatus = mSocket->GetConnectionStatus();
Listen();
}
void
BluetoothScoManager::OnDisconnect(BluetoothSocket* aSocket)
{
MOZ_ASSERT(aSocket == mSocket);
if (mPrevSocketStatus == SocketConnectionStatus::SOCKET_CONNECTED) {
Listen();
nsString address = NS_LITERAL_STRING("");
NotifyAudioManager(address);
}
}
bool
BluetoothScoManager::IsConnected()
{
if (mSocket) {
return mSocket->GetConnectionStatus() ==
SocketConnectionStatus::SOCKET_CONNECTED;
}
return false;
}

View File

@ -1,52 +0,0 @@
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_dom_bluetooth_bluetoothscomanager_h__
#define mozilla_dom_bluetooth_bluetoothscomanager_h__
#include "BluetoothCommon.h"
#include "BluetoothSocketObserver.h"
#include "nsIObserver.h"
BEGIN_BLUETOOTH_NAMESPACE
class BluetoothReplyRunnable;
class BluetoothScoManagerObserver;
class BluetoothSocket;
class BluetoothScoManager : public BluetoothSocketObserver
{
public:
static BluetoothScoManager* Get();
~BluetoothScoManager();
virtual void ReceiveSocketData(
BluetoothSocket* aSocket,
nsAutoPtr<mozilla::ipc::UnixSocketRawData>& aMessage) MOZ_OVERRIDE;
virtual void OnConnectSuccess(BluetoothSocket* aSocket) MOZ_OVERRIDE;
virtual void OnConnectError(BluetoothSocket* aSocket) MOZ_OVERRIDE;
virtual void OnDisconnect(BluetoothSocket* aSocket) MOZ_OVERRIDE;
bool Connect(const nsAString& aDeviceObjectPath);
void Disconnect();
bool Listen();
bool IsConnected();
private:
friend class BluetoothScoManagerObserver;
BluetoothScoManager();
bool Init();
void Cleanup();
nsresult HandleShutdown();
void NotifyAudioManager(const nsAString& aAddress);
SocketConnectionStatus mPrevSocketStatus;
nsRefPtr<BluetoothSocket> mSocket;
};
END_BLUETOOTH_NAMESPACE
#endif

View File

@ -44,7 +44,6 @@ CPPSRCS += \
BluetoothHfpManager.cpp \
BluetoothOppManager.cpp \
ObexBase.cpp \
BluetoothScoManager.cpp \
BluetoothUuid.cpp \
BluetoothSocket.cpp \
$(NULL)

View File

@ -21,7 +21,6 @@
#include "BluetoothHfpManager.h"
#include "BluetoothOppManager.h"
#include "BluetoothReplyRunnable.h"
#include "BluetoothScoManager.h"
#include "BluetoothUnixSocketConnector.h"
#include "BluetoothUtils.h"
#include "BluetoothUuid.h"
@ -838,12 +837,6 @@ public:
return NS_ERROR_FAILURE;
}
BluetoothScoManager* sco = BluetoothScoManager::Get();
if (!sco || !sco->Listen()) {
NS_WARNING("Failed to start listening for BluetoothScoManager!");
return NS_ERROR_FAILURE;
}
BluetoothOppManager* opp = BluetoothOppManager::Get();
if (!opp || !opp->Listen()) {
NS_WARNING("Failed to start listening for BluetoothOppManager!");
@ -854,31 +847,6 @@ public:
}
};
class ShutdownProfileManagersRunnable : public nsRunnable
{
public:
NS_IMETHOD
Run()
{
BluetoothHfpManager* hfp = BluetoothHfpManager::Get();
if (hfp) {
hfp->Disconnect();
}
BluetoothOppManager* opp = BluetoothOppManager::Get();
if (opp) {
opp->Disconnect();
}
BluetoothScoManager* sco = BluetoothScoManager::Get();
if (sco) {
sco->Disconnect();
}
return NS_OK;
}
};
class PrepareAdapterRunnable : public nsRunnable
{
public: