1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set sw=2 ts=8 et tw=80 :
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"
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
21 nsCOMPtr
<nsISupports
> kungFuDeathGrip(mOwner
);
23 // Prevent flushed events from flushing the queue recursively
27 for (i
= 0; i
< mEventQueue
.Length(); i
++) {
28 mEventQueue
[i
]->Run();
33 // We will always want to remove at least one finished callback.
34 if (i
< mEventQueue
.Length())
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
);
46 ChannelEventQueue::Resume()
48 // Resuming w/o suspend: error in debug mode, ignore in build
49 MOZ_ASSERT(mSuspendCount
> 0);
50 if (mSuspendCount
<= 0) {
54 if (!--mSuspendCount
) {
55 nsRefPtr
<nsRunnableMethod
<ChannelEventQueue
> > event
=
56 NS_NewRunnableMethod(this, &ChannelEventQueue::CompleteResume
);
58 mTargetThread
->Dispatch(event
, NS_DISPATCH_NORMAL
);
60 MOZ_RELEASE_ASSERT(NS_IsMainThread());
61 NS_DispatchToCurrentThread(event
);
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
);