Bug 1845715 - Check for failure when getting RegExp match result template r=iain
[gecko.git] / parser / html / nsHtml5StreamListener.cpp
blob86e18c0296b601eba16c831ea1eacd528cdc15a0
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 #include "nsHtml5StreamListener.h"
7 #include "nsHtml5StreamParserReleaser.h"
9 NS_IMPL_ADDREF(nsHtml5StreamListener)
10 NS_IMPL_RELEASE(nsHtml5StreamListener)
12 NS_INTERFACE_MAP_BEGIN(nsHtml5StreamListener)
13 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIRequestObserver)
14 NS_INTERFACE_MAP_ENTRY(nsIRequestObserver)
15 NS_INTERFACE_MAP_ENTRY(nsIStreamListener)
16 NS_INTERFACE_MAP_ENTRY(nsIThreadRetargetableStreamListener)
17 NS_INTERFACE_MAP_END
19 nsHtml5StreamListener::nsHtml5StreamListener(nsHtml5StreamParser* aDelegate)
20 : mDelegateMonitor("nsHtml5StreamListener mDelegateMonitor"),
21 mDelegate(aDelegate) {
22 MOZ_ASSERT(aDelegate, "Must have delegate");
23 aDelegate->AddRef();
26 nsHtml5StreamListener::~nsHtml5StreamListener() { DropDelegateImpl(); }
28 void nsHtml5StreamListener::DropDelegate() {
29 MOZ_ASSERT(NS_IsMainThread(),
30 "Must not call DropDelegate from non-main threads.");
31 DropDelegateImpl();
34 void nsHtml5StreamListener::DropDelegateImpl() {
35 mozilla::ReentrantMonitorAutoEnter autoEnter(mDelegateMonitor);
36 if (mDelegate) {
37 nsCOMPtr<nsIRunnable> releaser = new nsHtml5StreamParserReleaser(mDelegate);
38 if (NS_FAILED(((nsHtml5StreamParser*)mDelegate)
39 ->DispatchToMain(releaser.forget()))) {
40 NS_WARNING("Failed to dispatch releaser event.");
42 mDelegate = nullptr;
46 nsHtml5StreamParser* nsHtml5StreamListener::GetDelegate() {
47 MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
48 // Since this can be called only on the main
49 // thread and DropDelegate() can only be called on the main thread
50 // it's OK that the monitor here doesn't protect the use of the
51 // return value.
52 return mDelegate;
55 NS_IMETHODIMP
56 nsHtml5StreamListener::CheckListenerChain() {
57 if (MOZ_UNLIKELY(!mDelegate)) {
58 return NS_ERROR_NOT_AVAILABLE;
60 return NS_OK;
63 NS_IMETHODIMP
64 nsHtml5StreamListener::OnStartRequest(nsIRequest* aRequest) {
65 mozilla::ReentrantMonitorAutoEnter autoEnter(mDelegateMonitor);
66 if (MOZ_UNLIKELY(!mDelegate)) {
67 return NS_ERROR_NOT_AVAILABLE;
69 return ((nsHtml5StreamParser*)mDelegate)->OnStartRequest(aRequest);
72 NS_IMETHODIMP
73 nsHtml5StreamListener::OnStopRequest(nsIRequest* aRequest, nsresult aStatus) {
74 mozilla::ReentrantMonitorAutoEnter autoEnter(mDelegateMonitor);
75 if (MOZ_UNLIKELY(!mDelegate)) {
76 return NS_ERROR_NOT_AVAILABLE;
78 return ((nsHtml5StreamParser*)mDelegate)->OnStopRequest(aRequest, aStatus);
81 NS_IMETHODIMP
82 nsHtml5StreamListener::OnDataAvailable(nsIRequest* aRequest,
83 nsIInputStream* aInStream,
84 uint64_t aSourceOffset,
85 uint32_t aLength) {
86 mozilla::ReentrantMonitorAutoEnter autoEnter(mDelegateMonitor);
87 if (MOZ_UNLIKELY(!mDelegate)) {
88 return NS_ERROR_NOT_AVAILABLE;
90 return ((nsHtml5StreamParser*)mDelegate)
91 ->OnDataAvailable(aRequest, aInStream, aSourceOffset, aLength);