aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/blob.rs
diff options
context:
space:
mode:
authorKunal Mohan <kunalmohan99@gmail.com>2020-01-12 23:27:59 +0530
committerKunal Mohan <kunalmohan99@gmail.com>2020-01-28 14:38:32 +0530
commit9859410193b07f6bfddcb991765abe3fb47eef0c (patch)
tree704d17f5b61594b384d70044e59683ffe0e111e5 /components/script/dom/blob.rs
parent1b7223a284cd8f780d2856a50747ffc97beffd23 (diff)
downloadservo-9859410193b07f6bfddcb991765abe3fb47eef0c.tar.gz
servo-9859410193b07f6bfddcb991765abe3fb47eef0c.zip
Implement Blob methods (text/arraybuffer) and async file read method
Diffstat (limited to 'components/script/dom/blob.rs')
-rw-r--r--components/script/dom/blob.rs58
1 files changed, 58 insertions, 0 deletions
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<Promise> {
+ 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<Promise> {
+ 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