mirror of
https://github.com/servo/servo.git
synced 2025-07-23 07:13:52 +01:00
Make BrowsingContext JS-managed.
This commit is contained in:
parent
b6850853da
commit
f86502f80c
2 changed files with 52 additions and 46 deletions
|
@ -6,45 +6,70 @@ use dom::bindings::conversions::native_from_handleobject;
|
|||
use dom::bindings::conversions::{ToJSValConvertible};
|
||||
use dom::bindings::js::{JS, Root};
|
||||
use dom::bindings::proxyhandler::{fill_property_descriptor, get_property_descriptor};
|
||||
use dom::bindings::reflector::Reflectable;
|
||||
use dom::bindings::reflector::{Reflectable, Reflector};
|
||||
use dom::bindings::utils::WindowProxyHandler;
|
||||
use dom::bindings::utils::get_array_index_from_id;
|
||||
use dom::document::Document;
|
||||
use dom::element::Element;
|
||||
use dom::window::Window;
|
||||
use js::JSCLASS_IS_GLOBAL;
|
||||
use js::glue::{CreateWrapperProxyHandler, ProxyTraps, WrapperNew};
|
||||
use js::glue::{GetProxyPrivate};
|
||||
use js::jsapi::{Handle, Heap, JS_ForwardSetPropertyTo, ObjectOpResult, RootedObject, RootedValue};
|
||||
use js::glue::{GetProxyPrivate, SetProxyExtra};
|
||||
use js::jsapi::{Handle, JS_ForwardSetPropertyTo, ObjectOpResult, RootedObject, RootedValue};
|
||||
use js::jsapi::{HandleId, HandleObject, MutableHandle, MutableHandleValue};
|
||||
use js::jsapi::{JSAutoCompartment, JSAutoRequest};
|
||||
use js::jsapi::{JSAutoCompartment, JSAutoRequest, JS_GetClass};
|
||||
use js::jsapi::{JSContext, JSErrNum, JSObject, JSPropertyDescriptor};
|
||||
use js::jsapi::{JS_AlreadyHasOwnPropertyById, JS_ForwardGetPropertyTo};
|
||||
use js::jsapi::{JS_DefinePropertyById6, JS_GetPropertyDescriptorById};
|
||||
use js::jsval::{ObjectValue, UndefinedValue};
|
||||
use std::default::Default;
|
||||
use js::jsval::{ObjectValue, UndefinedValue, PrivateValue};
|
||||
use std::ptr;
|
||||
|
||||
#[derive(JSTraceable, HeapSizeOf)]
|
||||
#[privatize]
|
||||
#[allow(raw_pointer_derive)]
|
||||
#[must_root]
|
||||
#[dom_struct]
|
||||
pub struct BrowsingContext {
|
||||
reflector: Reflector,
|
||||
history: Vec<SessionHistoryEntry>,
|
||||
active_index: usize,
|
||||
window_proxy: Heap<*mut JSObject>,
|
||||
frame_element: Option<JS<Element>>,
|
||||
}
|
||||
|
||||
impl BrowsingContext {
|
||||
pub fn new(document: &Document, frame_element: Option<&Element>) -> BrowsingContext {
|
||||
pub fn new_inherited(document: &Document, frame_element: Option<&Element>) -> BrowsingContext {
|
||||
BrowsingContext {
|
||||
reflector: Reflector::new(),
|
||||
history: vec!(SessionHistoryEntry::new(document)),
|
||||
active_index: 0,
|
||||
window_proxy: Heap::default(),
|
||||
frame_element: frame_element.map(JS::from_ref),
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unsafe_code)]
|
||||
pub fn new(document: &Document, frame_element: Option<&Element>) -> Root<BrowsingContext> {
|
||||
unsafe {
|
||||
let window = document.window();
|
||||
|
||||
let WindowProxyHandler(handler) = window.windowproxy_handler();
|
||||
assert!(!handler.is_null());
|
||||
|
||||
let cx = window.get_cx();
|
||||
let _ar = JSAutoRequest::new(cx);
|
||||
let parent = window.reflector().get_jsobject();
|
||||
assert!(!parent.get().is_null());
|
||||
assert!(((*JS_GetClass(parent.get())).flags & JSCLASS_IS_GLOBAL) != 0);
|
||||
let _ac = JSAutoCompartment::new(cx, parent.get());
|
||||
let window_proxy = RootedObject::new(cx, WrapperNew(cx, parent, handler, ptr::null(), true));
|
||||
assert!(!window_proxy.ptr.is_null());
|
||||
|
||||
let object = box BrowsingContext::new_inherited(document, frame_element);
|
||||
|
||||
let raw = Box::into_raw(object);
|
||||
SetProxyExtra(window_proxy.ptr, 0, PrivateValue(raw as *const _));
|
||||
|
||||
(*raw).init_reflector(window_proxy.ptr);
|
||||
|
||||
Root::from_ref(&*raw)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn active_document(&self) -> &Document {
|
||||
&*self.history[self.active_index].document
|
||||
}
|
||||
|
@ -58,25 +83,9 @@ impl BrowsingContext {
|
|||
}
|
||||
|
||||
pub fn window_proxy(&self) -> *mut JSObject {
|
||||
assert!(!self.window_proxy.get().is_null());
|
||||
self.window_proxy.get()
|
||||
}
|
||||
|
||||
#[allow(unsafe_code)]
|
||||
pub fn create_window_proxy(&mut self) {
|
||||
// We inline self.active_window() because we can't borrow *self here.
|
||||
let win = self.history[self.active_index].document.window();
|
||||
|
||||
let WindowProxyHandler(handler) = win.windowproxy_handler();
|
||||
assert!(!handler.is_null());
|
||||
|
||||
let cx = win.get_cx();
|
||||
let _ar = JSAutoRequest::new(cx);
|
||||
let parent = win.reflector().get_jsobject();
|
||||
let _ac = JSAutoCompartment::new(cx, parent.get());
|
||||
let wrapper = unsafe { WrapperNew(cx, parent, handler, ptr::null(), false) };
|
||||
assert!(!wrapper.is_null());
|
||||
self.window_proxy.set(wrapper);
|
||||
let window_proxy = self.reflector.get_jsobject();
|
||||
assert!(!window_proxy.get().is_null());
|
||||
window_proxy.get()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -87,7 +96,7 @@ impl BrowsingContext {
|
|||
#[derive(JSTraceable, HeapSizeOf)]
|
||||
pub struct SessionHistoryEntry {
|
||||
document: JS<Document>,
|
||||
children: Vec<BrowsingContext>
|
||||
children: Vec<JS<BrowsingContext>>
|
||||
}
|
||||
|
||||
impl SessionHistoryEntry {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue