|
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/. */ |
|
7 |
|
8 #include "nsISupports.h" |
|
9 #include "mozilla/net/ChannelEventQueue.h" |
|
10 #include "nsThreadUtils.h" |
|
11 |
|
12 namespace mozilla { |
|
13 namespace net { |
|
14 |
|
15 void |
|
16 ChannelEventQueue::FlushQueue() |
|
17 { |
|
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); |
|
22 |
|
23 // Prevent flushed events from flushing the queue recursively |
|
24 mFlushing = true; |
|
25 |
|
26 uint32_t i; |
|
27 for (i = 0; i < mEventQueue.Length(); i++) { |
|
28 mEventQueue[i]->Run(); |
|
29 if (mSuspended) |
|
30 break; |
|
31 } |
|
32 |
|
33 // We will always want to remove at least one finished callback. |
|
34 if (i < mEventQueue.Length()) |
|
35 i++; |
|
36 |
|
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); |
|
41 |
|
42 mFlushing = false; |
|
43 } |
|
44 |
|
45 void |
|
46 ChannelEventQueue::Resume() |
|
47 { |
|
48 // Resuming w/o suspend: error in debug mode, ignore in build |
|
49 MOZ_ASSERT(mSuspendCount > 0); |
|
50 if (mSuspendCount <= 0) { |
|
51 return; |
|
52 } |
|
53 |
|
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); |
|
62 } |
|
63 } |
|
64 } |
|
65 |
|
66 nsresult |
|
67 ChannelEventQueue::RetargetDeliveryTo(nsIEventTarget* aTargetThread) |
|
68 { |
|
69 MOZ_RELEASE_ASSERT(NS_IsMainThread()); |
|
70 MOZ_RELEASE_ASSERT(!mTargetThread); |
|
71 MOZ_RELEASE_ASSERT(aTargetThread); |
|
72 |
|
73 mTargetThread = do_QueryInterface(aTargetThread); |
|
74 MOZ_RELEASE_ASSERT(mTargetThread); |
|
75 |
|
76 return NS_OK; |
|
77 } |
|
78 |
|
79 } |
|
80 } |