mirror of
https://github.com/servo/servo.git
synced 2025-07-22 23:03:42 +01:00
Refactor FileAPI implementation
This commit is contained in:
parent
9b01a4cc97
commit
aea99e081b
8 changed files with 224 additions and 178 deletions
|
@ -15,17 +15,29 @@ use encoding::all::UTF_8;
|
|||
use encoding::types::{EncoderTrap, Encoding};
|
||||
use ipc_channel::ipc;
|
||||
use net_traits::IpcSend;
|
||||
use net_traits::blob_url_store::BlobURLStoreEntry;
|
||||
use net_traits::blob_url_store::BlobBuf;
|
||||
use net_traits::filemanager_thread::{FileManagerThreadMsg, SelectedFileId, RelativePos};
|
||||
use std::ascii::AsciiExt;
|
||||
use std::cell::Cell;
|
||||
use std::ops::Index;
|
||||
use std::path::PathBuf;
|
||||
|
||||
/// File-based blob
|
||||
#[derive(JSTraceable)]
|
||||
pub struct FileBlob {
|
||||
id: SelectedFileId,
|
||||
name: PathBuf,
|
||||
cache: DOMRefCell<Option<Vec<u8>>>,
|
||||
size: u64,
|
||||
}
|
||||
|
||||
|
||||
/// Blob backend implementation
|
||||
#[must_root]
|
||||
#[derive(JSTraceable)]
|
||||
pub enum BlobImpl {
|
||||
/// File-based blob, including id and possibly cached content
|
||||
File(SelectedFileId, DOMRefCell<Option<Vec<u8>>>),
|
||||
/// File-based blob
|
||||
File(FileBlob),
|
||||
/// Memory-based blob
|
||||
Memory(Vec<u8>),
|
||||
/// Sliced blob, including parent blob and
|
||||
|
@ -42,8 +54,13 @@ impl BlobImpl {
|
|||
}
|
||||
|
||||
/// Construct file-backed BlobImpl from File ID
|
||||
pub fn new_from_file(file_id: SelectedFileId) -> BlobImpl {
|
||||
BlobImpl::File(file_id, DOMRefCell::new(None))
|
||||
pub fn new_from_file(file_id: SelectedFileId, name: PathBuf, size: u64) -> BlobImpl {
|
||||
BlobImpl::File(FileBlob {
|
||||
id: file_id,
|
||||
name: name,
|
||||
cache: DOMRefCell::new(None),
|
||||
size: size,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -79,8 +96,8 @@ impl Blob {
|
|||
relativeContentType: DOMString) -> Root<Blob> {
|
||||
let global = parent.global();
|
||||
let blob_impl = match *parent.blob_impl.borrow() {
|
||||
BlobImpl::File(ref id, _) => {
|
||||
inc_ref_id(global.r(), id.clone());
|
||||
BlobImpl::File(ref f) => {
|
||||
inc_ref_id(global.r(), f.id.clone());
|
||||
|
||||
// Create new parent node
|
||||
BlobImpl::Sliced(JS::from_ref(parent), rel_pos)
|
||||
|
@ -93,8 +110,8 @@ impl Blob {
|
|||
// Adjust the slicing position, using same parent
|
||||
let new_rel_pos = old_rel_pos.slice_inner(&rel_pos);
|
||||
|
||||
if let BlobImpl::File(ref id, _) = *grandparent.blob_impl.borrow() {
|
||||
inc_ref_id(global.r(), id.clone());
|
||||
if let BlobImpl::File(ref f) = *grandparent.blob_impl.borrow() {
|
||||
inc_ref_id(global.r(), f.id.clone());
|
||||
}
|
||||
|
||||
BlobImpl::Sliced(grandparent.clone(), new_rel_pos)
|
||||
|
@ -124,22 +141,22 @@ impl Blob {
|
|||
/// Get a slice to inner data, this might incur synchronous read and caching
|
||||
pub fn get_bytes(&self) -> Result<Vec<u8>, ()> {
|
||||
match *self.blob_impl.borrow() {
|
||||
BlobImpl::File(ref id, ref cached) => {
|
||||
let buffer = match *cached.borrow() {
|
||||
Some(ref s) => Ok(s.clone()),
|
||||
BlobImpl::File(ref f) => {
|
||||
let (buffer, is_new_buffer) = match *f.cache.borrow() {
|
||||
Some(ref bytes) => (bytes.clone(), false),
|
||||
None => {
|
||||
let global = self.global();
|
||||
let s = read_file(global.r(), id.clone())?;
|
||||
Ok(s)
|
||||
let bytes = read_file(global.r(), f.id.clone())?;
|
||||
(bytes, true)
|
||||
}
|
||||
};
|
||||
|
||||
// Cache
|
||||
if let Ok(buf) = buffer.clone() {
|
||||
*cached.borrow_mut() = Some(buf);
|
||||
if is_new_buffer {
|
||||
*f.cache.borrow_mut() = Some(buffer.clone());
|
||||
}
|
||||
|
||||
buffer
|
||||
Ok(buffer)
|
||||
}
|
||||
BlobImpl::Memory(ref s) => Ok(s.clone()),
|
||||
BlobImpl::Sliced(ref parent, ref rel_pos) => {
|
||||
|
@ -155,16 +172,16 @@ impl Blob {
|
|||
/// used by URL.createObjectURL
|
||||
pub fn get_blob_url_id(&self) -> SelectedFileId {
|
||||
match *self.blob_impl.borrow() {
|
||||
BlobImpl::File(ref id, _) => {
|
||||
BlobImpl::File(ref f) => {
|
||||
let global = self.global();
|
||||
let origin = global.r().get_url().origin().unicode_serialization();
|
||||
let filemanager = global.r().resource_threads().sender();
|
||||
let (tx, rx) = ipc::channel().unwrap();
|
||||
|
||||
let _ = filemanager.send(FileManagerThreadMsg::ActivateBlobURL(id.clone(), tx, origin.clone()));
|
||||
let _ = filemanager.send(FileManagerThreadMsg::ActivateBlobURL(f.id.clone(), tx, origin.clone()));
|
||||
|
||||
match rx.recv().unwrap() {
|
||||
Ok(_) => id.clone(),
|
||||
Ok(_) => f.id.clone(),
|
||||
Err(_) => SelectedFileId("".to_string()) // Return a dummy id on error
|
||||
}
|
||||
}
|
||||
|
@ -176,8 +193,8 @@ impl Blob {
|
|||
// Return dummy id
|
||||
SelectedFileId("".to_string())
|
||||
}
|
||||
BlobImpl::File(ref parent_id, _) =>
|
||||
self.create_sliced_url_id(parent_id, rel_pos),
|
||||
BlobImpl::File(ref f) =>
|
||||
self.create_sliced_url_id(&f.id, rel_pos),
|
||||
BlobImpl::Memory(ref bytes) => {
|
||||
let parent_id = parent.promote_to_file(bytes);
|
||||
*self.blob_impl.borrow_mut() = BlobImpl::Sliced(parent.clone(), rel_pos.clone());
|
||||
|
@ -195,14 +212,15 @@ impl Blob {
|
|||
let origin = global.r().get_url().origin().unicode_serialization();
|
||||
let filemanager = global.r().resource_threads().sender();
|
||||
|
||||
let entry = BlobURLStoreEntry {
|
||||
let blob_buf = BlobBuf {
|
||||
filename: None,
|
||||
type_string: self.typeString.clone(),
|
||||
size: self.Size(),
|
||||
bytes: bytes.to_vec(),
|
||||
};
|
||||
|
||||
let (tx, rx) = ipc::channel().unwrap();
|
||||
let _ = filemanager.send(FileManagerThreadMsg::PromoteMemory(entry, tx, origin.clone()));
|
||||
let _ = filemanager.send(FileManagerThreadMsg::PromoteMemory(blob_buf, tx, origin.clone()));
|
||||
|
||||
match rx.recv().unwrap() {
|
||||
Ok(new_id) => SelectedFileId(new_id.0),
|
||||
|
@ -232,14 +250,14 @@ impl Blob {
|
|||
|
||||
/// Cleanups at the time of destruction/closing
|
||||
fn clean_up_file_resource(&self) {
|
||||
if let BlobImpl::File(ref id, _) = *self.blob_impl.borrow() {
|
||||
if let BlobImpl::File(ref f) = *self.blob_impl.borrow() {
|
||||
let global = self.global();
|
||||
let origin = global.r().get_url().origin().unicode_serialization();
|
||||
|
||||
let filemanager = global.r().resource_threads().sender();
|
||||
let (tx, rx) = ipc::channel().unwrap();
|
||||
|
||||
let msg = FileManagerThreadMsg::DecRef(id.clone(), origin, tx);
|
||||
let msg = FileManagerThreadMsg::DecRef(f.id.clone(), origin, tx);
|
||||
let _ = filemanager.send(msg);
|
||||
let _ = rx.recv().unwrap();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue