aboutsummaryrefslogtreecommitdiffstats
path: root/components/servo/webview.rs
blob: 03dd954f688e5516b3055078ed7f7cc90c25176b (plain) (blame)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* 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/. */

use std::cell::RefCell;
use std::rc::Rc;

use base::id::WebViewId;
use compositing::IOCompositor;
use compositing_traits::ConstellationMsg;
use webrender_api::units::DeviceRect;

use crate::ConstellationProxy;

pub struct WebView(Rc<WebViewInner>);

struct WebViewInner {
    // TODO: ensure that WebView instances interact with the correct Servo instance
    pub(crate) id: WebViewId,
    pub(crate) constellation_proxy: ConstellationProxy,
    pub(crate) compositor: Rc<RefCell<IOCompositor>>,
}

impl Drop for WebViewInner {
    fn drop(&mut self) {
        self.constellation_proxy
            .send(ConstellationMsg::CloseWebView(self.id));
    }
}

/// Handle for a webview.
///
/// - The webview exists for exactly as long as there are WebView handles
///   (FIXME: this is not true yet; webviews can still close of their own volition)
/// - All methods are infallible; if the constellation dies, the embedder finds out when calling
///   [Servo::handle_events](crate::Servo::handle_events)
impl WebView {
    pub(crate) fn new(
        constellation_proxy: &ConstellationProxy,
        compositor: Rc<RefCell<IOCompositor>>,
        url: url::Url,
    ) -> Self {
        let webview_id = WebViewId::new();
        constellation_proxy.send(ConstellationMsg::NewWebView(url.into(), webview_id));

        Self(Rc::new(WebViewInner {
            id: webview_id,
            constellation_proxy: constellation_proxy.clone(),
            compositor,
        }))
    }

    /// FIXME: Remove this once we have a webview delegate.
    pub(crate) fn new_auxiliary(
        constellation_proxy: &ConstellationProxy,
        compositor: Rc<RefCell<IOCompositor>>,
    ) -> Self {
        let webview_id = WebViewId::new();

        Self(
            WebViewInner {
                id: webview_id,
                constellation_proxy: constellation_proxy.clone(),
                compositor,
            }
            .into(),
        )
    }

    /// FIXME: Remove this once we have a webview delegate.
    pub fn id(&self) -> WebViewId {
        self.0.id
    }

    pub fn focus(&self) {
        self.0
            .constellation_proxy
            .send(ConstellationMsg::FocusWebView(self.id()));
    }

    pub fn blur(&self) {
        self.0
            .constellation_proxy
            .send(ConstellationMsg::BlurWebView);
    }

    pub fn move_resize(&self, rect: DeviceRect) {
        self.0
            .compositor
            .borrow_mut()
            .move_resize_webview(self.id(), rect);
    }

    pub fn show(&self, hide_others: bool) {
        self.0
            .compositor
            .borrow_mut()
            .show_webview(self.id(), hide_others)
            .expect("BUG: invalid WebView instance");
    }

    pub fn hide(&self) {
        self.0
            .compositor
            .borrow_mut()
            .hide_webview(self.id())
            .expect("BUG: invalid WebView instance");
    }

    pub fn raise_to_top(&self, hide_others: bool) {
        self.0
            .compositor
            .borrow_mut()
            .raise_webview_to_top(self.id(), hide_others)
            .expect("BUG: invalid WebView instance");
    }
}