aboutsummaryrefslogtreecommitdiffstats
path: root/components
diff options
context:
space:
mode:
authorManish Goregaokar <manishsmail@gmail.com>2018-12-19 17:33:52 -0800
committerManish Goregaokar <manishsmail@gmail.com>2018-12-19 19:56:52 -0800
commitd5911816e1bd0bbfc64c33bebce45b117550b16f (patch)
tree28198fafd97aacd2c464ed226c7c7ed748ae80f6 /components
parent520bb23048361248c758bb4417d2ba681b861a0e (diff)
downloadservo-d5911816e1bd0bbfc64c33bebce45b117550b16f.tar.gz
servo-d5911816e1bd0bbfc64c33bebce45b117550b16f.zip
Fill in XR.requestSession
Diffstat (limited to 'components')
-rw-r--r--components/script/dom/navigator.rs9
-rw-r--r--components/script/dom/webidls/XR.webidl4
-rw-r--r--components/script/dom/webidls/XRSession.webidl6
-rw-r--r--components/script/dom/xr.rs67
-rw-r--r--components/script/dom/xrsession.rs41
5 files changed, 103 insertions, 24 deletions
diff --git a/components/script/dom/navigator.rs b/components/script/dom/navigator.rs
index fccc49aac9d..7ef74da204d 100644
--- a/components/script/dom/navigator.rs
+++ b/components/script/dom/navigator.rs
@@ -4,6 +4,7 @@
use crate::dom::bindings::codegen::Bindings::NavigatorBinding;
use crate::dom::bindings::codegen::Bindings::NavigatorBinding::NavigatorMethods;
+use crate::dom::bindings::error::Error;
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject, Reflector};
use crate::dom::bindings::root::{DomRoot, MutNullableDom};
use crate::dom::bindings::str::DOMString;
@@ -148,7 +149,13 @@ impl NavigatorMethods for Navigator {
// https://w3c.github.io/webvr/spec/1.1/#navigator-getvrdisplays-attribute
#[allow(unrooted_must_root)]
fn GetVRDisplays(&self) -> Rc<Promise> {
- self.Xr().get_displays()
+ let promise = Promise::new(&self.global());
+ let displays = self.Xr().get_displays();
+ match displays {
+ Ok(displays) => promise.resolve_native(&displays),
+ Err(e) => promise.reject_error(Error::Security),
+ }
+ promise
}
fn Xr(&self) -> DomRoot<XR> {
diff --git a/components/script/dom/webidls/XR.webidl b/components/script/dom/webidls/XR.webidl
index 9c973c39f6f..435558aa5e8 100644
--- a/components/script/dom/webidls/XR.webidl
+++ b/components/script/dom/webidls/XR.webidl
@@ -6,8 +6,8 @@
[SecureContext, Exposed=Window]
interface XR: EventTarget {
// Methods
- // Promise<void> supportsSessionMode(XRSessionMode mode);
- // Promise<XRSession> requestSession(optional XRSessionCreationOptions parameters);
+ Promise<void> supportsSessionMode(XRSessionMode mode);
+ Promise<XRSession> requestSession(optional XRSessionCreationOptions parameters);
// Events
// attribute EventHandler ondevicechange;
diff --git a/components/script/dom/webidls/XRSession.webidl b/components/script/dom/webidls/XRSession.webidl
index d18b6e18a16..4945880697a 100644
--- a/components/script/dom/webidls/XRSession.webidl
+++ b/components/script/dom/webidls/XRSession.webidl
@@ -12,12 +12,12 @@ enum XREnvironmentBlendMode {
[SecureContext, Exposed=Window] interface XRSession : EventTarget {
// // Attributes
- // readonly attribute XRSessionMode mode;
+ readonly attribute XRSessionMode mode;
// readonly attribute XRPresentationContext outputContext;
// readonly attribute XREnvironmentBlendMode environmentBlendMode;
- // attribute double depthNear;
- // attribute double depthFar;
+ attribute double depthNear;
+ attribute double depthFar;
// attribute XRLayer baseLayer;
// // Methods
diff --git a/components/script/dom/xr.rs b/components/script/dom/xr.rs
index e64be23663e..d5571ac60c7 100644
--- a/components/script/dom/xr.rs
+++ b/components/script/dom/xr.rs
@@ -5,6 +5,8 @@
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::VRDisplayBinding::VRDisplayMethods;
use crate::dom::bindings::codegen::Bindings::XRBinding;
+use crate::dom::bindings::codegen::Bindings::XRBinding::XRSessionCreationOptions;
+use crate::dom::bindings::codegen::Bindings::XRBinding::{XRMethods, XRSessionMode};
use crate::dom::bindings::error::Error;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject};
@@ -17,6 +19,7 @@ use crate::dom::globalscope::GlobalScope;
use crate::dom::promise::Promise;
use crate::dom::vrdisplay::VRDisplay;
use crate::dom::vrdisplayevent::VRDisplayEvent;
+use crate::dom::xrsession::XRSession;
use dom_struct::dom_struct;
use ipc_channel::ipc::IpcSender;
use profile_traits::ipc;
@@ -53,15 +56,59 @@ impl Drop for XR {
}
}
-impl XR {
+impl XRMethods for XR {
+ #[allow(unrooted_must_root)]
+ fn SupportsSessionMode(&self, mode: XRSessionMode) -> Rc<Promise> {
+ // XXXManishearth this should select an XR device first
+ let promise = Promise::new(&self.global());
+ if mode == XRSessionMode::Immersive_vr {
+ promise.resolve_native(&());
+ } else {
+ // XXXManishearth support other modes
+ promise.reject_error(Error::NotSupported);
+ }
+
+ promise
+ }
+
#[allow(unrooted_must_root)]
- pub fn get_displays(&self) -> Rc<Promise> {
+ fn RequestSession(&self, options: &XRSessionCreationOptions) -> Rc<Promise> {
let promise = Promise::new(&self.global());
+ if options.mode != XRSessionMode::Immersive_vr {
+ promise.reject_error(Error::NotSupported);
+ return promise;
+ }
+
+ let displays = self.get_displays();
+ let displays = match displays {
+ Ok(d) => d,
+ Err(_) => {
+ promise.reject_native(&());
+ return promise;
+ },
+ };
+
+ if displays.is_empty() {
+ promise.reject_error(Error::Security);
+ }
+
+ let session = XRSession::new(&self.global(), &displays[0]);
+ promise.resolve_native(&session);
+
+ promise
+ }
+}
+
+impl XR {
+ #[allow(unrooted_must_root)]
+ pub fn get_displays(&self) -> Result<Vec<DomRoot<VRDisplay>>, ()> {
if let Some(webvr_thread) = self.webvr_thread() {
let (sender, receiver) =
ipc::channel(self.global().time_profiler_chan().clone()).unwrap();
webvr_thread.send(WebVRMsg::GetDisplays(sender)).unwrap();
+
+ // FIXME(#22505) we should not block here and instead produce a promise
match receiver.recv().unwrap() {
Ok(displays) => {
// Sync displays
@@ -69,27 +116,19 @@ impl XR {
self.sync_display(&display);
}
},
- Err(e) => {
- promise.reject_native(&e);
- return promise;
- },
+ Err(e) => return Err(()),
}
} else {
// WebVR spec: The Promise MUST be rejected if WebVR is not enabled/supported.
- promise.reject_error(Error::Security);
- return promise;
+ return Err(());
}
// convert from Dom to DomRoot
- let displays: Vec<DomRoot<VRDisplay>> = self
- .displays
+ Ok(self.displays
.borrow()
.iter()
.map(|d| DomRoot::from_ref(&**d))
- .collect();
- promise.resolve_native(&displays);
-
- promise
+ .collect())
}
fn webvr_thread(&self) -> Option<IpcSender<WebVRMsg>> {
diff --git a/components/script/dom/xrsession.rs b/components/script/dom/xrsession.rs
index b0ebe383ae7..bbb4626b5e8 100644
--- a/components/script/dom/xrsession.rs
+++ b/components/script/dom/xrsession.rs
@@ -2,30 +2,63 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
+use crate::dom::bindings::codegen::Bindings::XRBinding::XRSessionMode;
use crate::dom::bindings::codegen::Bindings::XRSessionBinding;
+use crate::dom::bindings::codegen::Bindings::XRSessionBinding::XRSessionMethods;
+use crate::dom::bindings::num::Finite;
use crate::dom::bindings::reflector::reflect_dom_object;
-use crate::dom::bindings::root::DomRoot;
+use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::eventtarget::EventTarget;
use crate::dom::globalscope::GlobalScope;
+use crate::dom::vrdisplay::VRDisplay;
use dom_struct::dom_struct;
+use std::cell::Cell;
#[dom_struct]
pub struct XRSession {
eventtarget: EventTarget,
+ display: Dom<VRDisplay>,
+ depth_near: Cell<f64>,
+ depth_far: Cell<f64>,
}
impl XRSession {
- fn new_inherited() -> XRSession {
+ fn new_inherited(display: &VRDisplay) -> XRSession {
XRSession {
eventtarget: EventTarget::new_inherited(),
+ display: Dom::from_ref(display),
+ depth_near: Cell::new(0.1),
+ depth_far: Cell::new(1000.),
}
}
- pub fn new(global: &GlobalScope) -> DomRoot<XRSession> {
+ pub fn new(global: &GlobalScope, display: &VRDisplay) -> DomRoot<XRSession> {
reflect_dom_object(
- Box::new(XRSession::new_inherited()),
+ Box::new(XRSession::new_inherited(display)),
global,
XRSessionBinding::Wrap,
)
}
}
+
+impl XRSessionMethods for XRSession {
+ fn DepthNear(&self) -> Finite<f64> {
+ Finite::wrap(self.depth_near.get())
+ }
+
+ fn DepthFar(&self) -> Finite<f64> {
+ Finite::wrap(self.depth_far.get())
+ }
+
+ fn SetDepthNear(&self, d: Finite<f64>) {
+ self.depth_near.set(*d)
+ }
+
+ fn SetDepthFar(&self, d: Finite<f64>) {
+ self.depth_far.set(*d)
+ }
+
+ fn Mode(&self) -> XRSessionMode {
+ XRSessionMode::Immersive_vr
+ }
+}