File: DocumentImmersive.cpp

package info (click to toggle)
webkit2gtk 2.51.3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 477,912 kB
  • sloc: cpp: 3,898,343; javascript: 198,215; ansic: 165,229; python: 50,371; asm: 21,819; ruby: 18,095; perl: 16,953; xml: 4,623; sh: 2,398; yacc: 2,356; java: 2,019; lex: 1,358; pascal: 372; makefile: 197
file content (244 lines) | stat: -rw-r--r-- 10,781 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
 * Copyright (C) 2025 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include "DocumentImmersive.h"

#if ENABLE(MODEL_ELEMENT_IMMERSIVE)

#include "HTMLModelElement.h"
#include "PseudoClassChangeInvalidation.h"

namespace WebCore {

WTF_MAKE_TZONE_ALLOCATED_IMPL(DocumentImmersive);

DocumentImmersive::DocumentImmersive(Document& document)
    : m_document(document)
{
}

bool DocumentImmersive::immersiveEnabled(Document& document)
{
    if (!document.settings().modelElementImmersiveEnabled())
        return false;

    if (!document.isFullyActive())
        return false;

    return false; // Needs client support
}

Element* DocumentImmersive::immersiveElement(Document& document)
{
    RefPtr documentImmersive = document.immersiveIfExists();
    if (!documentImmersive)
        return nullptr;
    return document.ancestorElementInThisScope(documentImmersive->protectedImmersiveElement().get());
}

HTMLModelElement* DocumentImmersive::immersiveElement() const
{
    return m_immersiveElement.get();
}

void DocumentImmersive::exitImmersive(Document& document, RefPtr<DeferredPromise>&& promise)
{
    RefPtr protectedImmersive = document.immersiveIfExists();
    if (!document.isFullyActive() || !protectedImmersive) {
        promise->reject(Exception { ExceptionCode::TypeError, "Not in immersive"_s });
        return;
    }
    protectedImmersive->exitImmersive([promise = WTFMove(promise)] (auto result) {
        if (!promise)
            return;
        if (result.hasException())
            promise->reject(result.releaseException());
        else
            promise->resolve();
    });
}

void DocumentImmersive::requestImmersive(HTMLModelElement* element, CompletionHandler<void(ExceptionOr<void>)>&& completionHandler)
{
    enum class EmitErrorEvent : bool { No, Yes };
    auto handleError = [weakElement = WeakPtr { *element }, weakThis = WeakPtr { *this }](String message, EmitErrorEvent emitErrorEvent, CompletionHandler<void(ExceptionOr<void>)>&& completionHandler) mutable {
        RefPtr protectedThis = weakThis.get();
        RefPtr protectedElement = weakElement.get();
        if (!protectedThis || !protectedElement)
            return completionHandler(Exception { ExceptionCode::TypeError, message });
        RELEASE_LOG_ERROR(Immersive, "%p - DocumentImmersive: %s", protectedThis.get(), message.utf8().data());
        if (emitErrorEvent == EmitErrorEvent::Yes) {
            protectedThis->queueImmersiveEventForElement(DocumentImmersive::EventType::Error, *protectedElement);
            protectedThis->protectedDocument()->scheduleRenderingUpdate(RenderingUpdateStep::Immersive);
        }
        completionHandler(Exception { ExceptionCode::TypeError, message });
    };

    if (!protectedDocument()->isFullyActive())
        return handleError("Cannot request immersive on a document that is not fully active."_s, EmitErrorEvent::No, WTFMove(completionHandler));

    if (RefPtr window = document().window(); !window || !window->consumeTransientActivation())
        return handleError("Cannot request immersive without transient activation."_s, EmitErrorEvent::Yes, WTFMove(completionHandler));

    RefPtr protectedPage = document().page();
    if (!protectedPage || !protectedPage->settings().modelElementImmersiveEnabled())
        return handleError("Immersive API is disabled."_s, EmitErrorEvent::Yes, WTFMove(completionHandler));

    protectedPage->chrome().client().allowImmersiveElement(*element, [weakElement = WeakPtr { *element }, weakThis = WeakPtr { *this }, weakPage = WeakPtr { *protectedPage }, handleError, completionHandler = WTFMove(completionHandler)](auto allowed) mutable {
        if (!allowed)
            return handleError("Immersive request was denied."_s, EmitErrorEvent::Yes, WTFMove(completionHandler));

        RefPtr protectedElement = weakElement.get();
        if (!protectedElement)
            return completionHandler(Exception { ExceptionCode::TypeError });

        protectedElement->ensureImmersivePresentation([weakElement, weakThis, weakPage, handleError, completionHandler = WTFMove(completionHandler)](auto result) mutable {
            if (result.hasException())
                return handleError(result.releaseException().message(), EmitErrorEvent::Yes, WTFMove(completionHandler));

            RefPtr protectedElement = weakElement.get();
            if (!protectedElement)
                return completionHandler(Exception { ExceptionCode::TypeError });

            RefPtr protectedPage = weakPage.get();
            if (!protectedPage) {
                protectedElement->exitImmersivePresentation([] { });
                completionHandler(Exception { ExceptionCode::TypeError });
                return;
            }

            protectedPage->chrome().client().presentImmersiveElement(*protectedElement, WTFMove(result.releaseReturnValue()), [weakElement, weakThis, handleError, completionHandler = WTFMove(completionHandler)](bool success) mutable {
                RefPtr protectedElement = weakElement.get();
                if (!protectedElement)
                    return completionHandler(Exception { ExceptionCode::TypeError });

                RefPtr protectedThis = weakThis.get();
                if (!protectedThis || !success) {
                    protectedElement->exitImmersivePresentation([] { });
                    handleError("Failure to present the immersive element."_s, EmitErrorEvent::Yes, WTFMove(completionHandler));
                    return;
                }

                if (RefPtr oldImmersiveElement = protectedThis->m_immersiveElement.get()) {
                    oldImmersiveElement->exitImmersivePresentation([] { });
                    protectedThis->updateElementIsImmersive(oldImmersiveElement.get(), false);
                }

                protectedThis->m_immersiveElement = protectedElement.get();
                protectedThis->updateElementIsImmersive(protectedElement.get(), true);
                completionHandler({ });
            });
        });
    });
}

void DocumentImmersive::exitImmersive(CompletionHandler<void(ExceptionOr<void>)>&& completionHandler)
{
    RefPtr exitingImmersiveElement = m_immersiveElement.get();
    if (!exitingImmersiveElement) {
        completionHandler(Exception { ExceptionCode::TypeError, "Not in immersive"_s });
        return;
    }

    m_immersiveElement = nullptr;

    RefPtr protectedPage = document().page();
    if (!protectedPage) {
        exitingImmersiveElement->exitImmersivePresentation([] { });
        updateElementIsImmersive(exitingImmersiveElement.get(), false);
        completionHandler(Exception { ExceptionCode::TypeError });
        return;
    }

    protectedPage->chrome().client().dismissImmersiveElement(*exitingImmersiveElement, [weakElement = WeakPtr { *exitingImmersiveElement }, weakThis = WeakPtr { *this }, completionHandler = WTFMove(completionHandler)]() mutable {
        RefPtr protectedElement = weakElement.get();
        if (!protectedElement)
            return completionHandler(Exception { ExceptionCode::TypeError });

        protectedElement->exitImmersivePresentation([weakElement, weakThis, completionHandler = WTFMove(completionHandler)] () mutable {
            RefPtr protectedThis = weakThis.get();
            RefPtr protectedElement = weakElement.get();
            if (!protectedThis || !protectedElement)
                return completionHandler(Exception { ExceptionCode::TypeError });

            protectedThis->updateElementIsImmersive(protectedElement.get(), false);
            completionHandler({ });
        });
    });
}

void DocumentImmersive::exitRemovedImmersiveElement(HTMLModelElement* element)
{
    queueImmersiveEventForElement(DocumentImmersive::EventType::Error, *element);
    document().scheduleRenderingUpdate(RenderingUpdateStep::Immersive);
}

void DocumentImmersive::updateElementIsImmersive(HTMLModelElement* element, bool isImmersive)
{
    Style::PseudoClassChangeInvalidation styleInvalidation(*element, { { CSSSelector::PseudoClass::Immersive, isImmersive } });
    queueImmersiveEventForElement(DocumentImmersive::EventType::Change, *element);
    document().scheduleRenderingUpdate(RenderingUpdateStep::Immersive);
}

void DocumentImmersive::dispatchPendingEvents()
{
    auto pendingEvents = std::exchange(m_pendingEvents, { });

    while (!pendingEvents.isEmpty()) {
        auto [eventType, element] = pendingEvents.takeFirst();

        // Let target be element if element is connected and its node document is document, and otherwise let target be document.
        Ref target = [&]() -> Node& {
            if (element->isConnected() && &element->document() == &document())
                return element;
            return document();
        }();

        switch (eventType) {
        case EventType::Change: {
            target->dispatchEvent(Event::create(eventNames().immersivechangeEvent, Event::CanBubble::Yes, Event::IsCancelable::No, Event::IsComposed::Yes));
            break;
        }
        case EventType::Error:
            target->dispatchEvent(Event::create(eventNames().immersiveerrorEvent, Event::CanBubble::Yes, Event::IsCancelable::No, Event::IsComposed::Yes));
            break;
        }
    }
}

void DocumentImmersive::queueImmersiveEventForElement(EventType eventType, Element& target)
{
    m_pendingEvents.append({ eventType, GCReachableRef(target) });
}

void DocumentImmersive::clear()
{
    m_immersiveElement = nullptr;
}

}

#endif