Repository: servo/servo
File: components/constellation/constellation.rs (Rust)

Code before:
                        warn!("Could not forward {scroll:?} to {pipeline_id}: {error:?}");
                    }
                }
            },
            ScriptToConstellationMessage::RespondToScreenshotReadinessRequest(response) => {
                self.handle_screenshot_readiness_response(source_pipeline_id, response);
            },
            ScriptToConstellationMessage::ForceGC(callback) => {
                let event_loops = self.event_loops();
                let count = event_loops.len();
                // No event loops. Should never happen!
                if count == 0 {
                    warn!("No event loop to send ForceGC");
                    let _ = callback.send(true);
                } else {
                    // Return a failure if we are still waiting for previous ForceGC results.
                    if self.pending_force_gc.is_some() {
                        let _ = callback.send(false);
                        return;
                    }

                    self.pending_force_gc = Some((count, callback));
                    for event_loop in event_loops {
                        let _ = event_loop.send(ScriptThreadMessage::ForceGC);
                    }
                }
            },
            ScriptToConstellationMessage::ForceGCCompleted => {
                let current = self.pending_force_gc.take();

                if let Some((count, callback)) = current {
                    let remaining = count - 1;
                    if remaining == 0 {
                        let _ = callback.send(true);
                    } else {
                        self.pending_force_gc = Some((remaining, callback.clone()));
                    }
                } else {
                    error!("Unexpected ForceGCCompleted message!");
                }
            },
        }
    }

    /// Check the origin of a message against that of the pipeline it came from.
    /// Note: this is still limited as a security check,
    /// see <https://github.com/servo/servo/issues/11722>
    fn check_origin_against_pipeline(
        &self,
        pipeline_id: &PipelineId,
        origin: &ImmutableOrigin,

Review comment (suggestion):
I think this would be better as a series of early returns instead of nesting here:


```suggestion
                // This means that all event loops have closed before this garbage collection could be handled.
                if count == 0 {
                    let _ = callback.send(true);
                    return;
                }
                
                // Do not trigger another Servo-wide garbage collection if one is already pending.
                if self.pending_force_gc.is_some() {
                    let _ = callback.send(false);
                    return;
                }
                
                self.pending_force_gc = Some((count, callback));
                for event_loop in event_loops {
                    let _ = event_loop.send(ScriptThreadMessage::ForceGC);
                }
```

Code after:
                self.handle_screenshot_readiness_response(source_pipeline_id, response);
            },
            ScriptToConstellationMessage::TriggerGarbageCollection => {
                for event_loop in self.event_loops() {
                    let _ = event_loop.send(ScriptThreadMessage::TriggerGarbageCollection);
                }
            },
        }
    }

    /// Check the origin of a message against that of the pipeline it came from.
    /// Note: this is still limited as a security check,
    /// see <https://github.com/servo/servo/issues/11722>
    fn check_origin_against_pipeline(
        &self,
        pipeline_id: &PipelineId,
        origin: &ImmutableOrigin,
    ) -> Result<(), ()> {
        let pipeline_origin = match self.pipelines.get(pipeline_id) {
            Some(pipeline) => pipeline.load_data.url.origin(),
            None => {
                warn!("Received message from closed or unknown pipeline.");
                return Err(());
            },
        };
        if &pipeline_origin == origin {
            return Ok(());
        }
        Err(())
    }

    #[servo_tracing::instrument(skip_all)]
    #[cfg(feature = "webgpu")]
    fn handle_wgpu_request(
        &mut self,
        source_pipeline_id: PipelineId,
        browsing_context_id: BrowsingContextId,
        request: ScriptToConstellationMessage,
    ) {
        use webgpu::start_webgpu_thread;

        let browsing_context_group_id = match self.browsing_contexts.get(&browsing_context_id) {
            Some(bc) => &bc.bc_group_id,
            None => return warn!("Browsing context not found"),
        };
        let Some(source_pipeline) = self.pipelines.get(&source_pipeline_id) else {
            return warn!("{source_pipeline_id}: ScriptMsg from closed pipeline");
        };
        let Some(host) = registered_domain_name(&source_pipeline.url) else {
            return warn!("Invalid host url");
        };