From 6ac106ca76afc2793c1030d9941b29836db14bd2 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Mon, 22 May 2017 02:24:42 +0200 Subject: Remove some usage of rust-encoding --- components/script/dom/blob.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'components/script/dom/blob.rs') diff --git a/components/script/dom/blob.rs b/components/script/dom/blob.rs index c076282dcc1..ddf14fc8096 100644 --- a/components/script/dom/blob.rs +++ b/components/script/dom/blob.rs @@ -12,8 +12,6 @@ use dom::bindings::reflector::{DomObject, Reflector, reflect_dom_object}; use dom::bindings::str::DOMString; use dom::globalscope::GlobalScope; use dom_struct::dom_struct; -use encoding::all::UTF_8; -use encoding::types::{EncoderTrap, Encoding}; use ipc_channel::ipc; use net_traits::{CoreResourceMsg, IpcSend}; use net_traits::blob_url_store::{BlobBuf, get_blob_origin}; @@ -337,12 +335,11 @@ pub fn blob_parts_to_bytes(blobparts: Vec) -> Result, ()> for blobpart in &blobparts { match blobpart { &BlobOrString::String(ref s) => { - let mut bytes = UTF_8.encode(s, EncoderTrap::Replace).map_err(|_|())?; - ret.append(&mut bytes); + ret.extend(s.as_bytes()); }, &BlobOrString::Blob(ref b) => { - let mut bytes = b.get_bytes().unwrap_or(vec![]); - ret.append(&mut bytes); + let bytes = b.get_bytes().unwrap_or(vec![]); + ret.extend(bytes); }, } } -- cgit v1.2.3 From 23e5bfaf27312840092d9938bb99748a02e0d9bf Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Wed, 26 Jul 2017 22:58:19 +0000 Subject: Audit usages of unicode case-changing methods. --- components/script/dom/blob.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'components/script/dom/blob.rs') diff --git a/components/script/dom/blob.rs b/components/script/dom/blob.rs index ddf14fc8096..191ea6f84f7 100644 --- a/components/script/dom/blob.rs +++ b/components/script/dom/blob.rs @@ -16,6 +16,7 @@ use ipc_channel::ipc; use net_traits::{CoreResourceMsg, IpcSend}; use net_traits::blob_url_store::{BlobBuf, get_blob_origin}; use net_traits::filemanager_thread::{FileManagerThreadMsg, ReadFileProgress, RelativePos}; +use std::ascii::AsciiExt; use std::mem; use std::ops::Index; use std::path::PathBuf; @@ -381,7 +382,7 @@ impl BlobMethods for Blob { /// see https://github.com/w3c/FileAPI/issues/43 fn normalize_type_string(s: &str) -> String { if is_ascii_printable(s) { - let s_lower = s.to_lowercase(); + let s_lower = s.to_ascii_lowercase(); // match s_lower.parse() as Result { // Ok(_) => s_lower, // Err(_) => "".to_string() -- cgit v1.2.3 From 0e3c54c1911ba2c3bf305ee04f04fcd9bf2fc2fe Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Mon, 25 Sep 2017 23:30:24 +0200 Subject: Rename dom::bindings::js to dom::bindings::root --- components/script/dom/blob.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'components/script/dom/blob.rs') diff --git a/components/script/dom/blob.rs b/components/script/dom/blob.rs index 191ea6f84f7..5bb9109118a 100644 --- a/components/script/dom/blob.rs +++ b/components/script/dom/blob.rs @@ -7,8 +7,8 @@ use dom::bindings::codegen::Bindings::BlobBinding; use dom::bindings::codegen::Bindings::BlobBinding::BlobMethods; use dom::bindings::codegen::UnionTypes::BlobOrString; use dom::bindings::error::{Error, Fallible}; -use dom::bindings::js::{JS, Root}; use dom::bindings::reflector::{DomObject, Reflector, reflect_dom_object}; +use dom::bindings::root::{JS, Root}; use dom::bindings::str::DOMString; use dom::globalscope::GlobalScope; use dom_struct::dom_struct; -- cgit v1.2.3 From 7be32fb2371a14ba61b008a37e79761f66c073c7 Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Mon, 25 Sep 2017 23:56:32 +0200 Subject: Rename JS to Dom --- components/script/dom/blob.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'components/script/dom/blob.rs') diff --git a/components/script/dom/blob.rs b/components/script/dom/blob.rs index 5bb9109118a..835c17c9334 100644 --- a/components/script/dom/blob.rs +++ b/components/script/dom/blob.rs @@ -8,7 +8,7 @@ use dom::bindings::codegen::Bindings::BlobBinding::BlobMethods; use dom::bindings::codegen::UnionTypes::BlobOrString; use dom::bindings::error::{Error, Fallible}; use dom::bindings::reflector::{DomObject, Reflector, reflect_dom_object}; -use dom::bindings::root::{JS, Root}; +use dom::bindings::root::{Dom, Root}; use dom::bindings::str::DOMString; use dom::globalscope::GlobalScope; use dom_struct::dom_struct; @@ -44,7 +44,7 @@ pub enum BlobImpl { /// relative positions of current slicing range, /// IMPORTANT: The depth of tree is only two, i.e. the parent Blob must be /// either File-based or Memory-based - Sliced(JS, RelativePos), + Sliced(Dom, RelativePos), } impl BlobImpl { @@ -101,11 +101,11 @@ impl Blob { let blob_impl = match *parent.blob_impl.borrow() { BlobImpl::File(_) => { // Create new parent node - BlobImpl::Sliced(JS::from_ref(parent), rel_pos) + BlobImpl::Sliced(Dom::from_ref(parent), rel_pos) } BlobImpl::Memory(_) => { // Create new parent node - BlobImpl::Sliced(JS::from_ref(parent), rel_pos) + BlobImpl::Sliced(Dom::from_ref(parent), rel_pos) } BlobImpl::Sliced(ref grandparent, ref old_rel_pos) => { // Adjust the slicing position, using same parent -- cgit v1.2.3 From 577370746e2ce3da7fa25a20b8e1bbeed319df65 Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Tue, 26 Sep 2017 01:32:40 +0200 Subject: Rename DOMRefCell to DomRefCell I don't want to do such a gratuitous rename, but with all the other types now having "Dom" as part of their name, and especially with "DomOnceCell", I feel like the other cell type that we already have should also follow the convention. That argument loses weight though when we realise there is still DOMString and other things. --- components/script/dom/blob.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'components/script/dom/blob.rs') diff --git a/components/script/dom/blob.rs b/components/script/dom/blob.rs index 835c17c9334..7317f9f8d56 100644 --- a/components/script/dom/blob.rs +++ b/components/script/dom/blob.rs @@ -2,7 +2,7 @@ * 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::cell::DOMRefCell; +use dom::bindings::cell::DomRefCell; use dom::bindings::codegen::Bindings::BlobBinding; use dom::bindings::codegen::Bindings::BlobBinding::BlobMethods; use dom::bindings::codegen::UnionTypes::BlobOrString; @@ -27,7 +27,7 @@ use uuid::Uuid; pub struct FileBlob { id: Uuid, name: Option, - cache: DOMRefCell>>, + cache: DomRefCell>>, size: u64, } @@ -59,7 +59,7 @@ impl BlobImpl { BlobImpl::File(FileBlob { id: file_id, name: Some(name), - cache: DOMRefCell::new(None), + cache: DomRefCell::new(None), size: size, }) } @@ -70,7 +70,7 @@ impl BlobImpl { pub struct Blob { reflector_: Reflector, #[ignore_heap_size_of = "No clear owner"] - blob_impl: DOMRefCell, + blob_impl: DomRefCell, /// content-type string type_string: String, } @@ -88,7 +88,7 @@ impl Blob { pub fn new_inherited(blob_impl: BlobImpl, type_string: String) -> Blob { Blob { reflector_: Reflector::new(), - blob_impl: DOMRefCell::new(blob_impl), + blob_impl: DomRefCell::new(blob_impl), // NOTE: Guarding the format correctness here, // https://w3c.github.io/FileAPI/#dfn-type type_string: normalize_type_string(&type_string), @@ -237,7 +237,7 @@ impl Blob { *self.blob_impl.borrow_mut() = BlobImpl::File(FileBlob { id: id.clone(), name: None, - cache: DOMRefCell::new(Some(bytes.to_vec())), + cache: DomRefCell::new(Some(bytes.to_vec())), size: bytes.len() as u64, }); id @@ -262,7 +262,7 @@ impl Blob { *self.blob_impl.borrow_mut() = BlobImpl::File(FileBlob { id: new_id.clone(), name: None, - cache: DOMRefCell::new(None), + cache: DomRefCell::new(None), size: rel_pos.to_abs_range(parent_len as usize).len() as u64, }); -- cgit v1.2.3 From f87c2a8d7616112ca924e30292db2d244cf87eec Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Tue, 26 Sep 2017 01:53:40 +0200 Subject: Rename Root to DomRoot In a later PR, DomRoot will become a type alias of Root>, where Root will be able to handle all the things that need to be rooted that have a stable traceable address that doesn't move for the whole lifetime of the root. Stay tuned. --- components/script/dom/blob.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'components/script/dom/blob.rs') diff --git a/components/script/dom/blob.rs b/components/script/dom/blob.rs index 7317f9f8d56..66dcc5b475c 100644 --- a/components/script/dom/blob.rs +++ b/components/script/dom/blob.rs @@ -8,7 +8,7 @@ use dom::bindings::codegen::Bindings::BlobBinding::BlobMethods; use dom::bindings::codegen::UnionTypes::BlobOrString; use dom::bindings::error::{Error, Fallible}; use dom::bindings::reflector::{DomObject, Reflector, reflect_dom_object}; -use dom::bindings::root::{Dom, Root}; +use dom::bindings::root::{Dom, DomRoot}; use dom::bindings::str::DOMString; use dom::globalscope::GlobalScope; use dom_struct::dom_struct; @@ -79,7 +79,7 @@ impl Blob { #[allow(unrooted_must_root)] pub fn new( global: &GlobalScope, blob_impl: BlobImpl, typeString: String) - -> Root { + -> DomRoot { let boxed_blob = box Blob::new_inherited(blob_impl, typeString); reflect_dom_object(boxed_blob, global, BlobBinding::Wrap) } @@ -97,7 +97,7 @@ impl Blob { #[allow(unrooted_must_root)] fn new_sliced(parent: &Blob, rel_pos: RelativePos, - relative_content_type: DOMString) -> Root { + relative_content_type: DOMString) -> DomRoot { let blob_impl = match *parent.blob_impl.borrow() { BlobImpl::File(_) => { // Create new parent node @@ -120,7 +120,7 @@ impl Blob { pub fn Constructor(global: &GlobalScope, blobParts: Option>, blobPropertyBag: &BlobBinding::BlobPropertyBag) - -> Fallible> { + -> Fallible> { // TODO: accept other blobParts types - ArrayBuffer or ArrayBufferView let bytes: Vec = match blobParts { None => Vec::new(), @@ -369,7 +369,7 @@ impl BlobMethods for Blob { start: Option, end: Option, content_type: Option) - -> Root { + -> DomRoot { let rel_pos = RelativePos::from_opts(start, end); Blob::new_sliced(self, rel_pos, content_type.unwrap_or(DOMString::from(""))) } -- cgit v1.2.3 From aa15dc269f41503d81ad44cd7e85d69e6f4aeac7 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Mon, 16 Oct 2017 14:35:30 +0200 Subject: Remove use of unstable box syntax. http://www.robohornet.org gives a score of 101.36 on master, and 102.68 with this PR. The latter is slightly better, but probably within noise level. So it looks like this PR does not affect DOM performance. This is expected since `Box::new` is defined as: ```rust impl Box { #[inline(always)] pub fn new(x: T) -> Box { box x } } ``` With inlining, it should compile to the same as box syntax. --- components/script/dom/blob.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'components/script/dom/blob.rs') diff --git a/components/script/dom/blob.rs b/components/script/dom/blob.rs index 66dcc5b475c..4b30249d1d5 100644 --- a/components/script/dom/blob.rs +++ b/components/script/dom/blob.rs @@ -80,7 +80,7 @@ impl Blob { pub fn new( global: &GlobalScope, blob_impl: BlobImpl, typeString: String) -> DomRoot { - let boxed_blob = box Blob::new_inherited(blob_impl, typeString); + let boxed_blob = Box::new(Blob::new_inherited(blob_impl, typeString)); reflect_dom_object(boxed_blob, global, BlobBinding::Wrap) } -- cgit v1.2.3 From efc3683cc7ceff0cd8c8528a168a78d42fb1a0e8 Mon Sep 17 00:00:00 2001 From: Matt Brubeck Date: Tue, 17 Oct 2017 09:39:20 -0700 Subject: Fix commonmark Markdown warnings in docs, part 1 Fixes warnings from rust-lang/rust#44229 when `--enable-commonmark` is passed to rustdoc. This is mostly a global find-and-replace for bare URIs on lines by themselves in doc comments. --- components/script/dom/blob.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'components/script/dom/blob.rs') diff --git a/components/script/dom/blob.rs b/components/script/dom/blob.rs index 4b30249d1d5..a436564f4f2 100644 --- a/components/script/dom/blob.rs +++ b/components/script/dom/blob.rs @@ -329,7 +329,7 @@ fn read_file(global: &GlobalScope, id: Uuid) -> Result, ()> { } /// Extract bytes from BlobParts, used by Blob and File constructor -/// https://w3c.github.io/FileAPI/#constructorBlob +/// pub fn blob_parts_to_bytes(blobparts: Vec) -> Result, ()> { let mut ret = vec![]; @@ -376,7 +376,7 @@ impl BlobMethods for Blob { } /// Get the normalized, MIME-parsable type string -/// https://w3c.github.io/FileAPI/#dfn-type +/// /// XXX: We will relax the restriction here, /// since the spec has some problem over this part. /// see https://github.com/w3c/FileAPI/issues/43 -- cgit v1.2.3 From 4506f0d30cbbb02df32e9c16135ef288ad6b7e2e Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 18 Oct 2017 10:42:01 +1100 Subject: Replace all uses of the `heapsize` crate with `malloc_size_of`. Servo currently uses `heapsize`, but Stylo/Gecko use `malloc_size_of`. `malloc_size_of` is better -- it handles various cases that `heapsize` does not -- so this patch changes Servo to use `malloc_size_of`. This patch makes the following changes to the `malloc_size_of` crate. - Adds `MallocSizeOf` trait implementations for numerous types, some built-in (e.g. `VecDeque`), some external and Servo-only (e.g. `string_cache`). - Makes `enclosing_size_of_op` optional, because vanilla jemalloc doesn't support that operation. - For `HashSet`/`HashMap`, falls back to a computed estimate when `enclosing_size_of_op` isn't available. - Adds an extern "C" `malloc_size_of` function that does the actual heap measurement; this is based on the same functions from the `heapsize` crate. This patch makes the following changes elsewhere. - Converts all the uses of `heapsize` to instead use `malloc_size_of`. - Disables the "heapsize"/"heap_size" feature for the external crates that provide it. - Removes the `HeapSizeOf` implementation from `hashglobe`. - Adds `ignore` annotations to a few `Rc`/`Arc`, because `malloc_size_of` doesn't derive those types, unlike `heapsize`. --- components/script/dom/blob.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'components/script/dom/blob.rs') diff --git a/components/script/dom/blob.rs b/components/script/dom/blob.rs index a436564f4f2..394efd4647d 100644 --- a/components/script/dom/blob.rs +++ b/components/script/dom/blob.rs @@ -69,7 +69,7 @@ impl BlobImpl { #[dom_struct] pub struct Blob { reflector_: Reflector, - #[ignore_heap_size_of = "No clear owner"] + #[ignore_malloc_size_of = "No clear owner"] blob_impl: DomRefCell, /// content-type string type_string: String, -- cgit v1.2.3 From 793bebfc0ef5e3dbfbff094ec459198f2bceeaff Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Mon, 6 Nov 2017 14:24:03 +0100 Subject: Upgrade to rustc 1.23.0-nightly (02004ef78 2017-11-08) --- components/script/dom/blob.rs | 1 - 1 file changed, 1 deletion(-) (limited to 'components/script/dom/blob.rs') diff --git a/components/script/dom/blob.rs b/components/script/dom/blob.rs index 394efd4647d..32d923c7bc3 100644 --- a/components/script/dom/blob.rs +++ b/components/script/dom/blob.rs @@ -16,7 +16,6 @@ use ipc_channel::ipc; use net_traits::{CoreResourceMsg, IpcSend}; use net_traits::blob_url_store::{BlobBuf, get_blob_origin}; use net_traits::filemanager_thread::{FileManagerThreadMsg, ReadFileProgress, RelativePos}; -use std::ascii::AsciiExt; use std::mem; use std::ops::Index; use std::path::PathBuf; -- cgit v1.2.3 From 3df9492dcfa05f564ef5bbfd93282253a39c9810 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Tue, 20 Mar 2018 17:08:27 -0500 Subject: Blobs support typed arrays now --- components/script/dom/blob.rs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'components/script/dom/blob.rs') diff --git a/components/script/dom/blob.rs b/components/script/dom/blob.rs index 32d923c7bc3..beb7ed69df3 100644 --- a/components/script/dom/blob.rs +++ b/components/script/dom/blob.rs @@ -5,7 +5,7 @@ use dom::bindings::cell::DomRefCell; use dom::bindings::codegen::Bindings::BlobBinding; use dom::bindings::codegen::Bindings::BlobBinding::BlobMethods; -use dom::bindings::codegen::UnionTypes::BlobOrString; +use dom::bindings::codegen::UnionTypes::ArrayBufferOrArrayBufferViewOrBlobOrString; use dom::bindings::error::{Error, Fallible}; use dom::bindings::reflector::{DomObject, Reflector, reflect_dom_object}; use dom::bindings::root::{Dom, DomRoot}; @@ -43,7 +43,7 @@ pub enum BlobImpl { /// relative positions of current slicing range, /// IMPORTANT: The depth of tree is only two, i.e. the parent Blob must be /// either File-based or Memory-based - Sliced(Dom, RelativePos), + Sliced(Dom, RelativePos) } impl BlobImpl { @@ -117,7 +117,7 @@ impl Blob { // https://w3c.github.io/FileAPI/#constructorBlob pub fn Constructor(global: &GlobalScope, - blobParts: Option>, + blobParts: Option>, blobPropertyBag: &BlobBinding::BlobPropertyBag) -> Fallible> { // TODO: accept other blobParts types - ArrayBuffer or ArrayBufferView @@ -329,18 +329,26 @@ fn read_file(global: &GlobalScope, id: Uuid) -> Result, ()> { /// Extract bytes from BlobParts, used by Blob and File constructor /// -pub fn blob_parts_to_bytes(blobparts: Vec) -> Result, ()> { +#[allow(unsafe_code)] +pub fn blob_parts_to_bytes(mut blobparts: Vec) -> Result, ()> { let mut ret = vec![]; - - for blobpart in &blobparts { + for blobpart in &mut blobparts { match blobpart { - &BlobOrString::String(ref s) => { + &mut ArrayBufferOrArrayBufferViewOrBlobOrString::String(ref s) => { ret.extend(s.as_bytes()); }, - &BlobOrString::Blob(ref b) => { + &mut ArrayBufferOrArrayBufferViewOrBlobOrString::Blob(ref b) => { let bytes = b.get_bytes().unwrap_or(vec![]); ret.extend(bytes); }, + &mut ArrayBufferOrArrayBufferViewOrBlobOrString::ArrayBuffer(ref mut a) => unsafe { + let bytes = a.as_slice(); + ret.extend(bytes); + }, + &mut ArrayBufferOrArrayBufferViewOrBlobOrString::ArrayBufferView(ref mut a) => unsafe { + let bytes = a.as_slice(); + ret.extend(bytes); + } } } -- cgit v1.2.3 From 7d4e2b11e940545eaa74877b75908e1e02f6eeb5 Mon Sep 17 00:00:00 2001 From: Nakul Jindal Date: Mon, 26 Feb 2018 09:07:08 -0800 Subject: Implements profiler for blocked recv --- components/script/dom/blob.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'components/script/dom/blob.rs') diff --git a/components/script/dom/blob.rs b/components/script/dom/blob.rs index beb7ed69df3..d8017f23ffa 100644 --- a/components/script/dom/blob.rs +++ b/components/script/dom/blob.rs @@ -12,10 +12,10 @@ use dom::bindings::root::{Dom, DomRoot}; use dom::bindings::str::DOMString; use dom::globalscope::GlobalScope; use dom_struct::dom_struct; -use ipc_channel::ipc; use net_traits::{CoreResourceMsg, IpcSend}; use net_traits::blob_url_store::{BlobBuf, get_blob_origin}; use net_traits::filemanager_thread::{FileManagerThreadMsg, ReadFileProgress, RelativePos}; +use profile_traits::ipc; use std::mem; use std::ops::Index; use std::path::PathBuf; @@ -200,7 +200,7 @@ impl Blob { BlobImpl::File(ref f) => { if set_valid { let origin = get_blob_origin(&global_url); - let (tx, rx) = ipc::channel().unwrap(); + let (tx, rx) = ipc::channel(self.global().time_profiler_chan().clone()).unwrap(); let msg = FileManagerThreadMsg::ActivateBlobURL(f.id.clone(), tx, origin.clone()); self.send_to_file_manager(msg); @@ -227,7 +227,7 @@ impl Blob { bytes: bytes.to_vec(), }; - let (tx, rx) = ipc::channel().unwrap(); + let (tx, rx) = ipc::channel(self.global().time_profiler_chan().clone()).unwrap(); let msg = FileManagerThreadMsg::PromoteMemory(blob_buf, set_valid, tx, origin.clone()); self.send_to_file_manager(msg); @@ -251,7 +251,7 @@ impl Blob { rel_pos: &RelativePos, parent_len: u64) -> Uuid { let origin = get_blob_origin(&self.global().get_url()); - let (tx, rx) = ipc::channel().unwrap(); + let (tx, rx) = ipc::channel(self.global().time_profiler_chan().clone()).unwrap(); let msg = FileManagerThreadMsg::AddSlicedURLEntry(parent_id.clone(), rel_pos.clone(), tx, origin.clone()); @@ -280,7 +280,7 @@ impl Blob { if let BlobImpl::File(ref f) = *self.blob_impl.borrow() { let origin = get_blob_origin(&self.global().get_url()); - let (tx, rx) = ipc::channel().unwrap(); + let (tx, rx) = ipc::channel(self.global().time_profiler_chan().clone()).unwrap(); let msg = FileManagerThreadMsg::DecRef(f.id.clone(), origin, tx); self.send_to_file_manager(msg); @@ -303,7 +303,7 @@ impl Drop for Blob { fn read_file(global: &GlobalScope, id: Uuid) -> Result, ()> { let resource_threads = global.resource_threads(); - let (chan, recv) = ipc::channel().map_err(|_|())?; + let (chan, recv) = ipc::channel(global.time_profiler_chan().clone()).map_err(|_|())?; let origin = get_blob_origin(&global.get_url()); let check_url_validity = false; let msg = FileManagerThreadMsg::ReadFile(chan, id, check_url_validity, origin); -- cgit v1.2.3 From c37a345dc9f4dda6ea29c42f96f6c7201c42cbac Mon Sep 17 00:00:00 2001 From: chansuke Date: Tue, 18 Sep 2018 23:24:15 +0900 Subject: Format script component --- components/script/dom/blob.rs | 136 ++++++++++++++++++++++++------------------ 1 file changed, 79 insertions(+), 57 deletions(-) (limited to 'components/script/dom/blob.rs') diff --git a/components/script/dom/blob.rs b/components/script/dom/blob.rs index d8017f23ffa..490404bbf32 100644 --- a/components/script/dom/blob.rs +++ b/components/script/dom/blob.rs @@ -30,7 +30,6 @@ pub struct FileBlob { size: u64, } - /// Different backends of Blob #[must_root] #[derive(JSTraceable)] @@ -43,7 +42,7 @@ pub enum BlobImpl { /// relative positions of current slicing range, /// IMPORTANT: The depth of tree is only two, i.e. the parent Blob must be /// either File-based or Memory-based - Sliced(Dom, RelativePos) + Sliced(Dom, RelativePos), } impl BlobImpl { @@ -76,9 +75,7 @@ pub struct Blob { impl Blob { #[allow(unrooted_must_root)] - pub fn new( - global: &GlobalScope, blob_impl: BlobImpl, typeString: String) - -> DomRoot { + pub fn new(global: &GlobalScope, blob_impl: BlobImpl, typeString: String) -> DomRoot { let boxed_blob = Box::new(Blob::new_inherited(blob_impl, typeString)); reflect_dom_object(boxed_blob, global, BlobBinding::Wrap) } @@ -95,41 +92,49 @@ impl Blob { } #[allow(unrooted_must_root)] - fn new_sliced(parent: &Blob, rel_pos: RelativePos, - relative_content_type: DOMString) -> DomRoot { + fn new_sliced( + parent: &Blob, + rel_pos: RelativePos, + relative_content_type: DOMString, + ) -> DomRoot { let blob_impl = match *parent.blob_impl.borrow() { BlobImpl::File(_) => { // Create new parent node BlobImpl::Sliced(Dom::from_ref(parent), rel_pos) - } + }, BlobImpl::Memory(_) => { // Create new parent node BlobImpl::Sliced(Dom::from_ref(parent), rel_pos) - } + }, BlobImpl::Sliced(ref grandparent, ref old_rel_pos) => { // Adjust the slicing position, using same parent BlobImpl::Sliced(grandparent.clone(), old_rel_pos.slice_inner(&rel_pos)) - } + }, }; Blob::new(&parent.global(), blob_impl, relative_content_type.into()) } // https://w3c.github.io/FileAPI/#constructorBlob - pub fn Constructor(global: &GlobalScope, - blobParts: Option>, - blobPropertyBag: &BlobBinding::BlobPropertyBag) - -> Fallible> { + pub fn Constructor( + global: &GlobalScope, + blobParts: Option>, + blobPropertyBag: &BlobBinding::BlobPropertyBag, + ) -> Fallible> { // TODO: accept other blobParts types - ArrayBuffer or ArrayBufferView let bytes: Vec = match blobParts { None => Vec::new(), Some(blobparts) => match blob_parts_to_bytes(blobparts) { Ok(bytes) => bytes, Err(_) => return Err(Error::InvalidCharacter), - } + }, }; - Ok(Blob::new(global, BlobImpl::new_from_bytes(bytes), blobPropertyBag.type_.to_string())) + Ok(Blob::new( + global, + BlobImpl::new_from_bytes(bytes), + blobPropertyBag.type_.to_string(), + )) } /// Get a slice to inner data, this might incur synchronous read and caching @@ -141,7 +146,7 @@ impl Blob { None => { let bytes = read_file(&self.global(), f.id.clone())?; (bytes, true) - } + }, }; // Cache @@ -150,14 +155,12 @@ impl Blob { } Ok(buffer) - } + }, BlobImpl::Memory(ref s) => Ok(s.clone()), - BlobImpl::Sliced(ref parent, ref rel_pos) => { - parent.get_bytes().map(|v| { - let range = rel_pos.to_abs_range(v.len()); - v.index(range).to_vec() - }) - } + BlobImpl::Sliced(ref parent, ref rel_pos) => parent.get_bytes().map(|v| { + let range = rel_pos.to_abs_range(v.len()); + v.index(range).to_vec() + }), } } @@ -171,13 +174,19 @@ impl Blob { pub fn get_blob_url_id(&self) -> Uuid { let opt_sliced_parent = match *self.blob_impl.borrow() { BlobImpl::Sliced(ref parent, ref rel_pos) => { - Some((parent.promote(/* set_valid is */ false), rel_pos.clone(), parent.Size())) - } - _ => None + Some(( + parent.promote(/* set_valid is */ false), + rel_pos.clone(), + parent.Size(), + )) + }, + _ => None, }; match opt_sliced_parent { - Some((parent_id, rel_pos, size)) => self.create_sliced_url_id(&parent_id, &rel_pos, size), + Some((parent_id, rel_pos, size)) => { + self.create_sliced_url_id(&parent_id, &rel_pos, size) + }, None => self.promote(/* set_valid is */ true), } } @@ -196,13 +205,15 @@ impl Blob { debug!("Sliced can't have a sliced parent"); // Return dummy id return Uuid::new_v4(); - } + }, BlobImpl::File(ref f) => { if set_valid { let origin = get_blob_origin(&global_url); - let (tx, rx) = ipc::channel(self.global().time_profiler_chan().clone()).unwrap(); + let (tx, rx) = + ipc::channel(self.global().time_profiler_chan().clone()).unwrap(); - let msg = FileManagerThreadMsg::ActivateBlobURL(f.id.clone(), tx, origin.clone()); + let msg = + FileManagerThreadMsg::ActivateBlobURL(f.id.clone(), tx, origin.clone()); self.send_to_file_manager(msg); match rx.recv().unwrap() { @@ -214,7 +225,7 @@ impl Blob { // no need to activate return f.id.clone(); } - } + }, BlobImpl::Memory(ref mut bytes_in) => mem::swap(bytes_in, &mut bytes), }; @@ -240,21 +251,28 @@ impl Blob { size: bytes.len() as u64, }); id - } + }, // Dummy id Err(_) => Uuid::new_v4(), } } /// Get a FileID representing sliced parent-blob content - fn create_sliced_url_id(&self, parent_id: &Uuid, - rel_pos: &RelativePos, parent_len: u64) -> Uuid { + fn create_sliced_url_id( + &self, + parent_id: &Uuid, + rel_pos: &RelativePos, + parent_len: u64, + ) -> Uuid { let origin = get_blob_origin(&self.global().get_url()); let (tx, rx) = ipc::channel(self.global().time_profiler_chan().clone()).unwrap(); - let msg = FileManagerThreadMsg::AddSlicedURLEntry(parent_id.clone(), - rel_pos.clone(), - tx, origin.clone()); + let msg = FileManagerThreadMsg::AddSlicedURLEntry( + parent_id.clone(), + rel_pos.clone(), + tx, + origin.clone(), + ); self.send_to_file_manager(msg); match rx.recv().expect("File manager thread is down") { Ok(new_id) => { @@ -267,11 +285,11 @@ impl Blob { // Return the indirect id reference new_id - } + }, Err(_) => { // Return dummy id Uuid::new_v4() - } + }, } } @@ -303,7 +321,7 @@ impl Drop for Blob { fn read_file(global: &GlobalScope, id: Uuid) -> Result, ()> { let resource_threads = global.resource_threads(); - let (chan, recv) = ipc::channel(global.time_profiler_chan().clone()).map_err(|_|())?; + let (chan, recv) = ipc::channel(global.time_profiler_chan().clone()).map_err(|_| ())?; let origin = get_blob_origin(&global.get_url()); let check_url_validity = false; let msg = FileManagerThreadMsg::ReadFile(chan, id, check_url_validity, origin); @@ -315,13 +333,13 @@ fn read_file(global: &GlobalScope, id: Uuid) -> Result, ()> { match recv.recv().unwrap() { Ok(ReadFileProgress::Meta(mut blob_buf)) => { bytes.append(&mut blob_buf.bytes); - } + }, Ok(ReadFileProgress::Partial(mut bytes_in)) => { bytes.append(&mut bytes_in); - } + }, Ok(ReadFileProgress::EOF) => { return Ok(bytes); - } + }, Err(_) => return Err(()), } } @@ -330,7 +348,9 @@ fn read_file(global: &GlobalScope, id: Uuid) -> Result, ()> { /// Extract bytes from BlobParts, used by Blob and File constructor /// #[allow(unsafe_code)] -pub fn blob_parts_to_bytes(mut blobparts: Vec) -> Result, ()> { +pub fn blob_parts_to_bytes( + mut blobparts: Vec, +) -> Result, ()> { let mut ret = vec![]; for blobpart in &mut blobparts { match blobpart { @@ -348,7 +368,7 @@ pub fn blob_parts_to_bytes(mut blobparts: Vec unsafe { let bytes = a.as_slice(); ret.extend(bytes); - } + }, } } @@ -359,10 +379,11 @@ impl BlobMethods for Blob { // https://w3c.github.io/FileAPI/#dfn-size fn Size(&self) -> u64 { match *self.blob_impl.borrow() { - BlobImpl::File(ref f) => f.size, - BlobImpl::Memory(ref v) => v.len() as u64, - BlobImpl::Sliced(ref parent, ref rel_pos) => - rel_pos.to_abs_range(parent.Size() as usize).len() as u64, + BlobImpl::File(ref f) => f.size, + BlobImpl::Memory(ref v) => v.len() as u64, + BlobImpl::Sliced(ref parent, ref rel_pos) => { + rel_pos.to_abs_range(parent.Size() as usize).len() as u64 + }, } } @@ -372,11 +393,12 @@ impl BlobMethods for Blob { } // https://w3c.github.io/FileAPI/#slice-method-algo - fn Slice(&self, - start: Option, - end: Option, - content_type: Option) - -> DomRoot { + fn Slice( + &self, + start: Option, + end: Option, + content_type: Option, + ) -> DomRoot { let rel_pos = RelativePos::from_opts(start, end); Blob::new_sliced(self, rel_pos, content_type.unwrap_or(DOMString::from(""))) } @@ -391,8 +413,8 @@ fn normalize_type_string(s: &str) -> String { if is_ascii_printable(s) { let s_lower = s.to_ascii_lowercase(); // match s_lower.parse() as Result { - // Ok(_) => s_lower, - // Err(_) => "".to_string() + // Ok(_) => s_lower, + // Err(_) => "".to_string() s_lower } else { "".to_string() -- cgit v1.2.3 From 45f7199eee82c66637ec68287eafa40a651001c4 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Thu, 1 Nov 2018 23:45:06 +0100 Subject: `cargo fix --edition` --- components/script/dom/blob.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'components/script/dom/blob.rs') diff --git a/components/script/dom/blob.rs b/components/script/dom/blob.rs index 490404bbf32..41a2971b55d 100644 --- a/components/script/dom/blob.rs +++ b/components/script/dom/blob.rs @@ -2,15 +2,15 @@ * 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::cell::DomRefCell; -use dom::bindings::codegen::Bindings::BlobBinding; -use dom::bindings::codegen::Bindings::BlobBinding::BlobMethods; -use dom::bindings::codegen::UnionTypes::ArrayBufferOrArrayBufferViewOrBlobOrString; -use dom::bindings::error::{Error, Fallible}; -use dom::bindings::reflector::{DomObject, Reflector, reflect_dom_object}; -use dom::bindings::root::{Dom, DomRoot}; -use dom::bindings::str::DOMString; -use dom::globalscope::GlobalScope; +use crate::dom::bindings::cell::DomRefCell; +use crate::dom::bindings::codegen::Bindings::BlobBinding; +use crate::dom::bindings::codegen::Bindings::BlobBinding::BlobMethods; +use crate::dom::bindings::codegen::UnionTypes::ArrayBufferOrArrayBufferViewOrBlobOrString; +use crate::dom::bindings::error::{Error, Fallible}; +use crate::dom::bindings::reflector::{DomObject, Reflector, reflect_dom_object}; +use crate::dom::bindings::root::{Dom, DomRoot}; +use crate::dom::bindings::str::DOMString; +use crate::dom::globalscope::GlobalScope; use dom_struct::dom_struct; use net_traits::{CoreResourceMsg, IpcSend}; use net_traits::blob_url_store::{BlobBuf, get_blob_origin}; -- cgit v1.2.3 From 9e92eb205a2a12fe0be883e42cb7f82deebc9031 Mon Sep 17 00:00:00 2001 From: Pyfisch Date: Tue, 6 Nov 2018 20:38:02 +0100 Subject: Reorder imports --- components/script/dom/blob.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'components/script/dom/blob.rs') diff --git a/components/script/dom/blob.rs b/components/script/dom/blob.rs index 41a2971b55d..4842f6b29fa 100644 --- a/components/script/dom/blob.rs +++ b/components/script/dom/blob.rs @@ -7,14 +7,14 @@ use crate::dom::bindings::codegen::Bindings::BlobBinding; use crate::dom::bindings::codegen::Bindings::BlobBinding::BlobMethods; use crate::dom::bindings::codegen::UnionTypes::ArrayBufferOrArrayBufferViewOrBlobOrString; use crate::dom::bindings::error::{Error, Fallible}; -use crate::dom::bindings::reflector::{DomObject, Reflector, reflect_dom_object}; +use crate::dom::bindings::reflector::{reflect_dom_object, DomObject, Reflector}; use crate::dom::bindings::root::{Dom, DomRoot}; use crate::dom::bindings::str::DOMString; use crate::dom::globalscope::GlobalScope; use dom_struct::dom_struct; -use net_traits::{CoreResourceMsg, IpcSend}; -use net_traits::blob_url_store::{BlobBuf, get_blob_origin}; +use net_traits::blob_url_store::{get_blob_origin, BlobBuf}; use net_traits::filemanager_thread::{FileManagerThreadMsg, ReadFileProgress, RelativePos}; +use net_traits::{CoreResourceMsg, IpcSend}; use profile_traits::ipc; use std::mem; use std::ops::Index; -- cgit v1.2.3 From a1a14459c141afc6ac6771b8a6c9ca374537edf2 Mon Sep 17 00:00:00 2001 From: Jan Andre Ikenmeyer Date: Mon, 19 Nov 2018 14:47:12 +0100 Subject: Update MPL license to https (part 3) --- components/script/dom/blob.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'components/script/dom/blob.rs') diff --git a/components/script/dom/blob.rs b/components/script/dom/blob.rs index 4842f6b29fa..d8c3966469a 100644 --- a/components/script/dom/blob.rs +++ b/components/script/dom/blob.rs @@ -1,6 +1,6 @@ /* 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 crate::dom::bindings::cell::DomRefCell; use crate::dom::bindings::codegen::Bindings::BlobBinding; -- cgit v1.2.3 From d9beca6fec8ebd48f0e8d06b06b2c28c8183a253 Mon Sep 17 00:00:00 2001 From: Russell Cousineau Date: Fri, 15 Mar 2019 18:02:55 -0700 Subject: create uuid before sending PromoteMemory message --- components/script/dom/blob.rs | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) (limited to 'components/script/dom/blob.rs') diff --git a/components/script/dom/blob.rs b/components/script/dom/blob.rs index d8c3966469a..b97b703e4bb 100644 --- a/components/script/dom/blob.rs +++ b/components/script/dom/blob.rs @@ -238,23 +238,17 @@ impl Blob { bytes: bytes.to_vec(), }; - let (tx, rx) = ipc::channel(self.global().time_profiler_chan().clone()).unwrap(); - let msg = FileManagerThreadMsg::PromoteMemory(blob_buf, set_valid, tx, origin.clone()); + let id = Uuid::new_v4(); + let msg = FileManagerThreadMsg::PromoteMemory(id, blob_buf, set_valid, origin.clone()); self.send_to_file_manager(msg); - match rx.recv().unwrap() { - Ok(id) => { - *self.blob_impl.borrow_mut() = BlobImpl::File(FileBlob { - id: id.clone(), - name: None, - cache: DomRefCell::new(Some(bytes.to_vec())), - size: bytes.len() as u64, - }); - id - }, - // Dummy id - Err(_) => Uuid::new_v4(), - } + *self.blob_impl.borrow_mut() = BlobImpl::File(FileBlob { + id: id.clone(), + name: None, + cache: DomRefCell::new(Some(bytes.to_vec())), + size: bytes.len() as u64, + }); + id } /// Get a FileID representing sliced parent-blob content -- cgit v1.2.3 From 9e18166550d017224423fc006417fdaa7f1274eb Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Mon, 29 Jul 2019 16:03:29 -0400 Subject: Don't panic if file manager thread is unreachable when cleaning up Blobs. --- components/script/dom/blob.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'components/script/dom/blob.rs') diff --git a/components/script/dom/blob.rs b/components/script/dom/blob.rs index b97b703e4bb..fbf3e5819be 100644 --- a/components/script/dom/blob.rs +++ b/components/script/dom/blob.rs @@ -296,7 +296,7 @@ impl Blob { let msg = FileManagerThreadMsg::DecRef(f.id.clone(), origin, tx); self.send_to_file_manager(msg); - let _ = rx.recv().unwrap(); + let _ = rx.recv(); } } -- cgit v1.2.3 From bea73951db5a758f78842a0056daccba9d89a9c0 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Tue, 12 Nov 2019 22:16:08 +0100 Subject: Use `#![register_tool]` instead of `#![register_attr]` CC https://github.com/rust-lang/rust/issues/66079 --- components/script/dom/blob.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'components/script/dom/blob.rs') diff --git a/components/script/dom/blob.rs b/components/script/dom/blob.rs index fbf3e5819be..db90b2d2bda 100644 --- a/components/script/dom/blob.rs +++ b/components/script/dom/blob.rs @@ -31,7 +31,7 @@ pub struct FileBlob { } /// Different backends of Blob -#[must_root] +#[unrooted_must_root_lint::must_root] #[derive(JSTraceable)] pub enum BlobImpl { /// File-based blob, whose content lives in the net process -- cgit v1.2.3 From 6e8a85482c2068d4dbccb992954271f725570f91 Mon Sep 17 00:00:00 2001 From: Gregory Terzian Date: Sun, 1 Sep 2019 03:18:42 +0800 Subject: re-structure blob, structured serialization --- components/script/dom/blob.rs | 378 ++++++++++++------------------------------ 1 file changed, 102 insertions(+), 276 deletions(-) (limited to 'components/script/dom/blob.rs') diff --git a/components/script/dom/blob.rs b/components/script/dom/blob.rs index db90b2d2bda..2018a12ae88 100644 --- a/components/script/dom/blob.rs +++ b/components/script/dom/blob.rs @@ -2,119 +2,53 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -use crate::dom::bindings::cell::DomRefCell; use crate::dom::bindings::codegen::Bindings::BlobBinding; use crate::dom::bindings::codegen::Bindings::BlobBinding::BlobMethods; use crate::dom::bindings::codegen::UnionTypes::ArrayBufferOrArrayBufferViewOrBlobOrString; use crate::dom::bindings::error::{Error, Fallible}; use crate::dom::bindings::reflector::{reflect_dom_object, DomObject, Reflector}; -use crate::dom::bindings::root::{Dom, DomRoot}; +use crate::dom::bindings::root::DomRoot; +use crate::dom::bindings::serializable::{Serializable, StorageKey}; use crate::dom::bindings::str::DOMString; +use crate::dom::bindings::structuredclone::StructuredDataHolder; use crate::dom::globalscope::GlobalScope; use dom_struct::dom_struct; -use net_traits::blob_url_store::{get_blob_origin, BlobBuf}; -use net_traits::filemanager_thread::{FileManagerThreadMsg, ReadFileProgress, RelativePos}; -use net_traits::{CoreResourceMsg, IpcSend}; -use profile_traits::ipc; -use std::mem; -use std::ops::Index; -use std::path::PathBuf; +use msg::constellation_msg::{BlobId, BlobIndex, PipelineNamespaceId}; +use net_traits::filemanager_thread::RelativePos; +use script_traits::serializable::BlobImpl; +use std::collections::HashMap; +use std::num::NonZeroU32; use uuid::Uuid; -/// File-based blob -#[derive(JSTraceable)] -pub struct FileBlob { - id: Uuid, - name: Option, - cache: DomRefCell>>, - size: u64, -} - -/// Different backends of Blob -#[unrooted_must_root_lint::must_root] -#[derive(JSTraceable)] -pub enum BlobImpl { - /// File-based blob, whose content lives in the net process - File(FileBlob), - /// Memory-based blob, whose content lives in the script process - Memory(Vec), - /// Sliced blob, including parent blob reference and - /// relative positions of current slicing range, - /// IMPORTANT: The depth of tree is only two, i.e. the parent Blob must be - /// either File-based or Memory-based - Sliced(Dom, RelativePos), -} - -impl BlobImpl { - /// Construct memory-backed BlobImpl - #[allow(unrooted_must_root)] - pub fn new_from_bytes(bytes: Vec) -> BlobImpl { - BlobImpl::Memory(bytes) - } - - /// Construct file-backed BlobImpl from File ID - pub fn new_from_file(file_id: Uuid, name: PathBuf, size: u64) -> BlobImpl { - BlobImpl::File(FileBlob { - id: file_id, - name: Some(name), - cache: DomRefCell::new(None), - size: size, - }) - } -} - // https://w3c.github.io/FileAPI/#blob #[dom_struct] pub struct Blob { reflector_: Reflector, - #[ignore_malloc_size_of = "No clear owner"] - blob_impl: DomRefCell, - /// content-type string - type_string: String, + blob_id: BlobId, } impl Blob { - #[allow(unrooted_must_root)] - pub fn new(global: &GlobalScope, blob_impl: BlobImpl, typeString: String) -> DomRoot { - let boxed_blob = Box::new(Blob::new_inherited(blob_impl, typeString)); - reflect_dom_object(boxed_blob, global, BlobBinding::Wrap) + pub fn new(global: &GlobalScope, blob_impl: BlobImpl) -> DomRoot { + let dom_blob = reflect_dom_object( + Box::new(Blob { + reflector_: Reflector::new(), + blob_id: blob_impl.blob_id(), + }), + global, + BlobBinding::Wrap, + ); + global.track_blob(&dom_blob, blob_impl); + dom_blob } #[allow(unrooted_must_root)] - pub fn new_inherited(blob_impl: BlobImpl, type_string: String) -> Blob { + pub fn new_inherited(blob_impl: &BlobImpl) -> Blob { Blob { reflector_: Reflector::new(), - blob_impl: DomRefCell::new(blob_impl), - // NOTE: Guarding the format correctness here, - // https://w3c.github.io/FileAPI/#dfn-type - type_string: normalize_type_string(&type_string), + blob_id: blob_impl.blob_id(), } } - #[allow(unrooted_must_root)] - fn new_sliced( - parent: &Blob, - rel_pos: RelativePos, - relative_content_type: DOMString, - ) -> DomRoot { - let blob_impl = match *parent.blob_impl.borrow() { - BlobImpl::File(_) => { - // Create new parent node - BlobImpl::Sliced(Dom::from_ref(parent), rel_pos) - }, - BlobImpl::Memory(_) => { - // Create new parent node - BlobImpl::Sliced(Dom::from_ref(parent), rel_pos) - }, - BlobImpl::Sliced(ref grandparent, ref old_rel_pos) => { - // Adjust the slicing position, using same parent - BlobImpl::Sliced(grandparent.clone(), old_rel_pos.slice_inner(&rel_pos)) - }, - }; - - Blob::new(&parent.global(), blob_impl, relative_content_type.into()) - } - // https://w3c.github.io/FileAPI/#constructorBlob pub fn Constructor( global: &GlobalScope, @@ -130,212 +64,107 @@ impl Blob { }, }; - Ok(Blob::new( - global, - BlobImpl::new_from_bytes(bytes), - blobPropertyBag.type_.to_string(), - )) + let type_string = normalize_type_string(&blobPropertyBag.type_.to_string()); + let blob_impl = BlobImpl::new_from_bytes(bytes, type_string); + + Ok(Blob::new(global, blob_impl)) } /// Get a slice to inner data, this might incur synchronous read and caching pub fn get_bytes(&self) -> Result, ()> { - match *self.blob_impl.borrow() { - BlobImpl::File(ref f) => { - let (buffer, is_new_buffer) = match *f.cache.borrow() { - Some(ref bytes) => (bytes.clone(), false), - None => { - let bytes = read_file(&self.global(), f.id.clone())?; - (bytes, true) - }, - }; - - // Cache - if is_new_buffer { - *f.cache.borrow_mut() = Some(buffer.clone()); - } - - Ok(buffer) - }, - BlobImpl::Memory(ref s) => Ok(s.clone()), - BlobImpl::Sliced(ref parent, ref rel_pos) => parent.get_bytes().map(|v| { - let range = rel_pos.to_abs_range(v.len()); - v.index(range).to_vec() - }), - } + self.global().get_blob_bytes(&self.blob_id) } /// Get a copy of the type_string pub fn type_string(&self) -> String { - self.type_string.clone() + self.global().get_blob_type_string(&self.blob_id) } /// Get a FileID representing the Blob content, /// used by URL.createObjectURL pub fn get_blob_url_id(&self) -> Uuid { - let opt_sliced_parent = match *self.blob_impl.borrow() { - BlobImpl::Sliced(ref parent, ref rel_pos) => { - Some(( - parent.promote(/* set_valid is */ false), - rel_pos.clone(), - parent.Size(), - )) - }, - _ => None, - }; - - match opt_sliced_parent { - Some((parent_id, rel_pos, size)) => { - self.create_sliced_url_id(&parent_id, &rel_pos, size) - }, - None => self.promote(/* set_valid is */ true), - } + self.global().get_blob_url_id(&self.blob_id) } +} - /// Promote non-Slice blob: - /// 1. Memory-based: The bytes in data slice will be transferred to file manager thread. - /// 2. File-based: If set_valid, then activate the FileID so it can serve as URL - /// Depending on set_valid, the returned FileID can be part of - /// valid or invalid Blob URL. - fn promote(&self, set_valid: bool) -> Uuid { - let mut bytes = vec![]; - let global_url = self.global().get_url(); - - match *self.blob_impl.borrow_mut() { - BlobImpl::Sliced(_, _) => { - debug!("Sliced can't have a sliced parent"); - // Return dummy id - return Uuid::new_v4(); - }, - BlobImpl::File(ref f) => { - if set_valid { - let origin = get_blob_origin(&global_url); - let (tx, rx) = - ipc::channel(self.global().time_profiler_chan().clone()).unwrap(); - - let msg = - FileManagerThreadMsg::ActivateBlobURL(f.id.clone(), tx, origin.clone()); - self.send_to_file_manager(msg); - - match rx.recv().unwrap() { - Ok(_) => return f.id.clone(), - // Return a dummy id on error - Err(_) => return Uuid::new_v4(), - } - } else { - // no need to activate - return f.id.clone(); - } - }, - BlobImpl::Memory(ref mut bytes_in) => mem::swap(bytes_in, &mut bytes), +impl Serializable for Blob { + /// + fn serialize(&self, sc_holder: &mut StructuredDataHolder) -> Result { + let blob_impls = match sc_holder { + StructuredDataHolder::Write { blobs, .. } => blobs, + _ => panic!("Unexpected variant of StructuredDataHolder"), }; - let origin = get_blob_origin(&global_url); + let blob_id = self.blob_id.clone(); - let blob_buf = BlobBuf { - filename: None, - type_string: self.type_string.clone(), - size: bytes.len() as u64, - bytes: bytes.to_vec(), - }; - - let id = Uuid::new_v4(); - let msg = FileManagerThreadMsg::PromoteMemory(id, blob_buf, set_valid, origin.clone()); - self.send_to_file_manager(msg); - - *self.blob_impl.borrow_mut() = BlobImpl::File(FileBlob { - id: id.clone(), - name: None, - cache: DomRefCell::new(Some(bytes.to_vec())), - size: bytes.len() as u64, - }); - id - } + // 1. Get a clone of the blob impl. + let blob_impl = self.global().serialize_blob(&blob_id); - /// Get a FileID representing sliced parent-blob content - fn create_sliced_url_id( - &self, - parent_id: &Uuid, - rel_pos: &RelativePos, - parent_len: u64, - ) -> Uuid { - let origin = get_blob_origin(&self.global().get_url()); - - let (tx, rx) = ipc::channel(self.global().time_profiler_chan().clone()).unwrap(); - let msg = FileManagerThreadMsg::AddSlicedURLEntry( - parent_id.clone(), - rel_pos.clone(), - tx, - origin.clone(), - ); - self.send_to_file_manager(msg); - match rx.recv().expect("File manager thread is down") { - Ok(new_id) => { - *self.blob_impl.borrow_mut() = BlobImpl::File(FileBlob { - id: new_id.clone(), - name: None, - cache: DomRefCell::new(None), - size: rel_pos.to_abs_range(parent_len as usize).len() as u64, - }); - - // Return the indirect id reference - new_id - }, - Err(_) => { - // Return dummy id - Uuid::new_v4() - }, - } - } + // We clone the data, but the clone gets its own Id. + let new_blob_id = blob_impl.blob_id(); - /// Cleanups at the time of destruction/closing - fn clean_up_file_resource(&self) { - if let BlobImpl::File(ref f) = *self.blob_impl.borrow() { - let origin = get_blob_origin(&self.global().get_url()); + // 2. Store the object at a given key. + let blobs = blob_impls.get_or_insert_with(|| HashMap::new()); + blobs.insert(new_blob_id.clone(), blob_impl); - let (tx, rx) = ipc::channel(self.global().time_profiler_chan().clone()).unwrap(); + let PipelineNamespaceId(name_space) = new_blob_id.namespace_id; + let BlobIndex(index) = new_blob_id.index; + let index = index.get(); - let msg = FileManagerThreadMsg::DecRef(f.id.clone(), origin, tx); - self.send_to_file_manager(msg); - let _ = rx.recv(); - } - } + let name_space = name_space.to_ne_bytes(); + let index = index.to_ne_bytes(); - fn send_to_file_manager(&self, msg: FileManagerThreadMsg) { - let global = self.global(); - let resource_threads = global.resource_threads(); - let _ = resource_threads.send(CoreResourceMsg::ToFileManager(msg)); - } -} + let storage_key = StorageKey { + index: u32::from_ne_bytes(index), + name_space: u32::from_ne_bytes(name_space), + }; -impl Drop for Blob { - fn drop(&mut self) { - self.clean_up_file_resource(); - } -} + // 3. Return the storage key. + Ok(storage_key) + } + + /// + fn deserialize( + owner: &DomRoot, + sc_holder: &mut StructuredDataHolder, + storage_key: StorageKey, + ) -> Result<(), ()> { + // 1. Re-build the key for the storage location + // of the serialized object. + let namespace_id = PipelineNamespaceId(storage_key.name_space.clone()); + let index = BlobIndex( + NonZeroU32::new(storage_key.index.clone()).expect("Deserialized blob index is zero"), + ); -fn read_file(global: &GlobalScope, id: Uuid) -> Result, ()> { - let resource_threads = global.resource_threads(); - let (chan, recv) = ipc::channel(global.time_profiler_chan().clone()).map_err(|_| ())?; - let origin = get_blob_origin(&global.get_url()); - let check_url_validity = false; - let msg = FileManagerThreadMsg::ReadFile(chan, id, check_url_validity, origin); - let _ = resource_threads.send(CoreResourceMsg::ToFileManager(msg)); + let id = BlobId { + namespace_id, + index, + }; - let mut bytes = vec![]; + let (blobs, blob_impls) = match sc_holder { + StructuredDataHolder::Read { + blobs, blob_impls, .. + } => (blobs, blob_impls), + _ => panic!("Unexpected variant of StructuredDataHolder"), + }; - loop { - match recv.recv().unwrap() { - Ok(ReadFileProgress::Meta(mut blob_buf)) => { - bytes.append(&mut blob_buf.bytes); - }, - Ok(ReadFileProgress::Partial(mut bytes_in)) => { - bytes.append(&mut bytes_in); - }, - Ok(ReadFileProgress::EOF) => { - return Ok(bytes); - }, - Err(_) => return Err(()), + // 2. Get the transferred object from its storage, using the key. + let blob_impls_map = blob_impls + .as_mut() + .expect("The SC holder does not have any blob impls"); + let blob_impl = blob_impls_map + .remove(&id) + .expect("No blob to be deserialized found."); + if blob_impls_map.is_empty() { + *blob_impls = None; } + + let deserialized_blob = Blob::new(&**owner, blob_impl); + + let blobs = blobs.get_or_insert_with(|| HashMap::new()); + blobs.insert(storage_key, deserialized_blob); + + Ok(()) } } @@ -372,18 +201,12 @@ pub fn blob_parts_to_bytes( impl BlobMethods for Blob { // https://w3c.github.io/FileAPI/#dfn-size fn Size(&self) -> u64 { - match *self.blob_impl.borrow() { - BlobImpl::File(ref f) => f.size, - BlobImpl::Memory(ref v) => v.len() as u64, - BlobImpl::Sliced(ref parent, ref rel_pos) => { - rel_pos.to_abs_range(parent.Size() as usize).len() as u64 - }, - } + self.global().get_blob_size(&self.blob_id) } // https://w3c.github.io/FileAPI/#dfn-type fn Type(&self) -> DOMString { - DOMString::from(self.type_string.clone()) + DOMString::from(self.type_string()) } // https://w3c.github.io/FileAPI/#slice-method-algo @@ -393,8 +216,11 @@ impl BlobMethods for Blob { end: Option, content_type: Option, ) -> DomRoot { + let type_string = + normalize_type_string(&content_type.unwrap_or(DOMString::from("")).to_string()); let rel_pos = RelativePos::from_opts(start, end); - Blob::new_sliced(self, rel_pos, content_type.unwrap_or(DOMString::from(""))) + let blob_impl = BlobImpl::new_sliced(rel_pos, self.blob_id.clone(), type_string); + Blob::new(&*self.global(), blob_impl) } } @@ -403,7 +229,7 @@ impl BlobMethods for Blob { /// XXX: We will relax the restriction here, /// since the spec has some problem over this part. /// see https://github.com/w3c/FileAPI/issues/43 -fn normalize_type_string(s: &str) -> String { +pub fn normalize_type_string(s: &str) -> String { if is_ascii_printable(s) { let s_lower = s.to_ascii_lowercase(); // match s_lower.parse() as Result { -- cgit v1.2.3 From cd9195056c7a83b44ed439ef607b94ed4824431d Mon Sep 17 00:00:00 2001 From: lberrymage Date: Sat, 21 Dec 2019 12:44:34 -0900 Subject: Add lint check for `&DomRoot` `&DomRoot is strictly less expressive than `&T`, so using it is pointless. --- components/script/dom/blob.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'components/script/dom/blob.rs') diff --git a/components/script/dom/blob.rs b/components/script/dom/blob.rs index 2018a12ae88..86f967764c1 100644 --- a/components/script/dom/blob.rs +++ b/components/script/dom/blob.rs @@ -125,7 +125,7 @@ impl Serializable for Blob { /// fn deserialize( - owner: &DomRoot, + owner: &GlobalScope, sc_holder: &mut StructuredDataHolder, storage_key: StorageKey, ) -> Result<(), ()> { @@ -159,7 +159,7 @@ impl Serializable for Blob { *blob_impls = None; } - let deserialized_blob = Blob::new(&**owner, blob_impl); + let deserialized_blob = Blob::new(&*owner, blob_impl); let blobs = blobs.get_or_insert_with(|| HashMap::new()); blobs.insert(storage_key, deserialized_blob); -- cgit v1.2.3 From f7db4b7f8011239f01c3ba2e5e402c866fbe68fb Mon Sep 17 00:00:00 2001 From: Kunal Mohan Date: Sat, 18 Jan 2020 01:29:26 +0530 Subject: Modify `script` to prevent further violations of snake_case --- components/script/dom/blob.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'components/script/dom/blob.rs') diff --git a/components/script/dom/blob.rs b/components/script/dom/blob.rs index 86f967764c1..f8b29b895fd 100644 --- a/components/script/dom/blob.rs +++ b/components/script/dom/blob.rs @@ -50,6 +50,7 @@ impl Blob { } // https://w3c.github.io/FileAPI/#constructorBlob + #[allow(non_snake_case)] pub fn Constructor( global: &GlobalScope, blobParts: Option>, -- cgit v1.2.3 From 9859410193b07f6bfddcb991765abe3fb47eef0c Mon Sep 17 00:00:00 2001 From: Kunal Mohan Date: Sun, 12 Jan 2020 23:27:59 +0530 Subject: Implement Blob methods (text/arraybuffer) and async file read method --- components/script/dom/blob.rs | 58 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) (limited to 'components/script/dom/blob.rs') diff --git a/components/script/dom/blob.rs b/components/script/dom/blob.rs index f8b29b895fd..bc8799a3d8d 100644 --- a/components/script/dom/blob.rs +++ b/components/script/dom/blob.rs @@ -2,6 +2,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ +use crate::body::{run_array_buffer_data_algorithm, FetchedData}; use crate::dom::bindings::codegen::Bindings::BlobBinding; use crate::dom::bindings::codegen::Bindings::BlobBinding::BlobMethods; use crate::dom::bindings::codegen::UnionTypes::ArrayBufferOrArrayBufferViewOrBlobOrString; @@ -12,12 +13,16 @@ use crate::dom::bindings::serializable::{Serializable, StorageKey}; use crate::dom::bindings::str::DOMString; use crate::dom::bindings::structuredclone::StructuredDataHolder; use crate::dom::globalscope::GlobalScope; +use crate::dom::promise::Promise; +use crate::realms::{AlreadyInRealm, InRealm}; use dom_struct::dom_struct; +use encoding_rs::UTF_8; use msg::constellation_msg::{BlobId, BlobIndex, PipelineNamespaceId}; use net_traits::filemanager_thread::RelativePos; use script_traits::serializable::BlobImpl; use std::collections::HashMap; use std::num::NonZeroU32; +use std::rc::Rc; use uuid::Uuid; // https://w3c.github.io/FileAPI/#blob @@ -223,6 +228,59 @@ impl BlobMethods for Blob { let blob_impl = BlobImpl::new_sliced(rel_pos, self.blob_id.clone(), type_string); Blob::new(&*self.global(), blob_impl) } + + // https://w3c.github.io/FileAPI/#text-method-algo + fn Text(&self) -> Rc { + let global = self.global(); + let in_realm_proof = AlreadyInRealm::assert(&global); + let p = Promise::new_in_current_realm(&global, InRealm::Already(&in_realm_proof)); + let id = self.get_blob_url_id(); + global.read_file_async( + id, + p.clone(), + Box::new(|promise, bytes| match bytes { + Ok(b) => { + let (text, _, _) = UTF_8.decode(&b); + let text = DOMString::from(text); + promise.resolve_native(&text); + }, + Err(e) => { + promise.reject_error(e); + }, + }), + ); + p + } + + // https://w3c.github.io/FileAPI/#arraybuffer-method-algo + fn ArrayBuffer(&self) -> Rc { + let global = self.global(); + let in_realm_proof = AlreadyInRealm::assert(&global); + let p = Promise::new_in_current_realm(&global, InRealm::Already(&in_realm_proof)); + + let id = self.get_blob_url_id(); + + global.read_file_async( + id, + p.clone(), + Box::new(|promise, bytes| { + match bytes { + Ok(b) => { + let cx = promise.global().get_cx(); + let result = run_array_buffer_data_algorithm(cx, b); + + match result { + Ok(FetchedData::ArrayBuffer(a)) => promise.resolve_native(&a), + Err(e) => promise.reject_error(e), + _ => panic!("Unexpected result from run_array_buffer_data_algorithm"), + } + }, + Err(e) => promise.reject_error(e), + }; + }), + ); + p + } } /// Get the normalized, MIME-parsable type string -- cgit v1.2.3 From 3ea6d87bcc37167464e856949a4b9b77d0e9318a Mon Sep 17 00:00:00 2001 From: YUAN LYU Date: Fri, 20 Mar 2020 22:14:18 -0400 Subject: Add trait DomObjectWrap to provide WRAP function --- components/script/dom/blob.rs | 1 - 1 file changed, 1 deletion(-) (limited to 'components/script/dom/blob.rs') diff --git a/components/script/dom/blob.rs b/components/script/dom/blob.rs index bc8799a3d8d..aba1b99ab5e 100644 --- a/components/script/dom/blob.rs +++ b/components/script/dom/blob.rs @@ -40,7 +40,6 @@ impl Blob { blob_id: blob_impl.blob_id(), }), global, - BlobBinding::Wrap, ); global.track_blob(&dom_blob, blob_impl); dom_blob -- cgit v1.2.3 From 9b0b03a432aa00838245e4887524302d3a8fa71c Mon Sep 17 00:00:00 2001 From: Utsav Oza Date: Tue, 19 May 2020 16:32:10 +0530 Subject: Update wpt-tests metadata --- components/script/dom/blob.rs | 1 - 1 file changed, 1 deletion(-) (limited to 'components/script/dom/blob.rs') diff --git a/components/script/dom/blob.rs b/components/script/dom/blob.rs index aba1b99ab5e..46203a031b5 100644 --- a/components/script/dom/blob.rs +++ b/components/script/dom/blob.rs @@ -60,7 +60,6 @@ impl Blob { blobParts: Option>, blobPropertyBag: &BlobBinding::BlobPropertyBag, ) -> Fallible> { - // TODO: accept other blobParts types - ArrayBuffer or ArrayBufferView let bytes: Vec = match blobParts { None => Vec::new(), Some(blobparts) => match blob_parts_to_bytes(blobparts) { -- cgit v1.2.3 From bd5796c90b8e8e066a32e7da9cfa5251d1559046 Mon Sep 17 00:00:00 2001 From: Gregory Terzian Date: Sat, 29 Feb 2020 11:59:10 +0800 Subject: integrate readablestream with fetch and blob --- components/script/dom/blob.rs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'components/script/dom/blob.rs') diff --git a/components/script/dom/blob.rs b/components/script/dom/blob.rs index 46203a031b5..44011df9494 100644 --- a/components/script/dom/blob.rs +++ b/components/script/dom/blob.rs @@ -14,14 +14,18 @@ use crate::dom::bindings::str::DOMString; use crate::dom::bindings::structuredclone::StructuredDataHolder; use crate::dom::globalscope::GlobalScope; use crate::dom::promise::Promise; +use crate::dom::readablestream::ReadableStream; use crate::realms::{AlreadyInRealm, InRealm}; +use crate::script_runtime::JSContext; use dom_struct::dom_struct; use encoding_rs::UTF_8; +use js::jsapi::JSObject; use msg::constellation_msg::{BlobId, BlobIndex, PipelineNamespaceId}; use net_traits::filemanager_thread::RelativePos; use script_traits::serializable::BlobImpl; use std::collections::HashMap; use std::num::NonZeroU32; +use std::ptr::NonNull; use std::rc::Rc; use uuid::Uuid; @@ -34,13 +38,7 @@ pub struct Blob { impl Blob { pub fn new(global: &GlobalScope, blob_impl: BlobImpl) -> DomRoot { - let dom_blob = reflect_dom_object( - Box::new(Blob { - reflector_: Reflector::new(), - blob_id: blob_impl.blob_id(), - }), - global, - ); + let dom_blob = reflect_dom_object(Box::new(Blob::new_inherited(&blob_impl)), global); global.track_blob(&dom_blob, blob_impl); dom_blob } @@ -89,6 +87,11 @@ impl Blob { pub fn get_blob_url_id(&self) -> Uuid { self.global().get_blob_url_id(&self.blob_id) } + + /// + pub fn get_stream(&self) -> DomRoot { + self.global().get_blob_stream(&self.blob_id) + } } impl Serializable for Blob { @@ -213,6 +216,11 @@ impl BlobMethods for Blob { DOMString::from(self.type_string()) } + // + fn Stream(&self, _cx: JSContext) -> NonNull { + self.get_stream().get_js_stream() + } + // https://w3c.github.io/FileAPI/#slice-method-algo fn Slice( &self, -- cgit v1.2.3