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: #include "nsISupports.h" michael@0: #include "mozilla/net/ChannelEventQueue.h" michael@0: #include "nsThreadUtils.h" michael@0: michael@0: namespace mozilla { michael@0: namespace net { michael@0: michael@0: void michael@0: ChannelEventQueue::FlushQueue() michael@0: { michael@0: // Events flushed could include destruction of channel (and our own michael@0: // destructor) unless we make sure its refcount doesn't drop to 0 while this michael@0: // method is running. michael@0: nsCOMPtr kungFuDeathGrip(mOwner); michael@0: michael@0: // Prevent flushed events from flushing the queue recursively michael@0: mFlushing = true; michael@0: michael@0: uint32_t i; michael@0: for (i = 0; i < mEventQueue.Length(); i++) { michael@0: mEventQueue[i]->Run(); michael@0: if (mSuspended) michael@0: break; michael@0: } michael@0: michael@0: // We will always want to remove at least one finished callback. michael@0: if (i < mEventQueue.Length()) michael@0: i++; michael@0: michael@0: // It is possible for new callbacks to be enqueued as we are michael@0: // flushing the queue, so the queue must not be cleared until michael@0: // all callbacks have run. michael@0: mEventQueue.RemoveElementsAt(0, i); michael@0: michael@0: mFlushing = false; michael@0: } michael@0: michael@0: void michael@0: ChannelEventQueue::Resume() michael@0: { michael@0: // Resuming w/o suspend: error in debug mode, ignore in build michael@0: MOZ_ASSERT(mSuspendCount > 0); michael@0: if (mSuspendCount <= 0) { michael@0: return; michael@0: } michael@0: michael@0: if (!--mSuspendCount) { michael@0: nsRefPtr > event = michael@0: NS_NewRunnableMethod(this, &ChannelEventQueue::CompleteResume); michael@0: if (mTargetThread) { michael@0: mTargetThread->Dispatch(event, NS_DISPATCH_NORMAL); michael@0: } else { michael@0: MOZ_RELEASE_ASSERT(NS_IsMainThread()); michael@0: NS_DispatchToCurrentThread(event); michael@0: } michael@0: } michael@0: } michael@0: michael@0: nsresult michael@0: ChannelEventQueue::RetargetDeliveryTo(nsIEventTarget* aTargetThread) michael@0: { michael@0: MOZ_RELEASE_ASSERT(NS_IsMainThread()); michael@0: MOZ_RELEASE_ASSERT(!mTargetThread); michael@0: MOZ_RELEASE_ASSERT(aTargetThread); michael@0: michael@0: mTargetThread = do_QueryInterface(aTargetThread); michael@0: MOZ_RELEASE_ASSERT(mTargetThread); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: } michael@0: }