1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/ipc/ChannelEventQueue.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,80 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- 1.5 + * vim: set sw=2 ts=8 et tw=80 : 1.6 + */ 1.7 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.8 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.9 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.10 + 1.11 +#include "nsISupports.h" 1.12 +#include "mozilla/net/ChannelEventQueue.h" 1.13 +#include "nsThreadUtils.h" 1.14 + 1.15 +namespace mozilla { 1.16 +namespace net { 1.17 + 1.18 +void 1.19 +ChannelEventQueue::FlushQueue() 1.20 +{ 1.21 + // Events flushed could include destruction of channel (and our own 1.22 + // destructor) unless we make sure its refcount doesn't drop to 0 while this 1.23 + // method is running. 1.24 + nsCOMPtr<nsISupports> kungFuDeathGrip(mOwner); 1.25 + 1.26 + // Prevent flushed events from flushing the queue recursively 1.27 + mFlushing = true; 1.28 + 1.29 + uint32_t i; 1.30 + for (i = 0; i < mEventQueue.Length(); i++) { 1.31 + mEventQueue[i]->Run(); 1.32 + if (mSuspended) 1.33 + break; 1.34 + } 1.35 + 1.36 + // We will always want to remove at least one finished callback. 1.37 + if (i < mEventQueue.Length()) 1.38 + i++; 1.39 + 1.40 + // It is possible for new callbacks to be enqueued as we are 1.41 + // flushing the queue, so the queue must not be cleared until 1.42 + // all callbacks have run. 1.43 + mEventQueue.RemoveElementsAt(0, i); 1.44 + 1.45 + mFlushing = false; 1.46 +} 1.47 + 1.48 +void 1.49 +ChannelEventQueue::Resume() 1.50 +{ 1.51 + // Resuming w/o suspend: error in debug mode, ignore in build 1.52 + MOZ_ASSERT(mSuspendCount > 0); 1.53 + if (mSuspendCount <= 0) { 1.54 + return; 1.55 + } 1.56 + 1.57 + if (!--mSuspendCount) { 1.58 + nsRefPtr<nsRunnableMethod<ChannelEventQueue> > event = 1.59 + NS_NewRunnableMethod(this, &ChannelEventQueue::CompleteResume); 1.60 + if (mTargetThread) { 1.61 + mTargetThread->Dispatch(event, NS_DISPATCH_NORMAL); 1.62 + } else { 1.63 + MOZ_RELEASE_ASSERT(NS_IsMainThread()); 1.64 + NS_DispatchToCurrentThread(event); 1.65 + } 1.66 + } 1.67 +} 1.68 + 1.69 +nsresult 1.70 +ChannelEventQueue::RetargetDeliveryTo(nsIEventTarget* aTargetThread) 1.71 +{ 1.72 + MOZ_RELEASE_ASSERT(NS_IsMainThread()); 1.73 + MOZ_RELEASE_ASSERT(!mTargetThread); 1.74 + MOZ_RELEASE_ASSERT(aTargetThread); 1.75 + 1.76 + mTargetThread = do_QueryInterface(aTargetThread); 1.77 + MOZ_RELEASE_ASSERT(mTargetThread); 1.78 + 1.79 + return NS_OK; 1.80 +} 1.81 + 1.82 +} 1.83 +}