Bumping manifests a=b2g-bump
[gecko.git] / netwerk / ipc / ChannelEventQueue.cpp
blob8f236da2eec291019564f727739f276adc8f48ba
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set sw=2 ts=8 et tw=80 :
3 */
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include "nsISupports.h"
9 #include "mozilla/net/ChannelEventQueue.h"
10 #include "nsThreadUtils.h"
12 namespace mozilla {
13 namespace net {
15 void
16 ChannelEventQueue::FlushQueue()
18 // Events flushed could include destruction of channel (and our own
19 // destructor) unless we make sure its refcount doesn't drop to 0 while this
20 // method is running.
21 nsCOMPtr<nsISupports> kungFuDeathGrip(mOwner);
23 // Prevent flushed events from flushing the queue recursively
24 mFlushing = true;
26 uint32_t i;
27 for (i = 0; i < mEventQueue.Length(); i++) {
28 mEventQueue[i]->Run();
29 if (mSuspended)
30 break;
33 // We will always want to remove at least one finished callback.
34 if (i < mEventQueue.Length())
35 i++;
37 // It is possible for new callbacks to be enqueued as we are
38 // flushing the queue, so the queue must not be cleared until
39 // all callbacks have run.
40 mEventQueue.RemoveElementsAt(0, i);
42 mFlushing = false;
45 void
46 ChannelEventQueue::Resume()
48 // Resuming w/o suspend: error in debug mode, ignore in build
49 MOZ_ASSERT(mSuspendCount > 0);
50 if (mSuspendCount <= 0) {
51 return;
54 if (!--mSuspendCount) {
55 nsRefPtr<nsRunnableMethod<ChannelEventQueue> > event =
56 NS_NewRunnableMethod(this, &ChannelEventQueue::CompleteResume);
57 if (mTargetThread) {
58 mTargetThread->Dispatch(event, NS_DISPATCH_NORMAL);
59 } else {
60 MOZ_RELEASE_ASSERT(NS_IsMainThread());
61 NS_DispatchToCurrentThread(event);
66 nsresult
67 ChannelEventQueue::RetargetDeliveryTo(nsIEventTarget* aTargetThread)
69 MOZ_RELEASE_ASSERT(NS_IsMainThread());
70 MOZ_RELEASE_ASSERT(!mTargetThread);
71 MOZ_RELEASE_ASSERT(aTargetThread);
73 mTargetThread = do_QueryInterface(aTargetThread);
74 MOZ_RELEASE_ASSERT(mTargetThread);
76 return NS_OK;