aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/servointernals.rs
diff options
context:
space:
mode:
authorwebbeef <me@webbeef.org>2025-03-06 21:25:08 -0800
committerGitHub <noreply@github.com>2025-03-07 05:25:08 +0000
commit139774e6b55c297bc94f7fcb8c9bf5bb8c6a7474 (patch)
tree70f425d369998013ca8ed559a6719a6c280f7378 /components/script/dom/servointernals.rs
parent1864ebfb357cdf4ac6f97d53c5f74f989f08b2ec (diff)
downloadservo-139774e6b55c297bc94f7fcb8c9bf5bb8c6a7474.tar.gz
servo-139774e6b55c297bc94f7fcb8c9bf5bb8c6a7474.zip
Add an about:memory page (#35728)
This patch exposes a servo internal DOM API that is only made available to about: pages on the navigator object to request memory reports. The about:memory page itself is loaded like other html resources (eg. bad cert, net error) and makes use of this new API. On the implementation side, notable changes: - components/script/routed_promise.rs abstracts the setup used to fulfill a promise when the work needs to be routed through the constellation. The goal is to migrate other similar promise APIs in followup (eg. dom/webgpu/gpu.rs, bluetooth.rs). - a new message is added to request a report from the memory reporter, and the memory reporter creates a json representation of the set of memory reports. - the post-processing of memory reports is done in Javascript in the about-memory.html page, providing the same results as the current Rust code that outputs to stdout. We can decide later if we want to remove the current output. Signed-off-by: webbeef <me@webbeef.org>
Diffstat (limited to 'components/script/dom/servointernals.rs')
-rw-r--r--components/script/dom/servointernals.rs62
1 files changed, 62 insertions, 0 deletions
diff --git a/components/script/dom/servointernals.rs b/components/script/dom/servointernals.rs
new file mode 100644
index 00000000000..7fe0bf85122
--- /dev/null
+++ b/components/script/dom/servointernals.rs
@@ -0,0 +1,62 @@
+/* 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::rc::Rc;
+
+use dom_struct::dom_struct;
+use profile_traits::mem::MemoryReportResult;
+use script_traits::ScriptMsg;
+
+use crate::dom::bindings::codegen::Bindings::ServoInternalsBinding::ServoInternalsMethods;
+use crate::dom::bindings::error::Error;
+use crate::dom::bindings::reflector::{DomGlobal, Reflector, reflect_dom_object};
+use crate::dom::bindings::root::DomRoot;
+use crate::dom::globalscope::GlobalScope;
+use crate::dom::promise::Promise;
+use crate::realms::InRealm;
+use crate::routed_promise::{RoutedPromiseListener, route_promise};
+use crate::script_runtime::CanGc;
+
+#[dom_struct]
+pub(crate) struct ServoInternals {
+ reflector_: Reflector,
+}
+
+impl ServoInternals {
+ pub fn new_inherited() -> ServoInternals {
+ ServoInternals {
+ reflector_: Reflector::new(),
+ }
+ }
+
+ pub(crate) fn new(global: &GlobalScope, can_gc: CanGc) -> DomRoot<ServoInternals> {
+ reflect_dom_object(Box::new(ServoInternals::new_inherited()), global, can_gc)
+ }
+}
+
+impl ServoInternalsMethods<crate::DomTypeHolder> for ServoInternals {
+ /// <https://servo.org/internal-no-spec>
+ fn ReportMemory(&self, comp: InRealm, can_gc: CanGc) -> Rc<Promise> {
+ let global = &self.global();
+ let promise = Promise::new_in_current_realm(comp, can_gc);
+ let sender = route_promise(&promise, self);
+ let script_to_constellation_chan = global.script_to_constellation_chan();
+ if script_to_constellation_chan
+ .send(ScriptMsg::ReportMemory(sender))
+ .is_err()
+ {
+ promise.reject_error(Error::Operation, can_gc);
+ }
+ promise
+ }
+}
+
+impl RoutedPromiseListener for ServoInternals {
+ type Response = MemoryReportResult;
+
+ #[cfg_attr(crown, allow(crown::unrooted_must_root))]
+ fn handle_response(&self, response: Self::Response, promise: &Rc<Promise>, can_gc: CanGc) {
+ promise.resolve_native(&response.content, can_gc);
+ }
+}