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: #ifndef nsHtml5StreamListener_h michael@0: #define nsHtml5StreamListener_h michael@0: michael@0: #include "nsIStreamListener.h" michael@0: #include "nsIThreadRetargetableStreamListener.h" michael@0: #include "nsHtml5RefPtr.h" michael@0: #include "nsHtml5StreamParser.h" michael@0: michael@0: /** michael@0: * The purpose of this class is to reconcile the problem that michael@0: * nsHtml5StreamParser is a cycle collection participant, which means that it michael@0: * can only be refcounted on the main thread, but michael@0: * nsIThreadRetargetableStreamListener can be refcounted from another thread, michael@0: * so nsHtml5StreamParser being an nsIThreadRetargetableStreamListener was michael@0: * a memory corruption problem. michael@0: * michael@0: * mDelegate is an nsHtml5RefPtr, which releases the object that it points michael@0: * to from a runnable on the main thread. DropDelegate() is only called on michael@0: * the main thread. This call will finish before the main-thread derefs the michael@0: * nsHtml5StreamListener itself, so there is no risk of another thread making michael@0: * the refcount of nsHtml5StreamListener go to zero and running the destructor michael@0: * concurrently. Other than that, the thread-safe nsISupports implementation michael@0: * takes care of the destructor not running concurrently from different michael@0: * threads, so there is no need to have a mutex around nsHtml5RefPtr to michael@0: * prevent it from double-releasing nsHtml5StreamParser. michael@0: */ michael@0: class nsHtml5StreamListener : public nsIStreamListener, michael@0: public nsIThreadRetargetableStreamListener michael@0: { michael@0: public: michael@0: nsHtml5StreamListener(nsHtml5StreamParser* aDelegate); michael@0: virtual ~nsHtml5StreamListener(); michael@0: michael@0: NS_DECL_THREADSAFE_ISUPPORTS michael@0: NS_DECL_NSIREQUESTOBSERVER michael@0: NS_DECL_NSISTREAMLISTENER michael@0: NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER michael@0: michael@0: inline nsHtml5StreamParser* GetDelegate() michael@0: { michael@0: return mDelegate; michael@0: } michael@0: michael@0: void DropDelegate(); michael@0: michael@0: private: michael@0: nsHtml5RefPtr mDelegate; michael@0: }; michael@0: michael@0: #endif // nsHtml5StreamListener_h