Skip to content

Commit

Permalink
Burn SelectedFileId in fire
Browse files Browse the repository at this point in the history
  • Loading branch information
izgzhen committed Aug 22, 2016
1 parent 87cc453 commit 2527dc0
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 106 deletions.
2 changes: 1 addition & 1 deletion components/net/Cargo.toml
Expand Up @@ -39,7 +39,7 @@ time = "0.1.17"
unicase = "1.4.0"
url = {version = "1.2", features = ["heap_size", "rustc-serialize"]}
util = {path = "../util"}
uuid = {version = "0.3", features = ["v4"]}
uuid = {version = "0.3.1", features = ["v4"]}
websocket = "0.17"

[dependencies.webrender_traits]
Expand Down
3 changes: 1 addition & 2 deletions components/net/blob_loader.rs
Expand Up @@ -12,7 +12,7 @@ use mime::{Mime, Attr};
use mime_classifier::MimeClassifier;
use net_traits::ProgressMsg::{Payload, Done};
use net_traits::blob_url_store::parse_blob_url;
use net_traits::filemanager_thread::{FileManagerThreadMsg, SelectedFileId, ReadFileProgress};
use net_traits::filemanager_thread::{FileManagerThreadMsg, ReadFileProgress};
use net_traits::response::HttpsState;
use net_traits::{LoadConsumer, LoadData, Metadata, NetworkError};
use resource_thread::CancellationListener;
Expand Down Expand Up @@ -40,7 +40,6 @@ fn load_blob<UI: 'static + UIProvider>
cancel_listener: CancellationListener) {
let (chan, recv) = ipc::channel().unwrap();
if let Ok((id, origin, _fragment)) = parse_blob_url(&load_data.url.clone()) {
let id = SelectedFileId(id.simple().to_string());
let check_url_validity = true;
let msg = FileManagerThreadMsg::ReadFile(chan, id, check_url_validity, origin);
let _ = filemanager.handle(msg, Some(cancel_listener));
Expand Down
87 changes: 34 additions & 53 deletions components/net/filemanager_thread.rs
Expand Up @@ -6,8 +6,7 @@ use ipc_channel::ipc::IpcSender;
use mime_guess::guess_mime_type_opt;
use net_traits::blob_url_store::{BlobBuf, BlobURLStoreError};
use net_traits::filemanager_thread::{FileManagerThreadMsg, FileManagerResult, FilterPattern, FileOrigin};
use net_traits::filemanager_thread::{SelectedFile, RelativePos, FileManagerThreadError};
use net_traits::filemanager_thread::{SelectedFileId, ReadFileProgress};
use net_traits::filemanager_thread::{SelectedFile, RelativePos, FileManagerThreadError, ReadFileProgress};
use resource_thread::CancellationListener;
use std::collections::HashMap;
use std::fs::File;
Expand Down Expand Up @@ -157,31 +156,19 @@ impl<UI: 'static + UIProvider> FileManager<UI> {
})
}
FileManagerThreadMsg::DecRef(id, origin, sender) => {
if let Ok(id) = Uuid::parse_str(&id.0) {
spawn_named("dec ref".to_owned(), move || {
let _ = sender.send(store.dec_ref(&id, &origin));
})
} else {
let _ = sender.send(Err(BlobURLStoreError::InvalidFileID));
}
spawn_named("dec ref".to_owned(), move || {
let _ = sender.send(store.dec_ref(&id, &origin));
})
}
FileManagerThreadMsg::RevokeBlobURL(id, origin, sender) => {
if let Ok(id) = Uuid::parse_str(&id.0) {
spawn_named("revoke blob url".to_owned(), move || {
let _ = sender.send(store.set_blob_url_validity(false, &id, &origin));
})
} else {
let _ = sender.send(Err(BlobURLStoreError::InvalidFileID));
}
spawn_named("revoke blob url".to_owned(), move || {
let _ = sender.send(store.set_blob_url_validity(false, &id, &origin));
})
}
FileManagerThreadMsg::ActivateBlobURL(id, sender, origin) => {
if let Ok(id) = Uuid::parse_str(&id.0) {
spawn_named("activate blob url".to_owned(), move || {
let _ = sender.send(store.set_blob_url_validity(true, &id, &origin));
});
} else {
let _ = sender.send(Err(BlobURLStoreError::InvalidFileID));
}
spawn_named("activate blob url".to_owned(), move || {
let _ = sender.send(store.set_blob_url_validity(true, &id, &origin));
});
}
}
}
Expand Down Expand Up @@ -245,31 +232,27 @@ impl <UI: 'static + UIProvider> FileManagerStore<UI> {
}
}

fn add_sliced_url_entry(&self, parent_id: SelectedFileId, rel_pos: RelativePos,
sender: IpcSender<Result<SelectedFileId, BlobURLStoreError>>,
fn add_sliced_url_entry(&self, parent_id: Uuid, rel_pos: RelativePos,
sender: IpcSender<Result<Uuid, BlobURLStoreError>>,
origin_in: FileOrigin) {
if let Ok(parent_id) = Uuid::parse_str(&parent_id.0) {
match self.inc_ref(&parent_id, &origin_in) {
Ok(_) => {
let new_id = Uuid::new_v4();
self.insert(new_id, FileStoreEntry {
origin: origin_in,
file_impl: FileImpl::Sliced(parent_id, rel_pos),
refs: AtomicUsize::new(1),
// Valid here since AddSlicedURLEntry implies URL creation
// from a BlobImpl::Sliced
is_valid_url: AtomicBool::new(true),
});

// We assume that the returned id will be held by BlobImpl::File
let _ = sender.send(Ok(SelectedFileId(new_id.simple().to_string())));
}
Err(e) => {
let _ = sender.send(Err(e));
}
match self.inc_ref(&parent_id, &origin_in) {
Ok(_) => {
let new_id = Uuid::new_v4();
self.insert(new_id, FileStoreEntry {
origin: origin_in,
file_impl: FileImpl::Sliced(parent_id, rel_pos),
refs: AtomicUsize::new(1),
// Valid here since AddSlicedURLEntry implies URL creation
// from a BlobImpl::Sliced
is_valid_url: AtomicBool::new(true),
});

// We assume that the returned id will be held by BlobImpl::File
let _ = sender.send(Ok(new_id));
}
Err(e) => {
let _ = sender.send(Err(e));
}
} else {
let _ = sender.send(Err(BlobURLStoreError::InvalidFileID));
}
}

Expand Down Expand Up @@ -374,7 +357,7 @@ impl <UI: 'static + UIProvider> FileManagerStore<UI> {
};

Ok(SelectedFile {
id: SelectedFileId(id.simple().to_string()),
id: id,
filename: filename_path.to_path_buf(),
modified: modified_epoch,
size: file_size,
Expand Down Expand Up @@ -446,11 +429,9 @@ impl <UI: 'static + UIProvider> FileManagerStore<UI> {

// Convenient wrapper over get_blob_buf
fn try_read_file(&self, sender: IpcSender<FileManagerResult<ReadFileProgress>>,
id: SelectedFileId, check_url_validity: bool, origin_in: FileOrigin,
id: Uuid, check_url_validity: bool, origin_in: FileOrigin,
cancel_listener: Option<CancellationListener>) -> Result<(), BlobURLStoreError> {
let id = try!(Uuid::parse_str(&id.0).map_err(|_| BlobURLStoreError::InvalidFileID));
self.get_blob_buf(sender, &id, &origin_in, RelativePos::full_range(),
check_url_validity, cancel_listener)
self.get_blob_buf(sender, &id, &origin_in, RelativePos::full_range(), check_url_validity, cancel_listener)
}

fn dec_ref(&self, id: &Uuid, origin_in: &FileOrigin) -> Result<(), BlobURLStoreError> {
Expand Down Expand Up @@ -494,7 +475,7 @@ impl <UI: 'static + UIProvider> FileManagerStore<UI> {
}

fn promote_memory(&self, blob_buf: BlobBuf, set_valid: bool,
sender: IpcSender<Result<SelectedFileId, BlobURLStoreError>>, origin: FileOrigin) {
sender: IpcSender<Result<Uuid, BlobURLStoreError>>, origin: FileOrigin) {
match Url::parse(&origin) { // parse to check sanity
Ok(_) => {
let id = Uuid::new_v4();
Expand All @@ -505,7 +486,7 @@ impl <UI: 'static + UIProvider> FileManagerStore<UI> {
is_valid_url: AtomicBool::new(set_valid),
});

let _ = sender.send(Ok(SelectedFileId(id.simple().to_string())));
let _ = sender.send(Ok(id));
}
Err(_) => {
let _ = sender.send(Err(BlobURLStoreError::InvalidOrigin));
Expand Down
2 changes: 1 addition & 1 deletion components/net_traits/Cargo.toml
Expand Up @@ -25,5 +25,5 @@ serde = "0.8"
serde_macros = "0.8"
url = {version = "1.2", features = ["heap_size"]}
websocket = "0.17"
uuid = { version = "0.3", features = ["v4", "serde"] }
uuid = { version = "0.3.1", features = ["v4", "serde"] }
cookie = {version = "0.2.5", features = ["serialize-rustc"]}
20 changes: 8 additions & 12 deletions components/net_traits/filemanager_thread.rs
Expand Up @@ -8,6 +8,7 @@ use num_traits::ToPrimitive;
use std::cmp::{max, min};
use std::ops::Range;
use std::path::PathBuf;
use uuid::Uuid;

// HACK: Not really process-safe now, we should send Origin
// directly instead of this in future, blocked on #11722
Expand Down Expand Up @@ -98,15 +99,10 @@ impl RelativePos {
}
}

// XXX: We should opt to Uuid once it implements `Deserialize` and `Serialize`
/// FileID used in inter-process message
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct SelectedFileId(pub String);

/// Response to file selection request
#[derive(Debug, Deserialize, Serialize)]
pub struct SelectedFile {
pub id: SelectedFileId,
pub id: Uuid,
pub filename: PathBuf,
pub modified: u64,
pub size: u64,
Expand All @@ -128,24 +124,24 @@ pub enum FileManagerThreadMsg {
SelectFiles(Vec<FilterPattern>, IpcSender<FileManagerResult<Vec<SelectedFile>>>, FileOrigin, Option<Vec<String>>),

/// Read FileID-indexed file in chunks, optionally check URL validity based on boolean flag
ReadFile(IpcSender<FileManagerResult<ReadFileProgress>>, SelectedFileId, bool, FileOrigin),
ReadFile(IpcSender<FileManagerResult<ReadFileProgress>>, Uuid, bool, FileOrigin),

/// Add an entry as promoted memory-based blob and send back the associated FileID
/// as part of a valid/invalid Blob URL depending on the boolean flag
PromoteMemory(BlobBuf, bool, IpcSender<Result<SelectedFileId, BlobURLStoreError>>, FileOrigin),
PromoteMemory(BlobBuf, bool, IpcSender<Result<Uuid, BlobURLStoreError>>, FileOrigin),

/// Add a sliced entry pointing to the parent FileID, and send back the associated FileID
/// as part of a valid Blob URL
AddSlicedURLEntry(SelectedFileId, RelativePos, IpcSender<Result<SelectedFileId, BlobURLStoreError>>, FileOrigin),
AddSlicedURLEntry(Uuid, RelativePos, IpcSender<Result<Uuid, BlobURLStoreError>>, FileOrigin),

/// Decrease reference count and send back the acknowledgement
DecRef(SelectedFileId, FileOrigin, IpcSender<Result<(), BlobURLStoreError>>),
DecRef(Uuid, FileOrigin, IpcSender<Result<(), BlobURLStoreError>>),

/// Activate an internal FileID so it becomes valid as part of a Blob URL
ActivateBlobURL(SelectedFileId, IpcSender<Result<(), BlobURLStoreError>>, FileOrigin),
ActivateBlobURL(Uuid, IpcSender<Result<(), BlobURLStoreError>>, FileOrigin),

/// Revoke Blob URL and send back the acknowledgement
RevokeBlobURL(SelectedFileId, FileOrigin, IpcSender<Result<(), BlobURLStoreError>>),
RevokeBlobURL(Uuid, FileOrigin, IpcSender<Result<(), BlobURLStoreError>>),
}

#[derive(Debug, Deserialize, Serialize)]
Expand Down
2 changes: 1 addition & 1 deletion components/script/Cargo.toml
Expand Up @@ -70,7 +70,7 @@ style = {path = "../style"}
time = "0.1.12"
url = {version = "1.2", features = ["heap_size", "query_encoding"]}
util = {path = "../util"}
uuid = {version = "0.3", features = ["v4"]}
uuid = {version = "0.3.1", features = ["v4"]}
websocket = "0.17"
xml5ever = {version = "0.1.2", features = ["unstable"]}

Expand Down
3 changes: 1 addition & 2 deletions components/script/dom/bindings/trace.rs
Expand Up @@ -57,7 +57,7 @@ use js::jsval::JSVal;
use js::rust::Runtime;
use libc;
use msg::constellation_msg::{FrameType, PipelineId, SubpageId, WindowSizeType, ReferrerPolicy};
use net_traits::filemanager_thread::{SelectedFileId, RelativePos};
use net_traits::filemanager_thread::RelativePos;
use net_traits::image::base::{Image, ImageMetadata};
use net_traits::image_cache_thread::{ImageCacheChan, ImageCacheThread};
use net_traits::request::Request;
Expand Down Expand Up @@ -333,7 +333,6 @@ no_jsmanaged_fields!(USVString);
no_jsmanaged_fields!(ReferrerPolicy);
no_jsmanaged_fields!(ResourceThreads);
no_jsmanaged_fields!(SystemTime);
no_jsmanaged_fields!(SelectedFileId);
no_jsmanaged_fields!(RelativePos);
no_jsmanaged_fields!(OpaqueStyleAndLayoutData);
no_jsmanaged_fields!(PathBuf);
Expand Down
27 changes: 12 additions & 15 deletions components/script/dom/blob.rs
Expand Up @@ -15,7 +15,7 @@ use encoding::all::UTF_8;
use encoding::types::{EncoderTrap, Encoding};
use ipc_channel::ipc;
use net_traits::blob_url_store::{BlobBuf, get_blob_origin};
use net_traits::filemanager_thread::{FileManagerThreadMsg, SelectedFileId, RelativePos, ReadFileProgress};
use net_traits::filemanager_thread::{FileManagerThreadMsg, RelativePos, ReadFileProgress};
use net_traits::{CoreResourceMsg, IpcSend};
use std::cell::Cell;
use std::mem;
Expand All @@ -26,7 +26,7 @@ use uuid::Uuid;
/// File-based blob
#[derive(JSTraceable)]
pub struct FileBlob {
id: SelectedFileId,
id: Uuid,
name: Option<PathBuf>,
cache: DOMRefCell<Option<Vec<u8>>>,
size: u64,
Expand Down Expand Up @@ -56,7 +56,7 @@ impl BlobImpl {
}

/// Construct file-backed BlobImpl from File ID
pub fn new_from_file(file_id: SelectedFileId, name: PathBuf, size: u64) -> BlobImpl {
pub fn new_from_file(file_id: Uuid, name: PathBuf, size: u64) -> BlobImpl {
BlobImpl::File(FileBlob {
id: file_id,
name: Some(name),
Expand Down Expand Up @@ -167,7 +167,7 @@ impl Blob {

/// Get a FileID representing the Blob content,
/// used by URL.createObjectURL
pub fn get_blob_url_id(&self) -> SelectedFileId {
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()))
Expand All @@ -186,14 +186,14 @@ impl Blob {
/// 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) -> SelectedFileId {
fn promote(&self, set_valid: bool) -> Uuid {
let mut bytes = vec![];

match *self.blob_impl.borrow_mut() {
BlobImpl::Sliced(_, _) => {
debug!("Sliced can't have a sliced parent");
// Return dummy id
return SelectedFileId(Uuid::new_v4().simple().to_string());
return Uuid::new_v4();
}
BlobImpl::File(ref f) => {
if set_valid {
Expand All @@ -207,7 +207,7 @@ impl Blob {
match rx.recv().unwrap() {
Ok(_) => return f.id.clone(),
// Return a dummy id on error
Err(_) => return SelectedFileId(Uuid::new_v4().simple().to_string())
Err(_) => return Uuid::new_v4(),
}
} else {
// no need to activate
Expand All @@ -233,7 +233,6 @@ impl Blob {

match rx.recv().unwrap() {
Ok(id) => {
let id = SelectedFileId(id.0);
*self.blob_impl.borrow_mut() = BlobImpl::File(FileBlob {
id: id.clone(),
name: None,
Expand All @@ -243,13 +242,13 @@ impl Blob {
id
}
// Dummy id
Err(_) => SelectedFileId(Uuid::new_v4().simple().to_string()),
Err(_) => Uuid::new_v4(),
}
}

/// Get a FileID representing sliced parent-blob content
fn create_sliced_url_id(&self, parent_id: &SelectedFileId,
rel_pos: &RelativePos, parent_len: u64) -> SelectedFileId {
fn create_sliced_url_id(&self, parent_id: &Uuid,
rel_pos: &RelativePos, parent_len: u64) -> Uuid {
let global = self.global();

let origin = get_blob_origin(&global.r().get_url());
Expand All @@ -261,8 +260,6 @@ impl Blob {
self.send_to_file_manager(msg);
match rx.recv().expect("File manager thread is down") {
Ok(new_id) => {
let new_id = SelectedFileId(new_id.0);

*self.blob_impl.borrow_mut() = BlobImpl::File(FileBlob {
id: new_id.clone(),
name: None,
Expand All @@ -275,7 +272,7 @@ impl Blob {
}
Err(_) => {
// Return dummy id
SelectedFileId(Uuid::new_v4().simple().to_string())
Uuid::new_v4()
}
}
}
Expand Down Expand Up @@ -309,7 +306,7 @@ impl Drop for Blob {
}
}

fn read_file(global: GlobalRef, id: SelectedFileId) -> Result<Vec<u8>, ()> {
fn read_file(global: GlobalRef, id: Uuid) -> Result<Vec<u8>, ()> {
let resource_threads = global.resource_threads();
let (chan, recv) = ipc::channel().map_err(|_|())?;
let origin = get_blob_origin(&global.get_url());
Expand Down

0 comments on commit 2527dc0

Please sign in to comment.