diff options
Diffstat (limited to 'components/script/dom/file.rs')
-rw-r--r-- | components/script/dom/file.rs | 97 |
1 files changed, 60 insertions, 37 deletions
diff --git a/components/script/dom/file.rs b/components/script/dom/file.rs index 6cf144d10e0..cbbc16bd264 100644 --- a/components/script/dom/file.rs +++ b/components/script/dom/file.rs @@ -1,21 +1,21 @@ /* 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/. */ + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -use dom::bindings::codegen::Bindings::FileBinding; -use dom::bindings::codegen::Bindings::FileBinding::FileMethods; -use dom::bindings::codegen::UnionTypes::BlobOrString; -use dom::bindings::error::{Error, Fallible}; -use dom::bindings::inheritance::Castable; -use dom::bindings::js::Root; -use dom::bindings::reflector::reflect_dom_object; -use dom::bindings::str::DOMString; -use dom::blob::{Blob, BlobImpl, blob_parts_to_bytes}; -use dom::globalscope::GlobalScope; -use dom::window::Window; +use crate::dom::bindings::codegen::Bindings::FileBinding; +use crate::dom::bindings::codegen::Bindings::FileBinding::FileMethods; +use crate::dom::bindings::codegen::UnionTypes::ArrayBufferOrArrayBufferViewOrBlobOrString; +use crate::dom::bindings::error::{Error, Fallible}; +use crate::dom::bindings::inheritance::Castable; +use crate::dom::bindings::reflector::reflect_dom_object; +use crate::dom::bindings::root::DomRoot; +use crate::dom::bindings::str::DOMString; +use crate::dom::blob::{blob_parts_to_bytes, normalize_type_string, Blob}; +use crate::dom::globalscope::GlobalScope; +use crate::dom::window::Window; use dom_struct::dom_struct; use net_traits::filemanager_thread::SelectedFile; -use time; +use script_traits::serializable::BlobImpl; #[dom_struct] pub struct File { @@ -26,10 +26,9 @@ pub struct File { impl File { #[allow(unrooted_must_root)] - fn new_inherited(blob_impl: BlobImpl, name: DOMString, - modified: Option<i64>, type_string: &str) -> File { + fn new_inherited(blob_impl: &BlobImpl, name: DOMString, modified: Option<i64>) -> File { File { - blob: Blob::new_inherited(blob_impl, type_string.to_owned()), + blob: Blob::new_inherited(blob_impl), name: name, // https://w3c.github.io/FileAPI/#dfn-lastModified modified: match modified { @@ -37,50 +36,74 @@ impl File { None => { let time = time::get_time(); time.sec * 1000 + (time.nsec / 1000000) as i64 - } + }, }, } } #[allow(unrooted_must_root)] - pub fn new(global: &GlobalScope, blob_impl: BlobImpl, - name: DOMString, modified: Option<i64>, typeString: &str) -> Root<File> { - reflect_dom_object(box File::new_inherited(blob_impl, name, modified, typeString), - global, - FileBinding::Wrap) + pub fn new( + global: &GlobalScope, + blob_impl: BlobImpl, + name: DOMString, + modified: Option<i64>, + ) -> DomRoot<File> { + let file = reflect_dom_object( + Box::new(File::new_inherited(&blob_impl, name, modified)), + global, + ); + global.track_file(&file, blob_impl); + file } // Construct from selected file message from file manager thread - pub fn new_from_selected(window: &Window, selected: SelectedFile) -> Root<File> { - let name = DOMString::from(selected.filename.to_str().expect("File name encoding error")); + pub fn new_from_selected(window: &Window, selected: SelectedFile) -> DomRoot<File> { + let name = DOMString::from( + selected + .filename + .to_str() + .expect("File name encoding error"), + ); - File::new(window.upcast(), BlobImpl::new_from_file(selected.id, selected.filename, selected.size), - name, Some(selected.modified as i64), &selected.type_string) + File::new( + window.upcast(), + BlobImpl::new_from_file( + selected.id, + selected.filename, + selected.size, + normalize_type_string(&selected.type_string.to_string()), + ), + name, + Some(selected.modified as i64), + ) } // https://w3c.github.io/FileAPI/#file-constructor - pub fn Constructor(global: &GlobalScope, - fileBits: Vec<BlobOrString>, - filename: DOMString, - filePropertyBag: &FileBinding::FilePropertyBag) - -> Fallible<Root<File>> { + #[allow(non_snake_case)] + pub fn Constructor( + global: &GlobalScope, + fileBits: Vec<ArrayBufferOrArrayBufferViewOrBlobOrString>, + filename: DOMString, + filePropertyBag: &FileBinding::FilePropertyBag, + ) -> Fallible<DomRoot<File>> { let bytes: Vec<u8> = match blob_parts_to_bytes(fileBits) { Ok(bytes) => bytes, Err(_) => return Err(Error::InvalidCharacter), }; let ref blobPropertyBag = filePropertyBag.parent; - let ref typeString = blobPropertyBag.type_; let modified = filePropertyBag.lastModified; // NOTE: Following behaviour might be removed in future, // see https://github.com/w3c/FileAPI/issues/41 let replaced_filename = DOMString::from_string(filename.replace("/", ":")); - Ok(File::new(global, - BlobImpl::new_from_bytes(bytes), - replaced_filename, - modified, - typeString)) + let type_string = normalize_type_string(&blobPropertyBag.type_.to_string()); + Ok(File::new( + global, + BlobImpl::new_from_bytes(bytes, type_string), + replaced_filename, + modified, + )) } pub fn name(&self) -> &DOMString { |