Bug 1283193 - Add buffer mechanism in GamepadPlatformService to prevent dispatch failure in Mochitest. r=ted

This commit is contained in:
Chih-Yi Leu 2016-10-23 19:32:00 +02:00
parent 00c21dd6dd
commit 51042398ff
2 changed files with 38 additions and 0 deletions

View File

@ -70,6 +70,14 @@ GamepadPlatformService::NotifyGamepadChange(const T& aInfo)
// mChannelParents may be accessed by background thread in the
// same time, we use mutex to prevent possible race condtion
MutexAutoLock autoLock(mMutex);
// Buffer all events if we have no Channel to dispatch, which
// may happen when performing Mochitest.
if (mChannelParents.IsEmpty()) {
mPendingEvents.AppendElement(e);
return;
}
for(uint32_t i = 0; i < mChannelParents.Length(); ++i) {
mChannelParents[i]->DispatchUpdateEvent(e);
}
@ -163,6 +171,27 @@ GamepadPlatformService::AddChannelParent(GamepadEventChannelParent* aParent)
// We use mutex here to prevent race condition with monitor thread
MutexAutoLock autoLock(mMutex);
mChannelParents.AppendElement(aParent);
FlushPendingEvents();
}
void
GamepadPlatformService::FlushPendingEvents()
{
AssertIsOnBackgroundThread();
MOZ_ASSERT(!mChannelParents.IsEmpty());
if (mPendingEvents.IsEmpty()) {
return;
}
// NOTE: This method must be called with mMutex held because it accesses
// mChannelParents.
for (uint32_t i=0; i<mChannelParents.Length(); ++i) {
for (uint32_t j=0; j<mPendingEvents.Length();++j) {
mChannelParents[i]->DispatchUpdateEvent(mPendingEvents[j]);
}
}
mPendingEvents.Clear();
}
void

View File

@ -74,6 +74,10 @@ class GamepadPlatformService final
GamepadPlatformService();
~GamepadPlatformService();
template<class T> void NotifyGamepadChange(const T& aInfo);
// Flush all pending events buffered in mPendingEvents, must be called
// with mMutex held
void FlushPendingEvents();
void Cleanup();
// mGamepadIndex can only be accessed by monitor thread
@ -87,6 +91,11 @@ class GamepadPlatformService final
// This mutex protects mChannelParents from race condition
// between background and monitor thread
Mutex mMutex;
// In mochitest, it is possible that the test Events is synthesized
// before GamepadEventChannel created, we need to buffer all events
// until the channel is created if that happens.
nsTArray<GamepadChangeEvent> mPendingEvents;
};
} // namespace dom