Chromium Code Reviews| Index: third_party/WebKit/Source/core/dom/IntersectionObserver.cpp |
| diff --git a/third_party/WebKit/Source/core/dom/IntersectionObserver.cpp b/third_party/WebKit/Source/core/dom/IntersectionObserver.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..494f8bf0b76646b29774cb148de743bf6340ad33 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/core/dom/IntersectionObserver.cpp |
| @@ -0,0 +1,332 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "config.h" |
| +#include "core/dom/IntersectionObserver.h" |
| + |
| +#include "bindings/core/v8/ExceptionState.h" |
| +#include "core/css/parser/CSSParserTokenRange.h" |
| +#include "core/css/parser/CSSTokenizer.h" |
| +#include "core/dom/ElementIntersectionObserverData.h" |
| +#include "core/dom/ExceptionCode.h" |
| +#include "core/dom/ExecutionContext.h" |
| +#include "core/dom/IntersectionObserverCallback.h" |
| +#include "core/dom/IntersectionObserverController.h" |
| +#include "core/dom/IntersectionObserverEntry.h" |
| +#include "core/dom/IntersectionObserverInit.h" |
| +#include "core/html/HTMLFrameOwnerElement.h" |
| +#include "core/layout/LayoutView.h" |
| +#include "platform/Timer.h" |
| +#include "wtf/MainThread.h" |
| +#include <algorithm> |
| + |
| +namespace blink { |
| + |
| +static void parseRootMargin(String rootMarginParameter, Vector<Length>& rootMargin, ExceptionState& exceptionState) |
| +{ |
| + CSSTokenizer::Scope tokenizerScope(rootMarginParameter); |
| + CSSParserTokenRange tokenRange = tokenizerScope.tokenRange(); |
| + while (rootMargin.size() < 5 && tokenRange.peek().type() != EOFToken && !exceptionState.hadException()) { |
| + const CSSParserToken& token = tokenRange.consumeIncludingWhitespace(); |
| + switch (token.type()) { |
| + case PercentageToken: |
| + rootMargin.append(Length(token.numericValue(), Percent)); |
| + break; |
| + case DimensionToken: |
| + switch (token.unitType()) { |
| + case CSSPrimitiveValue::UnitType::Pixels: |
| + rootMargin.append(Length(static_cast<int>(floor(token.numericValue())), Fixed)); |
| + break; |
| + case CSSPrimitiveValue::UnitType::Percentage: |
| + rootMargin.append(Length(token.numericValue(), Percent)); |
| + break; |
| + default: |
| + exceptionState.throwTypeError("rootMargin must be specified in pixels or percent."); |
| + } |
| + break; |
| + default: |
| + exceptionState.throwTypeError("rootMargin must be specified in pixels or percent."); |
| + } |
| + } |
| +} |
| + |
| +static void parseThresholds(const DoubleOrDoubleArray& thresholdParameter, Vector<float>& thresholds, ExceptionState& exceptionState) |
| +{ |
| + if (thresholdParameter.isDouble()) { |
| + thresholds.append(static_cast<float>(thresholdParameter.getAsDouble())); |
| + } else { |
| + for (auto thresholdValue : thresholdParameter.getAsDoubleArray()) |
| + thresholds.append(static_cast<float>(thresholdValue)); |
| + } |
| + |
| + for (auto thresholdValue : thresholds) { |
| + if (thresholdValue < 0.0 || thresholdValue > 1.0) { |
| + exceptionState.throwTypeError("Threshold values must be between 0 and 1"); |
| + break; |
| + } |
| + } |
| + |
| + std::sort(thresholds.begin(), thresholds.end()); |
| +} |
| + |
| +IntersectionObserver* IntersectionObserver::create(const IntersectionObserverInit& observerInit, IntersectionObserverCallback& callback, ExceptionState& exceptionState) |
| +{ |
| + RefPtrWillBeRawPtr<Element> root = observerInit.root(); |
| + if (!root) { |
| + ExecutionContext* context = callback.executionContext(); |
| + ASSERT(context->isDocument()); |
| + Frame* mainFrame = toDocument(context)->frame()->tree().top(); |
| + if (mainFrame && mainFrame->isLocalFrame()) |
| + root = toLocalFrame(mainFrame)->document()->documentElement(); |
|
esprehn
2015/12/17 01:40:28
Add a TODO and maybe link to that new bug?
szager1
2015/12/17 20:27:26
Done.
|
| + } |
| + if (!root) { |
| + exceptionState.throwDOMException(HierarchyRequestError, "Unable to get root element in main frame to track."); |
| + return nullptr; |
| + } |
| + |
| + Vector<Length> rootMargin; |
| + if (observerInit.hasRootMargin()) |
| + parseRootMargin(observerInit.rootMargin(), rootMargin, exceptionState); |
| + if (exceptionState.hadException()) |
| + return nullptr; |
| + |
| + Vector<float> thresholds; |
| + if (observerInit.hasThreshold()) |
| + parseThresholds(observerInit.threshold(), thresholds, exceptionState); |
| + else |
| + thresholds.append(0); |
| + if (exceptionState.hadException()) |
| + return nullptr; |
| + |
| + return new IntersectionObserver(callback, *root, rootMargin, thresholds); |
| +} |
| + |
| +IntersectionObserver::IntersectionObserver(IntersectionObserverCallback& callback, Element& root, const Vector<Length>& rootMargin, const Vector<float>& thresholds) |
| + : m_callback(&callback) |
| + , m_root(root.intersectionObserverData().createWeakPtr(&root)) |
| + , m_thresholds(thresholds) |
| +{ |
| + switch (rootMargin.size()) { |
| + case 0: |
| + break; |
| + case 1: |
| + m_topMargin = m_rightMargin = m_bottomMargin = m_leftMargin = rootMargin[0]; |
| + break; |
| + case 2: |
| + m_topMargin = m_bottomMargin = rootMargin[0]; |
| + m_rightMargin = m_leftMargin = rootMargin[1]; |
| + break; |
| + case 3: |
| + m_topMargin = rootMargin[0]; |
| + m_rightMargin = m_leftMargin = rootMargin[1]; |
| + m_bottomMargin = rootMargin[2]; |
| + break; |
| + default: |
|
esprehn
2015/12/17 01:40:28
case 4:
and the default should ASSERT_NOT_REACHED
szager1
2015/12/17 20:27:26
Done.
|
| + m_topMargin = rootMargin[0]; |
| + m_rightMargin = rootMargin[1]; |
| + m_bottomMargin = rootMargin[2]; |
| + m_leftMargin = rootMargin[3]; |
| + break; |
|
esprehn
2015/12/17 01:40:28
I feel like we should be able to ASSERT_NOT_REACHE
szager1
2015/12/17 20:27:26
Done.
|
| + } |
| + root.document().intersectionObserverController()->addTrackedObserver(*this); |
| +} |
| + |
| +LayoutObject* IntersectionObserver::rootLayoutObject() |
| +{ |
| + Element* rootElement = root(); |
| + bool rootIsDocumentElement = (rootElement == rootElement->document().documentElement()); |
|
esprehn
2015/12/17 01:40:28
break this apart.
if (rootElement == rootElement-
szager1
2015/12/17 20:27:26
Done.
|
| + return rootIsDocumentElement ? rootElement->document().layoutView() : rootElement->layoutObject(); |
| +} |
| + |
| +bool IntersectionObserver::isDescendantOfRoot(const Element* target) const |
| +{ |
| + Element* rootElement = m_root.get(); |
| + if (!rootElement || !target || target == rootElement) |
| + return false; |
| + if (!target->inDocument() || !rootElement->inDocument()) |
| + return false; |
| + |
| + Document* rootDocument = &rootElement->document(); |
| + Document* targetDocument = &target->document(); |
| + while (targetDocument != rootDocument) { |
| + target = targetDocument->ownerElement(); |
| + if (!target) |
| + return false; |
| + targetDocument = &target->document(); |
| + } |
| + return target->isDescendantOf(rootElement); |
| +} |
| + |
| +void IntersectionObserver::observe(Element* target, ExceptionState& exceptionState) |
| +{ |
| + checkRootAndDetachIfNeeded(); |
| + if (!m_root) { |
| + exceptionState.throwDOMException(HierarchyRequestError, "Invalid observer: root element or containing document has been deleted."); |
| + return; |
| + } |
| + if (!target) { |
| + exceptionState.throwTypeError("Observation target must be an element."); |
| + return; |
| + } |
| + if (m_root.get() == target) { |
| + exceptionState.throwDOMException(HierarchyRequestError, "Cannot use the same element for root and target."); |
| + return; |
| + } |
| + if (!isDescendantOfRoot(target)) { |
| + exceptionState.throwDOMException(HierarchyRequestError, "Observed element must be a descendant of the observer's root element."); |
|
esprehn
2015/12/17 01:40:29
I do kind of wonder if we should relax this in the
szager1
2015/12/17 20:27:26
Acknowledged.
|
| + return; |
| + } |
| + |
| + bool shouldReportRootBounds = target->document().frame()->securityContext()->securityOrigin()->canAccess(root()->document().frame()->securityContext()->securityOrigin()); |
| + if (!shouldReportRootBounds && hasPercentMargin()) { |
| + exceptionState.throwDOMException(HierarchyRequestError, "Cannot observe a cross-origin target because the observer has a root margin value specified as a percent."); |
| + return; |
| + } |
| + |
| + if (target->intersectionObserverData().hasObservationFor(*this)) |
| + return; |
| + |
| + IntersectionObservation* observation = new IntersectionObservation(*this, *target, shouldReportRootBounds); |
| + m_observations.add(observation); |
| +} |
| + |
| +void IntersectionObserver::unobserve(Element* target, ExceptionState&) |
| +{ |
| + checkRootAndDetachIfNeeded(); |
| + if (!target || !target->hasIntersectionObserverData()) |
| + return; |
| + // TODO: unobserve callback |
|
esprehn
2015/12/17 01:40:28
TODO(szager):
It's good to attribute your TODO's
szager1
2015/12/17 20:27:26
Done, here and elsewhere.
|
| + target->intersectionObserverData().removeObservation(*this); |
| +} |
| + |
| +void IntersectionObserver::computeIntersectionObservations(double timestamp) |
| +{ |
| + checkRootAndDetachIfNeeded(); |
| + if (!m_root) |
| + return; |
| + for (auto& observation : m_observations) |
| + observation->computeIntersectionObservations(timestamp); |
| +} |
| + |
| +void IntersectionObserver::disconnect(IntersectionObservation& observation) |
| +{ |
| + m_observations.remove(&observation); |
| +} |
| + |
| +void IntersectionObserver::disconnect() |
| +{ |
| + checkRootAndDetachIfNeeded(); |
| + HeapVector<Member<IntersectionObservation>> toDisconnect; |
| + for (auto& observation : m_observations) |
| + toDisconnect.append(observation); |
| + for (auto& observation : toDisconnect) |
| + observation->disconnect(); |
| + ASSERT(m_observations.isEmpty()); |
| +} |
| + |
| +HeapVector<Member<IntersectionObserverEntry>> IntersectionObserver::takeRecords() |
| +{ |
| + checkRootAndDetachIfNeeded(); |
| + HeapVector<Member<IntersectionObserverEntry>> entries; |
| + entries.swap(m_entries); |
| + return entries; |
| +} |
| + |
| +void IntersectionObserver::enqueueIntersectionObserverEntry(IntersectionObserverEntry& entry) |
| +{ |
| + m_entries.append(&entry); |
| + toDocument(m_callback->executionContext())->intersectionObserverController()->scheduleIntersectionObserverForDelivery(*this); |
| +} |
| + |
| +static int computeMargin(const Length& length, LayoutUnit referenceLength) |
| +{ |
| + if (length.type() == Percent) |
| + return static_cast<int>(referenceLength.toFloat() * length.percent() / 100.0); |
| + return length.intValue(); |
| +} |
| + |
| +void IntersectionObserver::applyRootMargin(LayoutRect& rect) const |
| +{ |
| + int topMargin = computeMargin(m_topMargin, rect.height()); |
| + int rightMargin = computeMargin(m_rightMargin, rect.width()); |
| + int bottomMargin = computeMargin(m_bottomMargin, rect.height()); |
| + int leftMargin = computeMargin(m_leftMargin, rect.width()); |
| + |
| + rect.setX(rect.x() - leftMargin); |
| + rect.setWidth(rect.width() + leftMargin + rightMargin); |
| + rect.setY(rect.y() - topMargin); |
| + rect.setHeight(rect.height() + topMargin + bottomMargin); |
| +} |
| + |
| +unsigned IntersectionObserver::firstThresholdGreaterThan(float ratio) const |
| +{ |
| + unsigned result = 0; |
| + while (result < m_thresholds.size() && m_thresholds[result] < ratio) |
| + ++result; |
| + return result; |
| +} |
| + |
| +bool IntersectionObserver::shouldBeSuspended() const |
| +{ |
| + return m_callback->executionContext() && m_callback->executionContext()->activeDOMObjectsAreSuspended(); |
| +} |
| + |
| +void IntersectionObserver::deliver() |
| +{ |
| + checkRootAndDetachIfNeeded(); |
| + |
| + ASSERT(!shouldBeSuspended()); |
| + |
| + if (m_entries.isEmpty()) |
| + return; |
| + |
| + HeapVector<Member<IntersectionObserverEntry>> entries; |
| + entries.swap(m_entries); |
| + m_callback->handleEvent(entries, *this); |
| +} |
| + |
| +void IntersectionObserver::setActive(bool active) |
| +{ |
| + checkRootAndDetachIfNeeded(); |
| + for (auto& observation : m_observations) |
| + observation->setActive(m_root && active && isDescendantOfRoot(observation->target())); |
| +} |
| + |
| +bool IntersectionObserver::hasPercentMargin() const |
| +{ |
| + return m_topMargin.type() == Percent || m_rightMargin.type() == Percent || m_bottomMargin.type() == Percent || m_leftMargin.type() == Percent; |
|
esprehn
2015/12/17 01:40:29
I'd wrap each || so this is multiple lines
szager1
2015/12/17 20:27:26
Done.
|
| +} |
| + |
| +#if !ENABLE(OILPAN) |
| +void IntersectionObserver::dispose() |
| +{ |
| + m_root.clear(); |
| + checkRootAndDetachIfNeeded(); |
| +} |
| +#endif |
| + |
| +void IntersectionObserver::checkRootAndDetachIfNeeded() |
|
esprehn
2015/12/17 01:40:29
hmm, I think I need to understand why you need to
szager1
2015/12/17 20:27:26
Now that there's ElementIntersectionObserverData::
|
| +{ |
| + if (m_root) |
| + return; |
| + m_callback.clear(); |
| + HeapVector<Member<IntersectionObservation>> toDisconnect; |
| + for (auto& observation : m_observations) |
| + toDisconnect.append(observation); |
| + for (auto& observation : toDisconnect) |
| + observation->disconnect(); |
| + ASSERT(m_observations.isEmpty()); |
| + // TODO: should we deliver pending notifications? |
| + m_entries.clear(); |
| +} |
| + |
| +DEFINE_TRACE(IntersectionObserver) |
| +{ |
| + visitor->trace(m_callback); |
| + visitor->trace(m_root); |
| + visitor->trace(m_observations); |
| + visitor->trace(m_entries); |
| +} |
| + |
| +} // namespace blink |