Skip to content

Commit

Permalink
Remove some usage of unsafe code in HTMLCanvasElement
Browse files Browse the repository at this point in the history
  • Loading branch information
marmeladema committed Aug 8, 2019
1 parent d1282dc commit 1d92796
Showing 1 changed file with 26 additions and 33 deletions.
59 changes: 26 additions & 33 deletions components/script/dom/htmlcanvaselement.rs
Expand Up @@ -28,7 +28,7 @@ use crate::dom::webgl2renderingcontext::WebGL2RenderingContext;
use crate::dom::webglrenderingcontext::{
LayoutCanvasWebGLRenderingContextHelpers, WebGLRenderingContext,
};
use crate::script_runtime::JSContext as SafeJSContext;
use crate::script_runtime::JSContext;
use base64;
use canvas_traits::canvas::{CanvasId, CanvasMsg, FromScriptMsg};
use canvas_traits::webgl::{GLContextAttributes, WebGLVersion};
Expand All @@ -39,7 +39,6 @@ use image::png::PNGEncoder;
use image::ColorType;
use ipc_channel::ipc::IpcSharedMemory;
use js::error::throw_type_error;
use js::jsapi::JSContext;
use js::rust::HandleValue;
use profile_traits::ipc;
use script_layout_interface::{HTMLCanvasData, HTMLCanvasDataSource};
Expand Down Expand Up @@ -207,10 +206,9 @@ impl HTMLCanvasElement {
Some(context)
}

#[allow(unsafe_code)]
unsafe fn get_or_init_webgl_context(
fn get_or_init_webgl_context(
&self,
cx: *mut JSContext,
cx: JSContext,
options: HandleValue,
) -> Option<DomRoot<WebGLRenderingContext>> {
if let Some(ctx) = self.context() {
Expand All @@ -227,10 +225,9 @@ impl HTMLCanvasElement {
Some(context)
}

#[allow(unsafe_code)]
unsafe fn get_or_init_webgl2_context(
fn get_or_init_webgl2_context(
&self,
cx: *mut JSContext,
cx: JSContext,
options: HandleValue,
) -> Option<DomRoot<WebGL2RenderingContext>> {
if !pref!(dom.webgl2.enabled) {
Expand Down Expand Up @@ -260,20 +257,19 @@ impl HTMLCanvasElement {
}

#[allow(unsafe_code)]
unsafe fn get_gl_attributes(
cx: *mut JSContext,
options: HandleValue,
) -> Option<GLContextAttributes> {
match WebGLContextAttributes::new(SafeJSContext::from_ptr(cx), options) {
Ok(ConversionResult::Success(ref attrs)) => Some(From::from(attrs)),
Ok(ConversionResult::Failure(ref error)) => {
throw_type_error(cx, &error);
None
},
_ => {
debug!("Unexpected error on conversion of WebGLContextAttributes");
None
},
fn get_gl_attributes(cx: JSContext, options: HandleValue) -> Option<GLContextAttributes> {
unsafe {
match WebGLContextAttributes::new(cx, options) {
Ok(ConversionResult::Success(ref attrs)) => Some(From::from(attrs)),
Ok(ConversionResult::Failure(ref error)) => {
throw_type_error(*cx, &error);
None
},
_ => {
debug!("Unexpected error on conversion of WebGLContextAttributes");
None
},
}
}
}

Expand Down Expand Up @@ -329,33 +325,30 @@ impl HTMLCanvasElementMethods for HTMLCanvasElement {
make_uint_setter!(SetHeight, "height", DEFAULT_HEIGHT);

// https://html.spec.whatwg.org/multipage/#dom-canvas-getcontext
#[allow(unsafe_code)]
fn GetContext(
&self,
cx: SafeJSContext,
cx: JSContext,
id: DOMString,
options: HandleValue,
) -> Option<RenderingContext> {
match &*id {
"2d" => self
.get_or_init_2d_context()
.map(RenderingContext::CanvasRenderingContext2D),
"webgl" | "experimental-webgl" => unsafe {
self.get_or_init_webgl_context(*cx, options)
.map(RenderingContext::WebGLRenderingContext)
},
"webgl2" | "experimental-webgl2" => unsafe {
self.get_or_init_webgl2_context(*cx, options)
.map(RenderingContext::WebGL2RenderingContext)
},
"webgl" | "experimental-webgl" => self
.get_or_init_webgl_context(cx, options)
.map(RenderingContext::WebGLRenderingContext),
"webgl2" | "experimental-webgl2" => self
.get_or_init_webgl2_context(cx, options)
.map(RenderingContext::WebGL2RenderingContext),
_ => None,
}
}

// https://html.spec.whatwg.org/multipage/#dom-canvas-todataurl
fn ToDataURL(
&self,
_context: SafeJSContext,
_context: JSContext,
_mime_type: Option<DOMString>,
_quality: HandleValue,
) -> Fallible<USVString> {
Expand Down

0 comments on commit 1d92796

Please sign in to comment.