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
)
19 nsHtml5StreamListener::nsHtml5StreamListener(nsHtml5StreamParser
* aDelegate
)
20 : mDelegateMonitor("nsHtml5StreamListener mDelegateMonitor"),
21 mDelegate(aDelegate
) {
22 MOZ_ASSERT(aDelegate
, "Must have delegate");
26 nsHtml5StreamListener::~nsHtml5StreamListener() { DropDelegateImpl(); }
28 void nsHtml5StreamListener::DropDelegate() {
29 MOZ_ASSERT(NS_IsMainThread(),
30 "Must not call DropDelegate from non-main threads.");
34 void nsHtml5StreamListener::DropDelegateImpl() {
35 mozilla::ReentrantMonitorAutoEnter
autoEnter(mDelegateMonitor
);
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.");
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
56 nsHtml5StreamListener::CheckListenerChain() {
57 if (MOZ_UNLIKELY(!mDelegate
)) {
58 return NS_ERROR_NOT_AVAILABLE
;
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
);
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
);
82 nsHtml5StreamListener::OnDataAvailable(nsIRequest
* aRequest
,
83 nsIInputStream
* aInStream
,
84 uint64_t aSourceOffset
,
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
);