mirror of
https://github.com/servo/servo.git
synced 2025-06-19 14:48:59 +01:00
Store Console timers in globals
This commit is contained in:
parent
20b131af17
commit
7a942b1742
4 changed files with 67 additions and 21 deletions
|
@ -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 {
|
||||
|
|
|
@ -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;
|
||||
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);
|
||||
}
|
||||
if timers.len() >= 10000 {
|
||||
// Too many timers on page
|
||||
return;
|
||||
}
|
||||
|
||||
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
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue