Skip to content
Permalink
Browse files

Auto merge of #25524 - kunalmohan:24287-BlobAPI, r=<try>

Implement Blob methods (text/arraybuffer)

<!-- Please describe your changes on the following line: -->
#24287 (comment)
#24287 (comment)

r?@jdm

---
<!-- 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
- [X] These changes fix #24287  (GitHub issue number if applicable)

<!-- Either: -->
- [X] There are tests for these changes

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
  • Loading branch information
bors-servo committed Jan 14, 2020
2 parents 85ea8df + bc10ca9 commit 6a4c4716328a300fdfad1d2f1be5e8e887a23061
@@ -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::compartments::{AlreadyInCompartment, InCompartment};
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,18 @@ 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 dom_struct::dom_struct;
use encoding_rs::UTF_8;
use js::jsapi::JSObject;
use js::typedarray::{ArrayBuffer, CreateWith};
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;
use std::rc::Rc;
use uuid::Uuid;

// https://w3c.github.io/FileAPI/#blob
@@ -222,6 +229,42 @@ 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 in_compartment_proof = AlreadyInCompartment::assert(&self.global());
let p = Promise::new_in_current_compartment(
&self.global(),
InCompartment::Already(&in_compartment_proof),
);
let bytes = self.get_bytes().expect("Could not get blob contents");
let (text, _, _) = UTF_8.decode(&bytes);
let text = DOMString::from(text);
p.resolve_native(&text);
p
}

// https://w3c.github.io/FileAPI/#arraybuffer-method-algo
#[allow(unsafe_code)]
fn ArrayBuffer(&self) -> Rc<Promise> {
let in_compartment_proof = AlreadyInCompartment::assert(&self.global());
let p = Promise::new_in_current_compartment(
&self.global(),
InCompartment::Already(&in_compartment_proof),
);
let bytes = self.get_bytes().expect("Could not get blob contents");
let cx = &*self.global().get_cx();
unsafe {
rooted!(in(*cx) let mut array_buffer = ptr::null_mut::<JSObject>());
assert!(
ArrayBuffer::create(*cx, CreateWith::Slice(&bytes), array_buffer.handle_mut())
.is_ok()
);

p.resolve_native(&*array_buffer);
p
}
}
}

/// Get the normalized, MIME-parsable type string
@@ -10,7 +10,7 @@ use crate::dom::bindings::codegen::Bindings::WorkerGlobalScopeBinding::WorkerGlo
use crate::dom::bindings::conversions::{root_from_object, root_from_object_static};
use crate::dom::bindings::error::{report_pending_exception, ErrorInfo};
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::refcounted::Trusted;
use crate::dom::bindings::refcounted::{Trusted, TrustedPromise};
use crate::dom::bindings::reflector::DomObject;
use crate::dom::bindings::root::{DomRoot, MutNullableDom};
use crate::dom::bindings::settings_stack::{entry_global, incumbent_global, AutoEntryScript};
@@ -30,6 +30,7 @@ use crate::dom::messageevent::MessageEvent;
use crate::dom::messageport::MessagePort;
use crate::dom::paintworkletglobalscope::PaintWorkletGlobalScope;
use crate::dom::performance::Performance;
use crate::dom::promise::Promise;
use crate::dom::window::Window;
use crate::dom::workerglobalscope::WorkerGlobalScope;
use crate::dom::workletglobalscope::WorkletGlobalScope;
@@ -68,7 +69,9 @@ use js::rust::{HandleValue, MutableHandleValue};
use js::{JSCLASS_IS_DOMJSCLASS, JSCLASS_IS_GLOBAL};
use msg::constellation_msg::{BlobId, MessagePortId, MessagePortRouterId, PipelineId};
use net_traits::blob_url_store::{get_blob_origin, BlobBuf};
use net_traits::filemanager_thread::{FileManagerThreadMsg, ReadFileProgress, RelativePos};
use net_traits::filemanager_thread::{
FileManagerResult, FileManagerThreadMsg, ReadFileProgress, RelativePos,
};
use net_traits::image_cache::ImageCache;
use net_traits::{CoreResourceMsg, CoreResourceThread, IpcSend, ResourceThreads};
use profile_traits::{ipc as profile_ipc, mem as profile_mem, time as profile_time};
@@ -89,6 +92,7 @@ use std::ops::Index;
use std::rc::Rc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::thread;
use time::{get_time, Timespec};
use uuid::Uuid;

@@ -1241,18 +1245,47 @@ impl GlobalScope {
}

fn read_file(&self, id: Uuid) -> Result<Vec<u8>, ()> {
let recv = self.send_msg(id);
GlobalScope::read_msg(recv)
}

#[allow(dead_code)]
fn read_file_async(&self, id: Uuid, promise: Rc<Promise>) {
let recv = self.send_msg(id);
let trusted_promise = TrustedPromise::new(promise);
let canceller = self.task_canceller(TaskSourceName::FileReading);
let task_source = self.file_reading_task_source();
thread::spawn(move || {
let bytes = GlobalScope::read_msg(recv).unwrap();
let _ = task_source.queue_with_canceller(
task!(resolve_native: move || {
let promise = trusted_promise.root();
promise.resolve_native(&bytes);
}),
&canceller,
);
});
}

fn send_msg(&self, id: Uuid) -> profile_ipc::IpcReceiver<FileManagerResult<ReadFileProgress>> {
let resource_threads = self.resource_threads();
let (chan, recv) =
profile_ipc::channel(self.time_profiler_chan().clone()).map_err(|_| ())?;
let (chan, recv) = profile_ipc::channel(self.time_profiler_chan().clone())
.map_err(|_| ())
.unwrap();
let origin = get_blob_origin(&self.get_url());
let check_url_validity = false;
let msg = FileManagerThreadMsg::ReadFile(chan, id, check_url_validity, origin);
let _ = resource_threads.send(CoreResourceMsg::ToFileManager(msg));
recv
}

fn read_msg(
object: profile_ipc::IpcReceiver<FileManagerResult<ReadFileProgress>>,
) -> Result<Vec<u8>, ()> {
let mut bytes = vec![];

loop {
match recv.recv().unwrap() {
match object.recv().unwrap() {
Ok(ReadFileProgress::Meta(mut blob_buf)) => {
bytes.append(&mut blob_buf.bytes);
},
@@ -16,6 +16,9 @@ interface Blob {
Blob slice(optional [Clamp] long long start,
optional [Clamp] long long end,
optional DOMString contentType);

[NewObject] Promise<DOMString> text();
[NewObject] Promise<ArrayBuffer> arrayBuffer();
};

dictionary BlobPropertyBag {

This file was deleted.

This file was deleted.

0 comments on commit 6a4c471

Please sign in to comment.
You can’t perform that action at this time.