diff options
author | Jonas Reinwald <jonas.reinwald@htwg-konstanz.de> | 2017-12-07 16:57:06 +0100 |
---|---|---|
committer | Josh Matthews <josh@joshmatthews.net> | 2018-06-04 15:53:08 -0400 |
commit | 0fdafb08c8813229fb756bd0900ac54278cf1c85 (patch) | |
tree | 4571f448807c214fe50903279cf536c4e4b87f71 /components/script/dom/filereadersync.rs | |
parent | 3e8caa46792b552c6a7559714077d1b3d9f33194 (diff) | |
download | servo-0fdafb08c8813229fb756bd0900ac54278cf1c85.tar.gz servo-0fdafb08c8813229fb756bd0900ac54278cf1c85.zip |
Implement read methods on FileReaderSync
Diffstat (limited to 'components/script/dom/filereadersync.rs')
-rw-r--r-- | components/script/dom/filereadersync.rs | 86 |
1 files changed, 81 insertions, 5 deletions
diff --git a/components/script/dom/filereadersync.rs b/components/script/dom/filereadersync.rs index bbdb83a23a9..cc9a821a073 100644 --- a/components/script/dom/filereadersync.rs +++ b/components/script/dom/filereadersync.rs @@ -2,17 +2,25 @@ * 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/. */ -use dom::bindings::codegen::Bindings::FileReaderSyncBinding; -use dom::bindings::error::Fallible; +use dom::bindings::codegen::Bindings::BlobBinding::BlobMethods; +use dom::bindings::codegen::Bindings::FileReaderSyncBinding::{FileReaderSyncBinding, FileReaderSyncMethods}; +use dom::bindings::error::{Error, Fallible}; use dom::bindings::reflector::reflect_dom_object; use dom::bindings::root::DomRoot; +use dom::bindings::str::DOMString; +use dom::blob::Blob; use dom::eventtarget::EventTarget; +use dom::filereader::FileReaderSharedFunctionality; use dom::globalscope::GlobalScope; use dom_struct::dom_struct; +use js::jsapi::{JSContext, JSObject}; +use js::typedarray::{ArrayBuffer, CreateWith}; +use std::ptr; +use std::ptr::NonNull; #[dom_struct] pub struct FileReaderSync { - eventtarget: EventTarget + eventtarget: EventTarget, } impl FileReaderSync { @@ -23,11 +31,79 @@ impl FileReaderSync { } pub fn new(global: &GlobalScope) -> DomRoot<FileReaderSync> { - reflect_dom_object(Box::new(FileReaderSync::new_inherited()), - global, FileReaderSyncBinding::Wrap) + reflect_dom_object( + Box::new(FileReaderSync::new_inherited()), + global, + FileReaderSyncBinding::Wrap, + ) } pub fn Constructor(global: &GlobalScope) -> Fallible<DomRoot<FileReaderSync>> { Ok(FileReaderSync::new(global)) } + + fn get_blob_bytes(blob: &Blob) -> Result<Vec<u8>, Error> { + blob.get_bytes().map_err(|_| Error::NotReadable) + } +} + +impl FileReaderSyncMethods for FileReaderSync { + // https://w3c.github.io/FileAPI/#readAsBinaryStringSyncSection + fn ReadAsBinaryString(&self, blob: &Blob) -> Fallible<DOMString> { + // step 1 + let blob_contents = FileReaderSync::get_blob_bytes(blob)?; + + // step 2 + Ok(DOMString::from(String::from_utf8_lossy(&blob_contents))) + } + + // https://w3c.github.io/FileAPI/#readAsTextSync + fn ReadAsText(&self, blob: &Blob, label: Option<DOMString>) -> Fallible<DOMString> { + // step 1 + let blob_contents = FileReaderSync::get_blob_bytes(blob)?; + + // step 2 + let blob_label = label.map(String::from); + let blob_type = String::from(blob.Type()); + + let output = + FileReaderSharedFunctionality::text_decode(&blob_contents, &blob_type, &blob_label); + + Ok(output) + } + + // https://w3c.github.io/FileAPI/#readAsDataURLSync-section + fn ReadAsDataURL(&self, blob: &Blob) -> Fallible<DOMString> { + // step 1 + let blob_contents = FileReaderSync::get_blob_bytes(blob)?; + + // step 2 + let output = + FileReaderSharedFunctionality::dataurl_format(&blob_contents, blob.Type().to_string()); + + Ok(output) + } + + #[allow(unsafe_code)] + // https://w3c.github.io/FileAPI/#readAsArrayBufferSyncSection + unsafe fn ReadAsArrayBuffer( + &self, + cx: *mut JSContext, + blob: &Blob, + ) -> Fallible<NonNull<JSObject>> { + // step 1 + let blob_contents = FileReaderSync::get_blob_bytes(blob)?; + + // step 2 + rooted!(in(cx) let mut array_buffer = ptr::null_mut::<JSObject>()); + assert!( + ArrayBuffer::create( + cx, + CreateWith::Slice(&blob_contents), + array_buffer.handle_mut() + ).is_ok() + ); + + Ok(NonNull::new_unchecked(array_buffer.get())) + } } |