aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/blob_url_store.rs
blob: 7f6f34ea40697b9a3adb200d057d175758536e47 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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));
    }
}