Auto merge of #20615 - KiChjang:window-indexed-getter, r=emilio

Implement window indexed getter

Fixes #4589.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/20615)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2018-05-17 18:52:38 -04:00 committed by GitHub
commit 3d109b4574
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
40 changed files with 161 additions and 237 deletions

View file

@ -77,22 +77,26 @@ impl DissimilarOriginWindow {
pub fn origin(&self) -> &MutableOrigin {
self.upcast::<GlobalScope>().origin()
}
pub fn window_proxy(&self) -> DomRoot<WindowProxy> {
DomRoot::from_ref(&*self.window_proxy)
}
}
impl DissimilarOriginWindowMethods for DissimilarOriginWindow {
// https://html.spec.whatwg.org/multipage/#dom-window
fn Window(&self) -> DomRoot<WindowProxy> {
DomRoot::from_ref(&*self.window_proxy)
self.window_proxy()
}
// https://html.spec.whatwg.org/multipage/#dom-self
fn Self_(&self) -> DomRoot<WindowProxy> {
DomRoot::from_ref(&*self.window_proxy)
self.window_proxy()
}
// https://html.spec.whatwg.org/multipage/#dom-frames
fn Frames(&self) -> DomRoot<WindowProxy> {
DomRoot::from_ref(&*self.window_proxy)
self.window_proxy()
}
// https://html.spec.whatwg.org/multipage/#dom-parent

View file

@ -3792,7 +3792,7 @@ impl DocumentMethods for Document {
let y = *y as f32;
let point = &Point2D::new(x, y);
let window = window_from_node(self);
let viewport = window.window_size().unwrap().initial_viewport;
let viewport = window.window_size()?.initial_viewport;
if self.browsing_context().is_none() {
return None;
@ -3826,7 +3826,10 @@ impl DocumentMethods for Document {
let y = *y as f32;
let point = &Point2D::new(x, y);
let window = window_from_node(self);
let viewport = window.window_size().unwrap().initial_viewport;
let viewport = match window.window_size() {
Some(size) => size.initial_viewport,
None => return vec![]
};
if self.browsing_context().is_none() {
return vec!();

View file

@ -1649,11 +1649,6 @@ impl Window {
had_clip_rect
}
// https://html.spec.whatwg.org/multipage/#accessing-other-browsing-contexts
pub fn IndexedGetter(&self, _index: u32, _found: &mut bool) -> Option<DomRoot<Window>> {
None
}
pub fn suspend(&self) {
// Suspend timer events.
self.upcast::<GlobalScope>().suspend();

View file

@ -17,11 +17,12 @@ use dom::element::Element;
use dom::globalscope::GlobalScope;
use dom::window::Window;
use dom_struct::dom_struct;
use ipc_channel::ipc;
use js::JSCLASS_IS_GLOBAL;
use js::glue::{CreateWrapperProxyHandler, ProxyTraps};
use js::glue::{GetProxyPrivate, SetProxyExtra, GetProxyExtra};
use js::jsapi::{JSAutoCompartment, JSContext, JSErrNum, JSFreeOp, JSObject};
use js::jsapi::{JSPROP_READONLY, JSTracer, JS_DefinePropertyById};
use js::jsapi::{JSPROP_ENUMERATE, JSPROP_READONLY, JSTracer, JS_DefinePropertyById};
use js::jsapi::{JS_ForwardGetPropertyTo, JS_ForwardSetPropertyTo};
use js::jsapi::{JS_HasPropertyById, JS_HasOwnPropertyById};
use js::jsapi::{JS_IsExceptionPending, JS_GetOwnPropertyDescriptorById};
@ -40,6 +41,8 @@ use js::rust::wrappers::{NewWindowProxy, SetWindowProxy, JS_TransplantObject};
use msg::constellation_msg::BrowsingContextId;
use msg::constellation_msg::PipelineId;
use msg::constellation_msg::TopLevelBrowsingContextId;
use script_thread::ScriptThread;
use script_traits::ScriptMsg;
use std::cell::Cell;
use std::ptr;
@ -301,17 +304,45 @@ impl WindowProxy {
// This is only called from extern functions,
// there's no use using the lifetimed handles here.
// https://html.spec.whatwg.org/multipage/#accessing-other-browsing-contexts
#[allow(unsafe_code)]
unsafe fn GetSubframeWindow(cx: *mut JSContext,
proxy: RawHandleObject,
id: RawHandleId)
-> Option<DomRoot<Window>> {
unsafe fn GetSubframeWindowProxy(
cx: *mut JSContext,
proxy: RawHandleObject,
id: RawHandleId
) -> Option<(DomRoot<WindowProxy>, u32)> {
let index = get_array_index_from_id(cx, Handle::from_raw(id));
if let Some(index) = index {
rooted!(in(cx) let target = GetProxyPrivate(*proxy).to_object());
let win = root_from_handleobject::<Window>(target.handle()).unwrap();
let mut found = false;
return win.IndexedGetter(index, &mut found);
if let Ok(win) = root_from_handleobject::<Window>(target.handle()) {
let browsing_context_id = win.window_proxy().browsing_context_id();
let (result_sender, result_receiver) = ipc::channel().unwrap();
let _ = win.upcast::<GlobalScope>().script_to_constellation_chan().send(
ScriptMsg::GetChildBrowsingContextId(
browsing_context_id,
index as usize,
result_sender
)
);
return result_receiver.recv().ok()
.and_then(|maybe_bcid| maybe_bcid)
.and_then(ScriptThread::find_window_proxy)
.map(|proxy| (proxy, JSPROP_ENUMERATE | JSPROP_READONLY));
} else if let Ok(win) = root_from_handleobject::<DissimilarOriginWindow>(target.handle()) {
let browsing_context_id = win.window_proxy().browsing_context_id();
let (result_sender, result_receiver) = ipc::channel().unwrap();
let _ = win.global().script_to_constellation_chan().send(ScriptMsg::GetChildBrowsingContextId(
browsing_context_id,
index as usize,
result_sender
));
return result_receiver.recv().ok()
.and_then(|maybe_bcid| maybe_bcid)
.and_then(ScriptThread::find_window_proxy)
.map(|proxy| (proxy, JSPROP_READONLY));
}
}
None
@ -323,12 +354,12 @@ unsafe extern "C" fn getOwnPropertyDescriptor(cx: *mut JSContext,
id: RawHandleId,
mut desc: RawMutableHandle<PropertyDescriptor>)
-> bool {
let window = GetSubframeWindow(cx, proxy, id);
if let Some(window) = window {
let window = GetSubframeWindowProxy(cx, proxy, id);
if let Some((window, attrs)) = window {
rooted!(in(cx) let mut val = UndefinedValue());
window.to_jsval(cx, val.handle_mut());
desc.value = val.get();
fill_property_descriptor(MutableHandle::from_raw(desc), proxy.get(), JSPROP_READONLY);
fill_property_descriptor(MutableHandle::from_raw(desc), proxy.get(), attrs);
return true;
}
@ -372,7 +403,7 @@ unsafe extern "C" fn has(cx: *mut JSContext,
id: RawHandleId,
bp: *mut bool)
-> bool {
let window = GetSubframeWindow(cx, proxy, id);
let window = GetSubframeWindowProxy(cx, proxy, id);
if window.is_some() {
*bp = true;
return true;
@ -395,8 +426,8 @@ unsafe extern "C" fn get(cx: *mut JSContext,
id: RawHandleId,
vp: RawMutableHandleValue)
-> bool {
let window = GetSubframeWindow(cx, proxy, id);
if let Some(window) = window {
let window = GetSubframeWindowProxy(cx, proxy, id);
if let Some((window, _attrs)) = window {
window.to_jsval(cx, MutableHandle::from_raw(vp));
return true;
}