diff options
author | Anthony Ramine <n.oxyde@gmail.com> | 2016-09-06 15:32:09 +0200 |
---|---|---|
committer | Anthony Ramine <n.oxyde@gmail.com> | 2016-09-07 15:48:22 +0200 |
commit | 7a942b1742db82b6425edc2cb0579fa1a3b003ad (patch) | |
tree | 50506ca014fdc1c508750d0173b8486e8e057679 | |
parent | 20b131af172ac07ebc817bc24b3601d27a939646 (diff) | |
download | servo-7a942b1742db82b6425edc2cb0579fa1a3b003ad.tar.gz servo-7a942b1742db82b6425edc2cb0579fa1a3b003ad.zip |
Store Console timers in globals
-rw-r--r-- | components/script/dom/bindings/global.rs | 9 | ||||
-rw-r--r-- | components/script/dom/console.rs | 56 | ||||
-rw-r--r-- | components/script/dom/window.rs | 13 | ||||
-rw-r--r-- | components/script/dom/workerglobalscope.rs | 10 |
4 files changed, 67 insertions, 21 deletions
diff --git a/components/script/dom/bindings/global.rs b/components/script/dom/bindings/global.rs index 54efb11250d..1eed5473a39 100644 --- a/components/script/dom/bindings/global.rs +++ b/components/script/dom/bindings/global.rs @@ -13,6 +13,7 @@ use dom::bindings::conversions::root_from_object; use dom::bindings::error::ErrorInfo; use dom::bindings::js::Root; use dom::bindings::reflector::{Reflectable, Reflector}; +use dom::console::TimerSet; use dom::window::{self, ScriptHelpers}; use dom::workerglobalscope::WorkerGlobalScope; use ipc_channel::ipc::IpcSender; @@ -271,6 +272,14 @@ impl<'a> GlobalRef<'a> { } } + /// Returns the global's timers for the Console API. + pub fn console_timers(&self) -> &TimerSet { + match *self { + GlobalRef::Window(ref window) => window.console_timers(), + GlobalRef::Worker(ref worker) => worker.console_timers(), + } + } + /// Returns a wrapper for runnables to ensure they are cancelled if the global /// is being destroyed. pub fn get_runnable_wrapper(&self) -> RunnableWrapper { diff --git a/components/script/dom/console.rs b/components/script/dom/console.rs index 9382bf936a8..252cf2435b8 100644 --- a/components/script/dom/console.rs +++ b/components/script/dom/console.rs @@ -11,20 +11,19 @@ use dom::bindings::js::Root; use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object}; use dom::bindings::str::DOMString; use std::collections::HashMap; +use std::collections::hash_map::Entry; use time::{Timespec, get_time}; // https://developer.mozilla.org/en-US/docs/Web/API/Console #[dom_struct] pub struct Console { reflector_: Reflector, - timers: DOMRefCell<HashMap<DOMString, u64>>, } impl Console { fn new_inherited() -> Console { Console { reflector_: Reflector::new(), - timers: DOMRefCell::new(HashMap::new()), } } @@ -100,28 +99,20 @@ impl ConsoleMethods for Console { // https://developer.mozilla.org/en-US/docs/Web/API/Console/time fn Time(&self, label: DOMString) { - let mut timers = self.timers.borrow_mut(); - if timers.contains_key(&label) { - // Timer already started - return; - } - if timers.len() >= 10000 { - // Too many timers on page - return; + let global = self.global(); + if let Ok(()) = global.r().console_timers().time(label.clone()) { + let message = DOMString::from(format!("{}: timer started", label)); + println!("{}", message); + self.send_to_devtools(LogLevel::Log, message); } - - timers.insert(label.clone(), timestamp_in_ms(get_time())); - let message = DOMString::from(format!("{}: timer started", label)); - println!("{}", message); - self.send_to_devtools(LogLevel::Log, message); } // https://developer.mozilla.org/en-US/docs/Web/API/Console/timeEnd fn TimeEnd(&self, label: DOMString) { - let mut timers = self.timers.borrow_mut(); - if let Some(start) = timers.remove(&label) { + let global = self.global(); + if let Ok(delta) = global.r().console_timers().time_end(&label) { let message = DOMString::from( - format!("{}: {}ms", label, timestamp_in_ms(get_time()) - start) + format!("{}: {}ms", label, delta) ); println!("{}", message); self.send_to_devtools(LogLevel::Log, message); @@ -143,3 +134,32 @@ fn prepare_message(logLevel: LogLevel, message: DOMString) -> ConsoleMessage { columnNumber: 1, } } + +#[derive(HeapSizeOf, JSTraceable)] +pub struct TimerSet(DOMRefCell<HashMap<DOMString, u64>>); + +impl TimerSet { + pub fn new() -> Self { + TimerSet(DOMRefCell::new(Default::default())) + } + + fn time(&self, label: DOMString) -> Result<(), ()> { + let mut timers = self.0.borrow_mut(); + if timers.len() >= 10000 { + return Err(()); + } + match timers.entry(label) { + Entry::Vacant(entry) => { + entry.insert(timestamp_in_ms(get_time())); + Ok(()) + }, + Entry::Occupied(_) => Err(()), + } + } + + fn time_end(&self, label: &str) -> Result<u64, ()> { + self.0.borrow_mut().remove(label).ok_or(()).map(|start| { + timestamp_in_ms(get_time()) - start + }) + } +} diff --git a/components/script/dom/window.rs b/components/script/dom/window.rs index d1529b44b19..5d57cd679af 100644 --- a/components/script/dom/window.rs +++ b/components/script/dom/window.rs @@ -25,7 +25,7 @@ use dom::bindings::str::DOMString; use dom::bindings::structuredclone::StructuredCloneData; use dom::bindings::utils::{GlobalStaticData, WindowProxyHandler}; use dom::browsingcontext::BrowsingContext; -use dom::console::Console; +use dom::console::{Console, TimerSet}; use dom::crypto::Crypto; use dom::cssstyledeclaration::{CSSModificationAccess, CSSStyleDeclaration}; use dom::document::Document; @@ -276,7 +276,10 @@ pub struct Window { scroll_offsets: DOMRefCell<HashMap<UntrustedNodeAddress, Point2D<f32>>>, /// https://html.spec.whatwg.org/multipage/#in-error-reporting-mode - in_error_reporting_mode: Cell<bool> + in_error_reporting_mode: Cell<bool>, + + /// Timers used by the Console API. + console_timers: TimerSet, } impl Window { @@ -1747,10 +1750,16 @@ impl Window { error_reporter: error_reporter, scroll_offsets: DOMRefCell::new(HashMap::new()), in_error_reporting_mode: Cell::new(false), + console_timers: TimerSet::new(), }; WindowBinding::Wrap(runtime.cx(), win) } + + pub fn console_timers(&self) -> &TimerSet { + &self.console_timers + } + pub fn live_devtools_updates(&self) -> bool { return self.devtools_wants_updates.get(); } diff --git a/components/script/dom/workerglobalscope.rs b/components/script/dom/workerglobalscope.rs index 9f61753ca64..78fea9b50c8 100644 --- a/components/script/dom/workerglobalscope.rs +++ b/components/script/dom/workerglobalscope.rs @@ -11,7 +11,7 @@ use dom::bindings::inheritance::Castable; use dom::bindings::js::{JS, MutNullableHeap, Root}; use dom::bindings::reflector::Reflectable; use dom::bindings::str::DOMString; -use dom::console::Console; +use dom::console::{Console, TimerSet}; use dom::crypto::Crypto; use dom::dedicatedworkerglobalscope::DedicatedWorkerGlobalScope; use dom::eventtarget::EventTarget; @@ -109,6 +109,9 @@ pub struct WorkerGlobalScope { #[ignore_heap_size_of = "Defined in std"] scheduler_chan: IpcSender<TimerEventRequest>, + + /// Timers used by the Console API. + console_timers: TimerSet, } impl WorkerGlobalScope { @@ -140,9 +143,14 @@ impl WorkerGlobalScope { devtools_wants_updates: Cell::new(false), constellation_chan: init.constellation_chan, scheduler_chan: init.scheduler_chan, + console_timers: TimerSet::new(), } } + pub fn console_timers(&self) -> &TimerSet { + &self.console_timers + } + pub fn mem_profiler_chan(&self) -> &mem::ProfilerChan { &self.mem_profiler_chan } |