servo/components/script/dom/mutationobserver.rs

40 lines
1.4 KiB
Rust

/* 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::Bindings::MutationObserverBinding;
use dom::bindings::codegen::Bindings::MutationObserverBinding::MutationCallback;
use dom::bindings::error::Fallible;
use dom::bindings::js::Root;
use dom::bindings::reflector::{Reflector, reflect_dom_object};
use dom::window::Window;
use dom_struct::dom_struct;
use script_thread::ScriptThread;
use std::rc::Rc;
#[dom_struct]
pub struct MutationObserver {
reflector_: Reflector,
#[ignore_heap_size_of = "can't measure Rc values"]
callback: Rc<MutationCallback>,
}
impl MutationObserver {
fn new(global: &Window, callback: Rc<MutationCallback>) -> Root<MutationObserver> {
let boxed_observer = box MutationObserver::new_inherited(callback);
reflect_dom_object(boxed_observer, global, MutationObserverBinding::Wrap)
}
fn new_inherited(callback: Rc<MutationCallback>) -> MutationObserver {
MutationObserver {
reflector_: Reflector::new(),
callback: callback,
}
}
pub fn Constructor(global: &Window, callback: Rc<MutationCallback>) -> Fallible<Root<MutationObserver>> {
let observer = MutationObserver::new(global, callback);
ScriptThread::add_mutation_observer(&*observer);
Ok(observer)
}
}