1/*
2 * Copyright (C) 2020 Igalia S.L.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "LazyLoadFrameObserver.h"
28
29#include "Frame.h"
30#include "HTMLIFrameElement.h"
31#include "IntersectionObserverCallback.h"
32#include "RenderStyle.h"
33
34#include <limits>
35
36namespace WebCore {
37
38class LazyFrameLoadIntersectionObserverCallback final : public IntersectionObserverCallback {
39public:
40 static Ref<LazyFrameLoadIntersectionObserverCallback> create(Document& document)
41 {
42 return adoptRef(*new LazyFrameLoadIntersectionObserverCallback(document));
43 }
44
45private:
46 CallbackResult<void> handleEvent(IntersectionObserver&, const Vector<Ref<IntersectionObserverEntry>>& entries, IntersectionObserver&) final
47 {
48 ASSERT(!entries.isEmpty());
49
50 for (auto& entry : entries) {
51 if (!entry->isIntersecting())
52 continue;
53 auto* element = entry->target();
54 if (is<HTMLIFrameElement>(element)) {
55 downcast<HTMLIFrameElement>(*element).lazyLoadFrameObserver().unobserve();
56 downcast<HTMLIFrameElement>(*element).loadDeferredFrame();
57 }
58 }
59 return { };
60 }
61
62 LazyFrameLoadIntersectionObserverCallback(Document& document)
63 : IntersectionObserverCallback(&document)
64 {
65 }
66};
67
68LazyLoadFrameObserver::LazyLoadFrameObserver(HTMLIFrameElement& element)
69 : m_element(element)
70{
71}
72
73void LazyLoadFrameObserver::observe(const AtomString& url, const ReferrerPolicy& referrerPolicy)
74{
75 auto& observer = m_element.lazyLoadFrameObserver();
76 auto* intersectionObserver = observer.intersectionObserver(m_element.document());
77 if (!intersectionObserver)
78 return;
79 m_url = url;
80 m_referrerPolicy = referrerPolicy;
81 intersectionObserver->observe(m_element);
82}
83
84void LazyLoadFrameObserver::unobserve()
85{
86 auto& observer = m_element.lazyLoadFrameObserver();
87 ASSERT(observer.isObserved(m_element));
88 observer.m_lazyLoadIntersectionObserver->unobserve(m_element);
89}
90
91IntersectionObserver* LazyLoadFrameObserver::intersectionObserver(Document& document)
92{
93 if (!m_lazyLoadIntersectionObserver) {
94 auto callback = LazyFrameLoadIntersectionObserverCallback::create(document);
95 IntersectionObserver::Init options { WTF::nullopt, emptyString(), { } };
96 auto observer = IntersectionObserver::create(document, WTFMove(callback), WTFMove(options));
97 if (observer.hasException())
98 return nullptr;
99 m_lazyLoadIntersectionObserver = observer.returnValue().ptr();
100 }
101 return m_lazyLoadIntersectionObserver.get();
102}
103
104bool LazyLoadFrameObserver::isObserved(Element& element) const
105{
106 return m_lazyLoadIntersectionObserver && m_lazyLoadIntersectionObserver->observationTargets().contains(&element);
107}
108
109}