diff options
author | Gulshan Singh <gulshan@umich.edu> | 2014-05-01 16:23:32 -0400 |
---|---|---|
committer | Gulshan Singh <gulshan@umich.edu> | 2014-05-07 17:30:33 -0400 |
commit | fb0c433b701f19c52cafabc45f9469cbb9706de9 (patch) | |
tree | a45ab4ec0e4f7d96268514ecea8607148a8412e7 | |
parent | 0ab3444af9f0a1c8f7265308c0bf7c20e18df9f5 (diff) | |
download | servo-fb0c433b701f19c52cafabc45f9469cbb9706de9.tar.gz servo-fb0c433b701f19c52cafabc45f9469cbb9706de9.zip |
Add Performance object to Window and implement Performance::Now()
-rw-r--r-- | src/components/script/dom/bindings/codegen/Bindings.conf | 2 | ||||
-rw-r--r-- | src/components/script/dom/performance.rs | 59 | ||||
-rw-r--r-- | src/components/script/dom/performancetiming.rs | 57 | ||||
-rw-r--r-- | src/components/script/dom/webidls/Performance.webidl | 19 | ||||
-rw-r--r-- | src/components/script/dom/webidls/PerformanceTiming.webidl | 32 | ||||
-rw-r--r-- | src/components/script/dom/webidls/Window.webidl | 7 | ||||
-rw-r--r-- | src/components/script/dom/window.rs | 18 | ||||
-rw-r--r-- | src/components/script/script.rs | 2 | ||||
-rw-r--r-- | src/test/content/test_window_performance.html | 28 |
9 files changed, 223 insertions, 1 deletions
diff --git a/src/components/script/dom/bindings/codegen/Bindings.conf b/src/components/script/dom/bindings/codegen/Bindings.conf index c41856ccf86..eb8778432d9 100644 --- a/src/components/script/dom/bindings/codegen/Bindings.conf +++ b/src/components/script/dom/bindings/codegen/Bindings.conf @@ -41,6 +41,8 @@ DOMInterfaces = { 'Navigator': {}, 'Node': {}, 'NodeList': {}, +'Performance': {}, +'PerformanceTiming': {}, 'UIEvent': {}, 'ValidityState': {}, 'Window': { diff --git a/src/components/script/dom/performance.rs b/src/components/script/dom/performance.rs new file mode 100644 index 00000000000..3665c7944de --- /dev/null +++ b/src/components/script/dom/performance.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/. */ + +use dom::bindings::codegen::BindingDeclarations::PerformanceBinding; +use dom::bindings::js::{JS, JSRef, Temporary}; +use dom::bindings::utils::{Reflectable, Reflector, reflect_dom_object}; +use dom::performancetiming::{PerformanceTiming, PerformanceTimingMethods}; +use dom::window::Window; + +use time; + +pub type DOMHighResTimeStamp = f64; + +#[deriving(Encodable)] +pub struct Performance { + pub reflector_: Reflector, + pub timing: JS<PerformanceTiming>, +} + +impl Performance { + fn new_inherited(window: &JSRef<Window>) -> Performance { + Performance { + reflector_: Reflector::new(), + timing: PerformanceTiming::new(window).root().root_ref().unrooted(), + } + } + + pub fn new(window: &JSRef<Window>) -> Temporary<Performance> { + let performance = Performance::new_inherited(window); + reflect_dom_object(~performance, window, PerformanceBinding::Wrap) + } +} + +pub trait PerformanceMethods { + fn Timing(&self) -> Temporary<PerformanceTiming>; + fn Now(&self) -> DOMHighResTimeStamp; +} + +impl<'a> PerformanceMethods for JSRef<'a, Performance> { + fn Timing(&self) -> Temporary<PerformanceTiming> { + Temporary::new(self.timing.clone()) + } + + fn Now(&self) -> DOMHighResTimeStamp { + let navStart = self.timing.root().NavigationStartPrecise() as f64; + (time::precise_time_s() - navStart) as DOMHighResTimeStamp + } +} + +impl Reflectable for Performance { + fn reflector<'a>(&'a self) -> &'a Reflector { + &self.reflector_ + } + + fn mut_reflector<'a>(&'a mut self) -> &'a mut Reflector { + &mut self.reflector_ + } +} diff --git a/src/components/script/dom/performancetiming.rs b/src/components/script/dom/performancetiming.rs new file mode 100644 index 00000000000..7065c5f33b6 --- /dev/null +++ b/src/components/script/dom/performancetiming.rs @@ -0,0 +1,57 @@ +/* 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 dom::bindings::codegen::BindingDeclarations::PerformanceTimingBinding; +use dom::bindings::js::{JSRef, Temporary}; +use dom::bindings::utils::{Reflectable, Reflector, reflect_dom_object}; +use dom::window::Window; + +#[deriving(Encodable)] +pub struct PerformanceTiming { + pub reflector_: Reflector, + pub navigationStart: u64, + pub navigationStartPrecise: f64, +} + +impl PerformanceTiming { + pub fn new_inherited(navStart: u64, navStartPrecise: f64) + -> PerformanceTiming { + PerformanceTiming { + reflector_: Reflector::new(), + navigationStart: navStart, + navigationStartPrecise: navStartPrecise, + } + } + + pub fn new(window: &JSRef<Window>) -> Temporary<PerformanceTiming> { + let timing = PerformanceTiming::new_inherited(window.navigationStart, + window.navigationStartPrecise); + reflect_dom_object(~timing, window, PerformanceTimingBinding::Wrap) + } +} + +pub trait PerformanceTimingMethods { + fn NavigationStart(&self) -> u64; + fn NavigationStartPrecise(&self) -> f64; +} + +impl<'a> PerformanceTimingMethods for JSRef<'a, PerformanceTiming> { + fn NavigationStart(&self) -> u64 { + self.navigationStart + } + + fn NavigationStartPrecise(&self) -> f64 { + self.navigationStartPrecise + } +} + +impl Reflectable for PerformanceTiming { + fn reflector<'a>(&'a self) -> &'a Reflector { + &self.reflector_ + } + + fn mut_reflector<'a>(&'a mut self) -> &'a mut Reflector { + &mut self.reflector_ + } +} diff --git a/src/components/script/dom/webidls/Performance.webidl b/src/components/script/dom/webidls/Performance.webidl new file mode 100644 index 00000000000..ff7e0ee3754 --- /dev/null +++ b/src/components/script/dom/webidls/Performance.webidl @@ -0,0 +1,19 @@ +/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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/. + * + * The origin of this IDL file is + * https://dvcs.w3.org/hg/webperf/raw-file/tip/specs/NavigationTiming/Overview.html#sec-window.performance-attribute + */ + +typedef double DOMHighResTimeStamp; + +interface Performance { + readonly attribute PerformanceTiming timing; + /* readonly attribute PerformanceNavigation navigation; */ +}; + +partial interface Performance { + DOMHighResTimeStamp now(); +}; diff --git a/src/components/script/dom/webidls/PerformanceTiming.webidl b/src/components/script/dom/webidls/PerformanceTiming.webidl new file mode 100644 index 00000000000..c5dfd4502c7 --- /dev/null +++ b/src/components/script/dom/webidls/PerformanceTiming.webidl @@ -0,0 +1,32 @@ +/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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/. + * + * The origin of this IDL file is + * https://dvcs.w3.org/hg/webperf/raw-file/tip/specs/NavigationTiming/Overview.html#sec-navigation-timing-interface + */ + +interface PerformanceTiming { + readonly attribute unsigned long long navigationStart; + /* readonly attribute unsigned long long unloadEventStart; + readonly attribute unsigned long long unloadEventEnd; + readonly attribute unsigned long long redirectStart; + readonly attribute unsigned long long redirectEnd; + readonly attribute unsigned long long fetchStart; + readonly attribute unsigned long long domainLookupStart; + readonly attribute unsigned long long domainLookupEnd; + readonly attribute unsigned long long connectStart; + readonly attribute unsigned long long connectEnd; + readonly attribute unsigned long long secureConnectionStart; + readonly attribute unsigned long long requestStart; + readonly attribute unsigned long long responseStart; + readonly attribute unsigned long long responseEnd; + readonly attribute unsigned long long domLoading; + readonly attribute unsigned long long domInteractive; + readonly attribute unsigned long long domContentLoadedEventStart; + readonly attribute unsigned long long domContentLoadedEventEnd; + readonly attribute unsigned long long domComplete; + readonly attribute unsigned long long loadEventStart; + readonly attribute unsigned long long loadEventEnd; */ +}; diff --git a/src/components/script/dom/webidls/Window.webidl b/src/components/script/dom/webidls/Window.webidl index 1a9a2d60440..772b0f76f82 100644 --- a/src/components/script/dom/webidls/Window.webidl +++ b/src/components/script/dom/webidls/Window.webidl @@ -13,7 +13,7 @@ [Unforgeable] readonly attribute Window window; [Replaceable] readonly attribute Window self; [Unforgeable] readonly attribute Document document; - attribute DOMString name; + attribute DOMString name; /* [PutForwards=href, Unforgeable] */ readonly attribute Location location; /* readonly attribute History history; [Replaceable] readonly attribute BarProp locationbar; @@ -56,6 +56,11 @@ }; +// https://dvcs.w3.org/hg/webperf/raw-file/tip/specs/HighResolutionTime/Overview.html +partial interface Window { + [Replaceable] readonly attribute Performance performance; +}; + // Not part of any spec partial interface Window { // web developer niceties diff --git a/src/components/script/dom/window.rs b/src/components/script/dom/window.rs index de67d08a112..2af5f174430 100644 --- a/src/components/script/dom/window.rs +++ b/src/components/script/dom/window.rs @@ -13,6 +13,7 @@ use dom::eventtarget::{EventTarget, WindowTypeId}; use dom::console::Console; use dom::location::Location; use dom::navigator::Navigator; +use dom::performance::Performance; use layout_interface::{ReflowForDisplay, DocumentDamageLevel}; use script_task::{ExitWindowMsg, FireTimerMsg, Page, ScriptChan}; @@ -32,6 +33,8 @@ use std::hash::{Hash, sip}; use std::io::timer::Timer; use std::rc::Rc; +use time; + use serialize::{Encoder, Encodable}; use url::Url; @@ -71,6 +74,9 @@ pub struct Window { pub compositor: Untraceable<~ScriptListener>, pub browser_context: Option<BrowserContext>, pub page: Rc<Page>, + pub performance: Option<JS<Performance>>, + pub navigationStart: u64, + pub navigationStartPrecise: f64, } impl Window { @@ -131,6 +137,7 @@ pub trait WindowMethods { fn ClearInterval(&mut self, handle: i32); fn Window(&self) -> Temporary<Window>; fn Self(&self) -> Temporary<Window>; + fn Performance(&mut self) -> Temporary<Performance>; } impl<'a> WindowMethods for JSRef<'a, Window> { @@ -248,6 +255,14 @@ impl<'a> WindowMethods for JSRef<'a, Window> { fn Self(&self) -> Temporary<Window> { self.Window() } + + fn Performance(&mut self) -> Temporary<Performance> { + if self.performance.is_none() { + let performance = Performance::new(self); + self.performance.assign(Some(performance)); + } + Temporary::new(self.performance.get_ref().clone()) + } } impl Reflectable for Window { @@ -355,6 +370,9 @@ impl Window { active_timers: ~HashMap::new(), next_timer_handle: 0, browser_context: None, + performance: None, + navigationStart: time::get_time().sec as u64, + navigationStartPrecise: time::precise_time_s(), }; WindowBinding::Wrap(cx, win) diff --git a/src/components/script/script.rs b/src/components/script/script.rs index 158416771cc..5c8ed13d05d 100644 --- a/src/components/script/script.rs +++ b/src/components/script/script.rs @@ -154,6 +154,8 @@ pub mod dom { pub mod node; pub mod nodelist; pub mod processinginstruction; + pub mod performance; + pub mod performancetiming; pub mod uievent; pub mod text; pub mod validitystate; diff --git a/src/test/content/test_window_performance.html b/src/test/content/test_window_performance.html new file mode 100644 index 00000000000..a87e52225ff --- /dev/null +++ b/src/test/content/test_window_performance.html @@ -0,0 +1,28 @@ +<html> +<head> + <title></title> + <script src="harness.js"></script> +</head> +<body> +<script> + is_not(window.performance, undefined); + is_a(window.performance, Performance); + + is_not(window.performance.timing, undefined); + is_a(window.performance.timing, PerformanceTiming); + + gt(window.performance.timing.navigationStart, 0); + + var last = window.performance.now(); + gt(last, 0); + + // Check that window.performance.now() is monotonically increasing + for (var i = 0; i < 100; i++) { + var next = window.performance.now(); + gt(next, last); + last = next; + } + finish(); +</script> +</body> +</html> |