tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

ByteLengthQueuingStrategy.cpp (3793B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "mozilla/dom/ByteLengthQueuingStrategy.h"
      8 
      9 #include "js/PropertyAndElement.h"
     10 #include "js/TypeDecls.h"
     11 #include "mozilla/dom/FunctionBinding.h"
     12 #include "mozilla/dom/QueuingStrategyBinding.h"
     13 #include "nsCOMPtr.h"
     14 #include "nsISupports.h"
     15 
     16 namespace mozilla::dom {
     17 
     18 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_INHERITED(ByteLengthQueuingStrategy,
     19                                                BaseQueuingStrategy)
     20 NS_IMPL_ADDREF_INHERITED(ByteLengthQueuingStrategy, BaseQueuingStrategy)
     21 NS_IMPL_RELEASE_INHERITED(ByteLengthQueuingStrategy, BaseQueuingStrategy)
     22 
     23 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ByteLengthQueuingStrategy)
     24  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
     25 NS_INTERFACE_MAP_END_INHERITING(BaseQueuingStrategy)
     26 
     27 /* static */
     28 already_AddRefed<ByteLengthQueuingStrategy>
     29 ByteLengthQueuingStrategy::Constructor(const GlobalObject& aGlobal,
     30                                       const QueuingStrategyInit& aInit) {
     31  RefPtr<ByteLengthQueuingStrategy> strategy = new ByteLengthQueuingStrategy(
     32      aGlobal.GetAsSupports(), aInit.mHighWaterMark);
     33  return strategy.forget();
     34 }
     35 
     36 JSObject* ByteLengthQueuingStrategy::WrapObject(
     37    JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
     38  return ByteLengthQueuingStrategy_Binding::Wrap(aCx, this, aGivenProto);
     39 }
     40 
     41 static bool ByteLengthQueuingStrategySize(JSContext* cx, unsigned argc,
     42                                          JS::Value* vp) {
     43  // https://streams.spec.whatwg.org/#blqs-internal-slots
     44  JS::CallArgs args = CallArgsFromVp(argc, vp);
     45 
     46  // Step 1: Return ? GetV(chunk, "byteLength").
     47  JS::Rooted<JSObject*> chunkObj(cx, JS::ToObject(cx, args.get(0)));
     48  if (!chunkObj) {
     49    return false;
     50  }
     51 
     52  return JS_GetProperty(cx, chunkObj, "byteLength", args.rval());
     53 }
     54 
     55 // https://streams.spec.whatwg.org/#blqs-size
     56 already_AddRefed<Function> ByteLengthQueuingStrategy::GetSize(
     57    ErrorResult& aRv) {
     58  // Step 1. Return this's relevant global object's ByteLength queuing strategy
     59  // size function.
     60  if (RefPtr<Function> fun =
     61          mGlobal->GetByteLengthQueuingStrategySizeFunction()) {
     62    return fun.forget();
     63  }
     64 
     65  // Note: Instead of eagerly allocating a size function for every global object
     66  // we do it lazily once in this getter.
     67  // After this point the steps refer to:
     68  // https://streams.spec.whatwg.org/#byte-length-queuing-strategy-size-function
     69 
     70  AutoJSAPI jsapi;
     71  if (!jsapi.Init(mGlobal)) {
     72    aRv.ThrowUnknownError("Internal error");
     73    return nullptr;
     74  }
     75  JSContext* cx = jsapi.cx();
     76 
     77  // Step 1. Let steps be the following steps, given chunk
     78  // Note: See ByteLengthQueuingStrategySize instead.
     79 
     80  // Step 2. Let F be !CreateBuiltinFunction(steps, 1, "size", « »,
     81  // globalObject’s relevant Realm).
     82  JS::Rooted<JSFunction*> sizeFunction(
     83      cx, JS_NewFunction(cx, ByteLengthQueuingStrategySize, 1, 0, "size"));
     84  if (!sizeFunction) {
     85    aRv.StealExceptionFromJSContext(cx);
     86    return nullptr;
     87  }
     88 
     89  // Step 3. Set globalObject’s byte length queuing strategy size function to
     90  // a Function that represents a reference to F,
     91  // with callback context equal to globalObject’s relevant settings object.
     92  JS::Rooted<JSObject*> funObj(cx, JS_GetFunctionObject(sizeFunction));
     93  JS::Rooted<JSObject*> global(cx, mGlobal->GetGlobalJSObject());
     94  RefPtr<Function> function = new Function(cx, funObj, global, mGlobal);
     95  mGlobal->SetByteLengthQueuingStrategySizeFunction(function);
     96 
     97  return function.forget();
     98 }
     99 
    100 }  // namespace mozilla::dom