Skip to content

Commit

Permalink
Bug 1734239 - Implement the CountQueuingStrategy size function. r=smaug
Browse files Browse the repository at this point in the history
  • Loading branch information
evilpie committed Jan 5, 2022
1 parent 60c2444 commit 224a88a
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 5 deletions.
18 changes: 18 additions & 0 deletions dom/base/nsIGlobalObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ void nsIGlobalObject::UnlinkObjectsInGlobal() {

mReportRecords.Clear();
mReportingObservers.Clear();
#ifdef MOZ_DOM_STREAMS
mCountQueuingStrategySizeFunction = nullptr;
#endif
}

void nsIGlobalObject::TraverseObjectsInGlobal(
Expand All @@ -149,6 +152,9 @@ void nsIGlobalObject::TraverseObjectsInGlobal(
nsIGlobalObject* tmp = this;
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mReportRecords)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mReportingObservers)
#ifdef MOZ_DOM_STREAMS
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mCountQueuingStrategySizeFunction)
#endif
}

void nsIGlobalObject::AddEventTargetObject(DOMEventTargetHelper* aObject) {
Expand Down Expand Up @@ -349,6 +355,18 @@ void nsIGlobalObject::RemoveReportRecords() {
}
}

#ifdef MOZ_DOM_STREAMS
already_AddRefed<mozilla::dom::Function>
nsIGlobalObject::GetCountQueuingStrategySizeFunction() {
return do_AddRef(mCountQueuingStrategySizeFunction);
}

void nsIGlobalObject::SetCountQueuingStrategySizeFunction(
mozilla::dom::Function* aFunction) {
mCountQueuingStrategySizeFunction = aFunction;
}
#endif

bool nsIGlobalObject::ShouldResistFingerprinting() const {
return nsContentUtils::ShouldResistFingerprinting();
}
14 changes: 14 additions & 0 deletions dom/base/nsIGlobalObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ enum class StorageAccess;
namespace dom {
class VoidFunction;
class DebuggerNotificationManager;
class Function;
class Report;
class ReportBody;
class ReportingObserver;
Expand Down Expand Up @@ -212,6 +213,14 @@ class nsIGlobalObject : public nsISupports,

void RemoveReportRecords();

#ifdef MOZ_DOM_STREAMS
// https://streams.spec.whatwg.org/#count-queuing-strategy-size-function
// This function is set once by CountQueuingStrategy::GetSize.
already_AddRefed<mozilla::dom::Function>
GetCountQueuingStrategySizeFunction();
void SetCountQueuingStrategySizeFunction(mozilla::dom::Function* aFunction);
#endif

/**
* Check whether we should avoid leaking distinguishing information to JS/CSS.
* https://w3c.github.io/fingerprinting-guidance/
Expand Down Expand Up @@ -240,6 +249,11 @@ class nsIGlobalObject : public nsISupports,
// List of Report objects for ReportingObservers.
nsTArray<RefPtr<mozilla::dom::ReportingObserver>> mReportingObservers;
nsTArray<RefPtr<mozilla::dom::Report>> mReportRecords;

#ifdef MOZ_DOM_STREAMS
// https://streams.spec.whatwg.org/#count-queuing-strategy-size-function
RefPtr<mozilla::dom::Function> mCountQueuingStrategySizeFunction;
#endif
};

NS_DEFINE_STATIC_IID_ACCESSOR(nsIGlobalObject, NS_IGLOBALOBJECT_IID)
Expand Down
55 changes: 55 additions & 0 deletions dom/streams/CountQueuingStrategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/dom/CountQueuingStrategy.h"
#include "mozilla/dom/FunctionBinding.h"
#include "mozilla/dom/QueuingStrategyBinding.h"
#include "nsCOMPtr.h"

Expand Down Expand Up @@ -37,5 +38,59 @@ JSObject* CountQueuingStrategy::WrapObject(JSContext* aCx,
return CountQueuingStrategy_Binding::Wrap(aCx, this, aGivenProto);
}

// https://streams.spec.whatwg.org/#count-queuing-strategy-size-function
static bool CountQueuingStrategySize(JSContext* aCx, unsigned aArgc,
JS::Value* aVp) {
JS::CallArgs args = CallArgsFromVp(aArgc, aVp);

// Step 1.1. Return 1.
args.rval().setInt32(1);
return true;
}

// https://streams.spec.whatwg.org/#cqs-size
already_AddRefed<Function> CountQueuingStrategy::GetSize(ErrorResult& aRv) {
// Step 1. Return this's relevant global object's count queuing strategy
// size function.
if (RefPtr<Function> fun = mGlobal->GetCountQueuingStrategySizeFunction()) {
return fun.forget();
}

// Note: Instead of eagerly allocating a size function for every global object
// we do it lazily once in this getter.
// After this point the steps refer to:
// https://streams.spec.whatwg.org/#count-queuing-strategy-size-function.

AutoJSAPI jsapi;
if (!jsapi.Init(mGlobal)) {
aRv.ThrowUnknownError("Internal error");
return nullptr;
}
JSContext* cx = jsapi.cx();

// Step 1. Let steps be the following steps:
// Note: See CountQueuingStrategySize instead.

// Step 2. Let F be
// ! CreateBuiltinFunction(steps, 0, "size", « »,
// globalObject’s relevant Realm).
JS::Rooted<JSFunction*> sizeFunction(
cx, JS_NewFunction(cx, CountQueuingStrategySize, 0, 0, "size"));
if (!sizeFunction) {
aRv.StealExceptionFromJSContext(cx);
return nullptr;
}

// Step 3. Set globalObject’s count queuing strategy size function to
// a Function that represents a reference to F,
// with callback context equal to globalObject’s relevant settings object.
JS::Rooted<JSObject*> funObj(cx, JS_GetFunctionObject(sizeFunction));
JS::Rooted<JSObject*> global(cx, mGlobal->GetGlobalJSObject());
RefPtr<Function> function = new Function(cx, funObj, global, mGlobal);
mGlobal->SetCountQueuingStrategySizeFunction(function);

return function.forget();
}

} // namespace dom
} // namespace mozilla
2 changes: 1 addition & 1 deletion dom/streams/CountQueuingStrategy.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class CountQueuingStrategy final : public nsISupports, public nsWrapperCache {

double HighWaterMark() const { return mHighWaterMark; }

already_AddRefed<Function> Size() const;
already_AddRefed<Function> GetSize(ErrorResult& aRv);

private:
double mHighWaterMark = 0.0;
Expand Down
6 changes: 2 additions & 4 deletions dom/webidl/QueuingStrategy.webidl
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ interface CountQueuingStrategy {

readonly attribute unrestricted double highWaterMark;

// This is currently inlined, but will need to be implemented
// See Bug 1734239
//
// readonly attribute Function size;
[Throws]
readonly attribute Function size;
};

0 comments on commit 224a88a

Please sign in to comment.