Skip to content

Commit

Permalink
Add Performance object to Window and implement Performance::Now()
Browse files Browse the repository at this point in the history
  • Loading branch information
gsingh93 committed May 7, 2014
1 parent 0ab3444 commit fb0c433
Show file tree
Hide file tree
Showing 9 changed files with 223 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/components/script/dom/bindings/codegen/Bindings.conf
Expand Up @@ -41,6 +41,8 @@ DOMInterfaces = {
'Navigator': {},
'Node': {},
'NodeList': {},
'Performance': {},
'PerformanceTiming': {},
'UIEvent': {},
'ValidityState': {},
'Window': {
Expand Down
59 changes: 59 additions & 0 deletions 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_
}
}
57 changes: 57 additions & 0 deletions 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_
}
}
19 changes: 19 additions & 0 deletions 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();
};
32 changes: 32 additions & 0 deletions 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; */
};
7 changes: 6 additions & 1 deletion src/components/script/dom/webidls/Window.webidl
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions src/components/script/dom/window.rs
Expand Up @@ -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};
Expand All @@ -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;

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/components/script/script.rs
Expand Up @@ -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;
Expand Down
28 changes: 28 additions & 0 deletions 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>

5 comments on commit fb0c433

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging gsingh93/servo/master = fb0c433 into auto

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gsingh93/servo/master = fb0c433 merged ok, testing candidate = 86a6de2

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 86a6de2

Please sign in to comment.