Bug 1148161 - OfflineObserver seems to be racy r=honzab

This commit is contained in:
Valentin Gosu 2015-03-27 17:54:20 +02:00
parent 06cf358ad0
commit e7616f0f45
2 changed files with 16 additions and 1 deletions

View File

@ -63,6 +63,7 @@ OfflineObserver::RemoveOfflineObserverMainThread()
}
OfflineObserver::OfflineObserver(DisconnectableParent * parent)
: mLock("OfflineObserver")
{
mParent = parent;
RegisterOfflineObserver();
@ -71,8 +72,11 @@ OfflineObserver::OfflineObserver(DisconnectableParent * parent)
void
OfflineObserver::RemoveObserver()
{
{
mozilla::MutexAutoLock lock(mLock);
mParent = nullptr;
}
RemoveOfflineObserver();
mParent = nullptr;
}
NS_IMETHODIMP
@ -80,6 +84,10 @@ OfflineObserver::Observe(nsISupports *aSubject,
const char *aTopic,
const char16_t *aData)
{
mozilla::MutexAutoLock lock(mLock);
// Since the parent is supposed to call RemoveObserver in its destructor
// we need to keep the mutex locked while calling OfflineNotification
// to prevent mParent from going away.
if (mParent &&
!strcmp(aTopic, NS_IOSERVICE_APP_OFFLINE_STATUS_TOPIC)) {
mParent->OfflineNotification(aSubject);

View File

@ -33,6 +33,9 @@ public:
// OfflineDisconnect cancels all existing connections in the parent when
// the app becomes offline.
// Since the offline observer holds a mutex while calling this,
// the implementation must make sure it doesn't call back into OfflineObserver
// or issue a notification for the "network:app-offline-status-changed" topic
virtual void OfflineDisconnect() { }
};
@ -64,7 +67,11 @@ private:
void RemoveOfflineObserverMainThread();
private:
virtual ~OfflineObserver() { }
// This needs to be a raw pointer, or else the parent's destructor
// will not get called
DisconnectableParent * mParent;
// We need to lock this mutex when accessing the value of mParent
mozilla::Mutex mLock;
};
} // namespace net