aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/script/dom/window.rs
blob: 5c2bf7cda976e7ea4ccc6c17f0bd31d7cc5ccace (plain) (blame)
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
/* 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, Traceable};
use dom::bindings::utils::{CacheableWrapper, BindingObject, null_str_as_empty};
use dom::document::AbstractDocument;
use dom::node::{AbstractNode, ScriptView};
use dom::navigator::Navigator;

use layout_interface::ReflowForDisplay;
use script_task::{ExitWindowMsg, FireTimerMsg, Page, ScriptChan};
use servo_msg::compositor_msg::ScriptListener;
use servo_net::image_cache_task::ImageCacheTask;

use js::glue::*;
use js::jsapi::{JSObject, JSContext, JS_DefineProperty, JS_CallTracer};
use js::jsapi::{JSPropertyOp, JSStrictPropertyOp, JSTracer, JSTRACE_OBJECT};
use js::{JSVAL_NULL, JSPROP_ENUMERATE};

use std::cast;
use std::cell::Cell;
use std::comm;
use std::comm::SharedChan;
use std::hashmap::HashSet;
use std::io;
use std::ptr;
use std::int;
use std::libc;
use std::rt::rtio::RtioTimer;
use std::rt::io::timer::Timer;
use std::task::spawn_with;
use js::jsapi::JSVal;

pub enum TimerControlMsg {
    TimerMessage_Fire(~TimerData),
    TimerMessage_Close,
    TimerMessage_TriggerExit //XXXjdm this is just a quick hack to talk to the script task
}

pub struct Window {
    page: @mut Page,
    script_chan: ScriptChan,
    compositor: @ScriptListener,
    wrapper: WrapperCache,
    timer_chan: SharedChan<TimerControlMsg>,
    navigator: Option<@mut Navigator>,
    image_cache_task: ImageCacheTask,
    active_timers: ~HashSet<i32>,
    next_timer_handle: i32,
}

#[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 {
    handle: i32,
    funval: JSVal,
    args: ~[JSVal],
}

impl Window {
    pub fn Alert(&self, s: &DOMString) {
        // Right now, just print to the console
        io::println(fmt!("ALERT: %s", null_str_as_empty(s)));
    }

    pub fn Close(&self) {
        self.timer_chan.send(TimerMessage_TriggerExit);
    }

    pub fn Document(&self) -> AbstractDocument {
        self.page.frame.unwrap().document
    }

    pub fn Name(&self) -> DOMString {
        None
    }

    pub fn SetName(&self, _name: &DOMString) {
    }

    pub fn Status(&self) -> DOMString {
        None
    }

    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 Navigator(&mut self) -> @mut Navigator {
        if self.navigator.is_none() {
            self.navigator = Some(Navigator::new());
        }
        self.navigator.unwrap()
    }

    pub fn Confirm(&self, _message: &DOMString) -> bool {
        false
    }

    pub fn Prompt(&self, _message: &DOMString, _default: &DOMString) -> DOMString {
        None
    }

    pub fn Print(&self) {
    }

    pub fn ShowModalDialog(&self, _cx: *JSContext, _url: &DOMString, _argument: JSVal) -> JSVal {
        JSVAL_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(&mut self, _cx: *JSContext, callback: JSVal, timeout: i32) -> i32 {
        let timeout = int::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 = Cell::new(Timer::new().unwrap());
        let chan = self.timer_chan.clone();
        do spawn {
            let mut tm = tm.take();
            tm.sleep(timeout);
            chan.send(TimerMessage_Fire(~TimerData {
                handle: handle,
                funval: callback,
                args: ~[]
            }));
        }
        self.active_timers.insert(handle);
        handle
    }

    pub fn ClearTimeout(&mut self, handle: i32) {
        self.active_timers.remove(&handle);
    }

    pub fn content_changed(&self) {
        // 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.reflow_all(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();
    }

    #[fixed_stack_segment]
    pub fn new(cx: *JSContext,
               page: @mut Page,
               script_chan: ScriptChan,
               compositor: @ScriptListener,
               image_cache_task: ImageCacheTask)
               -> @mut Window {
        let win = @mut Window {
            page: page,
            script_chan: script_chan.clone(),
            compositor: compositor,
            wrapper: WrapperCache::new(),
            timer_chan: {
                let (timer_port, timer_chan) = comm::stream::<TimerControlMsg>();
                let id = page.id.clone();
                do spawn_with(script_chan) |script_chan| {
                    loop {
                        match timer_port.recv() {
                            TimerMessage_Close => break,
                            TimerMessage_Fire(td) => script_chan.send(FireTimerMsg(id, td)),
                            TimerMessage_TriggerExit => script_chan.send(ExitWindowMsg(id)),
                        }
                    }
                }
                SharedChan::new(timer_chan)
            },
            navigator: None,
            image_cache_task: image_cache_task,
            active_timers: ~HashSet::new(),
            next_timer_handle: 0
        };

        unsafe {
            let cache = ptr::to_unsafe_ptr(win.get_wrappercache());
            win.wrap_object_shared(cx, ptr::null()); //XXXjdm proper scope
            let global = (*cache).wrapper;
            do "window".to_c_str().with_ref |name| {
                JS_DefineProperty(cx, global,  name,
                                  RUST_OBJECT_TO_JSVAL(global),
                                  Some(GetJSClassHookStubPointer(PROPERTY_STUB) as JSPropertyOp),
                                  Some(GetJSClassHookStubPointer(STRICT_PROPERTY_STUB) as JSStrictPropertyOp),
                                  JSPROP_ENUMERATE);
            }
        }
        win
    }
}

impl Traceable for Window {
    #[fixed_stack_segment]
    fn trace(&self, tracer: *mut JSTracer) {
        debug!("tracing window");
        unsafe {
            match self.page.frame {
                Some(frame) => {
                    do frame.document.with_base |doc| {
                        (*tracer).debugPrinter = ptr::null();
                        (*tracer).debugPrintIndex = -1;
                        do "document".to_c_str().with_ref |name| {
                            (*tracer).debugPrintArg = name as *libc::c_void;
                            debug!("tracing document");
                            JS_CallTracer(tracer as *JSTracer,
                                          doc.wrapper.wrapper,
                                          JSTRACE_OBJECT as u32);
                        }
                    }
                }
                None => ()
            }
        }
    }
}