forked from mirrors/gecko-dev
		
	 f66935aba8
			
		
	
	
		f66935aba8
		
	
	
	
	
		
			
			Most are simple refactoring, cleanups and improvements, but still involving two slightly notable changes: + In `filemanager`, now we read the file content based on requested `RelativePos` by `seek` and `read_exact` (rather than `read_to_end` then do slicing). This strategy might be again adjusted in future performance tuning but certainly better than nothing. + Also, I cached more file meta-info in both sides and left a block of comment on `filemanager`'s file reading mentioning the snapshot-state problem (not solved now though). r? @Manishearth <!-- Please describe your changes on the following line: --> --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [x] `./mach build -d` does not report any errors - [x] `./mach test-tidy` does not report any errors <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> Source-Repo: https://github.com/servo/servo Source-Revision: 665559556f5aeac5820e17684b14311aa3767c0c
		
			
				
	
	
		
			94 lines
		
	
	
	
		
			3.2 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			94 lines
		
	
	
	
		
			3.2 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
| /* 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/. */
 | |
| 
 | |
| 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::global::GlobalRef;
 | |
| 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::window::Window;
 | |
| use net_traits::filemanager_thread::SelectedFile;
 | |
| use time;
 | |
| 
 | |
| #[dom_struct]
 | |
| pub struct File {
 | |
|     blob: Blob,
 | |
|     name: DOMString,
 | |
|     modified: i64,
 | |
| }
 | |
| 
 | |
| impl File {
 | |
|     #[allow(unrooted_must_root)]
 | |
|     fn new_inherited(blob_impl: BlobImpl, name: DOMString,
 | |
|                      modified: Option<i64>, typeString: &str) -> File {
 | |
|         File {
 | |
|             blob: Blob::new_inherited(blob_impl, typeString.to_owned()),
 | |
|             name: name,
 | |
|             // https://w3c.github.io/FileAPI/#dfn-lastModified
 | |
|             modified: match modified {
 | |
|                 Some(m) => m,
 | |
|                 None => {
 | |
|                     let time = time::get_time();
 | |
|                     time.sec * 1000 + (time.nsec / 1000000) as i64
 | |
|                 }
 | |
|             },
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     #[allow(unrooted_must_root)]
 | |
|     pub fn new(global: GlobalRef, 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)
 | |
|     }
 | |
| 
 | |
|     // 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"));
 | |
| 
 | |
|         let global = GlobalRef::Window(window);
 | |
| 
 | |
|         File::new(global, BlobImpl::new_from_file(selected.id, selected.filename, selected.size),
 | |
|                   name, Some(selected.modified as i64), "")
 | |
|     }
 | |
| 
 | |
|     // https://w3c.github.io/FileAPI/#file-constructor
 | |
|     pub fn Constructor(global: GlobalRef,
 | |
|                        fileBits: Vec<BlobOrString>,
 | |
|                        filename: DOMString,
 | |
|                        filePropertyBag: &FileBinding::FilePropertyBag)
 | |
|                        -> Fallible<Root<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 typeString = blobPropertyBag.get_typestring();
 | |
| 
 | |
|         let modified = filePropertyBag.lastModified;
 | |
|         Ok(File::new(global, BlobImpl::new_from_bytes(bytes), filename, modified, &typeString))
 | |
|     }
 | |
| 
 | |
|     pub fn name(&self) -> &DOMString {
 | |
|         &self.name
 | |
|     }
 | |
| }
 | |
| 
 | |
| impl FileMethods for File {
 | |
|     // https://w3c.github.io/FileAPI/#dfn-name
 | |
|     fn Name(&self) -> DOMString {
 | |
|         self.name.clone()
 | |
|     }
 | |
| 
 | |
|     // https://w3c.github.io/FileAPI/#dfn-lastModified
 | |
|     fn LastModified(&self) -> i64 {
 | |
|         self.modified
 | |
|     }
 | |
| }
 |