Repository: servo/servo
File: components/script/script_thread.rs (Rust)

Code before:
    }

    /// Step 13 of <https://html.spec.whatwg.org/multipage/#navigate>
    pub(crate) fn navigate(
        pipeline_id: PipelineId,
        mut load_data: LoadData,
        history_handling: NavigationHistoryBehavior,
    ) {
        with_script_thread(|script_thread| {
            let is_javascript = load_data.url.scheme() == "javascript";
            // If resource is a request whose url's scheme is "javascript"
            // https://html.spec.whatwg.org/multipage/#navigate-to-a-javascript:-url
            if is_javascript {
                let window = match script_thread.documents.borrow().find_window(pipeline_id) {
                    None => return,
                    Some(window) => window,
                };
                let global = window.as_global_scope();
                let trusted_global = Trusted::new(global);
                let sender = script_thread
                    .senders
                    .pipeline_to_constellation_sender
                    .clone();
                let task = task!(navigate_javascript: move || {
                    // Important re security. See https://github.com/servo/servo/issues/23373
                    if trusted_global.root().downcast::<Window>().is_some() {
                        let global = &trusted_global.root();
                        if Self::navigate_to_javascript_url(global, global, &mut load_data, None, CanGc::note()) {
                            sender
                                .send((pipeline_id, ScriptToConstellationMessage::LoadUrl(load_data, history_handling)))
                                .unwrap();
                        }
                    }
                });
                // Step 19 of <https://html.spec.whatwg.org/multipage/#navigate>
                global
                    .task_manager()
                    .dom_manipulation_task_source()
                    .queue(task);
            } else {
                script_thread
                    .senders
                    .pipeline_to_constellation_sender
                    .send((
                        pipeline_id,
                        ScriptToConstellationMessage::LoadUrl(load_data, history_handling),
                    ))
                    .expect("Sending a LoadUrl message to the constellation failed");
            }
        });
    }

Review comment (suggestion):
This can use `is::<Window>()` instead of downcast+is_some.

Code after:
    }

    /// Step 13 of <https://html.spec.whatwg.org/multipage/#navigate>
    pub(crate) fn navigate(
        pipeline_id: PipelineId,
        mut load_data: LoadData,
        history_handling: NavigationHistoryBehavior,
    ) {
        with_script_thread(|script_thread| {
            let is_javascript = load_data.url.scheme() == "javascript";
            // If resource is a request whose url's scheme is "javascript"
            // https://html.spec.whatwg.org/multipage/#navigate-to-a-javascript:-url
            if is_javascript {
                let window = match script_thread.documents.borrow().find_window(pipeline_id) {
                    None => return,
                    Some(window) => window,
                };
                let global = window.as_global_scope();
                let trusted_global = Trusted::new(global);
                let sender = script_thread
                    .senders
                    .pipeline_to_constellation_sender
                    .clone();
                let task = task!(navigate_javascript: move || {
                    // Important re security. See https://github.com/servo/servo/issues/23373
                    if trusted_global.root().is::<Window>() {
                        let global = &trusted_global.root();
                        if Self::navigate_to_javascript_url(global, global, &mut load_data, None, CanGc::note()) {
                            sender
                                .send((pipeline_id, ScriptToConstellationMessage::LoadUrl(load_data, history_handling)))
                                .unwrap();
                        }
                    }
                });
                // Step 19 of <https://html.spec.whatwg.org/multipage/#navigate>
                global
                    .task_manager()
                    .dom_manipulation_task_source()
                    .queue(task);
            } else {
                script_thread
                    .senders
                    .pipeline_to_constellation_sender
                    .send((
                        pipeline_id,
                        ScriptToConstellationMessage::LoadUrl(load_data, history_handling),
                    ))
                    .expect("Sending a LoadUrl message to the constellation failed");
            }
        });
    }