mirror of
https://github.com/servo/servo.git
synced 2025-08-14 09:55:35 +01:00
Integration and improvements of File API backends
1. More complete origin check in FileManagerThreadMsg 2. Add reference counting logic to file manage store and script API 3. Integrate the support of slicing
This commit is contained in:
parent
212aa4437e
commit
14d68968ed
14 changed files with 543 additions and 244 deletions
|
@ -18,6 +18,7 @@ hyper = { version = "0.9.9", features = [ "serde-serialization" ] }
|
|||
image = "0.10"
|
||||
lazy_static = "0.2"
|
||||
log = "0.3.5"
|
||||
num-traits = "0.1.32"
|
||||
serde = "0.7.11"
|
||||
serde_macros = "0.7.11"
|
||||
url = {version = "1.0.0", features = ["heap_size"]}
|
||||
|
|
|
@ -2,37 +2,24 @@
|
|||
* 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 ipc_channel::ipc::IpcSender;
|
||||
use std::str::FromStr;
|
||||
use url::Url;
|
||||
use uuid::Uuid;
|
||||
|
||||
/// Errors returns to BlobURLStoreMsg::Request
|
||||
#[derive(Clone, Serialize, Deserialize)]
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub enum BlobURLStoreError {
|
||||
/// Invalid UUID key
|
||||
InvalidKey,
|
||||
/// Invalid File UUID
|
||||
InvalidFileID,
|
||||
/// Invalid URL origin
|
||||
InvalidOrigin,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub enum BlobURLStoreMsg {
|
||||
/// Add an entry and send back the associated uuid
|
||||
/// XXX: Second field is an unicode-serialized Origin, it is a temporary workaround
|
||||
/// and should not be trusted. See issue https://github.com/servo/servo/issues/11722
|
||||
AddEntry(BlobURLStoreEntry, String, IpcSender<Result<String, BlobURLStoreError>>),
|
||||
/// Delete an entry by uuid
|
||||
DeleteEntry(String),
|
||||
}
|
||||
|
||||
/// Blob URL store entry, a packaged form of Blob DOM object
|
||||
#[derive(Clone, Serialize, Deserialize)]
|
||||
pub struct BlobURLStoreEntry {
|
||||
/// MIME type string
|
||||
pub type_string: String,
|
||||
/// Some filename if the backend of Blob is a file
|
||||
pub filename: Option<String>,
|
||||
/// Size of content in bytes
|
||||
pub size: u64,
|
||||
/// Content of blob
|
||||
|
|
|
@ -2,11 +2,102 @@
|
|||
* 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 blob_url_store::BlobURLStoreMsg;
|
||||
use blob_url_store::{BlobURLStoreEntry, BlobURLStoreError};
|
||||
use ipc_channel::ipc::IpcSender;
|
||||
use num_traits::ToPrimitive;
|
||||
use std::cmp::{max, min};
|
||||
use std::ops::Range;
|
||||
use std::path::PathBuf;
|
||||
use super::{LoadConsumer, LoadData};
|
||||
|
||||
// HACK: We should send Origin directly instead of this in future, blocked on #11722
|
||||
/// File manager store entry's origin
|
||||
pub type FileOrigin = String;
|
||||
|
||||
/// Relative slice positions of a sequence,
|
||||
/// whose semantic should be consistent with (start, end) parameters in
|
||||
/// https://w3c.github.io/FileAPI/#dfn-slice
|
||||
#[derive(Clone, Deserialize, Serialize)]
|
||||
pub struct RelativePos {
|
||||
/// Relative to first byte if non-negative,
|
||||
/// relative to one past last byte if negative,
|
||||
pub start: i64,
|
||||
/// Relative offset from first byte if Some(non-negative),
|
||||
/// relative to one past last byte if Some(negative),
|
||||
/// None if one past last byte
|
||||
pub end: Option<i64>,
|
||||
}
|
||||
|
||||
impl RelativePos {
|
||||
/// Full range from start to end
|
||||
pub fn full_range() -> RelativePos {
|
||||
RelativePos {
|
||||
start: 0,
|
||||
end: Some(0),
|
||||
}
|
||||
}
|
||||
|
||||
/// Instantiate optional slice position parameters
|
||||
pub fn from_opts(start: Option<i64>, end: Option<i64>) -> RelativePos {
|
||||
RelativePos {
|
||||
start: start.unwrap_or(0),
|
||||
end: end,
|
||||
}
|
||||
}
|
||||
|
||||
/// Slice the inner sliced range by repositioning
|
||||
pub fn slice_inner(&self, rel_pos: &RelativePos) -> RelativePos {
|
||||
RelativePos {
|
||||
start: self.start + rel_pos.start,
|
||||
end: match (self.end, rel_pos.end) {
|
||||
(Some(old_end), Some(rel_end)) => Some(old_end + rel_end),
|
||||
(old, None) => old,
|
||||
(None, rel) => rel,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Compute absolute range by giving the total size
|
||||
/// https://w3c.github.io/FileAPI/#slice-method-algo
|
||||
pub fn to_abs_range(&self, size: usize) -> Range<usize> {
|
||||
let size = size as i64;
|
||||
|
||||
let start = {
|
||||
if self.start < 0 {
|
||||
max(size + self.start, 0)
|
||||
} else {
|
||||
min(self.start, size)
|
||||
}
|
||||
};
|
||||
|
||||
let end = match self.end {
|
||||
Some(rel_end) => {
|
||||
if rel_end < 0 {
|
||||
max(size + rel_end, 0)
|
||||
} else {
|
||||
min(rel_end, size)
|
||||
}
|
||||
}
|
||||
None => size,
|
||||
};
|
||||
|
||||
let span: i64 = max(end - start, 0);
|
||||
|
||||
Range {
|
||||
start: start.to_usize().unwrap(),
|
||||
end: (start + span).to_usize().unwrap(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Inverse operation of to_abs_range
|
||||
pub fn from_abs_range(range: Range<usize>, size: usize) -> RelativePos {
|
||||
RelativePos {
|
||||
start: range.start as i64,
|
||||
end: Some(size as i64 - range.end as i64),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct SelectedFileId(pub String);
|
||||
|
||||
|
@ -27,23 +118,29 @@ pub struct FilterPattern(pub String);
|
|||
#[derive(Deserialize, Serialize)]
|
||||
pub enum FileManagerThreadMsg {
|
||||
/// Select a single file, return triple (FileID, FileName, lastModified)
|
||||
SelectFile(Vec<FilterPattern>, IpcSender<FileManagerResult<SelectedFile>>),
|
||||
SelectFile(Vec<FilterPattern>, IpcSender<FileManagerResult<SelectedFile>>, FileOrigin),
|
||||
|
||||
/// Select multiple files, return a vector of triples
|
||||
SelectFiles(Vec<FilterPattern>, IpcSender<FileManagerResult<Vec<SelectedFile>>>),
|
||||
SelectFiles(Vec<FilterPattern>, IpcSender<FileManagerResult<Vec<SelectedFile>>>, FileOrigin),
|
||||
|
||||
/// Read file, return the bytes
|
||||
ReadFile(IpcSender<FileManagerResult<Vec<u8>>>, SelectedFileId),
|
||||
|
||||
/// Delete the FileID entry
|
||||
DeleteFileID(SelectedFileId),
|
||||
|
||||
// Blob URL message
|
||||
BlobURLStoreMsg(BlobURLStoreMsg),
|
||||
ReadFile(IpcSender<FileManagerResult<Vec<u8>>>, SelectedFileId, FileOrigin),
|
||||
|
||||
/// Load resource by Blob URL
|
||||
LoadBlob(LoadData, LoadConsumer),
|
||||
|
||||
/// Add an entry and send back the associated uuid
|
||||
TransferMemory(BlobURLStoreEntry, RelativePos, IpcSender<Result<SelectedFileId, BlobURLStoreError>>, FileOrigin),
|
||||
|
||||
/// Add a sliced entry pointing to the parent id with a relative slicing positing
|
||||
AddSlicedEntry(SelectedFileId, RelativePos, IpcSender<Result<SelectedFileId, BlobURLStoreError>>, FileOrigin),
|
||||
|
||||
/// Decrease reference count
|
||||
DecRef(SelectedFileId, FileOrigin),
|
||||
|
||||
/// Increase reference count
|
||||
IncRef(SelectedFileId, FileOrigin),
|
||||
|
||||
/// Shut down this thread
|
||||
Exit,
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@ extern crate lazy_static;
|
|||
#[macro_use]
|
||||
extern crate log;
|
||||
extern crate msg;
|
||||
extern crate num_traits;
|
||||
extern crate serde;
|
||||
extern crate url;
|
||||
extern crate util;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue