Bug 1728955: part 3) Add logging to `nsBaseClipboard`. r=masayuki
[gecko.git] / parser / html / nsHtml5StreamListener.h
blob30dc7ced0511310eb587b9c9fb4cdca4d9d16d87
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef nsHtml5StreamListener_h
6 #define nsHtml5StreamListener_h
8 #include "nsIStreamListener.h"
9 #include "nsIThreadRetargetableStreamListener.h"
10 #include "nsHtml5StreamParser.h"
11 #include "mozilla/ReentrantMonitor.h"
13 /**
14 * The purpose of this class is to reconcile the problem that
15 * nsHtml5StreamParser is a cycle collection participant, which means that it
16 * can only be refcounted on the main thread, but
17 * nsIThreadRetargetableStreamListener can be refcounted from another thread,
18 * so nsHtml5StreamParser being an nsIThreadRetargetableStreamListener was
19 * a memory corruption problem.
21 * mDelegate is an nsHtml5StreamParserPtr, which releases the object that it
22 * points to from a runnable on the main thread. DropDelegate() is only called
23 * on the main thread. This call will finish before the main-thread derefs the
24 * nsHtml5StreamListener itself, so there is no risk of another thread making
25 * the refcount of nsHtml5StreamListener go to zero and running the destructor
26 * concurrently. Other than that, the thread-safe nsISupports implementation
27 * takes care of the destructor not running concurrently from different
28 * threads, so there is no need to have a mutex around nsHtml5StreamParserPtr to
29 * prevent it from double-releasing nsHtml5StreamParser.
31 class nsHtml5StreamListener : public nsIStreamListener,
32 public nsIThreadRetargetableStreamListener {
33 public:
34 explicit nsHtml5StreamListener(nsHtml5StreamParser* aDelegate);
36 NS_DECL_THREADSAFE_ISUPPORTS
37 NS_DECL_NSIREQUESTOBSERVER
38 NS_DECL_NSISTREAMLISTENER
39 NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
41 // Main-thread-only
42 nsHtml5StreamParser* GetDelegate();
44 // Main-thread-only
45 void DropDelegate();
47 private:
48 void DropDelegateImpl();
49 virtual ~nsHtml5StreamListener();
51 // ReentrantMonitor instead of Mutex, because `GetDelegate()`
52 // can be called from within the Necko callbacks when Necko events
53 // are delivered on the main thread.
54 mozilla::ReentrantMonitor mDelegateMonitor;
55 // Owning pointer with manually-managed refcounting, protected by
56 // mDelegateMonitor.
57 nsHtml5StreamParser* mDelegate;
60 #endif // nsHtml5StreamListener_h