michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * vim: set sw=2 ts=8 et tw=80 : michael@0: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef mozilla_net_ChannelEventQueue_h michael@0: #define mozilla_net_ChannelEventQueue_h michael@0: michael@0: #include michael@0: #include michael@0: michael@0: class nsISupports; michael@0: class nsIEventTarget; michael@0: class nsIThread; michael@0: michael@0: namespace mozilla { michael@0: namespace net { michael@0: michael@0: class ChannelEvent michael@0: { michael@0: public: michael@0: ChannelEvent() { MOZ_COUNT_CTOR(ChannelEvent); } michael@0: virtual ~ChannelEvent() { MOZ_COUNT_DTOR(ChannelEvent); } michael@0: virtual void Run() = 0; michael@0: }; michael@0: michael@0: // Workaround for Necko re-entrancy dangers. We buffer IPDL messages in a michael@0: // queue if still dispatching previous one(s) to listeners/observers. michael@0: // Otherwise synchronous XMLHttpRequests and/or other code that spins the michael@0: // event loop (ex: IPDL rpc) could cause listener->OnDataAvailable (for michael@0: // instance) to be dispatched and called before mListener->OnStartRequest has michael@0: // completed. michael@0: michael@0: class AutoEventEnqueuerBase; michael@0: michael@0: class ChannelEventQueue MOZ_FINAL michael@0: { michael@0: NS_INLINE_DECL_REFCOUNTING(ChannelEventQueue) michael@0: michael@0: public: michael@0: ChannelEventQueue(nsISupports *owner) michael@0: : mSuspendCount(0) michael@0: , mSuspended(false) michael@0: , mForced(false) michael@0: , mFlushing(false) michael@0: , mOwner(owner) {} michael@0: michael@0: // Checks to determine if an IPDL-generated channel event can be processed michael@0: // immediately, or needs to be queued using Enqueue(). michael@0: inline bool ShouldEnqueue(); michael@0: michael@0: // Puts IPDL-generated channel event into queue, to be run later michael@0: // automatically when EndForcedQueueing and/or Resume is called. michael@0: inline void Enqueue(ChannelEvent* callback); michael@0: michael@0: // After StartForcedQueueing is called, ShouldEnqueue() will return true and michael@0: // no events will be run/flushed until EndForcedQueueing is called. michael@0: // - Note: queueing may still be required after EndForcedQueueing() (if the michael@0: // queue is suspended, etc): always call ShouldEnqueue() to determine michael@0: // whether queueing is needed. michael@0: inline void StartForcedQueueing(); michael@0: inline void EndForcedQueueing(); michael@0: michael@0: // Suspend/resume event queue. ShouldEnqueue() will return true and no events michael@0: // will be run/flushed until resume is called. These should be called when michael@0: // the channel owning the event queue is suspended/resumed. michael@0: inline void Suspend(); michael@0: // Resume flushes the queue asynchronously, i.e. items in queue will be michael@0: // dispatched in a new event on the current thread. michael@0: void Resume(); michael@0: michael@0: // Retargets delivery of events to the target thread specified. michael@0: nsresult RetargetDeliveryTo(nsIEventTarget* aTargetThread); michael@0: michael@0: private: michael@0: // Private destructor, to discourage deletion outside of Release(): michael@0: ~ChannelEventQueue() michael@0: { michael@0: } michael@0: michael@0: inline void MaybeFlushQueue(); michael@0: void FlushQueue(); michael@0: inline void CompleteResume(); michael@0: michael@0: nsTArray > mEventQueue; michael@0: michael@0: uint32_t mSuspendCount; michael@0: bool mSuspended; michael@0: bool mForced; michael@0: bool mFlushing; michael@0: michael@0: // Keep ptr to avoid refcount cycle: only grab ref during flushing. michael@0: nsISupports *mOwner; michael@0: michael@0: // Target thread for delivery of events. michael@0: nsCOMPtr mTargetThread; michael@0: michael@0: friend class AutoEventEnqueuer; michael@0: }; michael@0: michael@0: inline bool michael@0: ChannelEventQueue::ShouldEnqueue() michael@0: { michael@0: bool answer = mForced || mSuspended || mFlushing; michael@0: michael@0: NS_ABORT_IF_FALSE(answer == true || mEventQueue.IsEmpty(), michael@0: "Should always enqueue if ChannelEventQueue not empty"); michael@0: michael@0: return answer; michael@0: } michael@0: michael@0: inline void michael@0: ChannelEventQueue::Enqueue(ChannelEvent* callback) michael@0: { michael@0: mEventQueue.AppendElement(callback); michael@0: } michael@0: michael@0: inline void michael@0: ChannelEventQueue::StartForcedQueueing() michael@0: { michael@0: mForced = true; michael@0: } michael@0: michael@0: inline void michael@0: ChannelEventQueue::EndForcedQueueing() michael@0: { michael@0: mForced = false; michael@0: MaybeFlushQueue(); michael@0: } michael@0: michael@0: inline void michael@0: ChannelEventQueue::Suspend() michael@0: { michael@0: mSuspended = true; michael@0: mSuspendCount++; michael@0: } michael@0: michael@0: inline void michael@0: ChannelEventQueue::CompleteResume() michael@0: { michael@0: // channel may have been suspended again since Resume fired event to call this. michael@0: if (!mSuspendCount) { michael@0: // we need to remain logically suspended (for purposes of queuing incoming michael@0: // messages) until this point, else new incoming messages could run before michael@0: // queued ones. michael@0: mSuspended = false; michael@0: MaybeFlushQueue(); michael@0: } michael@0: } michael@0: michael@0: inline void michael@0: ChannelEventQueue::MaybeFlushQueue() michael@0: { michael@0: // Don't flush if forced queuing on, we're already being flushed, or michael@0: // suspended, or there's nothing to flush michael@0: if (!mForced && !mFlushing && !mSuspended && !mEventQueue.IsEmpty()) michael@0: FlushQueue(); michael@0: } michael@0: michael@0: // Ensures that ShouldEnqueue() will be true during its lifetime (letting michael@0: // caller know incoming IPDL msgs should be queued). Flushes the queue when it michael@0: // goes out of scope. michael@0: class AutoEventEnqueuer michael@0: { michael@0: public: michael@0: AutoEventEnqueuer(ChannelEventQueue *queue) : mEventQueue(queue) { michael@0: mEventQueue->StartForcedQueueing(); michael@0: } michael@0: ~AutoEventEnqueuer() { michael@0: mEventQueue->EndForcedQueueing(); michael@0: } michael@0: private: michael@0: ChannelEventQueue* mEventQueue; michael@0: }; michael@0: michael@0: } michael@0: } michael@0: michael@0: #endif