Fri, 16 Jan 2015 04:50:19 +0100
Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "WebVTTListener.h" |
michael@0 | 7 | #include "mozilla/dom/TextTrackCue.h" |
michael@0 | 8 | #include "mozilla/dom/TextTrackRegion.h" |
michael@0 | 9 | #include "mozilla/dom/VTTRegionBinding.h" |
michael@0 | 10 | #include "mozilla/dom/HTMLTrackElement.h" |
michael@0 | 11 | #include "nsIInputStream.h" |
michael@0 | 12 | #include "nsIWebVTTParserWrapper.h" |
michael@0 | 13 | #include "nsComponentManagerUtils.h" |
michael@0 | 14 | |
michael@0 | 15 | namespace mozilla { |
michael@0 | 16 | namespace dom { |
michael@0 | 17 | |
michael@0 | 18 | NS_IMPL_CYCLE_COLLECTION(WebVTTListener, mElement, mParserWrapper) |
michael@0 | 19 | |
michael@0 | 20 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(WebVTTListener) |
michael@0 | 21 | NS_INTERFACE_MAP_ENTRY(nsIWebVTTListener) |
michael@0 | 22 | NS_INTERFACE_MAP_ENTRY(nsIStreamListener) |
michael@0 | 23 | NS_INTERFACE_MAP_ENTRY(nsIChannelEventSink) |
michael@0 | 24 | NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor) |
michael@0 | 25 | NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIWebVTTListener) |
michael@0 | 26 | NS_INTERFACE_MAP_END |
michael@0 | 27 | |
michael@0 | 28 | NS_IMPL_CYCLE_COLLECTING_ADDREF(WebVTTListener) |
michael@0 | 29 | NS_IMPL_CYCLE_COLLECTING_RELEASE(WebVTTListener) |
michael@0 | 30 | |
michael@0 | 31 | #ifdef PR_LOGGING |
michael@0 | 32 | PRLogModuleInfo* gTextTrackLog; |
michael@0 | 33 | # define VTT_LOG(...) PR_LOG(gTextTrackLog, PR_LOG_DEBUG, (__VA_ARGS__)) |
michael@0 | 34 | #else |
michael@0 | 35 | # define VTT_LOG(msg) |
michael@0 | 36 | #endif |
michael@0 | 37 | |
michael@0 | 38 | WebVTTListener::WebVTTListener(HTMLTrackElement* aElement) |
michael@0 | 39 | : mElement(aElement) |
michael@0 | 40 | { |
michael@0 | 41 | MOZ_ASSERT(mElement, "Must pass an element to the callback"); |
michael@0 | 42 | #ifdef PR_LOGGING |
michael@0 | 43 | if (!gTextTrackLog) { |
michael@0 | 44 | gTextTrackLog = PR_NewLogModule("TextTrack"); |
michael@0 | 45 | } |
michael@0 | 46 | #endif |
michael@0 | 47 | VTT_LOG("WebVTTListener created."); |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | WebVTTListener::~WebVTTListener() |
michael@0 | 51 | { |
michael@0 | 52 | VTT_LOG("WebVTTListener destroyed."); |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | NS_IMETHODIMP |
michael@0 | 56 | WebVTTListener::GetInterface(const nsIID &aIID, |
michael@0 | 57 | void** aResult) |
michael@0 | 58 | { |
michael@0 | 59 | return QueryInterface(aIID, aResult); |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | nsresult |
michael@0 | 63 | WebVTTListener::LoadResource() |
michael@0 | 64 | { |
michael@0 | 65 | if (!HTMLTrackElement::IsWebVTTEnabled()) { |
michael@0 | 66 | NS_WARNING("WebVTT support disabled." |
michael@0 | 67 | " See media.webvtt.enabled in about:config. "); |
michael@0 | 68 | return NS_ERROR_FAILURE; |
michael@0 | 69 | } |
michael@0 | 70 | nsresult rv; |
michael@0 | 71 | mParserWrapper = do_CreateInstance(NS_WEBVTTPARSERWRAPPER_CONTRACTID, &rv); |
michael@0 | 72 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 73 | |
michael@0 | 74 | nsPIDOMWindow* window = mElement->OwnerDoc()->GetWindow(); |
michael@0 | 75 | rv = mParserWrapper->LoadParser(window); |
michael@0 | 76 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 77 | |
michael@0 | 78 | rv = mParserWrapper->Watch(this); |
michael@0 | 79 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 80 | |
michael@0 | 81 | mElement->SetReadyState(TextTrackReadyState::Loading); |
michael@0 | 82 | return NS_OK; |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | NS_IMETHODIMP |
michael@0 | 86 | WebVTTListener::AsyncOnChannelRedirect(nsIChannel* aOldChannel, |
michael@0 | 87 | nsIChannel* aNewChannel, |
michael@0 | 88 | uint32_t aFlags, |
michael@0 | 89 | nsIAsyncVerifyRedirectCallback* cb) |
michael@0 | 90 | { |
michael@0 | 91 | if (mElement) { |
michael@0 | 92 | mElement->OnChannelRedirect(aOldChannel, aNewChannel, aFlags); |
michael@0 | 93 | } |
michael@0 | 94 | return NS_OK; |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | NS_IMETHODIMP |
michael@0 | 98 | WebVTTListener::OnStartRequest(nsIRequest* aRequest, |
michael@0 | 99 | nsISupports* aContext) |
michael@0 | 100 | { |
michael@0 | 101 | return NS_OK; |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | NS_IMETHODIMP |
michael@0 | 105 | WebVTTListener::OnStopRequest(nsIRequest* aRequest, |
michael@0 | 106 | nsISupports* aContext, |
michael@0 | 107 | nsresult aStatus) |
michael@0 | 108 | { |
michael@0 | 109 | if (NS_FAILED(aStatus)) { |
michael@0 | 110 | mElement->SetReadyState(TextTrackReadyState::FailedToLoad); |
michael@0 | 111 | } |
michael@0 | 112 | // Attempt to parse any final data the parser might still have. |
michael@0 | 113 | mParserWrapper->Flush(); |
michael@0 | 114 | if (mElement->ReadyState() != TextTrackReadyState::FailedToLoad) { |
michael@0 | 115 | mElement->SetReadyState(TextTrackReadyState::Loaded); |
michael@0 | 116 | } |
michael@0 | 117 | return aStatus; |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | NS_METHOD |
michael@0 | 121 | WebVTTListener::ParseChunk(nsIInputStream* aInStream, void* aClosure, |
michael@0 | 122 | const char* aFromSegment, uint32_t aToOffset, |
michael@0 | 123 | uint32_t aCount, uint32_t* aWriteCount) |
michael@0 | 124 | { |
michael@0 | 125 | nsCString buffer(aFromSegment, aCount); |
michael@0 | 126 | WebVTTListener* listener = static_cast<WebVTTListener*>(aClosure); |
michael@0 | 127 | |
michael@0 | 128 | if (NS_FAILED(listener->mParserWrapper->Parse(buffer))) { |
michael@0 | 129 | VTT_LOG("Unable to parse chunk of WEBVTT text. Aborting."); |
michael@0 | 130 | *aWriteCount = 0; |
michael@0 | 131 | return NS_ERROR_FAILURE; |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | *aWriteCount = aCount; |
michael@0 | 135 | return NS_OK; |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | NS_IMETHODIMP |
michael@0 | 139 | WebVTTListener::OnDataAvailable(nsIRequest* aRequest, |
michael@0 | 140 | nsISupports* aContext, |
michael@0 | 141 | nsIInputStream* aStream, |
michael@0 | 142 | uint64_t aOffset, |
michael@0 | 143 | uint32_t aCount) |
michael@0 | 144 | { |
michael@0 | 145 | uint32_t count = aCount; |
michael@0 | 146 | while (count > 0) { |
michael@0 | 147 | uint32_t read; |
michael@0 | 148 | nsresult rv = aStream->ReadSegments(ParseChunk, this, count, &read); |
michael@0 | 149 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 150 | if (!read) { |
michael@0 | 151 | return NS_ERROR_FAILURE; |
michael@0 | 152 | } |
michael@0 | 153 | count -= read; |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | return NS_OK; |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | NS_IMETHODIMP |
michael@0 | 160 | WebVTTListener::OnCue(JS::Handle<JS::Value> aCue, JSContext* aCx) |
michael@0 | 161 | { |
michael@0 | 162 | if (!aCue.isObject()) { |
michael@0 | 163 | return NS_ERROR_FAILURE; |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | TextTrackCue* cue; |
michael@0 | 167 | nsresult rv = UNWRAP_OBJECT(VTTCue, &aCue.toObject(), cue); |
michael@0 | 168 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 169 | |
michael@0 | 170 | cue->SetTrackElement(mElement); |
michael@0 | 171 | mElement->mTrack->AddCue(*cue); |
michael@0 | 172 | |
michael@0 | 173 | return NS_OK; |
michael@0 | 174 | } |
michael@0 | 175 | |
michael@0 | 176 | |
michael@0 | 177 | NS_IMETHODIMP |
michael@0 | 178 | WebVTTListener::OnRegion(JS::Handle<JS::Value> aRegion, JSContext* aCx) |
michael@0 | 179 | { |
michael@0 | 180 | // Nothing for this callback to do. |
michael@0 | 181 | return NS_OK; |
michael@0 | 182 | } |
michael@0 | 183 | |
michael@0 | 184 | NS_IMETHODIMP |
michael@0 | 185 | WebVTTListener::OnParsingError(int32_t errorCode, JSContext* cx) |
michael@0 | 186 | { |
michael@0 | 187 | // We only care about files that have a bad WebVTT file signature right now |
michael@0 | 188 | // as that means the file failed to load. |
michael@0 | 189 | if (errorCode == ErrorCodes::BadSignature) { |
michael@0 | 190 | mElement->SetReadyState(TextTrackReadyState::FailedToLoad); |
michael@0 | 191 | } |
michael@0 | 192 | return NS_OK; |
michael@0 | 193 | } |
michael@0 | 194 | |
michael@0 | 195 | } // namespace dom |
michael@0 | 196 | } // namespace mozilla |