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
//! A sandbox represents a virtual browser tab. It contains a document and a window,
//! as well as some configuration information for screen dimensions.

use std::marker::PhantomData;

use crate::internal_prelude::*;

use crate::config::ScreenMetrics;
use crate::node::Buildable;
use crate::window::Window;

/// A Builder<R> is a machine which can be used to build nodes of reference type R.
pub struct Builder<R: Buildable> {
    pub(crate) sandbox: Weak<Sandbox>,
    _phantom: PhantomData<R>,
}

/// A sandbox represents a virtual browser tab. It contains a document and a window,
/// as well as some configuration information for screen dimensions.
#[derive(Clone)]
pub struct Sandbox {
    screen_metrics: ScreenMetrics,
    window: Arc<Window>,
}

impl Sandbox {
    /// Creates a new sandbox (enclosure of a single DOM context)
    pub fn new(screen_metrics: ScreenMetrics) -> Arc<Sandbox> {
        Arc::new_cyclic(|sandbox_weak| -> Sandbox {
            let win = Window::new(sandbox_weak.clone());
            Sandbox {
                screen_metrics,
                window: win,
            }
        })
    }

    /// Gets the root window object
    pub fn window(&self) -> Arc<Window> {
        // Window is safe to unwrap, as it's only None during initialization.
        // This will be fixable when arc_new_cyclic is stable.
        self.window.clone()
    }

    /// Creates a builder for a specific type of node reference
    pub fn builder<T: Buildable>(self: &Arc<Self>) -> Builder<T> {
        Builder {
            sandbox: Arc::downgrade(self),
            _phantom: PhantomData,
        }
    }
}