servo: Merge #11534 - Add Blob URL store (from izgzhen:add-blob-url-store); r=Manishearth
authorZhen Zhang <izgzhen@gmail.com>
Fri, 03 Jun 2016 13:26:29 -0500
changeset 339003 e58e2c34c6eb801be7d22a8c7af9b979c0f70287
parent 339002 d26acf4dc243fdbbf25ddbbeb66928d92895068f
child 339004 c7c11a5820873ea3a0764ee2a14b5ca2712a2fbb
push id31307
push usergszorc@mozilla.com
push dateSat, 04 Feb 2017 00:59:06 +0000
treeherdermozilla-central@94079d43835f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersManishearth
servo: Merge #11534 - Add Blob URL store (from izgzhen:add-blob-url-store); r=Manishearth Spec: https://w3c.github.io/FileAPI/#BlobURLStore. I finally decide to put the store under `ScriptThread` and interpret the "global object" as the script thread itself. The new APIs will be used during the page loading (if scheme is `blob`) and `URL.createObjectURL/revokeObjectURL`. Related to #11131. <!-- Please describe your changes on the following line: --> --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [x] `./mach build -d` does not report any errors - [x] `./mach test-tidy` does not report any errors - [x] These changes fix part of #10539 <!-- Either: --> - [x] These changes do not require tests because it is new stub code which needs further integrating PRs. <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> Source-Repo: https://github.com/servo/servo Source-Revision: b389ecda67d834de1893c6e7a118c0f0fd713b8c
servo/components/script/blob_url_store.rs
servo/components/script/dom/window.rs
servo/components/script/dom/workerglobalscope.rs
servo/components/script/lib.rs
new file mode 100644
--- /dev/null
+++ b/servo/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));
+    }
+}
--- a/servo/components/script/dom/window.rs
+++ b/servo/components/script/dom/window.rs
@@ -1,13 +1,14 @@
 /* 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/. */
 
 use app_units::Au;
+use blob_url_store::BlobURLStore;
 use devtools_traits::{ScriptToDevtoolsControlMsg, TimelineMarker, TimelineMarkerType, WorkerId};
 use dom::bindings::callback::ExceptionHandling;
 use dom::bindings::cell::DOMRefCell;
 use dom::bindings::codegen::Bindings::DocumentBinding::{DocumentMethods, DocumentReadyState};
 use dom::bindings::codegen::Bindings::EventHandlerBinding::EventHandlerNonNull;
 use dom::bindings::codegen::Bindings::EventHandlerBinding::OnBeforeUnloadEventHandlerNonNull;
 use dom::bindings::codegen::Bindings::EventHandlerBinding::OnErrorEventHandlerNonNull;
 use dom::bindings::codegen::Bindings::FunctionBinding::Function;
@@ -160,16 +161,19 @@ pub struct Window {
     screen: MutNullableHeap<JS<Screen>>,
     session_storage: MutNullableHeap<JS<Storage>>,
     local_storage: MutNullableHeap<JS<Storage>>,
     status: DOMRefCell<DOMString>,
     #[ignore_heap_size_of = "channels are hard"]
     scheduler_chan: IpcSender<TimerEventRequest>,
     timers: OneshotTimers,
 
+    /// Blob URL store
+    blob_url_store: DOMRefCell<BlobURLStore>,
+
     next_worker_id: Cell<WorkerId>,
 
     /// For sending messages to the memory profiler.
     #[ignore_heap_size_of = "channels are hard"]
     mem_profiler_chan: mem::ProfilerChan,
 
     /// For sending messages to the memory profiler.
     #[ignore_heap_size_of = "channels are hard"]
@@ -1576,16 +1580,17 @@ impl Window {
             networking_task_source: network_task_source,
             history_traversal_task_source: history_task_source,
             file_reading_task_source: file_task_source,
             image_cache_chan: image_cache_chan,
             custom_message_chan: custom_message_chan,
             console: Default::default(),
             crypto: Default::default(),
             navigator: Default::default(),
+            blob_url_store: DOMRefCell::new(BlobURLStore::new()),
             image_cache_thread: image_cache_thread,
             mem_profiler_chan: mem_profiler_chan,
             time_profiler_chan: time_profiler_chan,
             devtools_chan: devtools_chan,
             browsing_context: Default::default(),
             performance: Default::default(),
             navigation_start: (current_time.sec * 1000 + current_time.nsec as i64 / 1000000) as u64,
             navigation_start_precise: time::precise_time_ns() as f64,
--- a/servo/components/script/dom/workerglobalscope.rs
+++ b/servo/components/script/dom/workerglobalscope.rs
@@ -1,13 +1,15 @@
 /* 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/. */
 
+use blob_url_store::BlobURLStore;
 use devtools_traits::{DevtoolScriptControlMsg, ScriptToDevtoolsControlMsg, WorkerId, DevtoolsPageInfo};
+use dom::bindings::cell::DOMRefCell;
 use dom::bindings::codegen::Bindings::FunctionBinding::Function;
 use dom::bindings::codegen::Bindings::WorkerGlobalScopeBinding::WorkerGlobalScopeMethods;
 use dom::bindings::error::{Error, ErrorResult, Fallible, report_pending_exception};
 use dom::bindings::global::GlobalRef;
 use dom::bindings::inheritance::Castable;
 use dom::bindings::js::{JS, MutNullableHeap, Root};
 use dom::bindings::reflector::Reflectable;
 use dom::bindings::str::DOMString;
@@ -108,16 +110,19 @@ pub struct WorkerGlobalScope {
     next_worker_id: Cell<WorkerId>,
     #[ignore_heap_size_of = "Defined in std"]
     resource_threads: ResourceThreads,
     location: MutNullableHeap<JS<WorkerLocation>>,
     navigator: MutNullableHeap<JS<WorkerNavigator>>,
     console: MutNullableHeap<JS<Console>>,
     crypto: MutNullableHeap<JS<Crypto>>,
     timers: OneshotTimers,
+    /// Blob URL store
+    blob_url_store: DOMRefCell<BlobURLStore>,
+
     #[ignore_heap_size_of = "Defined in std"]
     mem_profiler_chan: mem::ProfilerChan,
     #[ignore_heap_size_of = "Defined in std"]
     time_profiler_chan: time::ProfilerChan,
     #[ignore_heap_size_of = "Defined in ipc-channel"]
     to_devtools_sender: Option<IpcSender<ScriptToDevtoolsControlMsg>>,
 
     #[ignore_heap_size_of = "Defined in ipc-channel"]
@@ -167,16 +172,17 @@ impl WorkerGlobalScope {
             closing: init.closing,
             runtime: runtime,
             resource_threads: init.resource_threads,
             location: Default::default(),
             navigator: Default::default(),
             console: Default::default(),
             crypto: Default::default(),
             timers: OneshotTimers::new(timer_event_chan, init.scheduler_chan.clone()),
+            blob_url_store: DOMRefCell::new(BlobURLStore::new()),
             mem_profiler_chan: init.mem_profiler_chan,
             time_profiler_chan: init.time_profiler_chan,
             to_devtools_sender: init.to_devtools_sender,
             from_devtools_sender: init.from_devtools_sender,
             from_devtools_receiver: from_devtools_receiver,
             devtools_wants_updates: Cell::new(false),
             constellation_chan: init.constellation_chan,
             scheduler_chan: init.scheduler_chan,
--- a/servo/components/script/lib.rs
+++ b/servo/components/script/lib.rs
@@ -83,16 +83,17 @@ extern crate unicase;
 extern crate url;
 #[macro_use]
 extern crate util;
 extern crate uuid;
 extern crate webrender_traits;
 extern crate websocket;
 extern crate xml5ever;
 
+mod blob_url_store;
 pub mod bluetooth_blacklist;
 pub mod clipboard_provider;
 pub mod cors;
 mod devtools;
 pub mod document_loader;
 #[macro_use]
 pub mod dom;
 pub mod layout_interface;