diff options
author | Zhen Zhang <izgzhen@gmail.com> | 2016-06-01 13:45:13 +0800 |
---|---|---|
committer | Zhen Zhang <izgzhen@gmail.com> | 2016-06-03 08:17:24 +0800 |
commit | 3d7ed4265256de8e6f1a157be4f950b14d8b91ee (patch) | |
tree | ec70e28d4009b40e0736f2140667359c14d170ff /components/script/blob_url_store.rs | |
parent | 4e277d74e83bf9c3e1d237d1f779753c4c6a3a6e (diff) | |
download | servo-3d7ed4265256de8e6f1a157be4f950b14d8b91ee.tar.gz servo-3d7ed4265256de8e6f1a157be4f950b14d8b91ee.zip |
add Blob URL store
Diffstat (limited to 'components/script/blob_url_store.rs')
-rw-r--r-- | components/script/blob_url_store.rs | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/components/script/blob_url_store.rs b/components/script/blob_url_store.rs new file mode 100644 index 00000000000..7f6f34ea406 --- /dev/null +++ b/components/script/blob_url_store.rs @@ -0,0 +1,59 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * 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/. */ + +#![allow(dead_code)] + +use dom::bindings::js::JS; +use dom::blob::Blob; +use origin::Origin; +use std::collections::HashMap; +use uuid::Uuid; + +#[must_root] +#[derive(JSTraceable, HeapSizeOf)] +struct EntryPair(Origin, JS<Blob>); + +// HACK: to work around the HeapSizeOf of Uuid +#[derive(PartialEq, HeapSizeOf, Eq, Hash, JSTraceable)] +struct BlobUrlId(#[ignore_heap_size_of = "defined in uuid"] Uuid); + +#[must_root] +#[derive(JSTraceable, HeapSizeOf)] +pub struct BlobURLStore { + entries: HashMap<BlobUrlId, EntryPair>, +} + +pub enum BlobURLStoreError { + InvalidKey, + InvalidOrigin, +} + +impl BlobURLStore { + pub fn new() -> BlobURLStore { + BlobURLStore { + entries: HashMap::new(), + } + } + + pub fn request(&self, id: Uuid, origin: &Origin) -> Result<&Blob, BlobURLStoreError> { + match self.entries.get(&BlobUrlId(id)) { + Some(ref pair) => { + if pair.0.same_origin(origin) { + Ok(&pair.1) + } else { + Err(BlobURLStoreError::InvalidOrigin) + } + } + None => Err(BlobURLStoreError::InvalidKey) + } + } + + pub fn add_entry(&mut self, id: Uuid, origin: Origin, blob: &Blob) { + self.entries.insert(BlobUrlId(id), EntryPair(origin, JS::from_ref(blob))); + } + + pub fn delete_entry(&mut self, id: Uuid) { + self.entries.remove(&BlobUrlId(id)); + } +} |