1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* 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/. */
#include "ImageDecoderReadRequest.h"
#include "MediaResult.h"
#include "mozilla/CycleCollectedJSContext.h"
#include "mozilla/Logging.h"
#include "mozilla/dom/ImageDecoder.h"
#include "mozilla/dom/ReadableStream.h"
#include "mozilla/dom/ReadableStreamDefaultReader.h"
#include "mozilla/image/SourceBuffer.h"
extern mozilla::LazyLogModule gWebCodecsLog;
namespace mozilla::dom {
NS_IMPL_CYCLE_COLLECTION_INHERITED(ImageDecoderReadRequest, ReadRequest,
mDecoder, mReader)
NS_IMPL_ADDREF_INHERITED(ImageDecoderReadRequest, ReadRequest)
NS_IMPL_RELEASE_INHERITED(ImageDecoderReadRequest, ReadRequest)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ImageDecoderReadRequest)
NS_INTERFACE_MAP_END_INHERITING(ReadRequest)
ImageDecoderReadRequest::ImageDecoderReadRequest(
image::SourceBuffer* aSourceBuffer)
: mSourceBuffer(std::move(aSourceBuffer)) {
MOZ_LOG(gWebCodecsLog, LogLevel::Debug,
("ImageDecoderReadRequest %p ImageDecoderReadRequest", this));
}
ImageDecoderReadRequest::~ImageDecoderReadRequest() {
MOZ_LOG(gWebCodecsLog, LogLevel::Debug,
("ImageDecoderReadRequest %p ~ImageDecoderReadRequest", this));
}
bool ImageDecoderReadRequest::Initialize(const GlobalObject& aGlobal,
ImageDecoder* aDecoder,
ReadableStream& aStream) {
IgnoredErrorResult rv;
mReader = aStream.GetReader(rv);
if (NS_WARN_IF(rv.Failed())) {
MOZ_LOG(
gWebCodecsLog, LogLevel::Error,
("ImageDecoderReadRequest %p Initialize -- cannot get stream reader",
this));
mSourceBuffer->Complete(NS_ERROR_FAILURE);
Destroy(/* aCancel */ false);
return false;
}
mDecoder = aDecoder;
QueueRead();
return true;
}
void ImageDecoderReadRequest::Destroy(bool aCancel) {
MOZ_LOG(gWebCodecsLog, LogLevel::Debug,
("ImageDecoderReadRequest %p Destroy", this));
if (aCancel) {
// Ensure we stop reading from the ReadableStream.
Cancel();
}
if (mSourceBuffer) {
if (!mSourceBuffer->IsComplete()) {
mSourceBuffer->Complete(NS_ERROR_ABORT);
}
mSourceBuffer = nullptr;
}
mDecoder = nullptr;
mReader = nullptr;
}
void ImageDecoderReadRequest::QueueRead() {
class ReadRunnable final : public CancelableRunnable {
public:
explicit ReadRunnable(ImageDecoderReadRequest* aOwner)
: CancelableRunnable(
"mozilla::dom::ImageDecoderReadRequest::QueueRead"),
mOwner(aOwner) {}
NS_IMETHODIMP Run() override {
mOwner->Read();
mOwner = nullptr;
return NS_OK;
}
nsresult Cancel() override {
mOwner->Complete(
MediaResult(NS_ERROR_DOM_MEDIA_ABORT_ERR, "Read cancelled"_ns));
mOwner = nullptr;
return NS_OK;
}
private:
virtual ~ReadRunnable() {
if (mOwner) {
Cancel();
}
}
RefPtr<ImageDecoderReadRequest> mOwner;
};
if (!mReader) {
MOZ_LOG(gWebCodecsLog, LogLevel::Debug,
("ImageDecoderReadRequest %p QueueRead -- destroyed", this));
return;
}
MOZ_LOG(gWebCodecsLog, LogLevel::Debug,
("ImageDecoderReadRequest %p QueueRead -- queue", this));
auto task = MakeRefPtr<ReadRunnable>(this);
NS_DispatchToCurrentThread(task.forget());
}
void ImageDecoderReadRequest::Read() {
if (!mReader || !mDecoder) {
MOZ_LOG(gWebCodecsLog, LogLevel::Debug,
("ImageDecoderReadRequest %p Read -- destroyed", this));
return;
}
AutoJSAPI jsapi;
if (!jsapi.Init(mDecoder->GetParentObject())) {
MOZ_LOG(gWebCodecsLog, LogLevel::Debug,
("ImageDecoderReadRequest %p Read -- no jsapi", this));
Complete(MediaResult(NS_ERROR_DOM_FILE_NOT_READABLE_ERR,
"Reader cannot init jsapi"_ns));
return;
}
RefPtr<ImageDecoderReadRequest> self(this);
RefPtr<ReadableStreamDefaultReader> reader(mReader);
MOZ_LOG(gWebCodecsLog, LogLevel::Debug,
("ImageDecoderReadRequest %p Read -- begin read chunk", this));
IgnoredErrorResult err;
reader->ReadChunk(jsapi.cx(), *self, err);
if (NS_WARN_IF(err.Failed())) {
MOZ_LOG(gWebCodecsLog, LogLevel::Error,
("ImageDecoderReadRequest %p Read -- read chunk failed", this));
Complete(MediaResult(NS_ERROR_DOM_FILE_NOT_READABLE_ERR,
"Reader cannot read chunk from stream"_ns));
}
MOZ_LOG(gWebCodecsLog, LogLevel::Debug,
("ImageDecoderReadRequest %p Read -- end read chunk", this));
}
void ImageDecoderReadRequest::Cancel() {
RefPtr<ReadableStreamDefaultReader> reader = std::move(mReader);
if (!reader || !mDecoder) {
return;
}
RefPtr<ImageDecoderReadRequest> self(this);
AutoJSAPI jsapi;
if (!jsapi.Init(mDecoder->GetParentObject())) {
MOZ_LOG(gWebCodecsLog, LogLevel::Debug,
("ImageDecoderReadRequest %p Cancel -- no jsapi", this));
return;
}
ErrorResult rv;
rv.ThrowAbortError("ImageDecoderReadRequest destroyed");
JS::Rooted<JS::Value> errorValue(jsapi.cx());
if (ToJSValue(jsapi.cx(), std::move(rv), &errorValue)) {
IgnoredErrorResult ignoredRv;
if (RefPtr<Promise> p = reader->Cancel(jsapi.cx(), errorValue, ignoredRv)) {
MOZ_ALWAYS_TRUE(p->SetAnyPromiseIsHandled());
}
}
jsapi.ClearException();
}
void ImageDecoderReadRequest::Complete(const MediaResult& aResult) {
if (!mReader) {
return;
}
MOZ_LOG(gWebCodecsLog, LogLevel::Debug,
("ImageDecoderReadRequest %p Read -- complete, success %d", this,
NS_SUCCEEDED(aResult.Code())));
if (mSourceBuffer && !mSourceBuffer->IsComplete()) {
mSourceBuffer->Complete(aResult.Code());
}
if (mDecoder) {
mDecoder->OnSourceBufferComplete(aResult);
}
Destroy(/* aCancel */ false);
}
void ImageDecoderReadRequest::ChunkSteps(JSContext* aCx,
JS::Handle<JS::Value> aChunk,
ErrorResult& aRv) {
// 10.2.5. Fetch Stream Data Loop (with reader) - chunk steps
// 1. If [[closed]] is true, abort these steps.
if (!mSourceBuffer) {
return;
}
// 2. If chunk is not a Uint8Array object, queue a task to run the Close
// ImageDecoder algorithm with a DataError DOMException and abort these steps.
RootedSpiderMonkeyInterface<Uint8Array> chunk(aCx);
if (!aChunk.isObject() || !chunk.Init(&aChunk.toObject())) {
MOZ_LOG(gWebCodecsLog, LogLevel::Error,
("ImageDecoderReadRequest %p ChunkSteps -- bad chunk", this));
Complete(MediaResult(NS_ERROR_DOM_DATA_ERR,
"Reader cannot read chunk from stream"_ns));
return;
}
chunk.ProcessFixedData([&](const Span<uint8_t>& aData) {
MOZ_LOG(gWebCodecsLog, LogLevel::Debug,
("ImageDecoderReadRequest %p ChunkSteps -- write %zu bytes", this,
aData.Length()));
// 3. Let bytes be the byte sequence represented by the Uint8Array object.
// 4. Append bytes to the [[encoded data]] internal slot.
nsresult rv = mSourceBuffer->Append(
reinterpret_cast<const char*>(aData.Elements()), aData.Length());
if (NS_WARN_IF(NS_FAILED(rv))) {
MOZ_LOG(
gWebCodecsLog, LogLevel::Debug,
("ImageDecoderReadRequest %p ChunkSteps -- failed to append", this));
Complete(MediaResult(NS_ERROR_DOM_UNKNOWN_ERR,
"Reader cannot allocate storage for chunk"_ns));
}
// 5. If [[tracks established]] is false, run the Establish Tracks
// algorithm.
// 6. Otherwise, run the Update Tracks algorithm.
//
// Note that these steps will be triggered by the decoder promise callbacks.
});
// 7. Run the Fetch Stream Data Loop algorithm with reader.
QueueRead();
}
void ImageDecoderReadRequest::CloseSteps(JSContext* aCx, ErrorResult& aRv) {
MOZ_LOG(gWebCodecsLog, LogLevel::Debug,
("ImageDecoderReadRequest %p CloseSteps", this));
// 10.2.5. Fetch Stream Data Loop (with reader) - close steps
// 1. Assign true to [[complete]]
// 2. Resolve [[completed promise]].
Complete(MediaResult(NS_OK));
}
void ImageDecoderReadRequest::ErrorSteps(JSContext* aCx,
JS::Handle<JS::Value> aError,
ErrorResult& aRv) {
MOZ_LOG(gWebCodecsLog, LogLevel::Debug,
("ImageDecoderReadRequest %p ErrorSteps", this));
// 10.2.5. Fetch Stream Data Loop (with reader) - error steps
// 1. Queue a task to run the Close ImageDecoder algorithm with a
// NotReadableError DOMException
Complete(MediaResult(NS_ERROR_DOM_FILE_NOT_READABLE_ERR,
"Reader failed while waiting for chunk from stream"_ns));
}
} // namespace mozilla::dom
|