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
|
/* 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::WindowBinding;
use dom::bindings::utils::{WrapperCache, DOMString, null_string};
use dom::bindings::utils::{CacheableWrapper, BindingObject};
use dom::document::AbstractDocument;
use dom::node::{AbstractNode, ScriptView};
use layout_interface::ReflowForScriptQuery;
use script_task::{ExitMsg, FireTimerMsg, Page, ScriptChan};
use servo_msg::compositor_msg::ScriptListener;
use js::glue::*;
use js::jsapi::{JSObject, JSContext};
use js::{JSVAL_NULL, JSPROP_ENUMERATE};
use std::cast;
use std::comm;
use std::comm::Chan;
use std::io;
use std::ptr;
use js::jsapi::JSVal;
use extra::timer;
use extra::uv_global_loop;
pub enum TimerControlMsg {
TimerMessage_Fire(~TimerData),
TimerMessage_Close,
TimerMessage_TriggerExit //XXXjdm this is just a quick hack to talk to the script task
}
//FIXME If we're going to store the page, find a way to do so safely.
pub struct Window {
page: *mut Page,
script_chan: ScriptChan,
compositor: @ScriptListener,
wrapper: WrapperCache,
timer_chan: Chan<TimerControlMsg>,
}
#[unsafe_destructor]
impl Drop for Window {
fn drop(&self) {
self.timer_chan.send(TimerMessage_Close);
}
}
// 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)
pub struct TimerData {
funval: JSVal,
args: ~[JSVal],
}
impl Window {
pub fn Alert(&self, s: &DOMString) {
// Right now, just print to the console
io::println(fmt!("ALERT: %s", s.to_str()));
}
pub fn Close(&self) {
self.timer_chan.send(TimerMessage_TriggerExit);
}
pub fn Document(&self) -> AbstractDocument {
unsafe {
(*self.page).frame.unwrap().document
}
}
pub fn Name(&self) -> DOMString {
null_string
}
pub fn SetName(&self, _name: &DOMString) {
}
pub fn Status(&self) -> DOMString {
null_string
}
pub fn SetStatus(&self, _status: &DOMString) {
}
pub fn Closed(&self) -> bool {
false
}
pub fn Stop(&self) {
}
pub fn Focus(&self) {
}
pub fn Blur(&self) {
}
pub fn GetFrameElement(&self) -> Option<AbstractNode<ScriptView>> {
None
}
pub fn Confirm(&self, _message: &DOMString) -> bool {
false
}
pub fn Prompt(&self, _message: &DOMString, _default: &DOMString) -> DOMString {
null_string
}
pub fn Print(&self) {
}
pub fn ShowModalDialog(&self, _cx: *JSContext, _url: &DOMString, _argument: JSVal) -> JSVal {
JSVAL_NULL
}
pub fn NamedGetter(&self, _cx: *JSContext, _name: &DOMString, _found: &mut bool) -> *JSObject {
ptr::null()
}
}
impl CacheableWrapper for Window {
fn get_wrappercache(&mut self) -> &mut WrapperCache {
unsafe { cast::transmute(&self.wrapper) }
}
fn wrap_object_shared(@mut self, cx: *JSContext, scope: *JSObject) -> *JSObject {
let mut unused = false;
WindowBinding::Wrap(cx, scope, self, &mut unused)
}
}
impl BindingObject for Window {
fn GetParentObject(&self, _cx: *JSContext) -> Option<@mut CacheableWrapper> {
None
}
}
impl Window {
pub fn SetTimeout(&self, _cx: *JSContext, callback: JSVal, timeout: i32) -> i32 {
let timeout = int::max(0, timeout) as uint;
// 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 data = ~TimerData {
funval: callback,
args: ~[]
};
timer::delayed_send(&uv_global_loop::get(),
timeout,
&self.timer_chan,
TimerMessage_Fire(data));
return 0; //TODO return handle into list of active timers
}
pub fn content_changed(&self) {
unsafe {
(*self.page).reflow_all(ReflowForScriptQuery, self.script_chan.clone(), self.compositor);
}
}
pub fn new(page: *mut Page, script_chan: ScriptChan, compositor: @ScriptListener)
-> @mut Window {
let script_chan_clone = script_chan.clone();
let win = @mut Window {
page: page,
script_chan: script_chan,
compositor: compositor,
wrapper: WrapperCache::new(),
timer_chan: {
let (timer_port, timer_chan) = comm::stream::<TimerControlMsg>();
do spawn {
loop {
match timer_port.recv() {
TimerMessage_Close => break,
TimerMessage_Fire(td) => unsafe {script_chan_clone.chan.send(FireTimerMsg((*page).id.clone(), td))},
TimerMessage_TriggerExit => script_chan_clone.chan.send(ExitMsg),
}
}
}
timer_chan
},
};
unsafe {
// TODO(tkuehn): This just grabs the top-level page. Need to handle subframes.
let compartment = (*page).js_info.get_ref().js_compartment;
let cache = ptr::to_unsafe_ptr(win.get_wrappercache());
win.wrap_object_shared(compartment.cx.ptr, ptr::null()); //XXXjdm proper scope
compartment.define_property(~"window",
RUST_OBJECT_TO_JSVAL((*cache).wrapper),
GetJSClassHookStubPointer(PROPERTY_STUB) as *u8,
GetJSClassHookStubPointer(STRICT_PROPERTY_STUB) as *u8,
JSPROP_ENUMERATE);
}
win
}
}
|