script/dom/html/
htmliframeelement.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::cell::Cell;
6use std::rc::Rc;
7
8use base::id::{BrowsingContextId, PipelineId, WebViewId};
9use constellation_traits::{
10    IFrameLoadInfo, IFrameLoadInfoWithData, JsEvalResult, LoadData, LoadOrigin,
11    NavigationHistoryBehavior, ScriptToConstellationMessage,
12};
13use content_security_policy::sandboxing_directive::{
14    SandboxingFlagSet, parse_a_sandboxing_directive,
15};
16use dom_struct::dom_struct;
17use embedder_traits::ViewportDetails;
18use html5ever::{LocalName, Prefix, local_name, ns};
19use js::rust::HandleObject;
20use net_traits::ReferrerPolicy;
21use net_traits::request::Destination;
22use profile_traits::ipc as ProfiledIpc;
23use script_traits::{NewPipelineInfo, UpdatePipelineIdReason};
24use servo_url::ServoUrl;
25use style::attr::{AttrValue, LengthOrPercentageOrAuto};
26use stylo_atoms::Atom;
27
28use crate::document_loader::{LoadBlocker, LoadType};
29use crate::dom::attr::Attr;
30use crate::dom::bindings::cell::DomRefCell;
31use crate::dom::bindings::codegen::Bindings::HTMLIFrameElementBinding::HTMLIFrameElementMethods;
32use crate::dom::bindings::codegen::Bindings::WindowBinding::Window_Binding::WindowMethods;
33use crate::dom::bindings::codegen::UnionTypes::TrustedHTMLOrString;
34use crate::dom::bindings::error::Fallible;
35use crate::dom::bindings::inheritance::Castable;
36use crate::dom::bindings::reflector::DomGlobal;
37use crate::dom::bindings::root::{DomRoot, LayoutDom, MutNullableDom};
38use crate::dom::bindings::str::{DOMString, USVString};
39use crate::dom::document::Document;
40use crate::dom::domtokenlist::DOMTokenList;
41use crate::dom::element::{
42    AttributeMutation, Element, LayoutElementHelpers, reflect_referrer_policy_attribute,
43};
44use crate::dom::eventtarget::EventTarget;
45use crate::dom::globalscope::GlobalScope;
46use crate::dom::html::htmlelement::HTMLElement;
47use crate::dom::node::{BindContext, Node, NodeDamage, NodeTraits, UnbindContext};
48use crate::dom::performance::performanceresourcetiming::InitiatorType;
49use crate::dom::trustedhtml::TrustedHTML;
50use crate::dom::virtualmethods::VirtualMethods;
51use crate::dom::windowproxy::WindowProxy;
52use crate::network_listener::ResourceTimingListener;
53use crate::script_runtime::CanGc;
54use crate::script_thread::{ScriptThread, with_script_thread};
55use crate::script_window_proxies::ScriptWindowProxies;
56
57#[derive(PartialEq)]
58enum PipelineType {
59    InitialAboutBlank,
60    Navigation,
61}
62
63#[derive(PartialEq)]
64enum ProcessingMode {
65    FirstTime,
66    NotFirstTime,
67}
68
69#[dom_struct]
70pub(crate) struct HTMLIFrameElement {
71    htmlelement: HTMLElement,
72    #[no_trace]
73    webview_id: Cell<Option<WebViewId>>,
74    #[no_trace]
75    browsing_context_id: Cell<Option<BrowsingContextId>>,
76    #[no_trace]
77    pipeline_id: Cell<Option<PipelineId>>,
78    #[no_trace]
79    pending_pipeline_id: Cell<Option<PipelineId>>,
80    #[no_trace]
81    about_blank_pipeline_id: Cell<Option<PipelineId>>,
82    sandbox: MutNullableDom<DOMTokenList>,
83    #[no_trace]
84    sandboxing_flag_set: Cell<Option<SandboxingFlagSet>>,
85    load_blocker: DomRefCell<Option<LoadBlocker>>,
86    throttled: Cell<bool>,
87    #[conditional_malloc_size_of]
88    script_window_proxies: Rc<ScriptWindowProxies>,
89    /// Keeping track of whether the iframe will be navigated
90    /// outside of the processing of it's attribute(for example: form navigation).
91    /// This is necessary to prevent the iframe load event steps
92    /// from asynchronously running for the initial blank document
93    /// while script at this point(when the flag is set)
94    /// expects those to run only for the navigated documented.
95    pending_navigation: Cell<bool>,
96    /// Whether a load event was synchronously fired, for example when
97    /// an empty iframe is attached. In that case, we shouldn't fire a
98    /// subsequent asynchronous load event.
99    already_fired_synchronous_load_event: Cell<bool>,
100}
101
102impl HTMLIFrameElement {
103    /// <https://html.spec.whatwg.org/multipage/#shared-attribute-processing-steps-for-iframe-and-frame-elements>,
104    fn shared_attribute_processing_steps_for_iframe_and_frame_elements(&self) -> Option<ServoUrl> {
105        let element = self.upcast::<Element>();
106        // Step 2. If element has a src attribute specified, and its value is not the empty string, then:
107        let url = element
108            .get_attribute(&ns!(), &local_name!("src"))
109            .and_then(|src| {
110                let url = src.value();
111                if url.is_empty() {
112                    None
113                } else {
114                    // Step 2.1. Let maybeURL be the result of encoding-parsing a URL given that attribute's value,
115                    // relative to element's node document.
116                    // Step 2.2. If maybeURL is not failure, then set url to maybeURL.
117                    self.owner_document().base_url().join(&url).ok()
118                }
119            })
120            // Step 1. Let url be the URL record about:blank.
121            .unwrap_or_else(|| ServoUrl::parse("about:blank").unwrap());
122        // Step 3. If the inclusive ancestor navigables of element's node navigable contains
123        // a navigable whose active document's URL equals url with exclude fragments set to true, then return null.
124        // TODO
125
126        // Step 4. If url matches about:blank and initialInsertion is true, then perform the URL and history update steps
127        // given element's content navigable's active document and url.
128        // TODO
129
130        // Step 5. Return url.
131        Some(url)
132    }
133
134    pub(crate) fn navigate_or_reload_child_browsing_context(
135        &self,
136        load_data: LoadData,
137        history_handling: NavigationHistoryBehavior,
138        can_gc: CanGc,
139    ) {
140        // In case we fired a synchronous load event, but navigate away
141        // in the event listener of that event, then we should still
142        // fire a second asynchronous load event when that navigation
143        // finishes. Therefore, on any navigation (but not the initial
144        // about blank), we should always set this to false, regardless
145        // of whether we synchronously fired a load in the same microtask.
146        self.already_fired_synchronous_load_event.set(false);
147
148        self.start_new_pipeline(
149            load_data,
150            PipelineType::Navigation,
151            history_handling,
152            can_gc,
153        );
154    }
155
156    fn start_new_pipeline(
157        &self,
158        mut load_data: LoadData,
159        pipeline_type: PipelineType,
160        history_handling: NavigationHistoryBehavior,
161        can_gc: CanGc,
162    ) {
163        let browsing_context_id = match self.browsing_context_id() {
164            None => return warn!("Attempted to start a new pipeline on an unattached iframe."),
165            Some(id) => id,
166        };
167
168        let webview_id = match self.webview_id() {
169            None => return warn!("Attempted to start a new pipeline on an unattached iframe."),
170            Some(id) => id,
171        };
172
173        let document = self.owner_document();
174
175        {
176            let load_blocker = &self.load_blocker;
177            // Any oustanding load is finished from the point of view of the blocked
178            // document; the new navigation will continue blocking it.
179            LoadBlocker::terminate(load_blocker, can_gc);
180        }
181
182        if load_data.url.scheme() == "javascript" {
183            let window_proxy = self.GetContentWindow();
184            if let Some(window_proxy) = window_proxy {
185                if !ScriptThread::navigate_to_javascript_url(
186                    &document.global(),
187                    &window_proxy.global(),
188                    &mut load_data,
189                    Some(self.upcast()),
190                    can_gc,
191                ) {
192                    return;
193                }
194                load_data.about_base_url = document.about_base_url();
195            }
196        }
197
198        match load_data.js_eval_result {
199            Some(JsEvalResult::NoContent) => (),
200            _ => {
201                let mut load_blocker = self.load_blocker.borrow_mut();
202                *load_blocker = Some(LoadBlocker::new(
203                    &document,
204                    LoadType::Subframe(load_data.url.clone()),
205                ));
206            },
207        };
208
209        let window = self.owner_window();
210        let old_pipeline_id = self.pipeline_id();
211        let new_pipeline_id = PipelineId::new();
212        self.pending_pipeline_id.set(Some(new_pipeline_id));
213
214        let load_info = IFrameLoadInfo {
215            parent_pipeline_id: window.pipeline_id(),
216            browsing_context_id,
217            webview_id,
218            new_pipeline_id,
219            is_private: false, // FIXME
220            inherited_secure_context: load_data.inherited_secure_context,
221            history_handling,
222        };
223
224        let viewport_details = window
225            .get_iframe_viewport_details_if_known(browsing_context_id)
226            .unwrap_or_else(|| ViewportDetails {
227                hidpi_scale_factor: window.device_pixel_ratio(),
228                ..Default::default()
229            });
230
231        match pipeline_type {
232            PipelineType::InitialAboutBlank => {
233                self.about_blank_pipeline_id.set(Some(new_pipeline_id));
234
235                let load_info = IFrameLoadInfoWithData {
236                    info: load_info,
237                    load_data: load_data.clone(),
238                    old_pipeline_id,
239                    viewport_details,
240                    theme: window.theme(),
241                };
242                window
243                    .as_global_scope()
244                    .script_to_constellation_chan()
245                    .send(ScriptToConstellationMessage::ScriptNewIFrame(load_info))
246                    .unwrap();
247
248                let new_pipeline_info = NewPipelineInfo {
249                    parent_info: Some(window.pipeline_id()),
250                    new_pipeline_id,
251                    browsing_context_id,
252                    webview_id,
253                    opener: None,
254                    load_data,
255                    viewport_details,
256                    user_content_manager_id: None,
257                    theme: window.theme(),
258                };
259
260                self.pipeline_id.set(Some(new_pipeline_id));
261                with_script_thread(|script_thread| {
262                    script_thread.spawn_pipeline(new_pipeline_info);
263                });
264            },
265            PipelineType::Navigation => {
266                let load_info = IFrameLoadInfoWithData {
267                    info: load_info,
268                    load_data,
269                    old_pipeline_id,
270                    viewport_details,
271                    theme: window.theme(),
272                };
273                window
274                    .as_global_scope()
275                    .script_to_constellation_chan()
276                    .send(ScriptToConstellationMessage::ScriptLoadedURLInIFrame(
277                        load_info,
278                    ))
279                    .unwrap();
280            },
281        }
282    }
283
284    /// When an iframe is first inserted into the document,
285    /// an "about:blank" document is created,
286    /// and synchronously processed by the script thread.
287    /// This initial synchronous load should have no noticeable effect in script.
288    /// See the note in `iframe_load_event_steps`.
289    pub(crate) fn is_initial_blank_document(&self) -> bool {
290        self.pending_pipeline_id.get() == self.about_blank_pipeline_id.get()
291    }
292
293    /// <https://html.spec.whatwg.org/multipage/#process-the-iframe-attributes>
294    fn process_the_iframe_attributes(&self, mode: ProcessingMode, can_gc: CanGc) {
295        let element = self.upcast::<Element>();
296        // Step 1. If `element`'s `srcdoc` attribute is specified, then:
297        //
298        // Note that this also includes the empty string
299        if element.has_attribute(&local_name!("srcdoc")) {
300            let url = ServoUrl::parse("about:srcdoc").unwrap();
301            let document = self.owner_document();
302            let window = self.owner_window();
303            let pipeline_id = Some(window.pipeline_id());
304            let mut load_data = LoadData::new(
305                LoadOrigin::Script(document.origin().snapshot()),
306                url,
307                Some(document.base_url()),
308                pipeline_id,
309                window.as_global_scope().get_referrer(),
310                document.get_referrer_policy(),
311                Some(window.as_global_scope().is_secure_context()),
312                Some(document.insecure_requests_policy()),
313                document.has_trustworthy_ancestor_or_current_origin(),
314                self.sandboxing_flag_set(),
315            );
316            load_data.destination = Destination::IFrame;
317            load_data.policy_container = Some(window.as_global_scope().policy_container());
318            load_data.srcdoc = String::from(element.get_string_attribute(&local_name!("srcdoc")));
319            self.navigate_or_reload_child_browsing_context(
320                load_data,
321                NavigationHistoryBehavior::Push,
322                can_gc,
323            );
324            return;
325        }
326
327        let window = self.owner_window();
328
329        // https://html.spec.whatwg.org/multipage/#attr-iframe-name
330        // Note: the spec says to set the name 'when the nested browsing context is created'.
331        // The current implementation sets the name on the window,
332        // when the iframe attributes are first processed.
333        if mode == ProcessingMode::FirstTime {
334            if let Some(window) = self.GetContentWindow() {
335                window.set_name(
336                    element
337                        .get_name()
338                        .map_or(DOMString::from(""), |n| DOMString::from(&*n)),
339                );
340            }
341        }
342
343        // Step 2.1. Let url be the result of running the shared attribute processing steps
344        // for iframe and frame elements given element and initialInsertion.
345        let Some(url) = self.shared_attribute_processing_steps_for_iframe_and_frame_elements()
346        else {
347            // Step 2.2. If url is null, then return.
348            return;
349        };
350
351        // Step 2.3. If url matches about:blank and initialInsertion is true, then:
352        if url.matches_about_blank() && mode == ProcessingMode::FirstTime {
353            // We should **not** send a load event in `iframe_load_event_steps`.
354            self.already_fired_synchronous_load_event.set(true);
355            // Step 2.3.1. Run the iframe load event steps given element.
356            //
357            // Note: we are not actually calling that method. That's because
358            // `iframe_load_event_steps` currently doesn't adhere to the spec
359            // at all. In this case, WPT tests only care about the load event,
360            // so we can fire that. Following https://github.com/servo/servo/issues/31973
361            // we should call `iframe_load_event_steps` once it is spec-compliant.
362            self.upcast::<EventTarget>()
363                .fire_event(atom!("load"), can_gc);
364            // Step 2.3.2. Return.
365            return;
366        }
367
368        // Step 2.4: Let referrerPolicy be the current state of element's referrerpolicy content
369        // attribute.
370        let document = self.owner_document();
371        let referrer_policy_token = self.ReferrerPolicy();
372
373        // Note: despite not being explicitly stated in the spec steps, this falls back to
374        // document's referrer policy here because it satisfies the expectations that when unset,
375        // the iframe should inherit the referrer policy of its parent
376        let referrer_policy = match ReferrerPolicy::from(&*referrer_policy_token.str()) {
377            ReferrerPolicy::EmptyString => document.get_referrer_policy(),
378            policy => policy,
379        };
380
381        // TODO(#25748):
382        // By spec, we return early if there's an ancestor browsing context
383        // "whose active document's url, ignoring fragments, is equal".
384        // However, asking about ancestor browsing contexts is more nuanced than
385        // it sounds and not implemented here.
386        // Within a single origin, we can do it by walking window proxies,
387        // and this check covers only that single-origin case, protecting
388        // against simple typo self-includes but nothing more elaborate.
389        let mut ancestor = window.GetParent();
390        while let Some(a) = ancestor {
391            if let Some(ancestor_url) = a.document().map(|d| d.url()) {
392                if ancestor_url.scheme() == url.scheme() &&
393                    ancestor_url.username() == url.username() &&
394                    ancestor_url.password() == url.password() &&
395                    ancestor_url.host() == url.host() &&
396                    ancestor_url.port() == url.port() &&
397                    ancestor_url.path() == url.path() &&
398                    ancestor_url.query() == url.query()
399                {
400                    return;
401                }
402            }
403            ancestor = a.parent().map(DomRoot::from_ref);
404        }
405
406        let (creator_pipeline_id, about_base_url) = if url.matches_about_blank() {
407            (Some(window.pipeline_id()), Some(document.base_url()))
408        } else {
409            (None, document.about_base_url())
410        };
411
412        let propagate_encoding_to_child_document = url.origin().same_origin(window.origin());
413        let mut load_data = LoadData::new(
414            LoadOrigin::Script(document.origin().snapshot()),
415            url,
416            about_base_url,
417            creator_pipeline_id,
418            window.as_global_scope().get_referrer(),
419            referrer_policy,
420            Some(window.as_global_scope().is_secure_context()),
421            Some(document.insecure_requests_policy()),
422            document.has_trustworthy_ancestor_or_current_origin(),
423            self.sandboxing_flag_set(),
424        );
425        load_data.destination = Destination::IFrame;
426        load_data.policy_container = Some(window.as_global_scope().policy_container());
427        if propagate_encoding_to_child_document {
428            load_data.container_document_encoding = Some(document.encoding());
429        }
430
431        let pipeline_id = self.pipeline_id();
432        // If the initial `about:blank` page is the current page, load with replacement enabled,
433        // see https://html.spec.whatwg.org/multipage/#the-iframe-element:about:blank-3
434        let is_about_blank =
435            pipeline_id.is_some() && pipeline_id == self.about_blank_pipeline_id.get();
436
437        let history_handling = if is_about_blank {
438            NavigationHistoryBehavior::Replace
439        } else {
440            NavigationHistoryBehavior::Push
441        };
442
443        self.navigate_or_reload_child_browsing_context(load_data, history_handling, can_gc);
444    }
445
446    /// <https://html.spec.whatwg.org/multipage/#create-a-new-child-navigable>
447    /// Synchronously create a new browsing context(This is not a navigation).
448    /// The pipeline started here should remain unnoticeable to script, but this is not easy
449    /// to refactor because it appears other features have come to rely on the current behavior.
450    /// For now only the iframe load event steps are skipped in some cases for this initial document,
451    /// and we still fire load and pageshow events as part of `maybe_queue_document_completion`.
452    /// Also, some controversy spec-wise remains: <https://github.com/whatwg/html/issues/4965>
453    fn create_nested_browsing_context(&self, can_gc: CanGc) {
454        let url = ServoUrl::parse("about:blank").unwrap();
455        let document = self.owner_document();
456        let window = self.owner_window();
457        let pipeline_id = Some(window.pipeline_id());
458        let mut load_data = LoadData::new(
459            LoadOrigin::Script(document.origin().snapshot()),
460            url,
461            Some(document.base_url()),
462            pipeline_id,
463            window.as_global_scope().get_referrer(),
464            document.get_referrer_policy(),
465            Some(window.as_global_scope().is_secure_context()),
466            Some(document.insecure_requests_policy()),
467            document.has_trustworthy_ancestor_or_current_origin(),
468            self.sandboxing_flag_set(),
469        );
470        load_data.destination = Destination::IFrame;
471        load_data.policy_container = Some(window.as_global_scope().policy_container());
472
473        let browsing_context_id = BrowsingContextId::new();
474        let webview_id = window.window_proxy().webview_id();
475        self.pipeline_id.set(None);
476        self.pending_pipeline_id.set(None);
477        self.webview_id.set(Some(webview_id));
478        self.browsing_context_id.set(Some(browsing_context_id));
479        self.start_new_pipeline(
480            load_data,
481            PipelineType::InitialAboutBlank,
482            NavigationHistoryBehavior::Push,
483            can_gc,
484        );
485    }
486
487    fn destroy_nested_browsing_context(&self) {
488        self.pipeline_id.set(None);
489        self.pending_pipeline_id.set(None);
490        self.about_blank_pipeline_id.set(None);
491        self.webview_id.set(None);
492        if let Some(browsing_context_id) = self.browsing_context_id.take() {
493            self.script_window_proxies.remove(browsing_context_id)
494        }
495    }
496
497    pub(crate) fn update_pipeline_id(
498        &self,
499        new_pipeline_id: PipelineId,
500        reason: UpdatePipelineIdReason,
501        can_gc: CanGc,
502    ) {
503        // For all updates except the one for the initial blank document,
504        // we need to set the flag back to false because the navigation is complete,
505        // because the goal is to, when a navigation is pending, to skip the async load
506        // steps of the initial blank document.
507        if !self.is_initial_blank_document() {
508            self.pending_navigation.set(false);
509        }
510        if self.pending_pipeline_id.get() != Some(new_pipeline_id) &&
511            reason == UpdatePipelineIdReason::Navigation
512        {
513            return;
514        }
515
516        self.pipeline_id.set(Some(new_pipeline_id));
517
518        // Only terminate the load blocker if the pipeline id was updated due to a traversal.
519        // The load blocker will be terminated for a navigation in iframe_load_event_steps.
520        if reason == UpdatePipelineIdReason::Traversal {
521            let blocker = &self.load_blocker;
522            LoadBlocker::terminate(blocker, can_gc);
523        }
524
525        self.upcast::<Node>().dirty(NodeDamage::Other);
526    }
527
528    fn new_inherited(
529        local_name: LocalName,
530        prefix: Option<Prefix>,
531        document: &Document,
532    ) -> HTMLIFrameElement {
533        HTMLIFrameElement {
534            htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
535            browsing_context_id: Cell::new(None),
536            webview_id: Cell::new(None),
537            pipeline_id: Cell::new(None),
538            pending_pipeline_id: Cell::new(None),
539            about_blank_pipeline_id: Cell::new(None),
540            sandbox: Default::default(),
541            sandboxing_flag_set: Cell::new(None),
542            load_blocker: DomRefCell::new(None),
543            throttled: Cell::new(false),
544            script_window_proxies: ScriptThread::window_proxies(),
545            pending_navigation: Default::default(),
546            already_fired_synchronous_load_event: Default::default(),
547        }
548    }
549
550    pub(crate) fn new(
551        local_name: LocalName,
552        prefix: Option<Prefix>,
553        document: &Document,
554        proto: Option<HandleObject>,
555        can_gc: CanGc,
556    ) -> DomRoot<HTMLIFrameElement> {
557        Node::reflect_node_with_proto(
558            Box::new(HTMLIFrameElement::new_inherited(
559                local_name, prefix, document,
560            )),
561            document,
562            proto,
563            can_gc,
564        )
565    }
566
567    #[inline]
568    pub(crate) fn pipeline_id(&self) -> Option<PipelineId> {
569        self.pipeline_id.get()
570    }
571
572    #[inline]
573    pub(crate) fn browsing_context_id(&self) -> Option<BrowsingContextId> {
574        self.browsing_context_id.get()
575    }
576
577    #[inline]
578    pub(crate) fn webview_id(&self) -> Option<WebViewId> {
579        self.webview_id.get()
580    }
581
582    #[inline]
583    pub(crate) fn sandboxing_flag_set(&self) -> SandboxingFlagSet {
584        self.sandboxing_flag_set
585            .get()
586            .unwrap_or_else(SandboxingFlagSet::empty)
587    }
588
589    pub(crate) fn set_throttled(&self, throttled: bool) {
590        if self.throttled.get() != throttled {
591            self.throttled.set(throttled);
592        }
593    }
594
595    /// Note a pending navigation.
596    /// This is used to ignore the async load event steps for
597    /// the initial blank document if those haven't run yet.
598    pub(crate) fn note_pending_navigation(&self) {
599        self.pending_navigation.set(true);
600    }
601
602    /// <https://html.spec.whatwg.org/multipage/#iframe-load-event-steps>
603    pub(crate) fn iframe_load_event_steps(&self, loaded_pipeline: PipelineId, can_gc: CanGc) {
604        // TODO(#9592): assert that the load blocker is present at all times when we
605        //              can guarantee that it's created for the case of iframe.reload().
606        if Some(loaded_pipeline) != self.pending_pipeline_id.get() {
607            return;
608        }
609
610        // TODO 1. Assert: element's content navigable is not null.
611
612        // TODO 2-4 Mark resource timing.
613
614        // TODO 5 Set childDocument's iframe load in progress flag.
615
616        // Note: in the spec, these steps are either run synchronously as part of
617        // "If url matches about:blank and initialInsertion is true, then:"
618        // in `process the iframe attributes`,
619        // or asynchronously when navigation completes.
620        //
621        // In our current implementation,
622        // we arrive here always asynchronously in the following two cases:
623        // 1. as part of loading the initial blank document
624        //    created in `create_nested_browsing_context`
625        // 2. optionally, as part of loading a second document created as
626        //    as part of the first processing of the iframe attributes.
627        //
628        // To preserve the logic of the spec--firing the load event once--in the context of
629        // our current implementation, we must not fire the load event
630        // for the initial blank document if we know that a navigation is ongoing,
631        // which can be deducted from `pending_navigation` or the presence of an src.
632        //
633        // Additionally, to prevent a race condition with navigations,
634        // in all cases, skip the load event if there is a pending navigation.
635        // See #40348
636        //
637        // TODO: run these step synchronously as part of processing the iframe attributes.
638        let should_fire_event = if self.is_initial_blank_document() {
639            // If this is the initial blank doc:
640            // do not fire if there is a pending navigation,
641            // or if the iframe has an src.
642            !self.pending_navigation.get() &&
643                !self.upcast::<Element>().has_attribute(&local_name!("src"))
644        } else {
645            // If this is not the initial blank doc:
646            // do not fire if there is a pending navigation.
647            !self.pending_navigation.get()
648        };
649        // If we already fired a synchronous load event, we shouldn't fire another
650        // one in this method.
651        let should_fire_event =
652            !self.already_fired_synchronous_load_event.replace(false) && should_fire_event;
653        if should_fire_event {
654            // Step 6. Fire an event named load at element.
655            self.upcast::<EventTarget>()
656                .fire_event(atom!("load"), can_gc);
657        }
658
659        let blocker = &self.load_blocker;
660        LoadBlocker::terminate(blocker, can_gc);
661
662        // TODO Step 7 - unset child document `mute iframe load` flag
663    }
664
665    /// Parse the `sandbox` attribute value given the [`Attr`]. This sets the `sandboxing_flag_set`
666    /// property or clears it is the value isn't specified. Notably, an unspecified sandboxing
667    /// attribute (no sandboxing) is different from an empty one (full sandboxing).
668    fn parse_sandbox_attribute(&self) {
669        let attribute = self
670            .upcast::<Element>()
671            .get_attribute(&ns!(), &local_name!("sandbox"));
672        self.sandboxing_flag_set
673            .set(attribute.map(|attribute_value| {
674                let tokens: Vec<_> = attribute_value
675                    .value()
676                    .as_tokens()
677                    .iter()
678                    .map(|atom| atom.to_string().to_ascii_lowercase())
679                    .collect();
680                parse_a_sandboxing_directive(&tokens)
681            }));
682    }
683
684    /// Step 4.2. of <https://html.spec.whatwg.org/multipage/#destroy-a-document-and-its-descendants>
685    pub(crate) fn destroy_document_and_its_descendants(&self, can_gc: CanGc) {
686        let Some(pipeline_id) = self.pipeline_id.get() else {
687            return;
688        };
689        // Step 4.2. Destroy a document and its descendants given childNavigable's active document and incrementDestroyed.
690        if let Some(exited_document) = ScriptThread::find_document(pipeline_id) {
691            exited_document.destroy_document_and_its_descendants(can_gc);
692        }
693        self.destroy_nested_browsing_context();
694    }
695
696    /// <https://html.spec.whatwg.org/multipage/#destroy-a-child-navigable>
697    fn destroy_child_navigable(&self, can_gc: CanGc) {
698        let blocker = &self.load_blocker;
699        LoadBlocker::terminate(blocker, CanGc::note());
700
701        // Step 1. Let navigable be container's content navigable.
702        let Some(browsing_context_id) = self.browsing_context_id() else {
703            // Step 2. If navigable is null, then return.
704            return;
705        };
706        // Store now so that we can destroy the context and delete the
707        // document later
708        let pipeline_id = self.pipeline_id.get();
709
710        // Step 3. Set container's content navigable to null.
711        //
712        // Resetting the pipeline_id to None is required here so that
713        // if this iframe is subsequently re-added to the document
714        // the load doesn't think that it's a navigation, but instead
715        // a new iframe. Without this, the constellation gets very
716        // confused.
717        self.destroy_nested_browsing_context();
718
719        // Step 4. Inform the navigation API about child navigable destruction given navigable.
720        // TODO
721
722        // Step 5. Destroy a document and its descendants given navigable's active document.
723        let (sender, receiver) =
724            ProfiledIpc::channel(self.global().time_profiler_chan().clone()).unwrap();
725        let msg = ScriptToConstellationMessage::RemoveIFrame(browsing_context_id, sender);
726        self.owner_window()
727            .as_global_scope()
728            .script_to_constellation_chan()
729            .send(msg)
730            .unwrap();
731        let _exited_pipeline_ids = receiver.recv().unwrap();
732        let Some(pipeline_id) = pipeline_id else {
733            return;
734        };
735        if let Some(exited_document) = ScriptThread::find_document(pipeline_id) {
736            exited_document.destroy_document_and_its_descendants(can_gc);
737        }
738
739        // Step 6. Let parentDocState be container's node navigable's active session history entry's document state.
740        // TODO
741
742        // Step 7. Remove the nested history from parentDocState's nested histories whose id equals navigable's id.
743        // TODO
744
745        // Step 8. Let traversable be container's node navigable's traversable navigable.
746        // TODO
747
748        // Step 9. Append the following session history traversal steps to traversable:
749        // TODO
750
751        // Step 10. Invoke WebDriver BiDi navigable destroyed with navigable.
752        // TODO
753    }
754}
755
756pub(crate) trait HTMLIFrameElementLayoutMethods {
757    fn pipeline_id(self) -> Option<PipelineId>;
758    fn browsing_context_id(self) -> Option<BrowsingContextId>;
759    fn get_width(self) -> LengthOrPercentageOrAuto;
760    fn get_height(self) -> LengthOrPercentageOrAuto;
761}
762
763impl HTMLIFrameElementLayoutMethods for LayoutDom<'_, HTMLIFrameElement> {
764    #[inline]
765    fn pipeline_id(self) -> Option<PipelineId> {
766        (self.unsafe_get()).pipeline_id.get()
767    }
768
769    #[inline]
770    fn browsing_context_id(self) -> Option<BrowsingContextId> {
771        (self.unsafe_get()).browsing_context_id.get()
772    }
773
774    fn get_width(self) -> LengthOrPercentageOrAuto {
775        self.upcast::<Element>()
776            .get_attr_for_layout(&ns!(), &local_name!("width"))
777            .map(AttrValue::as_dimension)
778            .cloned()
779            .unwrap_or(LengthOrPercentageOrAuto::Auto)
780    }
781
782    fn get_height(self) -> LengthOrPercentageOrAuto {
783        self.upcast::<Element>()
784            .get_attr_for_layout(&ns!(), &local_name!("height"))
785            .map(AttrValue::as_dimension)
786            .cloned()
787            .unwrap_or(LengthOrPercentageOrAuto::Auto)
788    }
789}
790
791impl HTMLIFrameElementMethods<crate::DomTypeHolder> for HTMLIFrameElement {
792    // https://html.spec.whatwg.org/multipage/#dom-iframe-src
793    make_url_getter!(Src, "src");
794
795    // https://html.spec.whatwg.org/multipage/#dom-iframe-src
796    make_url_setter!(SetSrc, "src");
797
798    /// <https://html.spec.whatwg.org/multipage/#dom-iframe-srcdoc>
799    fn Srcdoc(&self) -> TrustedHTMLOrString {
800        let element = self.upcast::<Element>();
801        element.get_trusted_html_attribute(&local_name!("srcdoc"))
802    }
803
804    /// <https://html.spec.whatwg.org/multipage/#dom-iframe-srcdoc>
805    fn SetSrcdoc(&self, value: TrustedHTMLOrString, can_gc: CanGc) -> Fallible<()> {
806        // Step 1: Let compliantString be the result of invoking the
807        // Get Trusted Type compliant string algorithm with TrustedHTML,
808        // this's relevant global object, the given value, "HTMLIFrameElement srcdoc", and "script".
809        let element = self.upcast::<Element>();
810        let value = TrustedHTML::get_trusted_script_compliant_string(
811            &element.owner_global(),
812            value,
813            "HTMLIFrameElement srcdoc",
814            can_gc,
815        )?;
816        // Step 2: Set an attribute value given this, srcdoc's local name, and compliantString.
817        element.set_attribute(
818            &local_name!("srcdoc"),
819            AttrValue::String(value.str().to_owned()),
820            can_gc,
821        );
822        Ok(())
823    }
824
825    /// <https://html.spec.whatwg.org/multipage/#dom-iframe-sandbox>
826    ///
827    /// The supported tokens for sandbox's DOMTokenList are the allowed values defined in the
828    /// sandbox attribute and supported by the user agent. These range of possible values is
829    /// defined here: <https://html.spec.whatwg.org/multipage/#attr-iframe-sandbox>
830    fn Sandbox(&self, can_gc: CanGc) -> DomRoot<DOMTokenList> {
831        self.sandbox.or_init(|| {
832            DOMTokenList::new(
833                self.upcast::<Element>(),
834                &local_name!("sandbox"),
835                Some(vec![
836                    Atom::from("allow-downloads"),
837                    Atom::from("allow-forms"),
838                    Atom::from("allow-modals"),
839                    Atom::from("allow-orientation-lock"),
840                    Atom::from("allow-pointer-lock"),
841                    Atom::from("allow-popups"),
842                    Atom::from("allow-popups-to-escape-sandbox"),
843                    Atom::from("allow-presentation"),
844                    Atom::from("allow-same-origin"),
845                    Atom::from("allow-scripts"),
846                    Atom::from("allow-top-navigation"),
847                    Atom::from("allow-top-navigation-by-user-activation"),
848                    Atom::from("allow-top-navigation-to-custom-protocols"),
849                ]),
850                can_gc,
851            )
852        })
853    }
854
855    /// <https://html.spec.whatwg.org/multipage/#dom-iframe-contentwindow>
856    fn GetContentWindow(&self) -> Option<DomRoot<WindowProxy>> {
857        self.browsing_context_id
858            .get()
859            .and_then(|id| self.script_window_proxies.find_window_proxy(id))
860    }
861
862    /// <https://html.spec.whatwg.org/multipage/#concept-bcc-content-document>
863    fn GetContentDocument(&self) -> Option<DomRoot<Document>> {
864        // Step 1. If container's content navigable is null, then return null.
865        let pipeline_id = self.pipeline_id.get()?;
866
867        // Step 2. Let document be container's content navigable's active document.
868        // Note that this lookup will fail if the document is dissimilar-origin,
869        // so we should return None in that case.
870        let document = ScriptThread::find_document(pipeline_id)?;
871        // Step 3. If document's origin and container's node document's origin are not same origin-domain, then return null.
872        if !self
873            .owner_document()
874            .origin()
875            .same_origin_domain(document.origin())
876        {
877            return None;
878        }
879        // Step 4. Return document.
880        Some(document)
881    }
882
883    /// <https://html.spec.whatwg.org/multipage/#attr-iframe-referrerpolicy>
884    fn ReferrerPolicy(&self) -> DOMString {
885        reflect_referrer_policy_attribute(self.upcast::<Element>())
886    }
887
888    // https://html.spec.whatwg.org/multipage/#attr-iframe-referrerpolicy
889    make_setter!(SetReferrerPolicy, "referrerpolicy");
890
891    // https://html.spec.whatwg.org/multipage/#attr-iframe-allowfullscreen
892    make_bool_getter!(AllowFullscreen, "allowfullscreen");
893    // https://html.spec.whatwg.org/multipage/#attr-iframe-allowfullscreen
894    make_bool_setter!(SetAllowFullscreen, "allowfullscreen");
895
896    // <https://html.spec.whatwg.org/multipage/#dom-dim-width>
897    make_getter!(Width, "width");
898    // <https://html.spec.whatwg.org/multipage/#dom-dim-width>
899    make_dimension_setter!(SetWidth, "width");
900
901    // <https://html.spec.whatwg.org/multipage/#dom-dim-height>
902    make_getter!(Height, "height");
903    // <https://html.spec.whatwg.org/multipage/#dom-dim-height>
904    make_dimension_setter!(SetHeight, "height");
905
906    // https://html.spec.whatwg.org/multipage/#other-elements,-attributes-and-apis:attr-iframe-frameborder
907    make_getter!(FrameBorder, "frameborder");
908    // https://html.spec.whatwg.org/multipage/#other-elements,-attributes-and-apis:attr-iframe-frameborder
909    make_setter!(SetFrameBorder, "frameborder");
910
911    // https://html.spec.whatwg.org/multipage/#dom-iframe-name
912    // A child browsing context checks the name of its iframe only at the time
913    // it is created; subsequent name sets have no special effect.
914    make_atomic_setter!(SetName, "name");
915
916    // https://html.spec.whatwg.org/multipage/#dom-iframe-name
917    // This is specified as reflecting the name content attribute of the
918    // element, not the name of the child browsing context.
919    make_getter!(Name, "name");
920}
921
922impl VirtualMethods for HTMLIFrameElement {
923    fn super_type(&self) -> Option<&dyn VirtualMethods> {
924        Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
925    }
926
927    fn attribute_mutated(&self, attr: &Attr, mutation: AttributeMutation, can_gc: CanGc) {
928        self.super_type()
929            .unwrap()
930            .attribute_mutated(attr, mutation, can_gc);
931        match *attr.local_name() {
932            // From <https://html.spec.whatwg.org/multipage/#attr-iframe-sandbox>:
933            //
934            // > When an iframe element's sandbox attribute is set or changed while
935            // > it has a non-null content navigable, the user agent must parse the
936            // > sandboxing directive given the attribute's value and the iframe
937            // > element's iframe sandboxing flag set.
938            //
939            // > When an iframe element's sandbox attribute is removed while it has
940            // > a non-null content navigable, the user agent must empty the iframe
941            // > element's iframe sandboxing flag set.
942            local_name!("sandbox") if self.browsing_context_id.get().is_some() => {
943                self.parse_sandbox_attribute();
944            },
945            local_name!("srcdoc") => {
946                // https://html.spec.whatwg.org/multipage/#the-iframe-element:the-iframe-element-9
947                // "Whenever an iframe element with a non-null nested browsing context has its
948                // srcdoc attribute set, changed, or removed, the user agent must process the
949                // iframe attributes."
950                // but we can't check that directly, since the child browsing context
951                // may be in a different script thread. Instead, we check to see if the parent
952                // is in a document tree and has a browsing context, which is what causes
953                // the child browsing context to be created.
954
955                // trigger the processing of iframe attributes whenever "srcdoc" attribute is set, changed or removed
956                if self.upcast::<Node>().is_connected_with_browsing_context() {
957                    debug!("iframe srcdoc modified while in browsing context.");
958                    self.process_the_iframe_attributes(ProcessingMode::NotFirstTime, can_gc);
959                }
960            },
961            local_name!("src") => {
962                // https://html.spec.whatwg.org/multipage/#the-iframe-element
963                // "Similarly, whenever an iframe element with a non-null nested browsing context
964                // but with no srcdoc attribute specified has its src attribute set, changed, or removed,
965                // the user agent must process the iframe attributes,"
966                // but we can't check that directly, since the child browsing context
967                // may be in a different script thread. Instead, we check to see if the parent
968                // is in a document tree and has a browsing context, which is what causes
969                // the child browsing context to be created.
970                if self.upcast::<Node>().is_connected_with_browsing_context() {
971                    debug!("iframe src set while in browsing context.");
972                    self.process_the_iframe_attributes(ProcessingMode::NotFirstTime, can_gc);
973                }
974            },
975            _ => {},
976        }
977    }
978
979    fn attribute_affects_presentational_hints(&self, attr: &Attr) -> bool {
980        match attr.local_name() {
981            &local_name!("width") | &local_name!("height") => true,
982            _ => self
983                .super_type()
984                .unwrap()
985                .attribute_affects_presentational_hints(attr),
986        }
987    }
988
989    fn parse_plain_attribute(&self, name: &LocalName, value: DOMString) -> AttrValue {
990        match *name {
991            local_name!("sandbox") => AttrValue::from_serialized_tokenlist(value.into()),
992            local_name!("width") => AttrValue::from_dimension(value.into()),
993            local_name!("height") => AttrValue::from_dimension(value.into()),
994            _ => self
995                .super_type()
996                .unwrap()
997                .parse_plain_attribute(name, value),
998        }
999    }
1000
1001    /// <https://html.spec.whatwg.org/multipage/#the-iframe-element:html-element-post-connection-steps>
1002    fn post_connection_steps(&self, can_gc: CanGc) {
1003        if let Some(s) = self.super_type() {
1004            s.post_connection_steps(can_gc);
1005        }
1006
1007        // This isn't mentioned any longer in the specification, but still seems important. This is
1008        // likely due to the fact that we have deviated a great deal with it comes to navigables
1009        // and browsing contexts.
1010        if !self.upcast::<Node>().is_connected_with_browsing_context() {
1011            return;
1012        }
1013
1014        debug!("<iframe> running post connection steps");
1015
1016        // Step 1. Create a new child navigable for insertedNode.
1017        self.create_nested_browsing_context(can_gc);
1018
1019        // Step 2: If insertedNode has a sandbox attribute, then parse the sandboxing directive
1020        // given the attribute's value and insertedNode's iframe sandboxing flag set.
1021        self.parse_sandbox_attribute();
1022
1023        // Step 3. Process the iframe attributes for insertedNode, with initialInsertion set to true.
1024        self.process_the_iframe_attributes(ProcessingMode::FirstTime, can_gc);
1025    }
1026
1027    fn bind_to_tree(&self, context: &BindContext, can_gc: CanGc) {
1028        if let Some(s) = self.super_type() {
1029            s.bind_to_tree(context, can_gc);
1030        }
1031        self.owner_document().invalidate_iframes_collection();
1032    }
1033
1034    /// <https://html.spec.whatwg.org/multipage/#the-iframe-element:html-element-removing-steps>
1035    fn unbind_from_tree(&self, context: &UnbindContext, can_gc: CanGc) {
1036        self.super_type().unwrap().unbind_from_tree(context, can_gc);
1037
1038        // The iframe HTML element removing steps, given removedNode, are to destroy a child navigable given removedNode
1039        self.destroy_child_navigable(can_gc);
1040
1041        self.owner_document().invalidate_iframes_collection();
1042    }
1043}
1044
1045/// IframeContext is a wrapper around [`HTMLIFrameElement`] that implements the [`ResourceTimingListener`] trait.
1046/// Note: this implementation of `resource_timing_global` returns the parent document's global scope, not the iframe's global scope.
1047pub(crate) struct IframeContext<'a> {
1048    // The iframe element that this context is associated with.
1049    element: &'a HTMLIFrameElement,
1050    // The URL of the iframe document.
1051    url: ServoUrl,
1052}
1053
1054impl<'a> IframeContext<'a> {
1055    /// Creates a new IframeContext from a reference to an HTMLIFrameElement.
1056    pub fn new(element: &'a HTMLIFrameElement) -> Self {
1057        Self {
1058            element,
1059            url: element
1060                .shared_attribute_processing_steps_for_iframe_and_frame_elements()
1061                .expect("Must always have a URL when navigating"),
1062        }
1063    }
1064}
1065
1066impl<'a> ResourceTimingListener for IframeContext<'a> {
1067    fn resource_timing_information(&self) -> (InitiatorType, ServoUrl) {
1068        (
1069            InitiatorType::LocalName("iframe".to_string()),
1070            self.url.clone(),
1071        )
1072    }
1073
1074    fn resource_timing_global(&self) -> DomRoot<GlobalScope> {
1075        self.element.upcast::<Node>().owner_doc().global()
1076    }
1077}