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
/* 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 https://mozilla.org/MPL/2.0/. */

//! Implements the global methods required by Servo (not window/gl/compositor related).

use net::protocols::ProtocolRegistry;
use servo::compositing::windowing::EmbedderMethods;
use servo::embedder_traits::{EmbedderProxy, EventLoopWaker};
use servo::servo_config::pref;
use webxr::glwindow::GlWindowDiscovery;
#[cfg(target_os = "windows")]
use webxr::openxr::OpenXrDiscovery;

use crate::desktop::protocols::{resource, servo as servo_handler, urlinfo};

pub enum XrDiscovery {
    GlWindow(GlWindowDiscovery),
    #[cfg(target_os = "windows")]
    OpenXr(OpenXrDiscovery),
}

pub struct EmbedderCallbacks {
    event_loop_waker: Box<dyn EventLoopWaker>,
    xr_discovery: Option<XrDiscovery>,
}

impl EmbedderCallbacks {
    pub fn new(
        event_loop_waker: Box<dyn EventLoopWaker>,
        xr_discovery: Option<XrDiscovery>,
    ) -> EmbedderCallbacks {
        EmbedderCallbacks {
            event_loop_waker,
            xr_discovery,
        }
    }
}

impl EmbedderMethods for EmbedderCallbacks {
    fn create_event_loop_waker(&mut self) -> Box<dyn EventLoopWaker> {
        self.event_loop_waker.clone()
    }

    #[cfg(feature = "webxr")]
    fn register_webxr(
        &mut self,
        xr: &mut webxr::MainThreadRegistry,
        _embedder_proxy: EmbedderProxy,
    ) {
        if pref!(dom.webxr.test) {
            xr.register_mock(webxr::headless::HeadlessMockDiscovery::new());
        } else if let Some(xr_discovery) = self.xr_discovery.take() {
            match xr_discovery {
                XrDiscovery::GlWindow(discovery) => xr.register(discovery),
                #[cfg(target_os = "windows")]
                XrDiscovery::OpenXr(discovery) => xr.register(discovery),
            }
        }
    }

    fn get_protocol_handlers(&self) -> ProtocolRegistry {
        let mut registry = ProtocolRegistry::default();
        registry.register("urlinfo", urlinfo::UrlInfoProtocolHander::default());
        registry.register("servo", servo_handler::ServoProtocolHandler::default());
        registry.register("resource", resource::ResourceProtocolHandler::default());
        registry
    }

    fn get_version_string(&self) -> Option<String> {
        crate::servo_version().into()
    }
}