aboutsummaryrefslogtreecommitdiffstats
path: root/components
diff options
context:
space:
mode:
authorAnthony Ramine <n.oxyde@gmail.com>2016-10-01 03:01:54 +0200
committerAnthony Ramine <n.oxyde@gmail.com>2016-10-06 20:59:11 +0200
commit14a0b8d88c7def8a247603cb8548e50087acdbc9 (patch)
treef5e9b8a93228d39e95750289c01722108dc9c74f /components
parentd7c2da450bb7a4e1b393c1260cd13ec658d408f0 (diff)
downloadservo-14a0b8d88c7def8a247603cb8548e50087acdbc9.tar.gz
servo-14a0b8d88c7def8a247603cb8548e50087acdbc9.zip
Move console timers to GlobalScope
Diffstat (limited to 'components')
-rw-r--r--components/script/dom/bindings/global.rs9
-rw-r--r--components/script/dom/console.rs41
-rw-r--r--components/script/dom/globalscope.rs33
-rw-r--r--components/script/dom/window.rs9
-rw-r--r--components/script/dom/workerglobalscope.rs9
5 files changed, 35 insertions, 66 deletions
diff --git a/components/script/dom/bindings/global.rs b/components/script/dom/bindings/global.rs
index fdfd5edd82a..23925b314a5 100644
--- a/components/script/dom/bindings/global.rs
+++ b/components/script/dom/bindings/global.rs
@@ -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 {
diff --git a/components/script/dom/console.rs b/components/script/dom/console.rs
index ad28c585248..09180ced2c2 100644
--- a/components/script/dom/console.rs
+++ b/components/script/dom/console.rs
@@ -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
- })
- }
-}
diff --git a/components/script/dom/globalscope.rs b/components/script/dom/globalscope.rs
index e5f42c3f9ed..3fd485835e2 100644
--- a/components/script/dom/globalscope.rs
+++ b/components/script/dom/globalscope.rs
@@ -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
}
diff --git a/components/script/dom/window.rs b/components/script/dom/window.rs
index 046246191a1..92f99ec273d 100644
--- a/components/script/dom/window.rs
+++ b/components/script/dom/window.rs
@@ -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.
diff --git a/components/script/dom/workerglobalscope.rs b/components/script/dom/workerglobalscope.rs
index cc0a9dfcbf7..c3656daf8cf 100644
--- a/components/script/dom/workerglobalscope.rs
+++ b/components/script/dom/workerglobalscope.rs
@@ -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
}