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