summaryrefslogtreecommitdiffstats
path: root/dom/streams/TransformerCallbackHelpers.cpp
blob: 606c24adda7c36f2b29b8b78ae7b843bec62569f (plain)
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
/* -*- 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 "TransformerCallbackHelpers.h"

#include "StreamUtils.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/TransformStreamDefaultController.h"

using namespace mozilla::dom;

NS_IMPL_CYCLE_COLLECTION(TransformerAlgorithmsBase)
NS_IMPL_CYCLE_COLLECTING_ADDREF(TransformerAlgorithmsBase)
NS_IMPL_CYCLE_COLLECTING_RELEASE(TransformerAlgorithmsBase)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TransformerAlgorithmsBase)
  NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END

NS_IMPL_CYCLE_COLLECTION_INHERITED_WITH_JS_MEMBERS(
    TransformerAlgorithms, TransformerAlgorithmsBase,
    (mGlobal, mTransformCallback, mFlushCallback), (mTransformer))
NS_IMPL_ADDREF_INHERITED(TransformerAlgorithms, TransformerAlgorithmsBase)
NS_IMPL_RELEASE_INHERITED(TransformerAlgorithms, TransformerAlgorithmsBase)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TransformerAlgorithms)
NS_INTERFACE_MAP_END_INHERITING(TransformerAlgorithmsBase)

// https://streams.spec.whatwg.org/#set-up-transform-stream-default-controller-from-transformer
already_AddRefed<Promise> TransformerAlgorithms::TransformCallback(
    JSContext* aCx, JS::Handle<JS::Value> aChunk,
    TransformStreamDefaultController& aController, ErrorResult& aRv) {
  if (!mTransformCallback) {
    // Step 2.1. Let result be
    // TransformStreamDefaultControllerEnqueue(controller, chunk).
    aController.Enqueue(aCx, aChunk, aRv);

    // Step 2.2. If result is an abrupt completion, return a promise rejected
    // with result.[[Value]].
    if (aRv.MaybeSetPendingException(aCx)) {
      JS::Rooted<JS::Value> error(aCx);
      if (!JS_GetPendingException(aCx, &error)) {
        // Uncatchable exception; we should mark aRv and return.
        aRv.StealExceptionFromJSContext(aCx);
        return nullptr;
      }
      JS_ClearPendingException(aCx);

      return Promise::CreateRejected(aController.GetParentObject(), error, aRv);
    }

    // Step 2.3. Otherwise, return a promise resolved with undefined.
    return Promise::CreateResolvedWithUndefined(aController.GetParentObject(),
                                                aRv);
  }
  // Step 4. If transformerDict["transform"] exists, set transformAlgorithm to
  // an algorithm which takes an argument chunk and returns the result of
  // invoking transformerDict["transform"] with argument list « chunk,
  // controller » and callback this value transformer.
  JS::Rooted<JSObject*> thisObj(aCx, mTransformer);
  return MOZ_KnownLive(mTransformCallback)
      ->Call(thisObj, aChunk, aController, aRv,
             "TransformStreamDefaultController.[[transformAlgorithm]]",
             CallbackObject::eRethrowExceptions);
}

// https://streams.spec.whatwg.org/#set-up-transform-stream-default-controller-from-transformer
already_AddRefed<Promise> TransformerAlgorithms::FlushCallback(
    JSContext* aCx, TransformStreamDefaultController& aController,
    ErrorResult& aRv) {
  if (!mFlushCallback) {
    // Step 3. Let flushAlgorithm be an algorithm which returns a promise
    // resolved with undefined.
    return Promise::CreateResolvedWithUndefined(aController.GetParentObject(),
                                                aRv);
  }
  // Step 5. If transformerDict["flush"] exists, set flushAlgorithm to an
  // algorithm which returns the result of invoking transformerDict["flush"]
  // with argument list « controller » and callback this value transformer.
  JS::Rooted<JSObject*> thisObj(aCx, mTransformer);
  return MOZ_KnownLive(mFlushCallback)
      ->Call(thisObj, aController, aRv,
             "TransformStreamDefaultController.[[flushAlgorithm]]",
             CallbackObject::eRethrowExceptions);
}

already_AddRefed<Promise> TransformerAlgorithmsWrapper::TransformCallback(
    JSContext*, JS::Handle<JS::Value> aChunk,
    TransformStreamDefaultController& aController, ErrorResult& aRv) {
  nsCOMPtr<nsIGlobalObject> global = aController.GetParentObject();
  return PromisifyAlgorithm(
      global,
      [this, &aChunk, &aController](ErrorResult& aRv)
          MOZ_CAN_RUN_SCRIPT_FOR_DEFINITION {
            return TransformCallbackImpl(aChunk, aController, aRv);
          },
      aRv);
}

already_AddRefed<Promise> TransformerAlgorithmsWrapper::FlushCallback(
    JSContext*, TransformStreamDefaultController& aController,
    ErrorResult& aRv) {
  nsCOMPtr<nsIGlobalObject> global = aController.GetParentObject();
  return PromisifyAlgorithm(
      global,
      [this, &aController](ErrorResult& aRv) MOZ_CAN_RUN_SCRIPT_FOR_DEFINITION {
        return FlushCallbackImpl(aController, aRv);
      },
      aRv);
}