Bug 1736177 - Part 1. Rename CanvasRenderingContextHelper::GetContext to GetOrCreateContext. r=jgilbert

This patch contains no functional changes but sets us up for refactoring
in a later part in this series. It also helps silence a bunch of
compiler warnings.

Differential Revision: https://phabricator.services.mozilla.com/D130777
This commit is contained in:
Andrew Osmond
2021-12-10 02:57:51 +00:00
parent 6939bc180b
commit 86d592cb56
6 changed files with 33 additions and 23 deletions

View File

@@ -160,17 +160,23 @@ CanvasRenderingContextHelper::CreateContextHelper(
return ret.forget();
}
already_AddRefed<nsISupports> CanvasRenderingContextHelper::GetContext(
already_AddRefed<nsISupports> CanvasRenderingContextHelper::GetOrCreateContext(
JSContext* aCx, const nsAString& aContextId,
JS::Handle<JS::Value> aContextOptions, ErrorResult& aRv) {
CanvasContextType contextType;
if (!CanvasUtils::GetCanvasContextType(aContextId, &contextType))
return nullptr;
return GetOrCreateContext(aCx, contextType, aContextOptions, aRv);
}
already_AddRefed<nsISupports> CanvasRenderingContextHelper::GetOrCreateContext(
JSContext* aCx, CanvasContextType aContextType,
JS::Handle<JS::Value> aContextOptions, ErrorResult& aRv) {
if (!mCurrentContext) {
// This canvas doesn't have a context yet.
RefPtr<nsICanvasRenderingContextInternal> context;
context = CreateContext(contextType);
context = CreateContext(aContextType);
if (!context) {
return nullptr;
}
@@ -185,32 +191,32 @@ already_AddRefed<nsISupports> CanvasRenderingContextHelper::GetContext(
}
mCurrentContext = std::move(context);
mCurrentContextType = contextType;
mCurrentContextType = aContextType;
nsresult rv = UpdateContext(aCx, aContextOptions, aRv);
if (NS_FAILED(rv)) {
// See bug 645792 and bug 1215072.
// We want to throw only if dictionary initialization fails,
// so only in case aRv has been set to some error value.
if (contextType == CanvasContextType::WebGL1)
if (aContextType == CanvasContextType::WebGL1) {
Telemetry::Accumulate(Telemetry::CANVAS_WEBGL_SUCCESS, 0);
else if (contextType == CanvasContextType::WebGL2)
} else if (aContextType == CanvasContextType::WebGL2) {
Telemetry::Accumulate(Telemetry::CANVAS_WEBGL2_SUCCESS, 0);
else if (contextType == CanvasContextType::WebGPU) {
} else if (aContextType == CanvasContextType::WebGPU) {
// Telemetry::Accumulate(Telemetry::CANVAS_WEBGPU_SUCCESS, 0);
}
return nullptr;
}
if (contextType == CanvasContextType::WebGL1)
if (aContextType == CanvasContextType::WebGL1) {
Telemetry::Accumulate(Telemetry::CANVAS_WEBGL_SUCCESS, 1);
else if (contextType == CanvasContextType::WebGL2)
} else if (aContextType == CanvasContextType::WebGL2) {
Telemetry::Accumulate(Telemetry::CANVAS_WEBGL2_SUCCESS, 1);
else if (contextType == CanvasContextType::WebGPU) {
} else if (aContextType == CanvasContextType::WebGPU) {
// Telemetry::Accumulate(Telemetry::CANVAS_WEBGPU_SUCCESS, 1);
}
} else {
// We already have a context of some type.
if (contextType != mCurrentContextType) return nullptr;
if (aContextType != mCurrentContextType) return nullptr;
}
nsCOMPtr<nsICanvasRenderingContextInternal> context = mCurrentContext;

View File

@@ -39,10 +39,6 @@ class CanvasRenderingContextHelper {
public:
CanvasRenderingContextHelper();
virtual already_AddRefed<nsISupports> GetContext(
JSContext* aCx, const nsAString& aContextId,
JS::Handle<JS::Value> aContextOptions, ErrorResult& aRv);
virtual bool GetOpaqueAttr() = 0;
protected:
@@ -64,6 +60,14 @@ class CanvasRenderingContextHelper {
JS::Handle<JS::Value> aParams, bool aUsePlaceholder,
ErrorResult& aRv);
already_AddRefed<nsISupports> GetOrCreateContext(
JSContext* aCx, const nsAString& aContextId,
JS::Handle<JS::Value> aContextOptions, ErrorResult& aRv);
already_AddRefed<nsISupports> GetOrCreateContext(
JSContext* aCx, CanvasContextType aContextType,
JS::Handle<JS::Value> aContextOptions, ErrorResult& aRv);
virtual already_AddRefed<nsICanvasRenderingContextInternal> CreateContext(
CanvasContextType aContextType);

View File

@@ -111,8 +111,8 @@ already_AddRefed<nsISupports> OffscreenCanvas::GetContext(
return nullptr;
}
RefPtr<nsISupports> result = CanvasRenderingContextHelper::GetContext(
aCx, aContextId, aContextOptions, aRv);
RefPtr<nsISupports> result = CanvasRenderingContextHelper::GetOrCreateContext(
aCx, contextType, aContextOptions, aRv);
if (!mCurrentContext) {
return nullptr;

View File

@@ -103,6 +103,10 @@ class OffscreenCanvas final : public DOMEventTargetHelper,
}
}
already_AddRefed<nsISupports> GetContext(
JSContext* aCx, const nsAString& aContextId,
JS::Handle<JS::Value> aContextOptions, ErrorResult& aRv);
already_AddRefed<ImageBitmap> TransferToImageBitmap(ErrorResult& aRv);
already_AddRefed<Promise> ToBlob(JSContext* aCx, const nsAString& aType,
@@ -136,10 +140,6 @@ class OffscreenCanvas final : public DOMEventTargetHelper,
virtual already_AddRefed<nsICanvasRenderingContextInternal> CreateContext(
CanvasContextType aContextType) override;
virtual already_AddRefed<nsISupports> GetContext(
JSContext* aCx, const nsAString& aContextId,
JS::Handle<JS::Value> aContextOptions, ErrorResult& aRv) override;
void SetNeutered() { mNeutered = true; }
bool IsNeutered() const { return mNeutered; }

View File

@@ -954,7 +954,7 @@ already_AddRefed<nsISupports> HTMLCanvasElement::GetContext(
}
mMaybeModified = true; // For FirstContentfulPaint
return CanvasRenderingContextHelper::GetContext(
return CanvasRenderingContextHelper::GetOrCreateContext(
aCx, aContextId,
aContextOptions.isObject() ? aContextOptions : JS::NullHandleValue, aRv);
}

View File

@@ -162,9 +162,9 @@ class HTMLCanvasElement final : public nsGenericHTMLElement,
SetUnsignedIntAttr(nsGkAtoms::width, aWidth, DEFAULT_CANVAS_WIDTH, aRv);
}
virtual already_AddRefed<nsISupports> GetContext(
already_AddRefed<nsISupports> GetContext(
JSContext* aCx, const nsAString& aContextId,
JS::Handle<JS::Value> aContextOptions, ErrorResult& aRv) override;
JS::Handle<JS::Value> aContextOptions, ErrorResult& aRv);
void ToDataURL(JSContext* aCx, const nsAString& aType,
JS::Handle<JS::Value> aParams, nsAString& aDataURL,