diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /parser/html/nsHtml5StreamListener.h | |
parent | Initial commit. (diff) | |
download | firefox-esr-upstream.tar.xz firefox-esr-upstream.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'parser/html/nsHtml5StreamListener.h')
-rw-r--r-- | parser/html/nsHtml5StreamListener.h | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/parser/html/nsHtml5StreamListener.h b/parser/html/nsHtml5StreamListener.h new file mode 100644 index 0000000000..46056eacee --- /dev/null +++ b/parser/html/nsHtml5StreamListener.h @@ -0,0 +1,62 @@ +/* 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" +#include "mozilla/ReentrantMonitor.h" + +/** + * 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. + * + * 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 + * 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 + * threads, so there is no need to have a mutex around nsHtml5StreamParserPtr to + * prevent it from double-releasing nsHtml5StreamParser. + */ +class nsHtml5StreamListener : public nsIStreamListener, + public nsIThreadRetargetableStreamListener { + public: + explicit nsHtml5StreamListener(nsHtml5StreamParser* aDelegate); + + NS_DECL_THREADSAFE_ISUPPORTS + NS_DECL_NSIREQUESTOBSERVER + NS_DECL_NSISTREAMLISTENER + NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER + + // Main-thread-only + nsHtml5StreamParser* GetDelegate(); + + // Main-thread-only + void DropDelegate(); + + private: + void DropDelegateImpl(); + virtual ~nsHtml5StreamListener(); + + // ReentrantMonitor instead of Mutex, because `GetDelegate()` + // can be called from within the Necko callbacks when Necko events + // are delivered on the main thread. + mozilla::ReentrantMonitor mDelegateMonitor MOZ_UNANNOTATED; + // Owning pointer with manually-managed refcounting, protected by + // 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 + // it needs the monitor. MOZ_PT_GUARDED_BY() can't be used with Atomic<...*> + mozilla::Atomic<nsHtml5StreamParser*, mozilla::ReleaseAcquire> mDelegate; +}; + +#endif // nsHtml5StreamListener_h |