Bug 759726 - Move mCanvasElement to nsICanvasRenderingContextInternal and make SetCanvasElement return void; f=bz r=bjacob
authorMs2ger <ms2ger@gmail.com>
Wed, 06 Jun 2012 09:42:47 +0200
changeset 95861 6338a89889171684f17d64da4c0bca5e54c770f5
parent 95860 8216b90cef741838756c860d9bd8e6d9f661e4d7
child 95937 a6c39a15557befac8ce98359ee6c0789c7e80a1d
push id22858
push userMs2ger@gmail.com
push dateWed, 06 Jun 2012 07:44:41 +0000
treeherdermozilla-central@6338a8988917 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbjacob
bugs759726
milestone16.0a1
first release with
nightly linux32
6338a8988917 / 16.0a1 / 20120606030528 / files
nightly linux64
6338a8988917 / 16.0a1 / 20120606030528 / files
nightly mac
6338a8988917 / 16.0a1 / 20120606030528 / files
nightly win32
6338a8988917 / 16.0a1 / 20120606030528 / files
nightly win64
6338a8988917 / 16.0a1 / 20120606030528 / files
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
releases
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 759726 - Move mCanvasElement to nsICanvasRenderingContextInternal and make SetCanvasElement return void; f=bz r=bjacob
content/canvas/public/DocumentRendererChild.h
content/canvas/public/DocumentRendererParent.h
content/canvas/public/nsICanvasRenderingContextInternal.h
content/canvas/src/DocumentRendererChild.cpp
content/canvas/src/DocumentRendererParent.cpp
content/canvas/src/WebGLContext.cpp
content/canvas/src/WebGLContext.h
content/canvas/src/WebGLContextGL.cpp
content/canvas/src/nsCanvasRenderingContext2D.cpp
content/canvas/src/nsCanvasRenderingContext2DAzure.cpp
content/html/content/public/nsHTMLCanvasElement.h
content/html/content/src/nsHTMLCanvasElement.cpp
--- a/content/canvas/public/DocumentRendererChild.h
+++ b/content/canvas/public/DocumentRendererChild.h
@@ -1,17 +1,16 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef mozilla_dom_DocumentRendererChild
 #define mozilla_dom_DocumentRendererChild
 
 #include "mozilla/ipc/PDocumentRendererChild.h"
-#include "nsICanvasRenderingContextInternal.h"
 #include "nsString.h"
 #include "gfxContext.h"
 
 class nsIDOMWindow;
 
 namespace mozilla {
 namespace ipc {
 
--- a/content/canvas/public/DocumentRendererParent.h
+++ b/content/canvas/public/DocumentRendererParent.h
@@ -1,21 +1,22 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef mozilla_dom_DocumentRendererParent
 #define mozilla_dom_DocumentRendererParent
 
 #include "mozilla/ipc/PDocumentRendererParent.h"
-#include "nsICanvasRenderingContextInternal.h"
 #include "nsCOMPtr.h"
 #include "nsString.h"
 #include "gfxContext.h"
 
+class nsICanvasRenderingContextInternal;
+
 namespace mozilla {
 namespace ipc {
 
 class DocumentRendererParent : public PDocumentRendererParent
 {
 public:
     DocumentRendererParent();
     virtual ~DocumentRendererParent();
--- a/content/canvas/public/nsICanvasRenderingContextInternal.h
+++ b/content/canvas/public/nsICanvasRenderingContextInternal.h
@@ -4,24 +4,24 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef nsICanvasRenderingContextInternal_h___
 #define nsICanvasRenderingContextInternal_h___
 
 #include "nsISupports.h"
 #include "nsIInputStream.h"
 #include "nsIDocShell.h"
+#include "nsHTMLCanvasElement.h"
 #include "gfxPattern.h"
 #include "mozilla/RefPtr.h"
 
 #define NS_ICANVASRENDERINGCONTEXTINTERNAL_IID \
-{ 0xffb42d3c, 0x8281, 0x44c8, \
-  { 0xac, 0xba, 0x73, 0x15, 0x31, 0xaa, 0xe5, 0x07 } }
+{ 0x8b8da863, 0xd151, 0x4014, \
+  { 0x8b, 0xdc, 0x62, 0xb5, 0x0d, 0xc0, 0x2b, 0x62 } }
 
-class nsHTMLCanvasElement;
 class gfxContext;
 class gfxASurface;
 class nsIPropertyBag;
 class nsDisplayListBuilder;
 
 namespace mozilla {
 namespace layers {
 class CanvasLayer;
@@ -41,19 +41,20 @@ public:
   typedef mozilla::layers::LayerManager LayerManager;
 
   NS_DECLARE_STATIC_IID_ACCESSOR(NS_ICANVASRENDERINGCONTEXTINTERNAL_IID)
 
   enum {
     RenderFlagPremultAlpha = 0x1
   };
 
-  // This method should NOT hold a ref to aParentCanvas; it will be called
-  // with nsnull when the element is going away.
-  NS_IMETHOD SetCanvasElement(nsHTMLCanvasElement* aParentCanvas) = 0;
+  void SetCanvasElement(nsHTMLCanvasElement* aParentCanvas)
+  {
+    mCanvasElement = aParentCanvas;
+  }
 
   // Sets the dimensions of the canvas, in pixels.  Called
   // whenever the size of the element changes.
   NS_IMETHOD SetDimensions(PRInt32 width, PRInt32 height) = 0;
 
   NS_IMETHOD InitializeWithSurface(nsIDocShell *docShell, gfxASurface *surface, PRInt32 width, PRInt32 height) = 0;
 
   // Render the canvas at the origin of the given gfxContext
@@ -114,14 +115,17 @@ public:
   // shmem support
   //
 
   // If this context can be set to use Mozilla's Shmem segments as its backing
   // store, this will set it to that state. Note that if you have drawn
   // anything into this canvas before changing the shmem state, it will be
   // lost.
   NS_IMETHOD SetIsIPC(bool isIPC) = 0;
+
+protected:
+  nsRefPtr<nsHTMLCanvasElement> mCanvasElement;
 };
 
 NS_DEFINE_STATIC_IID_ACCESSOR(nsICanvasRenderingContextInternal,
                               NS_ICANVASRENDERINGCONTEXTINTERNAL_IID)
 
 #endif /* nsICanvasRenderingContextInternal_h___ */
--- a/content/canvas/src/DocumentRendererChild.cpp
+++ b/content/canvas/src/DocumentRendererChild.cpp
@@ -6,16 +6,17 @@
 
 #include "base/basictypes.h"
 
 #include "gfxImageSurface.h"
 #include "gfxPattern.h"
 #include "nsPIDOMWindow.h"
 #include "nsIDOMWindow.h"
 #include "nsIDOMDocument.h"
+#include "nsIDocShell.h"
 #include "nsIDocShellTreeNode.h"
 #include "nsIDocShellTreeItem.h"
 #include "nsIDocument.h"
 #include "nsIInterfaceRequestorUtils.h"
 #include "nsComponentManagerUtils.h"
 #include "nsCSSParser.h"
 #include "nsPresContext.h"
 #include "nsCOMPtr.h"
--- a/content/canvas/src/DocumentRendererParent.cpp
+++ b/content/canvas/src/DocumentRendererParent.cpp
@@ -1,15 +1,16 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "mozilla/ipc/DocumentRendererParent.h"
 #include "gfxImageSurface.h"
 #include "gfxPattern.h"
+#include "nsICanvasRenderingContextInternal.h"
 
 using namespace mozilla::ipc;
 
 DocumentRendererParent::DocumentRendererParent()
 {}
 
 DocumentRendererParent::~DocumentRendererParent()
 {}
--- a/content/canvas/src/WebGLContext.cpp
+++ b/content/canvas/src/WebGLContext.cpp
@@ -68,18 +68,17 @@ NS_NewCanvasRenderingContextWebGL(nsIDOM
     if (!ctx)
         return NS_ERROR_OUT_OF_MEMORY;
 
     NS_ADDREF(*aResult = ctx);
     return NS_OK;
 }
 
 WebGLContext::WebGLContext()
-    : mCanvasElement(nsnull),
-      gl(nsnull)
+    : gl(nsnull)
 {
     SetIsDOMBinding();
     mEnabledExtensions.SetLength(WebGLExtensionID_Max);
 
     mGeneration = 0;
     mInvalidated = false;
     mResetLayer = true;
     mOptionsFrozen = false;
@@ -238,43 +237,35 @@ void
 WebGLContext::Invalidate()
 {
     if (mInvalidated)
         return;
 
     if (!mCanvasElement)
         return;
 
-    nsSVGEffects::InvalidateDirectRenderingObservers(HTMLCanvasElement());
+    nsSVGEffects::InvalidateDirectRenderingObservers(mCanvasElement);
 
     mInvalidated = true;
-    HTMLCanvasElement()->InvalidateCanvasContent(nsnull);
+    mCanvasElement->InvalidateCanvasContent(nsnull);
 }
 
 /* readonly attribute nsIDOMHTMLCanvasElement canvas; */
 NS_IMETHODIMP
 WebGLContext::GetCanvas(nsIDOMHTMLCanvasElement **canvas)
 {
     NS_IF_ADDREF(*canvas = mCanvasElement);
 
     return NS_OK;
 }
 
 //
 // nsICanvasRenderingContextInternal
 //
 
-NS_IMETHODIMP
-WebGLContext::SetCanvasElement(nsHTMLCanvasElement* aParentCanvas)
-{
-    mCanvasElement = aParentCanvas;
-
-    return NS_OK;
-}
-
 static bool
 GetBoolFromPropertyBag(nsIPropertyBag *bag, const char *propName, bool *boolResult)
 {
     nsCOMPtr<nsIVariant> vv;
     bool bv;
 
     nsresult rv = bag->GetProperty(NS_ConvertASCIItoUTF16(propName), getter_AddRefs(vv));
     if (NS_FAILED(rv) || !vv)
@@ -327,17 +318,17 @@ WebGLContext::SetContextOptions(nsIPrope
 }
 
 NS_IMETHODIMP
 WebGLContext::SetDimensions(PRInt32 width, PRInt32 height)
 {
     /*** early success return cases ***/
   
     if (mCanvasElement) {
-        HTMLCanvasElement()->InvalidateCanvas();
+        mCanvasElement->InvalidateCanvas();
     }
 
     if (gl && mWidth == width && mHeight == height)
         return NS_OK;
 
     // Zero-sized surfaces can cause problems.
     if (width == 0 || height == 0) {
         width = 1;
@@ -722,17 +713,17 @@ WebGLContext::GetCanvasLayer(nsDisplayLi
       // and if there is, flushing the invalidation state more often than
       // necessary is harmless).
 
       // The layer will be destroyed when we tear down the presentation
       // (at the latest), at which time this userData will be destroyed,
       // releasing the reference to the element.
       // The userData will receive DidTransactionCallbacks, which flush the
       // the invalidation state to indicate that the canvas is up to date.
-      userData = new WebGLContextUserData(HTMLCanvasElement());
+      userData = new WebGLContextUserData(mCanvasElement);
       canvasLayer->SetDidTransactionCallback(
               WebGLContextUserData::DidTransactionCallback, userData);
     }
     canvasLayer->SetUserData(&gWebGLLayerUserData, userData);
 
     CanvasLayer::Data data;
 
     // the gl context may either provide a native PBuffer, in which case we want to initialize
@@ -1061,28 +1052,28 @@ WebGLContext::DummyFramebufferOperation(
 // are met.
 // At a bare minimum, from context lost to context restores, it would take 3
 // full timer iterations: detection, webglcontextlost, webglcontextrestored.
 NS_IMETHODIMP
 WebGLContext::Notify(nsITimer* timer)
 {
     TerminateContextLossTimer();
 
-    if (!HTMLCanvasElement()) {
+    if (!mCanvasElement) {
         // the canvas is gone. That happens when the page was closed before we got
         // this timer event. In this case, there's nothing to do here, just don't crash.
         return NS_OK;
     }
 
     // If the context has been lost and we're waiting for it to be restored, do
     // that now.
     if (mContextStatus == ContextLostAwaitingEvent) {
         bool defaultAction;
-        nsContentUtils::DispatchTrustedEvent(HTMLCanvasElement()->OwnerDoc(),
-                                             (nsIDOMHTMLCanvasElement*) HTMLCanvasElement(),
+        nsContentUtils::DispatchTrustedEvent(mCanvasElement->OwnerDoc(),
+                                             static_cast<nsIDOMHTMLCanvasElement*>(mCanvasElement),
                                              NS_LITERAL_STRING("webglcontextlost"),
                                              true,
                                              true,
                                              &defaultAction);
 
         // If the script didn't handle the event, we don't allow restores.
         if (defaultAction)
             mAllowRestore = false;
@@ -1100,18 +1091,18 @@ WebGLContext::Notify(nsITimer* timer)
         }
     } else if (mContextStatus == ContextLostAwaitingRestore) {
         // Try to restore the context. If it fails, try again later.
         if (NS_FAILED(SetDimensions(mWidth, mHeight))) {
             SetupContextLossTimer();
             return NS_OK;
         }
         mContextStatus = ContextStable;
-        nsContentUtils::DispatchTrustedEvent(HTMLCanvasElement()->OwnerDoc(),
-                                             (nsIDOMHTMLCanvasElement*) HTMLCanvasElement(),
+        nsContentUtils::DispatchTrustedEvent(mCanvasElement->OwnerDoc(),
+                                             static_cast<nsIDOMHTMLCanvasElement*>(mCanvasElement),
                                              NS_LITERAL_STRING("webglcontextrestored"),
                                              true,
                                              true);
         // Set all flags back to the state they were in before the context was
         // lost.
         mContextLostErrorSet = false;
         mAllowRestore = true;
     }
@@ -1210,17 +1201,17 @@ NS_IMPL_CYCLE_COLLECTION_TRACE_END
 
 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(WebGLContext)
   NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mCanvasElement)
   NS_IMPL_CYCLE_COLLECTION_UNLINK_NSTARRAY(mEnabledExtensions)
   NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
 
 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(WebGLContext)
-  NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mCanvasElement)
+  NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR_AMBIGUOUS(mCanvasElement, nsINode)
   NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSTARRAY_OF_NSCOMPTR(mEnabledExtensions)
   NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
 
 DOMCI_DATA(WebGLRenderingContext, WebGLContext)
 
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(WebGLContext)
   NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
--- a/content/canvas/src/WebGLContext.h
+++ b/content/canvas/src/WebGLContext.h
@@ -478,32 +478,27 @@ public:
     virtual ~WebGLContext();
 
     NS_DECL_CYCLE_COLLECTING_ISUPPORTS
 
     NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(WebGLContext,
                                                            nsIDOMWebGLRenderingContext)
 
     nsINode* GetParentObject() {
-        return HTMLCanvasElement();
+        return mCanvasElement;
     }
 
     virtual JSObject* WrapObject(JSContext *cx, JSObject *scope,
                                  bool *triedToWrap);
 
     NS_DECL_NSIDOMWEBGLRENDERINGCONTEXT
 
     NS_DECL_NSITIMERCALLBACK
 
     // nsICanvasRenderingContextInternal
-    NS_IMETHOD SetCanvasElement(nsHTMLCanvasElement* aParentCanvas);
-    nsHTMLCanvasElement* HTMLCanvasElement() const {
-        return static_cast<nsHTMLCanvasElement*>(mCanvasElement.get());
-    }
-
     NS_IMETHOD SetDimensions(PRInt32 width, PRInt32 height);
     NS_IMETHOD InitializeWithSurface(nsIDocShell *docShell, gfxASurface *surface, PRInt32 width, PRInt32 height)
         { return NS_ERROR_NOT_IMPLEMENTED; }
     NS_IMETHOD Reset()
         { /* (InitializeWithSurface) */ return NS_ERROR_NOT_IMPLEMENTED; }
     NS_IMETHOD Render(gfxContext *ctx,
                       gfxPattern::GraphicsFilter f,
                       PRUint32 aFlags = RenderFlagPremultAlpha);
@@ -614,17 +609,17 @@ public:
         if (mContextLossTimerRunning) {
             mContextRestorer->Cancel();
             mContextLossTimerRunning = false;
         }
     }
 
     // WebIDL WebGLRenderingContext API
     nsHTMLCanvasElement* GetCanvas() const {
-        return HTMLCanvasElement();
+        return mCanvasElement;
     }
     WebGLsizei GetDrawingBufferWidth() const {
         if (!IsContextStable())
             return 0;
         return mWidth;
     }
     WebGLsizei GetDrawingBufferHeight() const {
         if (!IsContextStable())
@@ -1102,18 +1097,16 @@ protected:
                                       uint32_t pixelSize,
                                       uint32_t alignment);
 
     // Returns x rounded to the next highest multiple of y.
     static CheckedUint32 RoundedToNextMultipleOf(CheckedUint32 x, CheckedUint32 y) {
         return ((x + y - 1) / y) * y;
     }
 
-    nsCOMPtr<nsIDOMHTMLCanvasElement> mCanvasElement;
-
     nsRefPtr<gl::GLContext> gl;
 
     CheckedUint32 mGeneration;
 
     WebGLContextOptions mOptions;
 
     bool mInvalidated;
     bool mResetLayer;
--- a/content/canvas/src/WebGLContextGL.cpp
+++ b/content/canvas/src/WebGLContextGL.cpp
@@ -3836,17 +3836,17 @@ WebGLContext::ReadPixels(WebGLint x, Web
                          WebGLsizei height, WebGLenum format,
                          WebGLenum type, ArrayBufferView* pixels,
                          ErrorResult& rv)
 {
     if (!IsContextStable()) {
         return;
     }
 
-    if (HTMLCanvasElement()->IsWriteOnly() && !nsContentUtils::IsCallerTrustedForRead()) {
+    if (mCanvasElement->IsWriteOnly() && !nsContentUtils::IsCallerTrustedForRead()) {
         GenerateWarning("readPixels: Not allowed");
         return rv.Throw(NS_ERROR_DOM_SECURITY_ERR);
     }
 
     if (width < 0 || height < 0)
         return ErrorInvalidValue("readPixels: negative size passed");
 
     const WebGLRectangleObject *framebufferRect = FramebufferRectangleObject();
@@ -4338,17 +4338,17 @@ WebGLContext::SurfaceFromElementResultTo
     // To prevent a loophole where a Canvas2D would be used as a proxy to load
     // cross-domain textures, we also disallow loading textures from write-only
     // Canvas2D's.
 
     // part 1: check that the DOM element is same-origin, or has otherwise been
     // validated for cross-domain use.
     if (!res.mCORSUsed) {
         bool subsumes;
-        nsresult rv = HTMLCanvasElement()->NodePrincipal()->Subsumes(res.mPrincipal, &subsumes);
+        nsresult rv = mCanvasElement->NodePrincipal()->Subsumes(res.mPrincipal, &subsumes);
         if (NS_FAILED(rv) || !subsumes) {
             GenerateWarning("It is forbidden to load a WebGL texture from a cross-domain element that has not been validated with CORS. "
                                 "See https://developer.mozilla.org/en/WebGL/Cross-Domain_Textures");
             return NS_ERROR_DOM_SECURITY_ERR;
         }
     }
 
     // part 2: if the DOM element is write-only, it might contain
--- a/content/canvas/src/nsCanvasRenderingContext2D.cpp
+++ b/content/canvas/src/nsCanvasRenderingContext2D.cpp
@@ -312,17 +312,16 @@ class nsCanvasRenderingContext2D :
 {
 public:
     nsCanvasRenderingContext2D();
     virtual ~nsCanvasRenderingContext2D();
 
     nsresult Redraw();
 
     // nsICanvasRenderingContextInternal
-    NS_IMETHOD SetCanvasElement(nsHTMLCanvasElement* aParentCanvas);
     NS_IMETHOD SetDimensions(PRInt32 width, PRInt32 height);
     void Initialize(nsIDocShell *shell, PRInt32 width, PRInt32 height);
     NS_IMETHOD InitializeWithSurface(nsIDocShell *shell, gfxASurface *surface, PRInt32 width, PRInt32 height);
     bool EnsureSurface();
     NS_IMETHOD Render(gfxContext *ctx,
                       gfxPattern::GraphicsFilter aFilter,
                       PRUint32 aFlags = RenderFlagPremultAlpha);
     NS_IMETHOD GetInputStream(const char* aMimeType,
@@ -440,22 +439,16 @@ protected:
     // Member vars
     PRInt32 mWidth, mHeight;
     bool mValid;
     bool mZero;
     bool mOpaque;
     bool mResetLayer;
     bool mIPC;
 
-    // the canvas element we're a context of
-    nsCOMPtr<nsIDOMHTMLCanvasElement> mCanvasElement;
-    nsHTMLCanvasElement *HTMLCanvasElement() {
-        return static_cast<nsHTMLCanvasElement*>(mCanvasElement.get());
-    }
-
     // Initialize the Thebes rendering context
     void CreateThebes();
 
     // If mCanvasElement is not provided, then a docshell is
     nsCOMPtr<nsIDocShell> mDocShell;
 
     // our drawing surfaces, contexts, and layers
     nsRefPtr<gfxContext> mThebes;
@@ -766,17 +759,17 @@ protected:
 NS_IMPL_CYCLE_COLLECTING_ADDREF(nsCanvasRenderingContext2D)
 NS_IMPL_CYCLE_COLLECTING_RELEASE(nsCanvasRenderingContext2D)
 
 NS_IMPL_CYCLE_COLLECTION_CLASS(nsCanvasRenderingContext2D)
 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsCanvasRenderingContext2D)
   NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mCanvasElement)
 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsCanvasRenderingContext2D)
-  NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mCanvasElement)
+  NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR_AMBIGUOUS(mCanvasElement, nsINode)
 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
 
 DOMCI_DATA(CanvasRenderingContext2D, nsCanvasRenderingContext2D)
 
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsCanvasRenderingContext2D)
   NS_INTERFACE_MAP_ENTRY(nsIDOMCanvasRenderingContext2D)
   NS_INTERFACE_MAP_ENTRY(nsICanvasRenderingContextInternal)
   NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMCanvasRenderingContext2D)
@@ -802,17 +795,16 @@ NS_NewCanvasRenderingContext2DThebes(nsI
 
     *aResult = ctx.forget().get();
     return NS_OK;
 }
 
 nsCanvasRenderingContext2D::nsCanvasRenderingContext2D()
     : mValid(false), mZero(false), mOpaque(false), mResetLayer(true)
     , mIPC(false)
-    , mCanvasElement(nsnull)
     , mSaveCount(0), mIsEntireFrameInvalid(false)
     , mPredictManyRedrawCalls(false), mHasPath(false), mInvalidateCount(0)
     , mLastStyle(STYLE_MAX), mStyleStack(20)
 {
     sNumLivingContexts++;
 }
 
 nsCanvasRenderingContext2D::~nsCanvasRenderingContext2D()
@@ -826,47 +818,47 @@ nsCanvasRenderingContext2D::~nsCanvasRen
         sPremultiplyTable = nsnull;
     }
 }
 
 bool
 nsCanvasRenderingContext2D::ParseColor(const nsAString& aString,
                                        nscolor* aColor)
 {
-    nsIDocument* document = HTMLCanvasElement()
-                             ? HTMLCanvasElement()->OwnerDoc()
-                             : nsnull;
+    nsIDocument* document = mCanvasElement
+                            ? mCanvasElement->OwnerDoc()
+                            : nsnull;
 
     // Pass the CSS Loader object to the parser, to allow parser error
     // reports to include the outer window ID.
     nsCSSParser parser(document ? document->CSSLoader() : nsnull);
     nsCSSValue value;
     if (!parser.ParseColorString(aString, nsnull, 0, value)) {
         return false;
     }
 
     nsIPresShell* presShell = GetPresShell();
     nsRefPtr<nsStyleContext> parentContext;
-    if (HTMLCanvasElement() && HTMLCanvasElement()->IsInDoc()) {
+    if (mCanvasElement && mCanvasElement->IsInDoc()) {
         // Inherit from the canvas element.
         parentContext = nsComputedDOMStyle::GetStyleContextForElement(
-            HTMLCanvasElement(), nsnull, presShell);
+            mCanvasElement, nsnull, presShell);
     }
 
     unused << nsRuleNode::ComputeColor(
         value, presShell ? presShell->GetPresContext() : nsnull, parentContext,
         *aColor);
     return true;
 }
 
 nsresult
 nsCanvasRenderingContext2D::Reset()
 {
     if (mCanvasElement) {
-        HTMLCanvasElement()->InvalidateCanvas();
+        mCanvasElement->InvalidateCanvas();
     }
 
     // only do this for non-docshell created contexts,
     // since those are the ones that we created a surface for
     if (mValid && !mDocShell && mSurface)
         gCanvasMemoryUsed -= mWidth * mHeight * 4;
 
     mSurface = nsnull;
@@ -908,17 +900,17 @@ nsCanvasRenderingContext2D::SetStyleFrom
             mDirtyStyle[aWhichStyle] = true;
             return NS_OK;
         }
     }
 
     nsContentUtils::ReportToConsole(
         nsIScriptError::warningFlag,
         "Canvas",
-        mCanvasElement ? HTMLCanvasElement()->OwnerDoc() : nsnull,
+        mCanvasElement ? mCanvasElement->OwnerDoc() : nsnull,
         nsContentUtils::eDOM_PROPERTIES,
         "UnexpectedCanvasVariantStyle");
 
     return NS_OK;
 }
 
 nsresult
 nsCanvasRenderingContext2D::GetStyleAsStringOrInterface(nsAString& aStr,
@@ -992,17 +984,17 @@ nsCanvasRenderingContext2D::ApplyStyle(S
     // if not using global alpha, don't optimize with dirty bit
     if (aUseGlobalAlpha)
         mDirtyStyle[aWhichStyle] = false;
     mLastStyle = aWhichStyle;
 
     nsCanvasPattern* pattern = CurrentState().patternStyles[aWhichStyle];
     if (pattern) {
         if (mCanvasElement)
-            CanvasUtils::DoDrawImageSecurityCheck(HTMLCanvasElement(),
+            CanvasUtils::DoDrawImageSecurityCheck(mCanvasElement,
                                                   pattern->Principal(),
                                                   pattern->GetForceWriteOnly(),
                                                   pattern->GetCORSUsed());
 
         gfxPattern* gpat = pattern->GetPattern();
 
         if (CurrentState().imageSmoothingEnabled)
             gpat->SetFilter(gfxPattern::FILTER_GOOD);
@@ -1033,19 +1025,19 @@ nsCanvasRenderingContext2D::Redraw()
         return NS_OK;
     mIsEntireFrameInvalid = true;
 
     if (!mCanvasElement) {
         NS_ASSERTION(mDocShell, "Redraw with no canvas element or docshell!");
         return NS_OK;
     }
 
-    nsSVGEffects::InvalidateDirectRenderingObservers(HTMLCanvasElement());
-
-    HTMLCanvasElement()->InvalidateCanvasContent(nsnull);
+    nsSVGEffects::InvalidateDirectRenderingObservers(mCanvasElement);
+
+    mCanvasElement->InvalidateCanvasContent(nsnull);
 
     return NS_OK;
 }
 
 NS_IMETHODIMP
 nsCanvasRenderingContext2D::Redraw(const gfxRect& r)
 {
     ++mInvalidateCount;
@@ -1058,19 +1050,19 @@ nsCanvasRenderingContext2D::Redraw(const
         return Redraw();
     }
 
     if (!mCanvasElement) {
         NS_ASSERTION(mDocShell, "Redraw with no canvas element or docshell!");
         return NS_OK;
     }
 
-    nsSVGEffects::InvalidateDirectRenderingObservers(HTMLCanvasElement());
-
-    HTMLCanvasElement()->InvalidateCanvasContent(&r);
+    nsSVGEffects::InvalidateDirectRenderingObservers(mCanvasElement);
+
+    mCanvasElement->InvalidateCanvasContent(&r);
 
     return NS_OK;
 }
 
 NS_IMETHODIMP
 nsCanvasRenderingContext2D::RedrawUser(const gfxRect& r)
 {
     if (mIsEntireFrameInvalid) {
@@ -1373,24 +1365,16 @@ nsCanvasRenderingContext2D::GetImageForm
     return format;
 }
 
 //
 // nsCanvasRenderingContext2D impl
 //
 
 NS_IMETHODIMP
-nsCanvasRenderingContext2D::SetCanvasElement(nsHTMLCanvasElement* aCanvasElement)
-{
-    mCanvasElement = aCanvasElement;
-
-    return NS_OK;
-}
-
-NS_IMETHODIMP
 nsCanvasRenderingContext2D::GetCanvas(nsIDOMHTMLCanvasElement **canvas)
 {
     NS_IF_ADDREF(*canvas = mCanvasElement);
 
     return NS_OK;
 }
 
 //
@@ -2467,19 +2451,18 @@ nsCanvasRenderingContext2D::SetFont(cons
     /*
      * If font is defined with relative units (e.g. ems) and the parent
      * style context changes in between calls, setting the font to the
      * same value as previous could result in a different computed value,
      * so we cannot have the optimization where we check if the new font
      * string is equal to the old one.
      */
 
-    nsCOMPtr<nsIContent> content = do_QueryInterface(mCanvasElement);
-    if (!content && !mDocShell) {
-        NS_WARNING("Canvas element must be an nsIContent and non-null or a docshell must be provided");
+    if (!mCanvasElement && !mDocShell) {
+        NS_WARNING("Canvas element must be non-null or a docshell must be provided");
         return NS_ERROR_FAILURE;
     }
 
     nsIPresShell* presShell = GetPresShell();
     if (!presShell)
       return NS_ERROR_FAILURE;
     nsIDocument* document = presShell->GetDocument();
 
@@ -2509,20 +2492,20 @@ nsCanvasRenderingContext2D::SetFont(cons
     rules.AppendObject(rule);
 
     nsStyleSet* styleSet = presShell->StyleSet();
 
     // have to get a parent style context for inherit-like relative
     // values (2em, bolder, etc.)
     nsRefPtr<nsStyleContext> parentContext;
 
-    if (content && content->IsInDoc()) {
+    if (mCanvasElement && mCanvasElement->IsInDoc()) {
         // inherit from the canvas element
         parentContext = nsComputedDOMStyle::GetStyleContextForElement(
-                content->AsElement(),
+                mCanvasElement,
                 nsnull,
                 presShell);
     } else {
         // otherwise inherit from default
         nsRefPtr<css::StyleRule> parentRule;
         rv = CreateFontStyleRule(kDefaultFontStyle,
                                  document,
                                  getter_AddRefs(parentRule));
@@ -2847,39 +2830,38 @@ nsCanvasRenderingContext2D::DrawOrMeasur
 
     // spec isn't clear on what should happen if aMaxWidth <= 0, so
     // treat it as an invalid argument
     // technically, 0 should be an invalid value as well, but 0 is the default
     // arg, and there is no way to tell if the default was used
     if (aMaxWidth < 0)
         return NS_ERROR_INVALID_ARG;
 
-    nsCOMPtr<nsIContent> content = do_QueryInterface(mCanvasElement);
-    if (!content && !mDocShell) {
-        NS_WARNING("Canvas element must be an nsIContent and non-null or a docshell must be provided");
+    if (!mCanvasElement && !mDocShell) {
+        NS_WARNING("Canvas element must be non-null or a docshell must be provided");
         return NS_ERROR_FAILURE;
     }
 
     nsIPresShell* presShell = GetPresShell();
     if (!presShell)
         return NS_ERROR_FAILURE;
 
     nsIDocument* document = presShell->GetDocument();
 
     // replace all the whitespace characters with U+0020 SPACE
     nsAutoString textToDraw(aRawText);
     TextReplaceWhitespaceCharacters(textToDraw);
 
     // for now, default to ltr if not in doc
     bool isRTL = false;
 
-    if (content && content->IsInDoc()) {
+    if (mCanvasElement && mCanvasElement->IsInDoc()) {
         // try to find the closest context
         nsRefPtr<nsStyleContext> canvasStyle =
-            nsComputedDOMStyle::GetStyleContextForElement(content->AsElement(),
+            nsComputedDOMStyle::GetStyleContextForElement(mCanvasElement,
                                                           nsnull,
                                                           presShell);
         if (!canvasStyle)
             return NS_ERROR_FAILURE;
         isRTL = canvasStyle->GetStyleVisibility()->mDirection ==
             NS_STYLE_DIRECTION_RTL;
     } else {
       isRTL = GET_BIDI_OPTION_DIRECTION(document->GetBidiOptions()) == IBMBIDI_TEXTDIRECTION_RTL;
@@ -3392,17 +3374,17 @@ nsCanvasRenderingContext2D::DrawImage(ns
             return NS_ERROR_DOM_INVALID_STATE_ERR;
         }
     }
 
     gfxMatrix matrix;
     nsRefPtr<gfxPattern> pattern;
     gfxIntSize imgSize;
     nsRefPtr<gfxASurface> imgsurf =
-      CanvasImageCache::Lookup(imgElt, HTMLCanvasElement(), &imgSize);
+      CanvasImageCache::Lookup(imgElt, mCanvasElement, &imgSize);
 
     if (!imgsurf) {
         // The canvas spec says that drawImage should draw the first frame
         // of animated images
         PRUint32 sfeFlags = nsLayoutUtils::SFE_WANT_FIRST_FRAME;
         nsLayoutUtils::SurfaceFromElementResult res =
             nsLayoutUtils::SurfaceFromElement(content->AsElement(), sfeFlags);
         if (!res.mSurface) {
@@ -3419,24 +3401,24 @@ nsCanvasRenderingContext2D::DrawImage(ns
             if (!res.mSurface)
                 return NS_ERROR_NOT_AVAILABLE;
         }
 
         imgsurf = res.mSurface.forget();
         imgSize = res.mSize;
 
         if (mCanvasElement) {
-            CanvasUtils::DoDrawImageSecurityCheck(HTMLCanvasElement(),
+            CanvasUtils::DoDrawImageSecurityCheck(mCanvasElement,
                                                   res.mPrincipal,
                                                   res.mIsWriteOnly,
                                                   res.mCORSUsed);
         }
 
         if (res.mImageRequest) {
-            CanvasImageCache::NotifyDrawImage(imgElt, HTMLCanvasElement(),
+            CanvasImageCache::NotifyDrawImage(imgElt, mCanvasElement,
                                               res.mImageRequest, imgsurf, imgSize);
         }
     }
 
     double sx,sy,sw,sh;
     double dx,dy,dw,dh;
     if (optional_argc == 0) {
         dx = a1;
@@ -3829,18 +3811,17 @@ nsCanvasRenderingContext2D::GetImageData
 {
     if (!mCanvasElement && !mDocShell) {
         NS_ERROR("No canvas element and no docshell in GetImageData!!!");
         return NS_ERROR_DOM_SECURITY_ERR;
     }
 
     // Check only if we have a canvas element; if we were created with a docshell,
     // then it's special internal use.
-    if (mCanvasElement &&
-        HTMLCanvasElement()->IsWriteOnly() &&
+    if (mCanvasElement && mCanvasElement->IsWriteOnly() &&
         !nsContentUtils::IsCallerTrustedForRead()) {
         // XXX ERRMSG we need to report an error to developers here! (bug 329026)
         return NS_ERROR_DOM_SECURITY_ERR;
     }
 
     if (!EnsureSurface()) {
         return NS_ERROR_FAILURE;
     }
@@ -4212,17 +4193,17 @@ nsCanvasRenderingContext2D::GetCanvasLay
       // and if there is, flushing the invalidation state more often than
       // necessary is harmless).
 
       // The layer will be destroyed when we tear down the presentation
       // (at the latest), at which time this userData will be destroyed,
       // releasing the reference to the element.
       // The userData will receive DidTransactionCallbacks, which flush the
       // the invalidation state to indicate that the canvas is up to date.
-      userData = new CanvasRenderingContext2DUserData(HTMLCanvasElement());
+      userData = new CanvasRenderingContext2DUserData(mCanvasElement);
       canvasLayer->SetDidTransactionCallback(
               CanvasRenderingContext2DUserData::DidTransactionCallback, userData);
     }
     canvasLayer->SetUserData(&g2DContextLayerUserData, userData);
 
     CanvasLayer::Data data;
 
     data.mSurface = mSurface.get();
--- a/content/canvas/src/nsCanvasRenderingContext2DAzure.cpp
+++ b/content/canvas/src/nsCanvasRenderingContext2DAzure.cpp
@@ -368,17 +368,16 @@ class nsCanvasRenderingContext2DAzure :
 {
 public:
   nsCanvasRenderingContext2DAzure();
   virtual ~nsCanvasRenderingContext2DAzure();
 
   nsresult Redraw();
 
   // nsICanvasRenderingContextInternal
-  NS_IMETHOD SetCanvasElement(nsHTMLCanvasElement* aParentCanvas);
   NS_IMETHOD SetDimensions(PRInt32 width, PRInt32 height);
   NS_IMETHOD InitializeWithSurface(nsIDocShell *shell, gfxASurface *surface, PRInt32 width, PRInt32 height)
   { return NS_ERROR_NOT_IMPLEMENTED; }
 
   NS_IMETHOD Render(gfxContext *ctx,
                     gfxPattern::GraphicsFilter aFilter,
                     PRUint32 aFlags = RenderFlagPremultAlpha);
   NS_IMETHOD GetInputStream(const char* aMimeType,
@@ -477,20 +476,16 @@ protected:
   void FillRuleChanged();
 
   /**
     * Returns the surface format this canvas should be allocated using. Takes
     * into account mOpaque, platform requirements, etc.
     */
   SurfaceFormat GetSurfaceFormat() const;
 
-  nsHTMLCanvasElement *HTMLCanvasElement() {
-    return static_cast<nsHTMLCanvasElement*>(mCanvasElement.get());
-  }
-
   // Member vars
   PRInt32 mWidth, mHeight;
 
   // This is true when the canvas is valid, false otherwise, this occurs when
   // for some reason initialization of the drawtarget fails. If the canvas
   // is invalid certain behavior is expected.
   bool mValid;
   // This is true when the canvas is valid, but of zero size, this requires
@@ -500,19 +495,16 @@ protected:
   bool mOpaque;
 
   // This is true when the next time our layer is retrieved we need to
   // recreate it (i.e. our backing surface changed)
   bool mResetLayer;
   // This is needed for drawing in drawAsyncXULElement
   bool mIPC;
 
-  // the canvas element we're a context of
-  nsCOMPtr<nsIDOMHTMLCanvasElement> mCanvasElement;
-
   // If mCanvasElement is not provided, then a docshell is
   nsCOMPtr<nsIDocShell> mDocShell;
 
   // our drawing surfaces, contexts, and layers
   RefPtr<DrawTarget> mTarget;
 
   /**
     * Flag to avoid duplicate calls to InvalidateFrame. Set to true whenever
@@ -779,17 +771,17 @@ protected:
         nsCanvasRadialGradientAzure *gradient =
           static_cast<nsCanvasRadialGradientAzure*>(state.gradientStyles[aStyle].get());
 
         mPattern = new (mRadialGradientPattern.addr())
           RadialGradientPattern(gradient->mCenter1, gradient->mCenter2, gradient->mRadius1,
                                 gradient->mRadius2, gradient->GetGradientStopsForTarget(aRT));
       } else if (state.patternStyles[aStyle]) {
         if (aCtx->mCanvasElement) {
-          CanvasUtils::DoDrawImageSecurityCheck(aCtx->HTMLCanvasElement(),
+          CanvasUtils::DoDrawImageSecurityCheck(aCtx->mCanvasElement,
                                                 state.patternStyles[aStyle]->mPrincipal,
                                                 state.patternStyles[aStyle]->mForceWriteOnly,
                                                 state.patternStyles[aStyle]->mCORSUsed);
         }
 
         ExtendMode mode;
         if (state.patternStyles[aStyle]->mRepeat == nsCanvasPatternAzure::NOREPEAT) {
           mode = EXTEND_CLAMP;
@@ -943,17 +935,17 @@ protected:
 NS_IMPL_CYCLE_COLLECTING_ADDREF(nsCanvasRenderingContext2DAzure)
 NS_IMPL_CYCLE_COLLECTING_RELEASE(nsCanvasRenderingContext2DAzure)
 
 NS_IMPL_CYCLE_COLLECTION_CLASS(nsCanvasRenderingContext2DAzure)
 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsCanvasRenderingContext2DAzure)
   NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mCanvasElement)
 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsCanvasRenderingContext2DAzure)
-  NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mCanvasElement)
+  NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR_AMBIGUOUS(mCanvasElement, nsINode)
 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
 
 // XXX
 // DOMCI_DATA(CanvasRenderingContext2D, nsCanvasRenderingContext2DAzure)
 
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsCanvasRenderingContext2DAzure)
   NS_INTERFACE_MAP_ENTRY(nsIDOMCanvasRenderingContext2D)
   NS_INTERFACE_MAP_ENTRY(nsICanvasRenderingContextInternal)
@@ -991,17 +983,16 @@ NS_NewCanvasRenderingContext2DAzure(nsID
 
   *aResult = ctx.forget().get();
   return NS_OK;
 }
 
 nsCanvasRenderingContext2DAzure::nsCanvasRenderingContext2DAzure()
   : mValid(false), mZero(false), mOpaque(false), mResetLayer(true)
   , mIPC(false)
-  , mCanvasElement(nsnull)
   , mIsEntireFrameInvalid(false)
   , mPredictManyRedrawCalls(false), mPathTransformWillUpdate(false)
   , mInvalidateCount(0)
 {
   sNumLivingContexts++;
 }
 
 nsCanvasRenderingContext2DAzure::~nsCanvasRenderingContext2DAzure()
@@ -1015,47 +1006,47 @@ nsCanvasRenderingContext2DAzure::~nsCanv
     sPremultiplyTable = nsnull;
   }
 }
 
 bool
 nsCanvasRenderingContext2DAzure::ParseColor(const nsAString& aString,
                                             nscolor* aColor)
 {
-  nsIDocument* document = HTMLCanvasElement()
-                           ? HTMLCanvasElement()->OwnerDoc()
-                           : nsnull;
+  nsIDocument* document = mCanvasElement
+                          ? mCanvasElement->OwnerDoc()
+                          : nsnull;
 
   // Pass the CSS Loader object to the parser, to allow parser error
   // reports to include the outer window ID.
   nsCSSParser parser(document ? document->CSSLoader() : nsnull);
   nsCSSValue value;
   if (!parser.ParseColorString(aString, nsnull, 0, value)) {
     return false;
   }
 
   nsIPresShell* presShell = GetPresShell();
   nsRefPtr<nsStyleContext> parentContext;
-  if (HTMLCanvasElement() && HTMLCanvasElement()->IsInDoc()) {
+  if (mCanvasElement && mCanvasElement->IsInDoc()) {
     // Inherit from the canvas element.
     parentContext = nsComputedDOMStyle::GetStyleContextForElement(
-      HTMLCanvasElement(), nsnull, presShell);
+      mCanvasElement, nsnull, presShell);
   }
 
   unused << nsRuleNode::ComputeColor(
     value, presShell ? presShell->GetPresContext() : nsnull, parentContext,
     *aColor);
   return true;
 }
 
 nsresult
 nsCanvasRenderingContext2DAzure::Reset()
 {
   if (mCanvasElement) {
-    HTMLCanvasElement()->InvalidateCanvas();
+    mCanvasElement->InvalidateCanvas();
   }
 
   // only do this for non-docshell created contexts,
   // since those are the ones that we created a surface for
   if (mValid && !mDocShell) {
     gCanvasAzureMemoryUsed -= mWidth * mHeight * 4;
   }
 
@@ -1098,17 +1089,17 @@ nsCanvasRenderingContext2DAzure::SetStyl
       CurrentState().SetPatternStyle(aWhichStyle, pattern);
       return NS_OK;
     }
   }
 
   nsContentUtils::ReportToConsole(
     nsIScriptError::warningFlag,
     "Canvas",
-    mCanvasElement ? HTMLCanvasElement()->OwnerDoc() : nsnull,
+    mCanvasElement ? mCanvasElement->OwnerDoc() : nsnull,
     nsContentUtils::eDOM_PROPERTIES,
     "UnexpectedCanvasVariantStyle");
 
   return NS_OK;
 }
 
 nsresult
 nsCanvasRenderingContext2DAzure::GetStyleAsStringOrInterface(nsAString& aStr,
@@ -1171,19 +1162,19 @@ nsCanvasRenderingContext2DAzure::Redraw(
     return NS_OK;
   }
 
   if (!mThebesSurface)
     mThebesSurface =
       gfxPlatform::GetPlatform()->GetThebesSurfaceForDrawTarget(mTarget);
   mThebesSurface->MarkDirty();
 
-  nsSVGEffects::InvalidateDirectRenderingObservers(HTMLCanvasElement());
-
-  HTMLCanvasElement()->InvalidateCanvasContent(nsnull);
+  nsSVGEffects::InvalidateDirectRenderingObservers(mCanvasElement);
+
+  mCanvasElement->InvalidateCanvasContent(nsnull);
 
   return NS_OK;
 }
 
 void
 nsCanvasRenderingContext2DAzure::Redraw(const mgfx::Rect &r)
 {
   ++mInvalidateCount;
@@ -1203,20 +1194,20 @@ nsCanvasRenderingContext2DAzure::Redraw(
     return;
   }
 
   if (!mThebesSurface)
     mThebesSurface =
       gfxPlatform::GetPlatform()->GetThebesSurfaceForDrawTarget(mTarget);
   mThebesSurface->MarkDirty();
 
-  nsSVGEffects::InvalidateDirectRenderingObservers(HTMLCanvasElement());
+  nsSVGEffects::InvalidateDirectRenderingObservers(mCanvasElement);
 
   gfxRect tmpR = ThebesRect(r);
-  HTMLCanvasElement()->InvalidateCanvasContent(&tmpR);
+  mCanvasElement->InvalidateCanvasContent(&tmpR);
 
   return;
 }
 
 nsresult
 nsCanvasRenderingContext2DAzure::RedrawUser(const gfxRect& r)
 {
   if (mIsEntireFrameInvalid) {
@@ -1493,24 +1484,16 @@ nsCanvasRenderingContext2DAzure::GetSurf
   return mOpaque ? FORMAT_B8G8R8X8 : FORMAT_B8G8R8A8;
 }
 
 //
 // nsCanvasRenderingContext2DAzure impl
 //
 
 NS_IMETHODIMP
-nsCanvasRenderingContext2DAzure::SetCanvasElement(nsHTMLCanvasElement* aCanvasElement)
-{
-  mCanvasElement = aCanvasElement;
-
-  return NS_OK;
-}
-
-NS_IMETHODIMP
 nsCanvasRenderingContext2DAzure::GetCanvas(nsIDOMHTMLCanvasElement **canvas)
 {
   NS_IF_ADDREF(*canvas = mCanvasElement);
 
   return NS_OK;
 }
 
 //
@@ -2675,19 +2658,18 @@ nsCanvasRenderingContext2DAzure::SetFont
   /*
     * If font is defined with relative units (e.g. ems) and the parent
     * style context changes in between calls, setting the font to the
     * same value as previous could result in a different computed value,
     * so we cannot have the optimization where we check if the new font
     * string is equal to the old one.
     */
 
-  nsCOMPtr<nsIContent> content = do_QueryInterface(mCanvasElement);
-  if (!content && !mDocShell) {
-      NS_WARNING("Canvas element must be an nsIContent and non-null or a docshell must be provided");
+  if (!mCanvasElement && !mDocShell) {
+      NS_WARNING("Canvas element must be non-null or a docshell must be provided");
       return NS_ERROR_FAILURE;
   }
 
   nsIPresShell* presShell = GetPresShell();
   if (!presShell) {
     return NS_ERROR_FAILURE;
   }
   nsIDocument* document = presShell->GetDocument();
@@ -2720,20 +2702,20 @@ nsCanvasRenderingContext2DAzure::SetFont
   rules.AppendObject(rule);
 
   nsStyleSet* styleSet = presShell->StyleSet();
 
   // have to get a parent style context for inherit-like relative
   // values (2em, bolder, etc.)
   nsRefPtr<nsStyleContext> parentContext;
 
-  if (content && content->IsInDoc()) {
+  if (mCanvasElement && mCanvasElement->IsInDoc()) {
       // inherit from the canvas element
       parentContext = nsComputedDOMStyle::GetStyleContextForElement(
-              content->AsElement(),
+              mCanvasElement,
               nsnull,
               presShell);
   } else {
     // otherwise inherit from default (10px sans-serif)
     nsRefPtr<css::StyleRule> parentRule;
     rv = CreateFontStyleRule(NS_LITERAL_STRING("10px sans-serif"),
                               document,
                               getter_AddRefs(parentRule));
@@ -3163,39 +3145,38 @@ nsCanvasRenderingContext2DAzure::DrawOrM
 
   // spec isn't clear on what should happen if aMaxWidth <= 0, so
   // treat it as an invalid argument
   // technically, 0 should be an invalid value as well, but 0 is the default
   // arg, and there is no way to tell if the default was used
   if (aMaxWidth < 0)
     return NS_ERROR_INVALID_ARG;
 
-  nsCOMPtr<nsIContent> content = do_QueryInterface(mCanvasElement);
-  if (!content && !mDocShell) {
-      NS_WARNING("Canvas element must be an nsIContent and non-null or a docshell must be provided");
+  if (!mCanvasElement && !mDocShell) {
+    NS_WARNING("Canvas element must be non-null or a docshell must be provided");
     return NS_ERROR_FAILURE;
   }
 
   nsIPresShell* presShell = GetPresShell();
   if (!presShell)
       return NS_ERROR_FAILURE;
 
   nsIDocument* document = presShell->GetDocument();
 
   // replace all the whitespace characters with U+0020 SPACE
   nsAutoString textToDraw(aRawText);
   TextReplaceWhitespaceCharacters(textToDraw);
 
   // for now, default to ltr if not in doc
   bool isRTL = false;
 
-  if (content && content->IsInDoc()) {
+  if (mCanvasElement && mCanvasElement->IsInDoc()) {
     // try to find the closest context
     nsRefPtr<nsStyleContext> canvasStyle =
-      nsComputedDOMStyle::GetStyleContextForElement(content->AsElement(),
+      nsComputedDOMStyle::GetStyleContextForElement(mCanvasElement,
                                                     nsnull,
                                                     presShell);
     if (!canvasStyle) {
       return NS_ERROR_FAILURE;
     }
 
     isRTL = canvasStyle->GetStyleVisibility()->mDirection ==
       NS_STYLE_DIRECTION_RTL;
@@ -3624,25 +3605,25 @@ nsCanvasRenderingContext2DAzure::DrawIma
       srcSurf = mTarget->Snapshot();
       imgSize = gfxIntSize(mWidth, mHeight);
     } else if (srcCanvas) {
       // This might not be an Azure canvas!
       srcSurf = srcCanvas->GetSurfaceSnapshot();
 
       if (srcSurf && mCanvasElement) {
         // Do security check here.
-        CanvasUtils::DoDrawImageSecurityCheck(HTMLCanvasElement(),
+        CanvasUtils::DoDrawImageSecurityCheck(mCanvasElement,
                                               content->NodePrincipal(), canvas->IsWriteOnly(),
                                               false);
         imgSize = gfxIntSize(srcSurf->GetSize().width, srcSurf->GetSize().height);
       }
     }
   } else {
     gfxASurface* imgsurf =
-      CanvasImageCache::Lookup(imgElt, HTMLCanvasElement(), &imgSize);
+      CanvasImageCache::Lookup(imgElt, mCanvasElement, &imgSize);
     if (imgsurf) {
       srcSurf = gfxPlatform::GetPlatform()->GetSourceSurfaceForSurface(mTarget, imgsurf);
     }
   }
 
   if (!srcSurf) {
     // The canvas spec says that drawImage should draw the first frame
     // of animated images
@@ -3658,23 +3639,23 @@ nsCanvasRenderingContext2DAzure::DrawIma
     // Ignore cairo surfaces that are bad! See bug 666312.
     if (res.mSurface->CairoStatus()) {
       return NS_OK;
     }
 
     imgSize = res.mSize;
 
     if (mCanvasElement) {
-      CanvasUtils::DoDrawImageSecurityCheck(HTMLCanvasElement(),
+      CanvasUtils::DoDrawImageSecurityCheck(mCanvasElement,
                                             res.mPrincipal, res.mIsWriteOnly,
                                             res.mCORSUsed);
     }
 
     if (res.mImageRequest) {
-      CanvasImageCache::NotifyDrawImage(imgElt, HTMLCanvasElement(),
+      CanvasImageCache::NotifyDrawImage(imgElt, mCanvasElement,
                                         res.mImageRequest, res.mSurface, imgSize);
     }
 
     srcSurf = gfxPlatform::GetPlatform()->GetSourceSurfaceForSurface(mTarget, res.mSurface);
   }
 
   if (optional_argc == 0) {
     dx = a1;
@@ -4004,18 +3985,17 @@ nsCanvasRenderingContext2DAzure::GetImag
 
   if (!mCanvasElement && !mDocShell) {
     NS_ERROR("No canvas element and no docshell in GetImageData!!!");
     return NS_ERROR_DOM_SECURITY_ERR;
   }
 
   // Check only if we have a canvas element; if we were created with a docshell,
   // then it's special internal use.
-  if (mCanvasElement &&
-      HTMLCanvasElement()->IsWriteOnly() &&
+  if (mCanvasElement && mCanvasElement->IsWriteOnly() &&
       !nsContentUtils::IsCallerTrustedForRead())
   {
     // XXX ERRMSG we need to report an error to developers here! (bug 329026)
     return NS_ERROR_DOM_SECURITY_ERR;
   }
 
   if (!NS_finite(aSx) || !NS_finite(aSy) ||
       !NS_finite(aSw) || !NS_finite(aSh)) {
@@ -4402,17 +4382,17 @@ nsCanvasRenderingContext2DAzure::GetCanv
     // and if there is, flushing the invalidation state more often than
     // necessary is harmless).
 
     // The layer will be destroyed when we tear down the presentation
     // (at the latest), at which time this userData will be destroyed,
     // releasing the reference to the element.
     // The userData will receive DidTransactionCallbacks, which flush the
     // the invalidation state to indicate that the canvas is up to date.
-    userData = new CanvasRenderingContext2DUserData(HTMLCanvasElement());
+    userData = new CanvasRenderingContext2DUserData(mCanvasElement);
     canvasLayer->SetDidTransactionCallback(
             CanvasRenderingContext2DUserData::DidTransactionCallback, userData);
   }
   canvasLayer->SetUserData(&g2DContextLayerUserData, userData);
 
   CanvasLayer::Data data;
 
   data.mDrawTarget = mTarget;
--- a/content/html/content/public/nsHTMLCanvasElement.h
+++ b/content/html/content/public/nsHTMLCanvasElement.h
@@ -11,24 +11,24 @@
 #include "nsGkAtoms.h"
 #include "nsSize.h"
 #include "nsIFrame.h"
 #include "nsIDocument.h"
 #include "nsIDOMDocument.h"
 #include "nsDOMError.h"
 #include "nsNodeInfoManager.h"
 
-#include "nsICanvasRenderingContextInternal.h"
 #include "nsICanvasElementExternal.h"
-#include "nsIDOMCanvasRenderingContext2D.h"
 #include "nsLayoutUtils.h"
 
 #include "Layers.h"
 
+class nsICanvasRenderingContextInternal;
 class nsIDOMFile;
+class nsIPropertyBag;
 
 class nsHTMLCanvasElement : public nsGenericHTMLElement,
                             public nsICanvasElementExternal,
                             public nsIDOMHTMLCanvasElement
 {
   typedef mozilla::layers::CanvasLayer CanvasLayer;
   typedef mozilla::layers::LayerManager LayerManager;
 
--- a/content/html/content/src/nsHTMLCanvasElement.cpp
+++ b/content/html/content/src/nsHTMLCanvasElement.cpp
@@ -6,16 +6,18 @@
 #include "nsHTMLCanvasElement.h"
 
 #include "mozilla/Base64.h"
 #include "mozilla/CheckedInt.h"
 #include "nsNetUtil.h"
 #include "prmem.h"
 #include "nsDOMFile.h"
 
+#include "nsICanvasRenderingContextInternal.h"
+#include "nsIDOMCanvasRenderingContext2D.h"
 #include "nsIScriptSecurityManager.h"
 #include "nsIXPConnect.h"
 #include "jsapi.h"
 #include "nsContentUtils.h"
 #include "nsJSUtils.h"
 #include "nsMathUtils.h"
 #include "nsStreamUtils.h"
 #include "mozilla/Preferences.h"
@@ -448,25 +450,19 @@ nsHTMLCanvasElement::GetContextHelper(co
     return NS_ERROR_OUT_OF_MEMORY;
   }
   if (NS_FAILED(rv)) {
     *aContext = nsnull;
     // XXX ERRMSG we need to report an error to developers here! (bug 329026)
     return NS_OK;
   }
 
-  rv = ctx->SetCanvasElement(this);
-  if (NS_FAILED(rv)) {
-    *aContext = nsnull;
-    return rv;
-  }
-
+  ctx->SetCanvasElement(this);
   ctx.forget(aContext);
-
-  return rv;
+  return NS_OK;
 }
 
 NS_IMETHODIMP
 nsHTMLCanvasElement::GetContext(const nsAString& aContextId,
                                 const JS::Value& aContextOptions,
                                 nsISupports **aContext)
 {
   nsresult rv;