Closed Bug 1212477 Opened 9 years ago Closed 9 years ago

Needs a way to access to <canvas>'s context (2d, webgl) from Anonymous Content API

Categories

(Core :: DOM: Core & HTML, defect)

defect
Not set
normal

Tracking

()

RESOLVED FIXED
mozilla45
Tracking Status
firefox45 --- fixed

People

(Reporter: zer0, Assigned: zer0)

References

(Blocks 1 open bug)

Details

Attachments

(1 file, 3 obsolete files)

Currently we don't have a proper way to access to a context in a <canvas> element injected using Anonymous Content API; because we don't have access to the node. We should have a method that, giving the ID of an element in the Anonymous Content, and the type of the context we want to ("2d", or "webgl"), it returns the context's object of the specified element if it's a <canvas>, or `null` otherwise.
Flags: needinfo?(ehsan)
Blocks: 1144575
Here is how to do this: 1. Copy the getContext declaration from HTMLCanvasElement.webidl to AnonymousContent.webidl. 2. You will need to add a GetContext method to the C++ AnonymousContext object similar to this: <https://dxr.mozilla.org/mozilla-central/source/dom/html/HTMLCanvasElement.h#129>. 3. In the definition of that method, you first need to make sure that you're actually dealing with a canvas element. You can do that by calling IsHTMLElement(nsGkAtoms::canvas) on your element. Once that passes, you can static_cast it to an HTMLCanvasElement* and forward the call to GetContext() to that method. 4. One problem is that the CanvasRenderingContext2D and WebGLRenderingContext interfaces both have a canvas property that can give you back the DOM node, which is undesirable here. In order to solve that, you need to do some surgery on the underlying C++ objects to make them return null from their GetCanvas() methods. One simple way to do that is to add a new method to nsICanvasRenderingContextInternal which would set a flag on the object telling it to return null from GetCanvas(). 5. Add tests!
Flags: needinfo?(ehsan)
Assignee: nobody → zer0
Okay, the attachment is the last version of many and I hope I did the things right. The patch adds `getCanvasContext` method to anonymous content API, returns `null` if the element is not a <canvas>. I handled the point four differently, because it seems to me that using a method to set an internal flag could lead to side effects. What I mean is: if any other methods is asking for the canvas' context, inserted in NAC, and then accessing the `canvas` property to the context, it could have different – unpredictable – result, depends if `getCanvasContext` was called before or not. Since the issue here is that we do not want to mess with the DOM once we add elements to the NAC, I consistently returns `null` if the canvas element of the context is in the NAC. I'm going to add the test in a separate, coming patch, in this bug.
I noticed now I left the WebGL part out; what I noticed is that compared to the 2d context, the webgl context internally uses the `GetCanvas` method: https://dxr.mozilla.org/mozilla-central/source/dom/canvas/WebGLContext.cpp?from=webglcontext.cpp#526 https://dxr.mozilla.org/mozilla-central/source/dom/canvas/WebGLContext.cpp?from=webglcontext.cpp#762 I'm not sure what would be the side effect of having `GetCanvas` returns `null` in case we are in NAC, here. In the worst scenario, I think we could probably doing what we have done for the events in NAC: we removed the original target, that points to the DOM node, from the javascript API. Any thoughts?
Attachment #8683052 - Flags: feedback+
Attachment #8683052 - Attachment is obsolete: true
Comment on attachment 8685453 [details] [diff] [review] Needs a way to access to <canvas>'s context (2d, webgl) from Anonymous Content API Added some unit test and webgl fix for canvas's property. Robert, since Ehsan is in vacation till 19th, Andrea mentioned to me that you could be a good reviewer for this patch, and maybe help me with the doubts I exposed above. Thanks!
Attachment #8685453 - Flags: review?(roc)
Comment on attachment 8685453 [details] [diff] [review] Needs a way to access to <canvas>'s context (2d, webgl) from Anonymous Content API Review of attachment 8685453 [details] [diff] [review]: ----------------------------------------------------------------- Looks good!!! You will probably need DOM peer review too.
Attachment #8685453 - Flags: review?(roc)
Attachment #8685453 - Flags: review?(bugs)
Attachment #8685453 - Flags: review+
Comment on attachment 8685453 [details] [diff] [review] Needs a way to access to <canvas>'s context (2d, webgl) from Anonymous Content API > HTMLCanvasElement* GetCanvas() const > { >+ if (mCanvasElement->IsInAnonymousSubtree()) >+ return nullptr; Always {} with if. And you want to use IsInNativeAnonymousSubtree(). (IsInAnonymousSubtree() returns true also for elements in XBL etc) > WebGLContext::GetCanvas(Nullable<dom::OwningHTMLCanvasElementOrOffscreenCanvas>& retval) > { > if (mCanvasElement) { > MOZ_RELEASE_ASSERT(!mOffscreenCanvas); >- retval.SetValue().SetAsHTMLCanvasElement() = mCanvasElement; >+ >+ if (mCanvasElement->IsInAnonymousSubtree()) { ditto
Attachment #8685453 - Flags: review?(bugs) → review+
I run all the linux's test on try; as suggested by Andrea. Here the current patch: https://treeherder.mozilla.org/#/jobs?repo=try&revision=35c28f677a57 Here the patch with `isInNativeAnonymousSubtree` instead: https://treeherder.mozilla.org/#/jobs?repo=try&revision=d670354afe3f I don't think the errors are related to this patch, but I'd like to double check with you. Before attach the new patch and set the checkin-needed flag, do I have to run on Try some specific tests on some other machine?
Flags: needinfo?(roc)
Flags: needinfo?(bugs)
those Marionette tests are odd, but indeed aren't probably about this bug. The code looks cross platform, so there shouldn't be need for running more tests on try.
Flags: needinfo?(bugs)
Attachment #8685453 - Attachment is obsolete: true
Attachment #8687369 - Flags: review+
Keywords: checkin-needed
Attachment #8687369 - Attachment is obsolete: true
Comment on attachment 8687552 [details] [diff] [review] Needs a way to access to <canvas>'s context (2d, webgl) from Anonymous Content API; r=roc;r=smaug Almost forgot the brackets!
Attachment #8687552 - Flags: review+
That's a followup that may or may not be the right thing to do, skipping the test on B2G where it fails like https://treeherder.mozilla.org/logviewer.html#?job_id=17301603&repo=mozilla-inbound, "uncaught exception - TypeError: webgl is null". Perhaps the right thing to do is instead to test whether webgl is supported and only run those parts of the test if it is, since I didn't find a whole litter of manifest annotations saying that tests were disabled on b2g because of the lack of webgl support.
The AnonymousContent API is documented on the wiki [1] for devtools people who want to create new highlighters. Can you update the doc to mention getCanvasContext? [1] https://wiki.mozilla.org/DevTools/Highlighter#The_AnonymousContent_API
Flags: needinfo?(zer0)
Status: NEW → RESOLVED
Closed: 9 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla45
(In reply to Patrick Brosset [:pbrosset] [:pbro] from comment #17) > The AnonymousContent API is documented on the wiki [1] for devtools people > who want to create new highlighters. > Can you update the doc to mention getCanvasContext? > > [1] https://wiki.mozilla.org/DevTools/Highlighter#The_AnonymousContent_API I added the new method on wiki, but I noticed the document is quite outdated; specifically it mention `tabActor` instead of `HighlighterEnvironment`. I was going to file a bug about it, but I didn't find a documentation component for the devtools / inspector. Any idea under which category I should file it?
Flags: needinfo?(zer0) → needinfo?(pbrosset)
(In reply to Matteo Ferretti [:matteo][:zer0] from comment #19) > (In reply to Patrick Brosset [:pbrosset] [:pbro] from comment #17) > > The AnonymousContent API is documented on the wiki [1] for devtools people > > who want to create new highlighters. > > Can you update the doc to mention getCanvasContext? > > > > [1] https://wiki.mozilla.org/DevTools/Highlighter#The_AnonymousContent_API > > I added the new method on wiki, but I noticed the document is quite > outdated; specifically it mention `tabActor` instead of > `HighlighterEnvironment`. I was going to file a bug about it, but I didn't > find a documentation component for the devtools / inspector. Any idea under > which category I should file it? Well we don't always file bugs for doc I think, but you can probably file it in the global devtools component.
Flags: needinfo?(pbrosset)
Component: DOM → DOM: Core & HTML
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: