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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
/* 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/. */

//! Application entry point, runs the event loop.

use std::cell::{Cell, RefCell, RefMut};
use std::collections::HashMap;
use std::rc::Rc;
use std::time::Instant;
use std::{env, fs};

use gleam::gl;
use log::{info, trace};
use servo::compositing::windowing::EmbedderEvent;
use servo::compositing::CompositeTarget;
use servo::config::{opts, set_pref};
use servo::embedder_traits::EventLoopWaker;
use servo::servo_config::pref;
use servo::url::ServoUrl;
use servo::Servo;
use surfman::GLApi;
use webxr::glwindow::GlWindowDiscovery;
#[cfg(target_os = "windows")]
use webxr::openxr::{AppInfo, OpenXrDiscovery};
use winit::application::ApplicationHandler;
use winit::event::WindowEvent;
use winit::event_loop::{ActiveEventLoop, ControlFlow};
use winit::window::WindowId;

use super::events_loop::{EventLoopGuard, EventsLoop, WakerEvent};
use super::minibrowser::Minibrowser;
use super::webview::WebViewManager;
use super::{headed_window, headless_window};
use crate::desktop::embedder::{EmbedderCallbacks, XrDiscovery};
use crate::desktop::events_loop::with_current_event_loop;
use crate::desktop::tracing::trace_winit_event;
use crate::desktop::window_trait::WindowPortsMethods;
use crate::parser::get_default_url;

pub struct App {
    servo: Option<Servo<dyn WindowPortsMethods>>,
    webviews: RefCell<WebViewManager<dyn WindowPortsMethods>>,
    event_queue: RefCell<Vec<EmbedderEvent>>,
    suspended: Cell<bool>,
    windows: HashMap<WindowId, Rc<dyn WindowPortsMethods>>,
    minibrowser: Option<RefCell<Minibrowser>>,
    user_agent: Option<String>,
    waker: Box<dyn EventLoopWaker>,
    initial_url: ServoUrl,
    t_start: Instant,
    t: Instant,
}

enum Present {
    Immediate,
    Deferred,
    None,
}

/// Action to be taken by the caller of [`App::handle_events`].
enum PumpResult {
    /// The caller should shut down Servo and its related context.
    Shutdown,
    Continue {
        update: bool,
        present: Present,
    },
}

impl App {
    pub fn new(
        events_loop: &EventsLoop,
        no_native_titlebar: bool,
        device_pixel_ratio_override: Option<f32>,
        user_agent: Option<String>,
        url: Option<String>,
    ) -> Self {
        // Implements window methods, used by compositor.
        let window = if opts::get().headless {
            // GL video rendering is not supported on headless windows.
            set_pref!(media.glvideo.enabled, false);
            headless_window::Window::new(
                opts::get().initial_window_size,
                device_pixel_ratio_override,
            )
        } else {
            Rc::new(headed_window::Window::new(
                opts::get().initial_window_size,
                events_loop.as_winit(),
                no_native_titlebar,
                device_pixel_ratio_override,
            ))
        };

        // Handle browser state.
        let webviews = WebViewManager::new(window.clone());
        let initial_url = get_default_url(url.as_deref(), env::current_dir().unwrap(), |path| {
            fs::metadata(path).is_ok()
        });
        let t = Instant::now();
        let mut app = App {
            event_queue: RefCell::new(vec![]),
            webviews: RefCell::new(webviews),
            servo: None,
            suspended: Cell::new(false),
            windows: HashMap::new(),
            minibrowser: None,
            user_agent,
            waker: events_loop.create_event_loop_waker(),
            initial_url: initial_url.clone(),
            t_start: t,
            t,
        };

        if window.winit_window().is_some() {
            // Make sure the gl context is made current.
            let rendering_context = window.rendering_context();
            let webrender_gl = match rendering_context.connection().gl_api() {
                GLApi::GL => unsafe {
                    gl::GlFns::load_with(|s| rendering_context.get_proc_address(s))
                },
                GLApi::GLES => unsafe {
                    gl::GlesFns::load_with(|s| rendering_context.get_proc_address(s))
                },
            };
            rendering_context.make_gl_context_current().unwrap();
            debug_assert_eq!(webrender_gl.get_error(), gleam::gl::NO_ERROR);

            app.minibrowser = Some(
                Minibrowser::new(
                    &rendering_context,
                    events_loop.as_winit(),
                    initial_url.clone(),
                )
                .into(),
            );
        }

        if let Some(mut minibrowser) = app.minibrowser() {
            // Servo is not yet initialised, so there is no `servo_framebuffer_id`.
            minibrowser.update(
                window.winit_window().unwrap(),
                &mut app.webviews.borrow_mut(),
                None,
                "init",
            );
            window.set_toolbar_height(minibrowser.toolbar_height);
        }

        app.windows.insert(window.id(), window);

        app
    }

    /// Initialize Application once event loop start running.
    pub fn init(&mut self) {
        self.suspended.set(false);
        self.event_queue.borrow_mut().push(EmbedderEvent::Idle);
        let (_, window) = self.windows.iter().next().unwrap();
        let surfman = window.rendering_context();

        let openxr_discovery = if pref!(dom.webxr.openxr.enabled) && !opts::get().headless {
            #[cfg(target_os = "windows")]
            let openxr = {
                let app_info = AppInfo::new("Servoshell", 0, "Servo", 0);
                Some(XrDiscovery::OpenXr(OpenXrDiscovery::new(None, app_info)))
            };
            #[cfg(not(target_os = "windows"))]
            let openxr = None;

            openxr
        } else {
            None
        };

        let glwindow_discovery = if pref!(dom.webxr.glwindow.enabled) && !opts::get().headless {
            let window = window.clone();
            let factory = Box::new(move || {
                with_current_event_loop(|w| Ok(window.new_glwindow(w)))
                    .expect("An event loop should always be active in headed mode")
            });
            Some(XrDiscovery::GlWindow(GlWindowDiscovery::new(
                surfman.connection(),
                surfman.adapter(),
                surfman.context_attributes(),
                factory,
            )))
        } else {
            None
        };

        let xr_discovery = openxr_discovery.or(glwindow_discovery);

        let window = window.clone();
        // Implements embedder methods, used by libservo and constellation.
        let embedder = Box::new(EmbedderCallbacks::new(self.waker.clone(), xr_discovery));

        let composite_target = if self.minibrowser.is_some() {
            CompositeTarget::Fbo
        } else {
            CompositeTarget::Window
        };
        let servo_data = Servo::new(
            embedder,
            window.clone(),
            self.user_agent.clone(),
            composite_target,
        );
        let mut servo = servo_data.servo;

        servo.handle_events(vec![EmbedderEvent::NewWebView(
            self.initial_url.to_owned(),
            servo_data.browser_id,
        )]);
        servo.setup_logging();

        self.servo = Some(servo);
    }

    pub fn is_animating(&self) -> bool {
        self.windows.iter().any(|(_, window)| window.is_animating())
    }

    fn get_events(&self) -> Vec<EmbedderEvent> {
        std::mem::take(&mut *self.event_queue.borrow_mut())
    }

    /// Pumps events and messages between the embedder and Servo, where embedder events flow
    /// towards Servo and embedder messages flow away from Servo, and also runs the compositor.
    ///
    /// As the embedder, we push embedder events through our event queues, from the App queue and
    /// Window queues to the WebViewManager queue, and from the WebViewManager queue to Servo. We
    /// receive and collect embedder messages from the various Servo components, then take them out
    /// of the Servo interface so that the WebViewManager can handle them.
    fn handle_events(&mut self) -> PumpResult {
        let mut webviews = self.webviews.borrow_mut();

        // Take any outstanding embedder events from the App and its Windows.
        let mut embedder_events = self.get_events();
        for window in self.windows.values() {
            embedder_events.extend(window.get_events());
        }

        // Catch some keyboard events, and push the rest onto the WebViewManager event queue.
        webviews.handle_window_events(embedder_events);

        // If the Gamepad API is enabled, handle gamepad events from GilRs.
        // Checking for focused_webview_id should ensure we'll have a valid browsing context.
        if pref!(dom.gamepad.enabled) && webviews.focused_webview_id().is_some() {
            webviews.handle_gamepad_events();
        }

        // Take any new embedder messages from Servo itself.
        let mut embedder_messages = self.servo.as_mut().unwrap().get_events();
        let mut need_resize = false;
        let mut need_present = false;
        let mut need_update = false;
        loop {
            // Consume and handle those embedder messages.
            let servo_event_response = webviews.handle_servo_events(embedder_messages);
            need_present |= servo_event_response.need_present;
            need_update |= servo_event_response.need_update;

            // Route embedder events from the WebViewManager to the relevant Servo components,
            // receives and collects embedder messages from various Servo components,
            // and runs the compositor.
            need_resize |= self
                .servo
                .as_mut()
                .unwrap()
                .handle_events(webviews.get_events());
            if webviews.shutdown_requested() {
                return PumpResult::Shutdown;
            }

            // Take any new embedder messages from Servo itself.
            embedder_messages = self.servo.as_mut().unwrap().get_events();
            if embedder_messages.len() == 0 {
                break;
            }
        }

        let present = if need_resize {
            Present::Immediate
        } else if need_present {
            Present::Deferred
        } else {
            Present::None
        };

        PumpResult::Continue {
            update: need_update,
            present,
        }
    }

    /// Handle events with winit contexts
    pub fn handle_events_with_winit(
        &mut self,
        event_loop: &ActiveEventLoop,
        window: Rc<dyn WindowPortsMethods>,
    ) {
        match self.handle_events() {
            PumpResult::Shutdown => {
                event_loop.exit();
                self.servo.take().unwrap().deinit();
                if let Some(mut minibrowser) = self.minibrowser() {
                    minibrowser.context.destroy();
                }
            },
            PumpResult::Continue { update, present } => {
                if update {
                    if let Some(mut minibrowser) = self.minibrowser() {
                        let webviews = &mut self.webviews.borrow_mut();
                        if minibrowser.update_webview_data(webviews) {
                            // Update the minibrowser immediately. While we could update by requesting a
                            // redraw, doing so would delay the location update by two frames.
                            minibrowser.update(
                                window.winit_window().unwrap(),
                                webviews,
                                self.servo.as_ref().unwrap().offscreen_framebuffer_id(),
                                "update_location_in_toolbar",
                            );
                        }
                    }
                }
                match present {
                    Present::Immediate => {
                        // The window was resized.
                        trace!("PumpResult::Present::Immediate");

                        // If we had resized any of the viewports in response to this, we would need to
                        // call Servo::repaint_synchronously. At the moment we don’t, so there won’t be
                        // any paint scheduled, and calling it would hang the compositor forever.
                        if let Some(mut minibrowser) = self.minibrowser() {
                            minibrowser.update(
                                window.winit_window().unwrap(),
                                &mut self.webviews.borrow_mut(),
                                self.servo.as_ref().unwrap().offscreen_framebuffer_id(),
                                "PumpResult::Present::Immediate",
                            );
                            minibrowser.paint(window.winit_window().unwrap());
                        }
                        self.servo.as_mut().unwrap().present();
                    },
                    Present::Deferred => {
                        // The compositor has painted to this frame.
                        trace!("PumpResult::Present::Deferred");
                        // Request a winit redraw event, so we can paint the minibrowser and present.
                        // Otherwise, it's in headless mode and we present directly.
                        if let Some(window) = window.winit_window() {
                            window.request_redraw();
                        } else {
                            self.servo.as_mut().unwrap().present();
                        }
                    },
                    Present::None => {},
                }
            },
        }
    }

    /// Handle all servo events with headless mode. Return true if servo request to shutdown.
    pub fn handle_events_with_headless(&mut self) -> bool {
        let now = Instant::now();
        let event = winit::event::Event::UserEvent(WakerEvent);
        trace_winit_event!(
            event,
            "@{:?} (+{:?}) {event:?}",
            now - self.t_start,
            now - self.t
        );
        self.t = now;
        // If self.servo is None here, it means that we're in the process of shutting down,
        // let's ignore events.
        if self.servo.is_none() {
            return false;
        }
        self.event_queue.borrow_mut().push(EmbedderEvent::Idle);

        let mut exit = false;
        match self.handle_events() {
            PumpResult::Shutdown => {
                exit = true;
                self.servo.take().unwrap().deinit();
                if let Some(mut minibrowser) = self.minibrowser() {
                    minibrowser.context.destroy();
                }
            },
            PumpResult::Continue { present, .. } => {
                match present {
                    Present::Immediate => {
                        // The window was resized.
                        trace!("PumpResult::Present::Immediate");
                        self.servo.as_mut().unwrap().present();
                    },
                    Present::Deferred => {
                        // The compositor has painted to this frame.
                        trace!("PumpResult::Present::Deferred");
                        // In headless mode, we present directly.
                        self.servo.as_mut().unwrap().present();
                    },
                    Present::None => {},
                }
            },
        }
        exit
    }

    fn minibrowser(&self) -> Option<RefMut<Minibrowser>> {
        self.minibrowser.as_ref().map(|x| x.borrow_mut())
    }
}

impl ApplicationHandler<WakerEvent> for App {
    fn resumed(&mut self, event_loop: &ActiveEventLoop) {
        let _guard = EventLoopGuard::new(event_loop);
        self.init();
    }

    fn window_event(
        &mut self,
        event_loop: &ActiveEventLoop,
        window_id: WindowId,
        event: WindowEvent,
    ) {
        let now = Instant::now();
        trace_winit_event!(
            event,
            "@{:?} (+{:?}) {event:?}",
            now - self.t_start,
            now - self.t
        );
        self.t = now;
        // If self.servo is None here, it means that we're in the process of shutting down,
        // let's ignore events.
        if self.servo.is_none() {
            return;
        }

        let Some(window) = self.windows.get(&window_id) else {
            return;
        };
        let window = window.clone();

        if event == winit::event::WindowEvent::RedrawRequested {
            // We need to redraw the window for some reason.
            trace!("RedrawRequested");

            // WARNING: do not defer painting or presenting to some later tick of the event
            // loop or servoshell may become unresponsive! (servo#30312)
            if let Some(mut minibrowser) = self.minibrowser() {
                minibrowser.update(
                    window.winit_window().unwrap(),
                    &mut self.webviews.borrow_mut(),
                    self.servo.as_ref().unwrap().offscreen_framebuffer_id(),
                    "RedrawRequested",
                );
                minibrowser.paint(window.winit_window().unwrap());
            }

            self.servo.as_mut().unwrap().present();
        }

        // Handle the event
        let mut consumed = false;
        if let Some(mut minibrowser) = self.minibrowser() {
            match event {
                WindowEvent::ScaleFactorChanged { scale_factor, .. } => {
                    // Intercept any ScaleFactorChanged events away from EguiGlow::on_window_event, so
                    // we can use our own logic for calculating the scale factor and set egui’s
                    // scale factor to that value manually.
                    let desired_scale_factor = window.hidpi_factor().get();
                    let effective_egui_zoom_factor = desired_scale_factor / scale_factor as f32;

                    info!(
                        "window scale factor changed to {}, setting egui zoom factor to {}",
                        scale_factor, effective_egui_zoom_factor
                    );

                    minibrowser
                        .context
                        .egui_ctx
                        .set_zoom_factor(effective_egui_zoom_factor);

                    // Request a winit redraw event, so we can recomposite, update and paint
                    // the minibrowser, and present the new frame.
                    window.winit_window().unwrap().request_redraw();
                },
                ref event => {
                    let response =
                        minibrowser.on_window_event(window.winit_window().unwrap(), event);
                    // Update minibrowser if there's resize event to sync up with window.
                    if let WindowEvent::Resized(_) = event {
                        minibrowser.update(
                            window.winit_window().unwrap(),
                            &mut self.webviews.borrow_mut(),
                            self.servo.as_ref().unwrap().offscreen_framebuffer_id(),
                            "Sync WebView size with Window Resize event",
                        );
                    }
                    if response.repaint && *event != winit::event::WindowEvent::RedrawRequested {
                        // Request a winit redraw event, so we can recomposite, update and paint
                        // the minibrowser, and present the new frame.
                        window.winit_window().unwrap().request_redraw();
                    }

                    // TODO how do we handle the tab key? (see doc for consumed)
                    // Note that servo doesn’t yet support tabbing through links and inputs
                    consumed = response.consumed;
                },
            }
        }
        if !consumed {
            if event == winit::event::WindowEvent::RedrawRequested {
                self.event_queue.borrow_mut().push(EmbedderEvent::Idle);
            }

            window.queue_embedder_events_for_winit_event(event);
        }

        let animating = self.is_animating();

        // Block until the window gets an event
        if !animating || self.suspended.get() {
            event_loop.set_control_flow(ControlFlow::Wait);
        } else {
            event_loop.set_control_flow(ControlFlow::Poll);
        }

        // Consume and handle any events from the Minibrowser.
        if let Some(minibrowser) = self.minibrowser() {
            let webviews = &mut self.webviews.borrow_mut();
            let app_event_queue = &mut self.event_queue.borrow_mut();
            minibrowser.queue_embedder_events_for_minibrowser_events(webviews, app_event_queue);
        }

        self.handle_events_with_winit(event_loop, window);
    }

    fn user_event(&mut self, event_loop: &ActiveEventLoop, event: WakerEvent) {
        let now = Instant::now();
        let event = winit::event::Event::UserEvent(event);
        trace_winit_event!(
            event,
            "@{:?} (+{:?}) {event:?}",
            now - self.t_start,
            now - self.t
        );
        self.t = now;
        // If self.servo is None here, it means that we're in the process of shutting down,
        // let's ignore events.
        if self.servo.is_none() {
            return;
        }
        self.event_queue.borrow_mut().push(EmbedderEvent::Idle);

        let Some(window) = self.windows.values().next() else {
            return;
        };
        let window = window.clone();

        let animating = self.is_animating();

        // Block until the window gets an event
        if !animating || self.suspended.get() {
            event_loop.set_control_flow(ControlFlow::Wait);
        } else {
            event_loop.set_control_flow(ControlFlow::Poll);
        }

        // Consume and handle any events from the Minibrowser.
        if let Some(minibrowser) = self.minibrowser() {
            let webviews = &mut self.webviews.borrow_mut();
            let app_event_queue = &mut self.event_queue.borrow_mut();
            minibrowser.queue_embedder_events_for_minibrowser_events(webviews, app_event_queue);
        }

        self.handle_events_with_winit(event_loop, window);
    }

    fn suspended(&mut self, _: &ActiveEventLoop) {
        self.suspended.set(true);
    }
}