1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
/* 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::WindowBinding;
use dom::bindings::js::{JS, JSRef, Temporary, OptionalAssignable};
use dom::bindings::trace::{Traceable, Untraceable};
use dom::bindings::utils::{Reflectable, Reflector};
use dom::browsercontext::BrowserContext;
use dom::document::Document;
use dom::element::Element;
use dom::eventtarget::{EventTarget, WindowTypeId};
use dom::console::Console;
use dom::location::Location;
use dom::navigator::Navigator;
use layout_interface::{ReflowForDisplay, DocumentDamageLevel};
use script_task::{ExitWindowMsg, FireTimerMsg, Page, ScriptChan};
use servo_msg::compositor_msg::ScriptListener;
use servo_net::image_cache_task::ImageCacheTask;
use servo_util::str::DOMString;
use servo_util::task::{spawn_named};
use js::jsapi::JSContext;
use js::jsval::{NullValue, JSVal};
use collections::hashmap::HashMap;
use std::cmp;
use std::comm::{channel, Sender};
use std::comm::Select;
use std::hash::{Hash, sip};
use std::io::timer::Timer;
use std::rc::Rc;
use serialize::{Encoder, Encodable};
use url::Url;
#[deriving(Eq, Encodable, TotalEq)]
pub struct TimerId(i32);
#[deriving(Encodable)]
pub struct TimerHandle {
pub handle: TimerId,
pub data: TimerData,
pub cancel_chan: Untraceable<Option<Sender<()>>>,
}
impl Hash for TimerId {
fn hash(&self, state: &mut sip::SipState) {
let TimerId(id) = *self;
id.hash(state);
}
}
impl TimerHandle {
fn cancel(&mut self) {
self.cancel_chan.as_ref().map(|chan| chan.send(()));
}
}
#[deriving(Encodable)]
pub struct Window {
pub eventtarget: EventTarget,
pub script_chan: ScriptChan,
pub console: Option<JS<Console>>,
pub location: Option<JS<Location>>,
pub navigator: Option<JS<Navigator>>,
pub image_cache_task: ImageCacheTask,
pub active_timers: ~HashMap<TimerId, TimerHandle>,
pub next_timer_handle: i32,
pub compositor: Untraceable<~ScriptListener>,
pub browser_context: Option<BrowserContext>,
pub page: Rc<Page>,
}
impl Window {
pub fn get_cx(&self) -> *JSContext {
let js_info = self.page().js_info();
(**js_info.get_ref().js_context).ptr
}
pub fn page<'a>(&'a self) -> &'a Page {
&*self.page
}
pub fn get_url(&self) -> Url {
self.page().get_url()
}
}
#[unsafe_destructor]
impl Drop for Window {
fn drop(&mut self) {
for (_, timer_handle) in self.active_timers.mut_iter() {
timer_handle.cancel();
}
}
}
// Holder for the various JS values associated with setTimeout
// (ie. function value to invoke and all arguments to pass
// to the function when calling it)
#[deriving(Encodable)]
pub struct TimerData {
pub is_interval: bool,
pub funval: Traceable<JSVal>,
}
pub trait WindowMethods {
fn Alert(&self, s: DOMString);
fn Close(&self);
fn Document(&self) -> Temporary<Document>;
fn Name(&self) -> DOMString;
fn SetName(&self, _name: DOMString);
fn Status(&self) -> DOMString;
fn SetStatus(&self, _status: DOMString);
fn Closed(&self) -> bool;
fn Stop(&self);
fn Focus(&self);
fn Blur(&self);
fn GetFrameElement(&self) -> Option<Temporary<Element>>;
fn Location(&mut self) -> Temporary<Location>;
fn Console(&mut self) -> Temporary<Console>;
fn Navigator(&mut self) -> Temporary<Navigator>;
fn Confirm(&self, _message: DOMString) -> bool;
fn Prompt(&self, _message: DOMString, _default: DOMString) -> Option<DOMString>;
fn Print(&self);
fn ShowModalDialog(&self, _cx: *JSContext, _url: DOMString, _argument: Option<JSVal>) -> JSVal;
fn SetTimeout(&mut self, _cx: *JSContext, callback: JSVal, timeout: i32) -> i32;
fn ClearTimeout(&mut self, handle: i32);
fn SetInterval(&mut self, _cx: *JSContext, callback: JSVal, timeout: i32) -> i32;
fn ClearInterval(&mut self, handle: i32);
fn Window(&self) -> Temporary<Window>;
fn Self(&self) -> Temporary<Window>;
}
impl<'a> WindowMethods for JSRef<'a, Window> {
fn Alert(&self, s: DOMString) {
// Right now, just print to the console
println!("ALERT: {:s}", s);
}
fn Close(&self) {
let ScriptChan(ref chan) = self.script_chan;
chan.send(ExitWindowMsg(self.page.id.clone()));
}
fn Document(&self) -> Temporary<Document> {
let frame = self.page().frame();
Temporary::new(frame.get_ref().document.clone())
}
fn Name(&self) -> DOMString {
~""
}
fn SetName(&self, _name: DOMString) {
}
fn Status(&self) -> DOMString {
~""
}
fn SetStatus(&self, _status: DOMString) {
}
fn Closed(&self) -> bool {
false
}
fn Stop(&self) {
}
fn Focus(&self) {
}
fn Blur(&self) {
}
fn GetFrameElement(&self) -> Option<Temporary<Element>> {
None
}
fn Location(&mut self) -> Temporary<Location> {
if self.location.is_none() {
let page = self.deref().page.clone();
let location = Location::new(self, page);
self.location.assign(Some(location));
}
Temporary::new(self.location.get_ref().clone())
}
fn Console(&mut self) -> Temporary<Console> {
if self.console.is_none() {
let console = Console::new(self);
self.console.assign(Some(console));
}
Temporary::new(self.console.get_ref().clone())
}
fn Navigator(&mut self) -> Temporary<Navigator> {
if self.navigator.is_none() {
let navigator = Navigator::new(self);
self.navigator.assign(Some(navigator));
}
Temporary::new(self.navigator.get_ref().clone())
}
fn Confirm(&self, _message: DOMString) -> bool {
false
}
fn Prompt(&self, _message: DOMString, _default: DOMString) -> Option<DOMString> {
None
}
fn Print(&self) {
}
fn ShowModalDialog(&self, _cx: *JSContext, _url: DOMString, _argument: Option<JSVal>) -> JSVal {
NullValue()
}
fn SetTimeout(&mut self, _cx: *JSContext, callback: JSVal, timeout: i32) -> i32 {
self.set_timeout_or_interval(callback, timeout, false)
}
fn ClearTimeout(&mut self, handle: i32) {
let mut timer_handle = self.active_timers.pop(&TimerId(handle));
match timer_handle {
Some(ref mut handle) => handle.cancel(),
None => { }
}
self.active_timers.remove(&TimerId(handle));
}
fn SetInterval(&mut self, _cx: *JSContext, callback: JSVal, timeout: i32) -> i32 {
self.set_timeout_or_interval(callback, timeout, true)
}
fn ClearInterval(&mut self, handle: i32) {
self.ClearTimeout(handle);
}
fn Window(&self) -> Temporary<Window> {
Temporary::new_rooted(self)
}
fn Self(&self) -> Temporary<Window> {
self.Window()
}
}
impl Reflectable for Window {
fn reflector<'a>(&'a self) -> &'a Reflector {
self.eventtarget.reflector()
}
fn mut_reflector<'a>(&'a mut self) -> &'a mut Reflector {
self.eventtarget.mut_reflector()
}
}
impl Window {
fn set_timeout_or_interval(&mut self, callback: JSVal, timeout: i32, is_interval: bool) -> i32 {
let timeout = cmp::max(0, timeout) as u64;
let handle = self.next_timer_handle;
self.next_timer_handle += 1;
// Post a delayed message to the per-window timer task; it will dispatch it
// to the relevant script handler that will deal with it.
let tm = Timer::new().unwrap();
let (cancel_chan, cancel_port) = channel();
let chan = self.script_chan.clone();
let page_id = self.page.id.clone();
let spawn_name = if is_interval {
"Window:SetInterval"
} else {
"Window:SetTimeout"
};
spawn_named(spawn_name, proc() {
let mut tm = tm;
let timeout_port = if is_interval {
tm.periodic(timeout)
} else {
tm.oneshot(timeout)
};
let cancel_port = cancel_port;
let select = Select::new();
let mut timeout_handle = select.handle(&timeout_port);
unsafe { timeout_handle.add() };
let mut cancel_handle = select.handle(&cancel_port);
unsafe { cancel_handle.add() };
loop {
let id = select.wait();
if id == timeout_handle.id() {
timeout_port.recv();
let ScriptChan(ref chan) = chan;
chan.send(FireTimerMsg(page_id, TimerId(handle)));
if !is_interval {
break;
}
} else if id == cancel_handle.id() {
break;
}
}
});
let timer_id = TimerId(handle);
let timer = TimerHandle {
handle: timer_id,
cancel_chan: Untraceable::new(Some(cancel_chan)),
data: TimerData {
is_interval: is_interval,
funval: Traceable::new(callback),
}
};
self.active_timers.insert(timer_id, timer);
handle
}
pub fn damage_and_reflow(&self, damage: DocumentDamageLevel) {
// FIXME This should probably be ReflowForQuery, not Display. All queries currently
// currently rely on the display list, which means we can't destroy it by
// doing a query reflow.
self.page().damage(damage);
self.page().reflow(ReflowForDisplay, self.script_chan.clone(), *self.compositor);
}
pub fn wait_until_safe_to_modify_dom(&self) {
// FIXME: This disables concurrent layout while we are modifying the DOM, since
// our current architecture is entirely unsafe in the presence of races.
self.page().join_layout();
}
pub fn init_browser_context(&mut self, doc: &JSRef<Document>) {
self.browser_context = Some(BrowserContext::new(doc));
}
pub fn new(cx: *JSContext,
page: Rc<Page>,
script_chan: ScriptChan,
compositor: ~ScriptListener,
image_cache_task: ImageCacheTask)
-> JS<Window> {
let win = ~Window {
eventtarget: EventTarget::new_inherited(WindowTypeId),
script_chan: script_chan,
console: None,
compositor: Untraceable::new(compositor),
page: page.clone(),
location: None,
navigator: None,
image_cache_task: image_cache_task,
active_timers: ~HashMap::new(),
next_timer_handle: 0,
browser_context: None,
};
WindowBinding::Wrap(cx, win)
}
}
|