Skip to content

Commit fb0c433

Browse files
committed
Add Performance object to Window and implement Performance::Now()
1 parent 0ab3444 commit fb0c433

File tree

9 files changed

+223
-1
lines changed

9 files changed

+223
-1
lines changed

src/components/script/dom/bindings/codegen/Bindings.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ DOMInterfaces = {
4141
'Navigator': {},
4242
'Node': {},
4343
'NodeList': {},
44+
'Performance': {},
45+
'PerformanceTiming': {},
4446
'UIEvent': {},
4547
'ValidityState': {},
4648
'Window': {
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
use dom::bindings::codegen::BindingDeclarations::PerformanceBinding;
6+
use dom::bindings::js::{JS, JSRef, Temporary};
7+
use dom::bindings::utils::{Reflectable, Reflector, reflect_dom_object};
8+
use dom::performancetiming::{PerformanceTiming, PerformanceTimingMethods};
9+
use dom::window::Window;
10+
11+
use time;
12+
13+
pub type DOMHighResTimeStamp = f64;
14+
15+
#[deriving(Encodable)]
16+
pub struct Performance {
17+
pub reflector_: Reflector,
18+
pub timing: JS<PerformanceTiming>,
19+
}
20+
21+
impl Performance {
22+
fn new_inherited(window: &JSRef<Window>) -> Performance {
23+
Performance {
24+
reflector_: Reflector::new(),
25+
timing: PerformanceTiming::new(window).root().root_ref().unrooted(),
26+
}
27+
}
28+
29+
pub fn new(window: &JSRef<Window>) -> Temporary<Performance> {
30+
let performance = Performance::new_inherited(window);
31+
reflect_dom_object(~performance, window, PerformanceBinding::Wrap)
32+
}
33+
}
34+
35+
pub trait PerformanceMethods {
36+
fn Timing(&self) -> Temporary<PerformanceTiming>;
37+
fn Now(&self) -> DOMHighResTimeStamp;
38+
}
39+
40+
impl<'a> PerformanceMethods for JSRef<'a, Performance> {
41+
fn Timing(&self) -> Temporary<PerformanceTiming> {
42+
Temporary::new(self.timing.clone())
43+
}
44+
45+
fn Now(&self) -> DOMHighResTimeStamp {
46+
let navStart = self.timing.root().NavigationStartPrecise() as f64;
47+
(time::precise_time_s() - navStart) as DOMHighResTimeStamp
48+
}
49+
}
50+
51+
impl Reflectable for Performance {
52+
fn reflector<'a>(&'a self) -> &'a Reflector {
53+
&self.reflector_
54+
}
55+
56+
fn mut_reflector<'a>(&'a mut self) -> &'a mut Reflector {
57+
&mut self.reflector_
58+
}
59+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
use dom::bindings::codegen::BindingDeclarations::PerformanceTimingBinding;
6+
use dom::bindings::js::{JSRef, Temporary};
7+
use dom::bindings::utils::{Reflectable, Reflector, reflect_dom_object};
8+
use dom::window::Window;
9+
10+
#[deriving(Encodable)]
11+
pub struct PerformanceTiming {
12+
pub reflector_: Reflector,
13+
pub navigationStart: u64,
14+
pub navigationStartPrecise: f64,
15+
}
16+
17+
impl PerformanceTiming {
18+
pub fn new_inherited(navStart: u64, navStartPrecise: f64)
19+
-> PerformanceTiming {
20+
PerformanceTiming {
21+
reflector_: Reflector::new(),
22+
navigationStart: navStart,
23+
navigationStartPrecise: navStartPrecise,
24+
}
25+
}
26+
27+
pub fn new(window: &JSRef<Window>) -> Temporary<PerformanceTiming> {
28+
let timing = PerformanceTiming::new_inherited(window.navigationStart,
29+
window.navigationStartPrecise);
30+
reflect_dom_object(~timing, window, PerformanceTimingBinding::Wrap)
31+
}
32+
}
33+
34+
pub trait PerformanceTimingMethods {
35+
fn NavigationStart(&self) -> u64;
36+
fn NavigationStartPrecise(&self) -> f64;
37+
}
38+
39+
impl<'a> PerformanceTimingMethods for JSRef<'a, PerformanceTiming> {
40+
fn NavigationStart(&self) -> u64 {
41+
self.navigationStart
42+
}
43+
44+
fn NavigationStartPrecise(&self) -> f64 {
45+
self.navigationStartPrecise
46+
}
47+
}
48+
49+
impl Reflectable for PerformanceTiming {
50+
fn reflector<'a>(&'a self) -> &'a Reflector {
51+
&self.reflector_
52+
}
53+
54+
fn mut_reflector<'a>(&'a mut self) -> &'a mut Reflector {
55+
&mut self.reflector_
56+
}
57+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2+
/* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
4+
* You can obtain one at http://mozilla.org/MPL/2.0/.
5+
*
6+
* The origin of this IDL file is
7+
* https://dvcs.w3.org/hg/webperf/raw-file/tip/specs/NavigationTiming/Overview.html#sec-window.performance-attribute
8+
*/
9+
10+
typedef double DOMHighResTimeStamp;
11+
12+
interface Performance {
13+
readonly attribute PerformanceTiming timing;
14+
/* readonly attribute PerformanceNavigation navigation; */
15+
};
16+
17+
partial interface Performance {
18+
DOMHighResTimeStamp now();
19+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2+
/* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
4+
* You can obtain one at http://mozilla.org/MPL/2.0/.
5+
*
6+
* The origin of this IDL file is
7+
* https://dvcs.w3.org/hg/webperf/raw-file/tip/specs/NavigationTiming/Overview.html#sec-navigation-timing-interface
8+
*/
9+
10+
interface PerformanceTiming {
11+
readonly attribute unsigned long long navigationStart;
12+
/* readonly attribute unsigned long long unloadEventStart;
13+
readonly attribute unsigned long long unloadEventEnd;
14+
readonly attribute unsigned long long redirectStart;
15+
readonly attribute unsigned long long redirectEnd;
16+
readonly attribute unsigned long long fetchStart;
17+
readonly attribute unsigned long long domainLookupStart;
18+
readonly attribute unsigned long long domainLookupEnd;
19+
readonly attribute unsigned long long connectStart;
20+
readonly attribute unsigned long long connectEnd;
21+
readonly attribute unsigned long long secureConnectionStart;
22+
readonly attribute unsigned long long requestStart;
23+
readonly attribute unsigned long long responseStart;
24+
readonly attribute unsigned long long responseEnd;
25+
readonly attribute unsigned long long domLoading;
26+
readonly attribute unsigned long long domInteractive;
27+
readonly attribute unsigned long long domContentLoadedEventStart;
28+
readonly attribute unsigned long long domContentLoadedEventEnd;
29+
readonly attribute unsigned long long domComplete;
30+
readonly attribute unsigned long long loadEventStart;
31+
readonly attribute unsigned long long loadEventEnd; */
32+
};

src/components/script/dom/webidls/Window.webidl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
[Unforgeable] readonly attribute Window window;
1414
[Replaceable] readonly attribute Window self;
1515
[Unforgeable] readonly attribute Document document;
16-
attribute DOMString name;
16+
attribute DOMString name;
1717
/* [PutForwards=href, Unforgeable] */ readonly attribute Location location;
1818
/* readonly attribute History history;
1919
[Replaceable] readonly attribute BarProp locationbar;
@@ -56,6 +56,11 @@
5656

5757
};
5858

59+
// https://dvcs.w3.org/hg/webperf/raw-file/tip/specs/HighResolutionTime/Overview.html
60+
partial interface Window {
61+
[Replaceable] readonly attribute Performance performance;
62+
};
63+
5964
// Not part of any spec
6065
partial interface Window {
6166
// web developer niceties

src/components/script/dom/window.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use dom::eventtarget::{EventTarget, WindowTypeId};
1313
use dom::console::Console;
1414
use dom::location::Location;
1515
use dom::navigator::Navigator;
16+
use dom::performance::Performance;
1617

1718
use layout_interface::{ReflowForDisplay, DocumentDamageLevel};
1819
use script_task::{ExitWindowMsg, FireTimerMsg, Page, ScriptChan};
@@ -32,6 +33,8 @@ use std::hash::{Hash, sip};
3233
use std::io::timer::Timer;
3334
use std::rc::Rc;
3435

36+
use time;
37+
3538
use serialize::{Encoder, Encodable};
3639
use url::Url;
3740

@@ -71,6 +74,9 @@ pub struct Window {
7174
pub compositor: Untraceable<~ScriptListener>,
7275
pub browser_context: Option<BrowserContext>,
7376
pub page: Rc<Page>,
77+
pub performance: Option<JS<Performance>>,
78+
pub navigationStart: u64,
79+
pub navigationStartPrecise: f64,
7480
}
7581

7682
impl Window {
@@ -131,6 +137,7 @@ pub trait WindowMethods {
131137
fn ClearInterval(&mut self, handle: i32);
132138
fn Window(&self) -> Temporary<Window>;
133139
fn Self(&self) -> Temporary<Window>;
140+
fn Performance(&mut self) -> Temporary<Performance>;
134141
}
135142

136143
impl<'a> WindowMethods for JSRef<'a, Window> {
@@ -248,6 +255,14 @@ impl<'a> WindowMethods for JSRef<'a, Window> {
248255
fn Self(&self) -> Temporary<Window> {
249256
self.Window()
250257
}
258+
259+
fn Performance(&mut self) -> Temporary<Performance> {
260+
if self.performance.is_none() {
261+
let performance = Performance::new(self);
262+
self.performance.assign(Some(performance));
263+
}
264+
Temporary::new(self.performance.get_ref().clone())
265+
}
251266
}
252267

253268
impl Reflectable for Window {
@@ -355,6 +370,9 @@ impl Window {
355370
active_timers: ~HashMap::new(),
356371
next_timer_handle: 0,
357372
browser_context: None,
373+
performance: None,
374+
navigationStart: time::get_time().sec as u64,
375+
navigationStartPrecise: time::precise_time_s(),
358376
};
359377

360378
WindowBinding::Wrap(cx, win)

src/components/script/script.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ pub mod dom {
154154
pub mod node;
155155
pub mod nodelist;
156156
pub mod processinginstruction;
157+
pub mod performance;
158+
pub mod performancetiming;
157159
pub mod uievent;
158160
pub mod text;
159161
pub mod validitystate;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<html>
2+
<head>
3+
<title></title>
4+
<script src="harness.js"></script>
5+
</head>
6+
<body>
7+
<script>
8+
is_not(window.performance, undefined);
9+
is_a(window.performance, Performance);
10+
11+
is_not(window.performance.timing, undefined);
12+
is_a(window.performance.timing, PerformanceTiming);
13+
14+
gt(window.performance.timing.navigationStart, 0);
15+
16+
var last = window.performance.now();
17+
gt(last, 0);
18+
19+
// Check that window.performance.now() is monotonically increasing
20+
for (var i = 0; i < 100; i++) {
21+
var next = window.performance.now();
22+
gt(next, last);
23+
last = next;
24+
}
25+
finish();
26+
</script>
27+
</body>
28+
</html>

0 commit comments

Comments
 (0)