Bug 838421 - After an instance of BluetoothAdapter is destroyed, no events can be received by other instances, r=echou

This commit is contained in:
Gina Yeh 2013-02-08 18:03:09 +08:00
parent 0b3f770fb9
commit c6d8aea525
6 changed files with 35 additions and 92 deletions

View File

@ -161,15 +161,18 @@ BluetoothAdapter::BluetoothAdapter(nsPIDOMWindow* aWindow,
for (uint32_t i = 0; i < values.Length(); ++i) {
SetPropertyByValue(values[i]);
}
BluetoothService* bs = BluetoothService::Get();
NS_ENSURE_TRUE_VOID(bs);
bs->RegisterBluetoothSignalHandler(mPath, this);
}
BluetoothAdapter::~BluetoothAdapter()
{
BluetoothService* bs = BluetoothService::Get();
// We can be null on shutdown, where this might happen
if (bs) {
bs->UnregisterBluetoothSignalHandler(mPath, this);
}
NS_ENSURE_TRUE_VOID(bs);
bs->UnregisterBluetoothSignalHandler(mPath, this);
Unroot();
}
@ -263,12 +266,7 @@ BluetoothAdapter::Create(nsPIDOMWindow* aWindow, const BluetoothValue& aValue)
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aWindow);
BluetoothService* bs = BluetoothService::Get();
NS_ENSURE_TRUE(bs, nullptr);
nsRefPtr<BluetoothAdapter> adapter = new BluetoothAdapter(aWindow, aValue);
bs->RegisterBluetoothSignalHandler(adapter->GetPath(), adapter);
return adapter.forget();
}

View File

@ -61,15 +61,18 @@ BluetoothDevice::BluetoothDevice(nsPIDOMWindow* aWindow,
for (uint32_t i = 0; i < values.Length(); ++i) {
SetPropertyByValue(values[i]);
}
BluetoothService* bs = BluetoothService::Get();
NS_ENSURE_TRUE_VOID(bs);
bs->RegisterBluetoothSignalHandler(mPath, this);
}
BluetoothDevice::~BluetoothDevice()
{
BluetoothService* bs = BluetoothService::Get();
// bs can be null on shutdown, where destruction might happen.
if (bs) {
bs->UnregisterBluetoothSignalHandler(mPath, this);
}
NS_ENSURE_TRUE_VOID(bs);
bs->UnregisterBluetoothSignalHandler(mPath, this);
Unroot();
}
@ -156,13 +159,8 @@ BluetoothDevice::Create(nsPIDOMWindow* aWindow,
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aWindow);
BluetoothService* bs = BluetoothService::Get();
NS_ENSURE_TRUE(bs, nullptr);
nsRefPtr<BluetoothDevice> device =
new BluetoothDevice(aWindow, aAdapterPath, aValue);
bs->RegisterBluetoothSignalHandler(device->mPath, device);
return device.forget();
}

View File

@ -94,28 +94,24 @@ private:
nsRefPtr<BluetoothManager> mManagerPtr;
};
nsresult
BluetoothManager::FireEnabledDisabledEvent(bool aEnabled)
{
return DispatchTrustedEvent(aEnabled ? NS_LITERAL_STRING("enabled")
: NS_LITERAL_STRING("disabled"));
}
BluetoothManager::BluetoothManager(nsPIDOMWindow *aWindow)
: BluetoothPropertyContainer(BluetoothObjectType::TYPE_MANAGER)
: BluetoothPropertyContainer(BluetoothObjectType::TYPE_MANAGER)
{
MOZ_ASSERT(aWindow);
BindToOwner(aWindow);
mPath.AssignLiteral("/");
BluetoothService* bs = BluetoothService::Get();
NS_ENSURE_TRUE_VOID(bs);
bs->RegisterBluetoothSignalHandler(mPath, this);
}
BluetoothManager::~BluetoothManager()
{
BluetoothService* bs = BluetoothService::Get();
if (bs) {
bs->UnregisterManager(this);
}
NS_ENSURE_TRUE_VOID(bs);
bs->UnregisterBluetoothSignalHandler(mPath, this);
}
void
@ -171,11 +167,6 @@ BluetoothManager::Create(nsPIDOMWindow* aWindow)
MOZ_ASSERT(aWindow);
nsRefPtr<BluetoothManager> manager = new BluetoothManager(aWindow);
BluetoothService* bs = BluetoothService::Get();
NS_ENSURE_TRUE(bs, nullptr);
bs->RegisterManager(manager);
return manager.forget();
}
@ -212,6 +203,10 @@ BluetoothManager::Notify(const BluetoothSignal& aData)
if (aData.name().EqualsLiteral("AdapterAdded")) {
DispatchTrustedEvent(NS_LITERAL_STRING("adapteradded"));
} else if (aData.name().EqualsLiteral("Enabled")) {
DispatchTrustedEvent(NS_LITERAL_STRING("enabled"));
} else if (aData.name().EqualsLiteral("Disabled")) {
DispatchTrustedEvent(NS_LITERAL_STRING("disabled"));
} else {
#ifdef DEBUG
nsCString warningMsg;

View File

@ -35,7 +35,6 @@ public:
Create(nsPIDOMWindow* aWindow);
void Notify(const BluetoothSignal& aData);
virtual void SetPropertyByValue(const BluetoothNamedValue& aValue);
nsresult FireEnabledDisabledEvent(bool aEnabled);
private:
BluetoothManager(nsPIDOMWindow* aWindow);
~BluetoothManager();

View File

@ -127,8 +127,18 @@ public:
}
if (!gInShutdown) {
// Notify all the managers about the state change.
gBluetoothService->SetEnabled(mEnabled);
nsAutoString signalName, signalPath;
BluetoothValue v = true;
if (mEnabled) {
signalName = NS_LITERAL_STRING("Enabled");
} else {
signalName = NS_LITERAL_STRING("Disabled");
}
signalPath = NS_LITERAL_STRING("/");
BluetoothSignal signal(signalName, signalPath, v);
gBluetoothService->DistributeSignal(signal);
}
if (!mEnabled || gInShutdown) {
@ -351,7 +361,6 @@ BluetoothService::RegisterBluetoothSignalHandler(
mBluetoothSignalObserverTable.Put(aNodeName, ol);
}
ol->RemoveObserver(aHandler);
ol->AddObserver(aHandler);
}
@ -468,20 +477,7 @@ BluetoothService::SetEnabled(bool aEnabled)
unused << childActors[index]->SendEnabled(aEnabled);
}
if (aEnabled) {
BluetoothManagerList::ForwardIterator iter(mLiveManagers);
nsString managerPath = NS_LITERAL_STRING("/");
/**
* Re-register managers since table mBluetoothSignalObserverTable was
* cleared after turned off bluetooth
*/
while (iter.HasMore()) {
RegisterBluetoothSignalHandler(
managerPath,
(BluetoothSignalObserver*)iter.GetNext());
}
} else {
if (!aEnabled) {
/**
* Remove all handlers except BluetoothManager when turning off bluetooth
* since it is possible that the event 'onAdapterAdded' would be fired after
@ -503,14 +499,6 @@ BluetoothService::SetEnabled(bool aEnabled)
mEnabled = aEnabled;
// Fire onenabled/ondisabled event for each BluetoothManager
BluetoothManagerList::ForwardIterator iter(mLiveManagers);
while (iter.HasMore()) {
if (NS_FAILED(iter.GetNext()->FireEnabledDisabledEvent(aEnabled))) {
NS_WARNING("FireEnabledDisabledEvent failed!");
}
}
gToggleInProgress = false;
}
@ -709,26 +697,6 @@ BluetoothService::HandleShutdown()
return NS_OK;
}
void
BluetoothService::RegisterManager(BluetoothManager* aManager)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aManager);
MOZ_ASSERT(!mLiveManagers.Contains(aManager));
mLiveManagers.AppendElement(aManager);
}
void
BluetoothService::UnregisterManager(BluetoothManager* aManager)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aManager);
MOZ_ASSERT(mLiveManagers.Contains(aManager));
mLiveManagers.RemoveElement(aManager);
}
// static
BluetoothService*
BluetoothService::Get()

View File

@ -87,18 +87,6 @@ public:
void
DistributeSignal(const BluetoothSignal& aEvent);
/**
* Called when a BluetoothManager is created.
*/
void
RegisterManager(BluetoothManager* aManager);
/**
* Called when a BluetoothManager is destroyed.
*/
void
UnregisterManager(BluetoothManager* aManager);
/**
* Called when get a Bluetooth Signal from BluetoothDBusService
*
@ -382,9 +370,6 @@ protected:
BluetoothSignalObserverTable mBluetoothSignalObserverTable;
typedef nsTObserverArray<BluetoothManager*> BluetoothManagerList;
BluetoothManagerList mLiveManagers;
bool mEnabled;
};