tor-browser

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

CountQueuingStrategy.cpp (3861B)


      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/CountQueuingStrategy.h"
      8 
      9 #include "mozilla/dom/FunctionBinding.h"
     10 #include "mozilla/dom/QueuingStrategyBinding.h"
     11 #include "nsCOMPtr.h"
     12 #include "nsISupports.h"
     13 
     14 namespace mozilla::dom {
     15 
     16 NS_IMPL_CYCLE_COLLECTION(BaseQueuingStrategy, mGlobal)
     17 NS_IMPL_CYCLE_COLLECTING_ADDREF(BaseQueuingStrategy)
     18 NS_IMPL_CYCLE_COLLECTING_RELEASE(BaseQueuingStrategy)
     19 
     20 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(BaseQueuingStrategy)
     21  NS_INTERFACE_MAP_ENTRY(nsISupports)
     22 NS_INTERFACE_MAP_END
     23 
     24 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_INHERITED(CountQueuingStrategy,
     25                                                BaseQueuingStrategy)
     26 NS_IMPL_ADDREF_INHERITED(CountQueuingStrategy, BaseQueuingStrategy)
     27 NS_IMPL_RELEASE_INHERITED(CountQueuingStrategy, BaseQueuingStrategy)
     28 
     29 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CountQueuingStrategy)
     30  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
     31 NS_INTERFACE_MAP_END_INHERITING(BaseQueuingStrategy)
     32 
     33 /* static */
     34 already_AddRefed<CountQueuingStrategy> CountQueuingStrategy::Constructor(
     35    const GlobalObject& aGlobal, const QueuingStrategyInit& aInit) {
     36  RefPtr<CountQueuingStrategy> strategy =
     37      new CountQueuingStrategy(aGlobal.GetAsSupports(), aInit.mHighWaterMark);
     38  return strategy.forget();
     39 }
     40 
     41 nsIGlobalObject* BaseQueuingStrategy::GetParentObject() const {
     42  return mGlobal;
     43 }
     44 
     45 JSObject* CountQueuingStrategy::WrapObject(JSContext* aCx,
     46                                           JS::Handle<JSObject*> aGivenProto) {
     47  return CountQueuingStrategy_Binding::Wrap(aCx, this, aGivenProto);
     48 }
     49 
     50 // https://streams.spec.whatwg.org/#count-queuing-strategy-size-function
     51 static bool CountQueuingStrategySize(JSContext* aCx, unsigned aArgc,
     52                                     JS::Value* aVp) {
     53  JS::CallArgs args = CallArgsFromVp(aArgc, aVp);
     54 
     55  // Step 1.1. Return 1.
     56  args.rval().setInt32(1);
     57  return true;
     58 }
     59 
     60 // https://streams.spec.whatwg.org/#cqs-size
     61 already_AddRefed<Function> CountQueuingStrategy::GetSize(ErrorResult& aRv) {
     62  // Step 1. Return this's relevant global object's count queuing strategy
     63  // size function.
     64  if (RefPtr<Function> fun = mGlobal->GetCountQueuingStrategySizeFunction()) {
     65    return fun.forget();
     66  }
     67 
     68  // Note: Instead of eagerly allocating a size function for every global object
     69  // we do it lazily once in this getter.
     70  // After this point the steps refer to:
     71  // https://streams.spec.whatwg.org/#count-queuing-strategy-size-function.
     72 
     73  AutoJSAPI jsapi;
     74  if (!jsapi.Init(mGlobal)) {
     75    aRv.ThrowUnknownError("Internal error");
     76    return nullptr;
     77  }
     78  JSContext* cx = jsapi.cx();
     79 
     80  // Step 1. Let steps be the following steps:
     81  // Note: See CountQueuingStrategySize instead.
     82 
     83  // Step 2. Let F be
     84  // ! CreateBuiltinFunction(steps, 0, "size", « »,
     85  //                         globalObject’s relevant Realm).
     86  JS::Rooted<JSFunction*> sizeFunction(
     87      cx, JS_NewFunction(cx, CountQueuingStrategySize, 0, 0, "size"));
     88  if (!sizeFunction) {
     89    aRv.StealExceptionFromJSContext(cx);
     90    return nullptr;
     91  }
     92 
     93  // Step 3. Set globalObject’s count queuing strategy size function to
     94  // a Function that represents a reference to F,
     95  // with callback context equal to globalObject’s relevant settings object.
     96  JS::Rooted<JSObject*> funObj(cx, JS_GetFunctionObject(sizeFunction));
     97  JS::Rooted<JSObject*> global(cx, mGlobal->GetGlobalJSObject());
     98  RefPtr<Function> function = new Function(cx, funObj, global, mGlobal);
     99  mGlobal->SetCountQueuingStrategySizeFunction(function);
    100 
    101  return function.forget();
    102 }
    103 
    104 }  // namespace mozilla::dom