From 096dddd2e4ce7a211f6e7af78224c3759ad223fc Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Sat, 13 Apr 2013 09:05:33 +0200 Subject: [PATCH] Bug 826740 - Part d: Use implicit_jscontext to simplify nsIDOMHTMLCanvasElement.getContext; r=khuey --- .../html/content/public/HTMLCanvasElement.h | 2 + .../html/content/src/HTMLCanvasElement.cpp | 37 +++++++++++-------- .../html/nsIDOMHTMLCanvasElement.idl | 3 +- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/content/html/content/public/HTMLCanvasElement.h b/content/html/content/public/HTMLCanvasElement.h index 6677537285f0..f5011ac08e53 100644 --- a/content/html/content/public/HTMLCanvasElement.h +++ b/content/html/content/public/HTMLCanvasElement.h @@ -152,6 +152,8 @@ public: // take a snapshot of the canvas that needs to be "live" (e.g. -moz-element). void MarkContextClean(); + nsresult GetContext(const nsAString& aContextId, nsISupports** aContext); + virtual nsXPCClassInfo* GetClassInfo(); virtual nsIDOMNode* AsDOMNode() { return this; } diff --git a/content/html/content/src/HTMLCanvasElement.cpp b/content/html/content/src/HTMLCanvasElement.cpp index 39bf3ec9b0be..ec0ab24029ae 100644 --- a/content/html/content/src/HTMLCanvasElement.cpp +++ b/content/html/content/src/HTMLCanvasElement.cpp @@ -236,8 +236,7 @@ HTMLCanvasElement::DispatchPrintCallback(nsITimerCallback* aCallback) if (!mCurrentContext) { nsresult rv; nsCOMPtr context; - rv = GetContext(NS_LITERAL_STRING("2d"), JSVAL_VOID, - getter_AddRefs(context)); + rv = GetContext(NS_LITERAL_STRING("2d"), getter_AddRefs(context)); NS_ENSURE_SUCCESS(rv, rv); } mPrintState = new HTMLCanvasPrintState(this, mCurrentContext, aCallback); @@ -286,14 +285,13 @@ HTMLCanvasElement::CopyInnerTo(Element* aDest) NS_ENSURE_SUCCESS(rv, rv); if (aDest->OwnerDoc()->IsStaticDocument()) { HTMLCanvasElement* dest = static_cast(aDest); - HTMLCanvasElement* self = const_cast(this); - dest->mOriginalCanvas = self; + dest->mOriginalCanvas = this; nsCOMPtr cxt; - dest->GetContext(NS_LITERAL_STRING("2d"), JSVAL_VOID, getter_AddRefs(cxt)); + dest->GetContext(NS_LITERAL_STRING("2d"), getter_AddRefs(cxt)); nsRefPtr context2d = static_cast(cxt.get()); - if (context2d && !self->mPrintCallback) { + if (context2d && !mPrintCallback) { HTMLImageOrCanvasOrVideoElement element; element.SetAsHTMLCanvasElement() = this; ErrorResult err; @@ -702,11 +700,21 @@ HTMLCanvasElement::GetContextHelper(const nsAString& aContextId, return NS_OK; } +nsresult +HTMLCanvasElement::GetContext(const nsAString& aContextId, + nsISupports** aContext) +{ + return GetContext(aContextId, JS::UndefinedValue(), nullptr, aContext); +} + NS_IMETHODIMP HTMLCanvasElement::GetContext(const nsAString& aContextId, const JS::Value& aContextOptions, + JSContext* aCx, nsISupports **aContext) { + MOZ_ASSERT_IF(!aCx, aContextOptions.isUndefined()); + nsresult rv; if (mCurrentContextId.IsEmpty()) { @@ -731,23 +739,22 @@ HTMLCanvasElement::GetContext(const nsAString& aContextId, nsCOMPtr contextProps; if (aContextOptions.isObject()) { - JSContext* cx = nsContentUtils::GetCurrentJSContext(); - + MOZ_ASSERT(aCx); contextProps = do_CreateInstance("@mozilla.org/hash-property-bag;1"); JSObject& opts = aContextOptions.toObject(); - JS::AutoIdArray props(cx, JS_Enumerate(cx, &opts)); + JS::AutoIdArray props(aCx, JS_Enumerate(aCx, &opts)); for (size_t i = 0; !!props && i < props.length(); ++i) { jsid propid = props[i]; JS::Value propname, propval; - if (!JS_IdToValue(cx, propid, &propname) || - !JS_GetPropertyById(cx, &opts, propid, &propval)) { + if (!JS_IdToValue(aCx, propid, &propname) || + !JS_GetPropertyById(aCx, &opts, propid, &propval)) { return NS_ERROR_FAILURE; } - JSString *propnameString = JS_ValueToString(cx, propname); + JSString *propnameString = JS_ValueToString(aCx, propname); nsDependentJSString pstr; - if (!propnameString || !pstr.init(cx, propnameString)) { + if (!propnameString || !pstr.init(aCx, propnameString)) { mCurrentContext = nullptr; return NS_ERROR_FAILURE; } @@ -759,9 +766,9 @@ HTMLCanvasElement::GetContext(const nsAString& aContextId, } else if (JSVAL_IS_DOUBLE(propval)) { contextProps->SetPropertyAsDouble(pstr, JSVAL_TO_DOUBLE(propval)); } else if (JSVAL_IS_STRING(propval)) { - JSString *propvalString = JS_ValueToString(cx, propval); + JSString *propvalString = JS_ValueToString(aCx, propval); nsDependentJSString vstr; - if (!propvalString || !vstr.init(cx, propvalString)) { + if (!propvalString || !vstr.init(aCx, propvalString)) { mCurrentContext = nullptr; return NS_ERROR_FAILURE; } diff --git a/dom/interfaces/html/nsIDOMHTMLCanvasElement.idl b/dom/interfaces/html/nsIDOMHTMLCanvasElement.idl index 26db1267a1c2..2ac9f51ab2d2 100644 --- a/dom/interfaces/html/nsIDOMHTMLCanvasElement.idl +++ b/dom/interfaces/html/nsIDOMHTMLCanvasElement.idl @@ -46,13 +46,14 @@ interface nsIFileCallback : nsISupports { void receive(in nsIDOMBlob file); }; -[scriptable, uuid(ba777030-783e-43b6-b85d-0670da81acb8)] +[scriptable, uuid(eb5ea539-f177-4276-bd4c-3b49c2dc9b2f)] interface nsIDOMHTMLCanvasElement : nsIDOMHTMLElement { attribute unsigned long width; attribute unsigned long height; attribute boolean mozOpaque; + [implicit_jscontext] nsISupports getContext(in DOMString contextId, [optional] in jsval contextOptions);