-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathContentChangeObserver.cpp
776 lines (677 loc) · 28.1 KB
/
ContentChangeObserver.cpp
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
/*
* Copyright (C) 2019 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 "ContentChangeObserver.h"
#if ENABLE(CONTENT_CHANGE_OBSERVER)
#include "Animation.h"
#include "Chrome.h"
#include "ChromeClient.h"
#include "DOMTimer.h"
#include "Document.h"
#include "FullscreenManager.h"
#include "HTMLIFrameElement.h"
#include "HTMLImageElement.h"
#include "Logging.h"
#include "NodeRenderStyle.h"
#include "Page.h"
#include "Quirks.h"
#include "RenderDescendantIterator.h"
#include "RenderStyleInlines.h"
#include "Settings.h"
namespace WebCore {
static const Seconds maximumDelayForTimers { 400_ms };
static const Seconds maximumDelayForTransitions { 300_ms };
#if ENABLE(FULLSCREEN_API)
static bool isHiddenBehindFullscreenElement(const Node& descendantCandidate)
{
// Fullscreen status is propagated on the ancestor document chain all the way to the top document.
auto& document = descendantCandidate.document();
auto* topMostFullScreenElement = document.topDocument().fullscreenManager().fullscreenElement();
if (!topMostFullScreenElement)
return false;
// If the document where the node lives does not have an active fullscreen element, it is a sibling/nephew document -> not a descendant.
auto* fullscreenElement = document.fullscreenManager().fullscreenElement();
if (!fullscreenElement)
return true;
return !descendantCandidate.isDescendantOf(*fullscreenElement);
}
#endif
bool ContentChangeObserver::isContentChangeObserverEnabled()
{
return m_document.settings().contentChangeObserverEnabled() && !m_document.quirks().shouldDisableContentChangeObserver();
}
bool ContentChangeObserver::isVisuallyHidden(const Node& node)
{
if (!node.renderStyle())
return true;
auto& style = *node.renderStyle();
if (style.display() == DisplayType::None)
return true;
if (style.visibility() == Visibility::Hidden)
return true;
if (!style.opacity())
return true;
auto width = style.logicalWidth();
auto height = style.logicalHeight();
if ((width.isFixed() && !width.value()) || (height.isFixed() && !height.value()))
return true;
auto top = style.logicalTop();
auto left = style.logicalLeft();
// FIXME: This is trying to check if the element is outside of the viewport. This is incorrect for many reasons.
if (left.isFixed() && width.isFixed() && -left.value() >= width.value())
return true;
if (top.isFixed() && height.isFixed() && -top.value() >= height.value())
return true;
// It's a common technique used to position content offscreen.
if (style.hasOutOfFlowPosition() && left.isFixed() && left.value() <= -999)
return true;
// FIXME: Check for other cases like zero height with overflow hidden.
auto maxHeight = style.maxHeight();
if (maxHeight.isFixed() && !maxHeight.value())
return true;
// Special case opacity, because a descendant with non-zero opacity should still be considered hidden when one of its ancetors has opacity: 0;
// YouTube.com has this setup with the bottom control bar.
constexpr static unsigned numberOfAncestorsToCheckForOpacity = 4;
unsigned i = 0;
for (auto* parent = node.parentNode(); parent && i < numberOfAncestorsToCheckForOpacity; parent = parent->parentNode(), ++i) {
if (!parent->renderStyle() || !parent->renderStyle()->opacity())
return true;
}
#if ENABLE(FULLSCREEN_API)
if (isHiddenBehindFullscreenElement(node))
return true;
#endif
return false;
}
bool ContentChangeObserver::isConsideredVisible(const Node& node)
{
if (isVisuallyHidden(node))
return false;
auto& style = *node.renderStyle();
auto width = style.logicalWidth();
// 1px width or height content is not considered visible.
if (width.isFixed() && width.value() <= 1)
return false;
auto height = style.logicalHeight();
if (height.isFixed() && height.value() <= 1)
return false;
return true;
}
bool ContentChangeObserver::isConsideredActionableContent(const Element& candidateElement, ElementHadRenderer hadRenderer) const
{
if (m_document.quirks().shouldTooltipPreventFromProceedingWithClick(candidateElement))
return true;
auto isConsideredClickable = [&] {
auto& element = const_cast<Element&>(candidateElement);
if (element.isInUserAgentShadowTree())
return false;
if (is<HTMLIFrameElement>(element))
return true;
if (auto imageElement = dynamicDowncast<HTMLImageElement>(element)) {
// This is required to avoid HTMLImageElement's touch callout override logic. See rdar://problem/48937767.
return imageElement->willRespondToMouseClickEventsWithEditability(imageElement->computeEditabilityForMouseClickEvents(), HTMLImageElement::IgnoreTouchCallout::Yes);
}
bool hasRenderer = element.renderer();
auto willRespondToMouseClickEvents = element.willRespondToMouseClickEvents();
if (willRespondToMouseClickEvents || !hasRenderer || hadRenderer == ElementHadRenderer::No)
return willRespondToMouseClickEvents;
// In case when the content already had renderers it's not sufficient to check the candidate element only since it might just be the container for the clickable content.
for (auto& descendant : descendantsOfType<RenderElement>(*element.renderer())) {
if (!descendant.element())
continue;
if (descendant.element()->willRespondToMouseClickEvents())
return true;
}
return false;
};
return isConsideredClickable();
}
ContentChangeObserver::ContentChangeObserver(Document& document)
: m_document(document)
, m_contentObservationTimer([this] { completeDurationBasedContentObservation(); })
{
}
static void willNotProceedWithClick(LocalFrame& mainFrame)
{
for (Frame* frame = &mainFrame; frame; frame = frame->tree().traverseNext()) {
auto* localFrame = dynamicDowncast<LocalFrame>(frame);
if (!localFrame)
continue;
if (auto* document = localFrame->document())
document->contentChangeObserver().willNotProceedWithClick();
}
}
void ContentChangeObserver::didCancelPotentialTap(LocalFrame& mainFrame)
{
LOG(ContentObservation, "didCancelPotentialTap: cancel ongoing content change observing.");
WebCore::willNotProceedWithClick(mainFrame);
}
void ContentChangeObserver::didRecognizeLongPress(LocalFrame& mainFrame)
{
LOG(ContentObservation, "didRecognizeLongPress: cancel ongoing content change observing.");
WebCore::willNotProceedWithClick(mainFrame);
}
void ContentChangeObserver::didPreventDefaultForEvent(LocalFrame& mainFrame)
{
LOG(ContentObservation, "didPreventDefaultForEvent: cancel ongoing content change observing.");
WebCore::willNotProceedWithClick(mainFrame);
}
void ContentChangeObserver::startContentObservationForDuration(Seconds duration)
{
if (!isContentChangeObserverEnabled())
return;
ASSERT(!hasVisibleChangeState());
LOG_WITH_STREAM(ContentObservation, stream << "startContentObservationForDuration: start observing the content for " << duration.milliseconds() << "ms");
adjustObservedState(Event::StartedFixedObservationTimeWindow);
m_contentObservationTimer.startOneShot(duration);
}
void ContentChangeObserver::completeDurationBasedContentObservation()
{
LOG_WITH_STREAM(ContentObservation, stream << "completeDurationBasedContentObservation: complete duration based content observing ");
adjustObservedState(Event::EndedFixedObservationTimeWindow);
}
static bool isObservedPropertyForTransition(AnimatableProperty property)
{
return WTF::switchOn(property,
[] (CSSPropertyID propertyId) {
return propertyId == CSSPropertyLeft || propertyId == CSSPropertyOpacity;
},
[] (const AtomString&) {
return false;
}
);
}
void ContentChangeObserver::didAddTransition(const Element& element, const Animation& transition)
{
if (!isContentChangeObserverEnabled())
return;
if (hasVisibleChangeState())
return;
if (!isObservingContentChanges())
return;
if (!isObservingTransitions())
return;
if (!transition.isDurationSet() || !transition.isPropertySet())
return;
if (!isObservedPropertyForTransition(transition.property().animatableProperty))
return;
auto transitionEnd = Seconds { transition.duration() + std::max<double>(0, transition.isDelaySet() ? transition.delay() : 0) };
if (transitionEnd > maximumDelayForTransitions)
return;
if (!isVisuallyHidden(element))
return;
// In case of multiple transitions, the first tranistion wins (and it has to produce a visible content change in order to show up as hover).
if (m_elementsWithTransition.contains(element))
return;
LOG_WITH_STREAM(ContentObservation, stream << "didAddTransition: transition created on " << &element << " (" << transitionEnd.milliseconds() << "ms).");
m_elementsWithTransition.add(element);
adjustObservedState(Event::AddedTransition);
}
void ContentChangeObserver::didFinishTransition(const Element& element, CSSPropertyID propertyID)
{
if (!isObservedPropertyForTransition(propertyID))
return;
if (!m_elementsWithTransition.remove(element))
return;
LOG_WITH_STREAM(ContentObservation, stream << "didFinishTransition: transition finished (" << &element << ").");
// isConsideredActionableContent may trigger style update through Node::computeEditability. Let's adjust the state in the next runloop.
callOnMainThread([weakThis = WeakPtr { *this }, targetElement = WeakPtr { element }] {
if (!weakThis || !targetElement)
return;
if (isVisuallyHidden(*targetElement)) {
weakThis->adjustObservedState(Event::EndedTransitionButFinalStyleIsNotDefiniteYet);
return;
}
if (weakThis->isConsideredActionableContent(*targetElement, ElementHadRenderer::Yes))
weakThis->elementDidBecomeVisible(*targetElement);
weakThis->adjustObservedState(Event::CompletedTransition);
});
}
void ContentChangeObserver::didRemoveTransition(const Element& element, CSSPropertyID propertyID)
{
if (!isObservedPropertyForTransition(propertyID))
return;
if (!m_elementsWithTransition.remove(element))
return;
LOG_WITH_STREAM(ContentObservation, stream << "didRemoveTransition: transition got interrupted (" << &element << ").");
adjustObservedState(Event::CanceledTransition);
}
void ContentChangeObserver::didInstallDOMTimer(const DOMTimer& timer, Seconds timeout, bool singleShot)
{
if (!isContentChangeObserverEnabled())
return;
if (!isObservingContentChanges())
return;
if (!isObservingDOMTimerScheduling())
return;
if (hasVisibleChangeState())
return;
if (m_document.activeDOMObjectsAreSuspended())
return;
if (timeout > maximumDelayForTimers || !singleShot)
return;
LOG_WITH_STREAM(ContentObservation, stream << "didInstallDOMTimer: register this timer: (" << &timer << ") and observe when it fires.");
registerDOMTimer(timer);
adjustObservedState(Event::InstalledDOMTimer);
}
void ContentChangeObserver::didRemoveDOMTimer(const DOMTimer& timer)
{
if (!containsObservedDOMTimer(timer))
return;
LOG_WITH_STREAM(ContentObservation, stream << "removeDOMTimer: remove registered timer (" << &timer << ")");
unregisterDOMTimer(timer);
adjustObservedState(Event::RemovedDOMTimer);
}
void ContentChangeObserver::willNotProceedWithClick()
{
LOG(ContentObservation, "willNotProceedWithClick: click will not happen.");
adjustObservedState(Event::WillNotProceedWithClick);
}
void ContentChangeObserver::domTimerExecuteDidStart(const DOMTimer& timer)
{
if (!containsObservedDOMTimer(timer))
return;
LOG_WITH_STREAM(ContentObservation, stream << "startObservingDOMTimerExecute: start observing (" << &timer << ") timer callback.");
m_observedDomTimerIsBeingExecuted = true;
adjustObservedState(Event::StartedDOMTimerExecution);
}
void ContentChangeObserver::domTimerExecuteDidFinish(const DOMTimer& timer)
{
if (!m_observedDomTimerIsBeingExecuted)
return;
LOG_WITH_STREAM(ContentObservation, stream << "stopObservingDOMTimerExecute: stop observing (" << &timer << ") timer callback.");
m_observedDomTimerIsBeingExecuted = false;
unregisterDOMTimer(timer);
adjustObservedState(Event::EndedDOMTimerExecution);
}
void ContentChangeObserver::registerDOMTimer(const DOMTimer& timer)
{
m_DOMTimerList.add(timer);
}
void ContentChangeObserver::unregisterDOMTimer(const DOMTimer& timer)
{
m_DOMTimerList.remove(timer);
}
void ContentChangeObserver::clearObservedDOMTimers()
{
m_DOMTimerList.clear();
}
bool ContentChangeObserver::containsObservedDOMTimer(const DOMTimer& timer) const
{
return m_DOMTimerList.contains(timer);
}
bool ContentChangeObserver::hasObservedDOMTimer() const
{
return !m_DOMTimerList.isEmptyIgnoringNullReferences();
}
void ContentChangeObserver::styleRecalcDidStart()
{
if (!isWaitingForStyleRecalc())
return;
LOG(ContentObservation, "startObservingStyleRecalc: start observing style recalc.");
m_isInObservedStyleRecalc = true;
adjustObservedState(Event::StartedStyleRecalc);
}
void ContentChangeObserver::styleRecalcDidFinish()
{
if (!m_isInObservedStyleRecalc)
return;
LOG(ContentObservation, "stopObservingStyleRecalc: stop observing style recalc");
m_isInObservedStyleRecalc = false;
adjustObservedState(Event::EndedStyleRecalc);
}
void ContentChangeObserver::stopObservingPendingActivities()
{
setShouldObserveNextStyleRecalc(false);
setShouldObserveDOMTimerSchedulingAndTransitions(false);
clearObservedDOMTimers();
clearObservedTransitions();
}
void ContentChangeObserver::stopContentObservation()
{
reset();
}
void ContentChangeObserver::reset()
{
stopObservingPendingActivities();
setHasNoChangeState();
setTouchEventIsBeingDispatched(false);
setIsBetweenTouchEndAndMouseMoved(false);
setMouseMovedEventIsBeingDispatched(false);
m_isInObservedStyleRecalc = false;
m_observedDomTimerIsBeingExecuted = false;
m_visibilityCandidateList.clear();
m_contentObservationTimer.stop();
m_elementsWithDestroyedVisibleRenderer.clear();
resetHiddenTouchTarget();
}
void ContentChangeObserver::didSuspendActiveDOMObjects()
{
LOG(ContentObservation, "didSuspendActiveDOMObjects");
reset();
}
void ContentChangeObserver::willDetachPage()
{
LOG(ContentObservation, "willDetachPage");
reset();
}
void ContentChangeObserver::rendererWillBeDestroyed(const Element& element)
{
if (!isContentChangeObserverEnabled())
return;
if (!isObservingContentChanges())
return;
LOG_WITH_STREAM(ContentObservation, stream << "rendererWillBeDestroyed element: " << &element);
if (!isVisuallyHidden(element))
m_elementsWithDestroyedVisibleRenderer.add(element);
elementDidBecomeHidden(element);
}
void ContentChangeObserver::elementDidBecomeVisible(const Element& element)
{
LOG_WITH_STREAM(ContentObservation, stream << "elementDidBecomeVisible: element went from hidden to visible: " << &element);
m_visibilityCandidateList.add(element);
adjustObservedState(Event::ElementDidBecomeVisible);
}
void ContentChangeObserver::elementDidBecomeHidden(const Element& element)
{
LOG_WITH_STREAM(ContentObservation, stream << "elementDidBecomeHidden: element went from visible to hidden: " << &element);
// Candidate element is no longer visible.
if (!m_visibilityCandidateList.remove(element))
return;
// ASSERT(hasVisibleChangeState());
if (m_visibilityCandidateList.isEmptyIgnoringNullReferences())
setHasIndeterminateState();
}
void ContentChangeObserver::touchEventDidStart(PlatformEvent::Type eventType)
{
#if ENABLE(TOUCH_EVENTS)
if (!isContentChangeObserverEnabled() || m_document.quirks().shouldDisableContentChangeObserverTouchEventAdjustment())
return;
if (eventType != PlatformEvent::Type::TouchStart)
return;
LOG(ContentObservation, "touchEventDidStart: touch start event started.");
setTouchEventIsBeingDispatched(true);
adjustObservedState(Event::StartedTouchStartEventDispatching);
#else
UNUSED_PARAM(eventType);
#endif
}
void ContentChangeObserver::touchEventDidFinish()
{
#if ENABLE(TOUCH_EVENTS)
if (!isTouchEventBeingDispatched())
return;
ASSERT(isContentChangeObserverEnabled());
LOG(ContentObservation, "touchEventDidFinish: touch start event finished.");
setTouchEventIsBeingDispatched(false);
adjustObservedState(Event::EndedTouchStartEventDispatching);
#endif
}
void ContentChangeObserver::mouseMovedDidStart()
{
if (!isContentChangeObserverEnabled())
return;
LOG(ContentObservation, "mouseMovedDidStart: mouseMoved started.");
setMouseMovedEventIsBeingDispatched(true);
adjustObservedState(Event::StartedMouseMovedEventDispatching);
}
void ContentChangeObserver::mouseMovedDidFinish()
{
if (!isMouseMovedEventBeingDispatched())
return;
ASSERT(isContentChangeObserverEnabled());
LOG(ContentObservation, "mouseMovedDidFinish: mouseMoved finished.");
adjustObservedState(Event::EndedMouseMovedEventDispatching);
setMouseMovedEventIsBeingDispatched(false);
}
void ContentChangeObserver::willNotProceedWithFixedObservationTimeWindow()
{
ASSERT(!isMouseMovedEventBeingDispatched());
adjustObservedState(Event::WillNotProceedWithFixedObservationTimeWindow);
}
void ContentChangeObserver::setShouldObserveNextStyleRecalc(bool shouldObserve)
{
if (shouldObserve)
LOG(ContentObservation, "Wait until next style recalc fires.");
m_isWaitingForStyleRecalc = shouldObserve;
}
void ContentChangeObserver::adjustObservedState(Event event)
{
auto resetToStartObserving = [&] {
setHasNoChangeState();
clearObservedDOMTimers();
clearObservedTransitions();
setIsBetweenTouchEndAndMouseMoved(false);
setShouldObserveNextStyleRecalc(false);
setShouldObserveDOMTimerSchedulingAndTransitions(false);
ASSERT(!m_isInObservedStyleRecalc);
ASSERT(!m_observedDomTimerIsBeingExecuted);
};
auto notifyClientIfNeeded = [&] {
if (isTouchEventBeingDispatched()) {
LOG(ContentObservation, "notifyClientIfNeeded: Touch event is being dispatched. No need to notify the client.");
return;
}
if (isBetweenTouchEndAndMouseMoved()) {
LOG(ContentObservation, "notifyClientIfNeeded: Not reached mouseMoved yet. No need to notify the client.");
return;
}
if (isMouseMovedEventBeingDispatched()) {
LOG(ContentObservation, "notifyClientIfNeeded: in mouseMoved call. No need to notify the client.");
return;
}
if (isObservationTimeWindowActive()) {
LOG(ContentObservation, "notifyClientIfNeeded: Inside the fixed window observation. No need to notify the client.");
return;
}
// The fixed observation window (which is the final step in content observation) is closed and now we check if are still waiting for timers or animations to finish.
if (hasPendingActivity()) {
LOG(ContentObservation, "notifyClientIfNeeded: We are still waiting on some events.");
return;
}
// First demote to "no change" because we've got no pending activity anymore.
if (observedContentChange() == WKContentIndeterminateChange)
setHasNoChangeState();
LOG_WITH_STREAM(ContentObservation, stream << "notifyClientIfNeeded: sending observedContentChange ->" << observedContentChange());
ASSERT(m_document.page());
ASSERT(m_document.frame());
m_document.page()->chrome().client().didFinishContentChangeObserving(*m_document.frame(), observedContentChange());
stopContentObservation();
};
// These user initiated events trigger content observation (touchStart and mouseMove).
{
if (event == Event::StartedTouchStartEventDispatching) {
resetToStartObserving();
setShouldObserveDOMTimerSchedulingAndTransitions(true);
return;
}
if (event == Event::EndedTouchStartEventDispatching) {
setShouldObserveDOMTimerSchedulingAndTransitions(false);
setIsBetweenTouchEndAndMouseMoved(true);
return;
}
if (event == Event::StartedMouseMovedEventDispatching) {
ASSERT(!m_document.hasPendingStyleRecalc());
if (!isBetweenTouchEndAndMouseMoved())
resetToStartObserving();
setIsBetweenTouchEndAndMouseMoved(false);
setShouldObserveDOMTimerSchedulingAndTransitions(!hasVisibleChangeState());
return;
}
if (event == Event::EndedMouseMovedEventDispatching) {
setShouldObserveDOMTimerSchedulingAndTransitions(false);
return;
}
}
// Fixed window observation starts soon after mouseMove when we don't have a definite answer to whether we should proceed with hover or click.
{
if (event == Event::StartedFixedObservationTimeWindow) {
ASSERT(!hasVisibleChangeState());
setHasIndeterminateState();
return;
}
if (event == Event::EndedFixedObservationTimeWindow) {
notifyClientIfNeeded();
return;
}
if (event == Event::WillNotProceedWithFixedObservationTimeWindow) {
notifyClientIfNeeded();
return;
}
}
// These events (DOM timer, transition and style recalc) could trigger style changes that are candidates to visibility checking.
{
if (event == Event::InstalledDOMTimer || event == Event::AddedTransition) {
ASSERT(!hasVisibleChangeState());
setHasIndeterminateState();
return;
}
if (event == Event::RemovedDOMTimer || event == Event::CanceledTransition) {
notifyClientIfNeeded();
return;
}
if (event == Event::StartedDOMTimerExecution) {
ASSERT(isObservationTimeWindowActive() || observedContentChange() == WKContentIndeterminateChange);
return;
}
if (event == Event::EndedDOMTimerExecution) {
if (m_document.hasPendingStyleRecalc()) {
setShouldObserveNextStyleRecalc(true);
return;
}
notifyClientIfNeeded();
return;
}
if (event == Event::EndedTransitionButFinalStyleIsNotDefiniteYet) {
// onAnimationEnd can be called while in the middle of resolving the document (synchronously) or
// asynchronously right before the style update is issued. It also means we don't know whether this animation ends up producing visible content yet.
if (m_document.inStyleRecalc()) {
// We need to start observing this style change synchronously.
m_isInObservedStyleRecalc = true;
return;
}
setShouldObserveNextStyleRecalc(true);
return;
}
if (event == Event::CompletedTransition) {
if (m_document.inStyleRecalc()) {
m_isInObservedStyleRecalc = true;
return;
}
notifyClientIfNeeded();
return;
}
if (event == Event::StartedStyleRecalc) {
setShouldObserveNextStyleRecalc(false);
ASSERT(isObservationTimeWindowActive() || observedContentChange() == WKContentIndeterminateChange);
return;
}
if (event == Event::EndedStyleRecalc) {
notifyClientIfNeeded();
return;
}
}
// Either the page decided to call preventDefault on the touch action or the tap gesture evolved to some other gesture (long press, double tap).
if (event == Event::WillNotProceedWithClick) {
stopContentObservation();
return;
}
// The page produced an visible change on an actionable content.
if (event == Event::ElementDidBecomeVisible) {
setHasVisibleChangeState();
// Stop pending activities. We don't need to observe them anymore.
stopObservingPendingActivities();
return;
}
}
bool ContentChangeObserver::shouldObserveVisibilityChangeForElement(const Element& element)
{
return isObservingContentChanges() && !visibleRendererWasDestroyed(element);
}
ContentChangeObserver::StyleChangeScope::StyleChangeScope(Document& document, const Element& element)
: m_contentChangeObserver(document.contentChangeObserver())
, m_element(element)
, m_hadRenderer(element.renderer())
{
if (m_contentChangeObserver.shouldObserveVisibilityChangeForElement(element))
m_wasHidden = isVisuallyHidden(m_element);
}
ContentChangeObserver::StyleChangeScope::~StyleChangeScope()
{
// Do we track this element?
if (!m_wasHidden.has_value())
return;
if (!m_contentChangeObserver.isConsideredActionableContent(m_element, m_hadRenderer ? ElementHadRenderer::Yes : ElementHadRenderer::No))
return;
auto wasVisible = !m_wasHidden.value();
auto isVisible = isConsideredVisible(m_element);
if (!wasVisible && isVisible)
m_contentChangeObserver.elementDidBecomeVisible(m_element);
else if (wasVisible && !isVisible)
m_contentChangeObserver.elementDidBecomeHidden(m_element);
}
#if ENABLE(TOUCH_EVENTS)
ContentChangeObserver::TouchEventScope::TouchEventScope(Document& document, PlatformEvent::Type eventType)
: m_contentChangeObserver(document.contentChangeObserver())
{
m_contentChangeObserver.touchEventDidStart(eventType);
}
ContentChangeObserver::TouchEventScope::~TouchEventScope()
{
m_contentChangeObserver.touchEventDidFinish();
}
#endif
ContentChangeObserver::MouseMovedScope::MouseMovedScope(Document& document)
: m_contentChangeObserver(document.contentChangeObserver())
{
m_contentChangeObserver.mouseMovedDidStart();
}
ContentChangeObserver::MouseMovedScope::~MouseMovedScope()
{
m_contentChangeObserver.mouseMovedDidFinish();
m_contentChangeObserver.resetHiddenTouchTarget();
}
ContentChangeObserver::StyleRecalcScope::StyleRecalcScope(Document& document)
: m_contentChangeObserver(document.contentChangeObserver())
{
m_contentChangeObserver.styleRecalcDidStart();
}
ContentChangeObserver::StyleRecalcScope::~StyleRecalcScope()
{
m_contentChangeObserver.styleRecalcDidFinish();
}
ContentChangeObserver::DOMTimerScope::DOMTimerScope(Document* document, const DOMTimer& domTimer)
: m_contentChangeObserver(document ? &document->contentChangeObserver() : nullptr)
, m_domTimer(domTimer)
{
if (m_contentChangeObserver)
m_contentChangeObserver->domTimerExecuteDidStart(m_domTimer);
}
ContentChangeObserver::DOMTimerScope::~DOMTimerScope()
{
if (m_contentChangeObserver)
m_contentChangeObserver->domTimerExecuteDidFinish(m_domTimer);
}
}
#endif // ENABLE(CONTENT_CHANGE_OBSERVER)