Move console timers to GlobalScope

This commit is contained in:
Anthony Ramine 2016-10-01 03:01:54 +02:00
parent d7c2da450b
commit 14a0b8d88c
5 changed files with 35 additions and 66 deletions

View file

@ -14,7 +14,6 @@ use dom::bindings::error::{ErrorInfo, report_pending_exception};
use dom::bindings::inheritance::Castable;
use dom::bindings::js::Root;
use dom::bindings::reflector::{Reflectable, Reflector};
use dom::console::TimerSet;
use dom::globalscope::GlobalScope;
use dom::window;
use dom::workerglobalscope::WorkerGlobalScope;
@ -278,14 +277,6 @@ 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 {

View file

@ -3,12 +3,8 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use devtools_traits::{ConsoleMessage, LogLevel, ScriptToDevtoolsControlMsg};
use dom::bindings::cell::DOMRefCell;
use dom::bindings::global::GlobalRef;
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
pub struct Console(());
@ -83,7 +79,7 @@ impl Console {
// https://developer.mozilla.org/en-US/docs/Web/API/Console/time
pub fn Time(global: GlobalRef, label: DOMString) {
if let Ok(()) = global.console_timers().time(label.clone()) {
if let Ok(()) = global.as_global_scope().time(label.clone()) {
let message = DOMString::from(format!("{}: timer started", label));
println!("{}", message);
Self::send_to_devtools(global, LogLevel::Log, message);
@ -92,7 +88,7 @@ impl Console {
// https://developer.mozilla.org/en-US/docs/Web/API/Console/timeEnd
pub fn TimeEnd(global: GlobalRef, label: DOMString) {
if let Ok(delta) = global.console_timers().time_end(&label) {
if let Ok(delta) = global.as_global_scope().time_end(&label) {
let message = DOMString::from(
format!("{}: {}ms", label, delta)
);
@ -102,10 +98,6 @@ impl Console {
}
}
fn timestamp_in_ms(time: Timespec) -> u64 {
(time.sec * 1000 + (time.nsec / 1000000) as i64) as u64
}
fn prepare_message(log_level: LogLevel, message: DOMString) -> ConsoleMessage {
// TODO: Sending fake values for filename, lineNumber and columnNumber in LogMessage; adjust later
ConsoleMessage {
@ -116,32 +108,3 @@ fn prepare_message(log_level: 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
})
}
}

View file

@ -3,12 +3,17 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use devtools_traits::WorkerId;
use dom::bindings::cell::DOMRefCell;
use dom::bindings::js::{JS, MutNullableHeap, Root};
use dom::bindings::reflector::Reflectable;
use dom::bindings::str::DOMString;
use dom::crypto::Crypto;
use dom::eventtarget::EventTarget;
use js::jsapi::{JS_GetContext, JS_GetObjectRuntime, JSContext};
use std::cell::Cell;
use std::collections::HashMap;
use std::collections::hash_map::Entry;
use time::{Timespec, get_time};
#[dom_struct]
pub struct GlobalScope {
@ -19,6 +24,9 @@ pub struct GlobalScope {
/// A flag to indicate whether the developer tools has requested
/// live updates from the worker.
devtools_wants_updates: Cell<bool>,
/// Timers used by the Console API.
console_timers: DOMRefCell<HashMap<DOMString, u64>>,
}
impl GlobalScope {
@ -28,6 +36,7 @@ impl GlobalScope {
crypto: Default::default(),
next_worker_id: Cell::new(WorkerId(0)),
devtools_wants_updates: Default::default(),
console_timers: DOMRefCell::new(Default::default()),
}
}
@ -62,4 +71,28 @@ impl GlobalScope {
pub fn set_devtools_wants_updates(&self, value: bool) {
self.devtools_wants_updates.set(value);
}
pub fn time(&self, label: DOMString) -> Result<(), ()> {
let mut timers = self.console_timers.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(()),
}
}
pub fn time_end(&self, label: &str) -> Result<u64, ()> {
self.console_timers.borrow_mut().remove(label).ok_or(()).map(|start| {
timestamp_in_ms(get_time()) - start
})
}
}
fn timestamp_in_ms(time: Timespec) -> u64 {
(time.sec * 1000 + (time.nsec / 1000000) as i64) as u64
}

View file

@ -27,7 +27,6 @@ use dom::bindings::str::DOMString;
use dom::bindings::structuredclone::StructuredCloneData;
use dom::bindings::utils::{GlobalStaticData, WindowProxyHandler};
use dom::browsingcontext::BrowsingContext;
use dom::console::TimerSet;
use dom::crypto::Crypto;
use dom::cssstyledeclaration::{CSSModificationAccess, CSSStyleDeclaration};
use dom::document::Document;
@ -266,9 +265,6 @@ pub struct Window {
/// https://html.spec.whatwg.org/multipage/#in-error-reporting-mode
in_error_reporting_mode: Cell<bool>,
/// Timers used by the Console API.
console_timers: TimerSet,
}
impl Window {
@ -1661,16 +1657,11 @@ 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
}
/// https://html.spec.whatwg.org/multipage/#report-the-error
pub fn report_an_error(&self, error_info: ErrorInfo, value: HandleValue) {
// Step 1.

View file

@ -15,7 +15,6 @@ use dom::bindings::js::{JS, MutNullableHeap, Root};
use dom::bindings::refcounted::Trusted;
use dom::bindings::reflector::Reflectable;
use dom::bindings::str::DOMString;
use dom::console::TimerSet;
use dom::crypto::Crypto;
use dom::dedicatedworkerglobalscope::DedicatedWorkerGlobalScope;
use dom::errorevent::ErrorEvent;
@ -113,9 +112,6 @@ pub struct WorkerGlobalScope {
#[ignore_heap_size_of = "Defined in std"]
scheduler_chan: IpcSender<TimerEventRequest>,
/// Timers used by the Console API.
console_timers: TimerSet,
promise_job_queue: PromiseJobQueue,
/// https://html.spec.whatwg.org/multipage/#in-error-reporting-mode
@ -148,16 +144,11 @@ impl WorkerGlobalScope {
from_devtools_receiver: from_devtools_receiver,
constellation_chan: init.constellation_chan,
scheduler_chan: init.scheduler_chan,
console_timers: TimerSet::new(),
promise_job_queue: PromiseJobQueue::new(),
in_error_reporting_mode: Default::default(),
}
}
pub fn console_timers(&self) -> &TimerSet {
&self.console_timers
}
pub fn mem_profiler_chan(&self) -> &mem::ProfilerChan {
&self.mem_profiler_chan
}