diff --git a/dom/base/EventSource.cpp b/dom/base/EventSource.cpp index 56ed03548234..3f780a4543bc 100644 --- a/dom/base/EventSource.cpp +++ b/dom/base/EventSource.cpp @@ -248,7 +248,12 @@ class EventSourceImpl final : public nsIObserver, struct Message { nsString mEventName; - nsString mLastEventID; + // We need to be able to distinguish between different states of id field: + // 1) is not given at all + // 2) is given but is empty + // 3) is given and has a value + // We can't check for the 1st state with a simple nsString. + Maybe mLastEventID; nsString mData; }; @@ -1373,12 +1378,6 @@ nsresult EventSourceImpl::DispatchCurrentMessageEvent() { message->mEventName.AssignLiteral("message"); } - if (message->mLastEventID.IsEmpty() && !mLastEventID.IsEmpty()) { - message->mLastEventID.Assign(mLastEventID); - } - - mServiceNotifier->EventReceived(message->mEventName, message->mLastEventID, - message->mData, mReconnectionTime, PR_Now()); mMessagesToDispatch.Push(message.release()); if (!mGoingToDispatchAllMessages) { @@ -1418,6 +1417,19 @@ void EventSourceImpl::DispatchAllMessageEvents() { while (mMessagesToDispatch.GetSize() > 0) { UniquePtr message( static_cast(mMessagesToDispatch.PopFront())); + + if (message->mLastEventID.isSome()) { + mLastEventID.Assign(message->mLastEventID.value()); + } + + if (message->mLastEventID.isNothing() && !mLastEventID.IsEmpty()) { + message->mLastEventID = Some(mLastEventID); + } + + mServiceNotifier->EventReceived(message->mEventName, mLastEventID, + message->mData, mReconnectionTime, + PR_Now()); + // Now we can turn our string into a jsval JS::Rooted jsData(cx); { @@ -1436,9 +1448,8 @@ void EventSourceImpl::DispatchAllMessageEvents() { new MessageEvent(mEventSource, nullptr, nullptr); event->InitMessageEvent(nullptr, message->mEventName, CanBubble::eNo, - Cancelable::eNo, jsData, mOrigin, - message->mLastEventID, nullptr, - Sequence>()); + Cancelable::eNo, jsData, mOrigin, mLastEventID, + nullptr, Sequence>()); event->SetTrusted(true); IgnoredErrorResult err; @@ -1448,7 +1459,6 @@ void EventSourceImpl::DispatchAllMessageEvents() { return; } - mLastEventID.Assign(message->mLastEventID); if (IsClosed() || IsFrozen()) { return; } @@ -1495,7 +1505,7 @@ nsresult EventSourceImpl::SetFieldAndClear() { case char16_t('i'): if (mLastFieldName.EqualsLiteral("id")) { - mCurrentMessage->mLastEventID.Assign(mLastFieldValue); + mCurrentMessage->mLastEventID = Some(mLastFieldValue); } break;