2014-04-16 09:41:39 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef nsHtml5StreamListener_h
|
|
|
|
#define nsHtml5StreamListener_h
|
|
|
|
|
|
|
|
#include "nsIStreamListener.h"
|
|
|
|
#include "nsIThreadRetargetableStreamListener.h"
|
|
|
|
#include "nsHtml5StreamParser.h"
|
2021-08-13 15:13:48 +03:00
|
|
|
#include "mozilla/ReentrantMonitor.h"
|
2014-04-16 09:41:39 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The purpose of this class is to reconcile the problem that
|
|
|
|
* nsHtml5StreamParser is a cycle collection participant, which means that it
|
|
|
|
* can only be refcounted on the main thread, but
|
|
|
|
* nsIThreadRetargetableStreamListener can be refcounted from another thread,
|
|
|
|
* so nsHtml5StreamParser being an nsIThreadRetargetableStreamListener was
|
|
|
|
* a memory corruption problem.
|
|
|
|
*
|
2018-03-16 18:26:06 +03:00
|
|
|
* mDelegate is an nsHtml5StreamParserPtr, which releases the object that it
|
|
|
|
* points to from a runnable on the main thread. DropDelegate() is only called
|
|
|
|
* on the main thread. This call will finish before the main-thread derefs the
|
2014-04-16 09:41:39 +04:00
|
|
|
* nsHtml5StreamListener itself, so there is no risk of another thread making
|
|
|
|
* the refcount of nsHtml5StreamListener go to zero and running the destructor
|
|
|
|
* concurrently. Other than that, the thread-safe nsISupports implementation
|
|
|
|
* takes care of the destructor not running concurrently from different
|
2017-06-27 20:38:30 +03:00
|
|
|
* threads, so there is no need to have a mutex around nsHtml5StreamParserPtr to
|
2014-04-16 09:41:39 +04:00
|
|
|
* prevent it from double-releasing nsHtml5StreamParser.
|
|
|
|
*/
|
2023-10-27 16:30:11 +03:00
|
|
|
class nsHtml5StreamListener : public nsIThreadRetargetableStreamListener {
|
2014-04-16 09:41:39 +04:00
|
|
|
public:
|
2014-09-02 02:04:20 +04:00
|
|
|
explicit nsHtml5StreamListener(nsHtml5StreamParser* aDelegate);
|
2014-04-16 09:41:39 +04:00
|
|
|
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
NS_DECL_NSIREQUESTOBSERVER
|
|
|
|
NS_DECL_NSISTREAMLISTENER
|
|
|
|
NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
|
|
|
|
|
2021-08-13 15:13:48 +03:00
|
|
|
// Main-thread-only
|
|
|
|
nsHtml5StreamParser* GetDelegate();
|
2014-04-16 09:41:39 +04:00
|
|
|
|
2021-08-13 15:13:48 +03:00
|
|
|
// Main-thread-only
|
2014-04-16 09:41:39 +04:00
|
|
|
void DropDelegate();
|
|
|
|
|
|
|
|
private:
|
2021-08-13 15:13:48 +03:00
|
|
|
void DropDelegateImpl();
|
2014-06-27 22:41:03 +04:00
|
|
|
virtual ~nsHtml5StreamListener();
|
|
|
|
|
2021-08-13 15:13:48 +03:00
|
|
|
// ReentrantMonitor instead of Mutex, because `GetDelegate()`
|
|
|
|
// can be called from within the Necko callbacks when Necko events
|
|
|
|
// are delivered on the main thread.
|
2022-03-16 21:47:08 +03:00
|
|
|
mozilla::ReentrantMonitor mDelegateMonitor MOZ_UNANNOTATED;
|
2021-08-13 15:13:48 +03:00
|
|
|
// Owning pointer with manually-managed refcounting, protected by
|
2022-02-11 23:56:58 +03:00
|
|
|
// mDelegateMonitor. Access to it is Atomic, which avoids getting a lock
|
|
|
|
// to check if it's set or to return the pointer. Access to the data within
|
2022-08-03 19:39:41 +03:00
|
|
|
// it needs the monitor. MOZ_PT_GUARDED_BY() can't be used with Atomic<...*>
|
2022-02-11 23:56:58 +03:00
|
|
|
mozilla::Atomic<nsHtml5StreamParser*, mozilla::ReleaseAcquire> mDelegate;
|
2014-04-16 09:41:39 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // nsHtml5StreamListener_h
|