Title: [270729] trunk/Source/WebCore
Revision
270729
Author
[email protected]
Date
2020-12-11 19:19:41 -0800 (Fri, 11 Dec 2020)

Log Message

Infrastructure to store and restore AppHighlights
https://bugs.webkit.org/show_bug.cgi?id=219769
<rdar://problem/70297699>

Reviewed by Tim Horton.

AppHighlights can be pulled out of their HighlightRegister and encoded for storage by the app.
Also, an encoded buffer can be unpacked in ranges added back into the HighlightRegister.
Fallbacks for ranges that are not found are not designed yet, so currently they will just be stored
with the intention to communicate back to the app that they were not found in the current version of
the document. The API to interact with the client is not designed yet, so currently this encoded data does not get
shuttle anywhere but that plumbing will be added in a later patch.

* Modules/highlight/AppHighlightListData.cpp: Added.
(WebCore::computePathIndex):
(WebCore::createNodePathComponent):
(WebCore::makeNodePath):
(WebCore::createAppHiglightRangeData):
(WebCore::AppHighlightListData::create):
(WebCore::AppHighlightListData::toData const):
* Modules/highlight/AppHighlightListData.h: Added.
(WebCore::AppHighlightRangeData::NodePathComponent::NodePathComponent):
(WebCore::AppHighlightRangeData::NodePathComponent::operator== const):
(WebCore::AppHighlightRangeData::NodePathComponent::operator!= const):
(WebCore::AppHighlightRangeData::AppHighlightRangeData):
(WebCore::AppHighlightRangeData::text const):
(WebCore::AppHighlightRangeData::startContainer const):
(WebCore::AppHighlightRangeData::startOffset const):
(WebCore::AppHighlightRangeData::endContainer const):
(WebCore::AppHighlightRangeData::endOffset const):
(WebCore::AppHighlightListData::AppHighlightListData):
(WebCore::AppHighlightListData::ranges const):
(WebCore::AppHighlightListData::setRanges):
(WebCore::AppHighlightListData::addRanges):
(WebCore::AppHighlightListData::size const):
(WebCore::AppHighlightListData::isEmpty const):
(WebCore::AppHighlightRangeData::NodePathComponent::encode const):
(WebCore::AppHighlightRangeData::NodePathComponent::decode):
(WebCore::AppHighlightRangeData::encode const):
(WebCore::AppHighlightRangeData::decode):
(WebCore::AppHighlightListData::encode const):
(WebCore::AppHighlightListData::decode):
* Modules/highlight/AppHighlightStorageController.cpp: Added.
(WebCore::findNodeByPathIndex):
(WebCore::findNodeStartingAtPathComponentIndex):
(WebCore::findNode):
(WebCore::findRangeByIdentifyingStartAndEndPositions):
(WebCore::findRangeBySearchingText):
(WebCore::findRange):
(WebCore::AppHighlightStorageController::AppHighlightStorageController):
(WebCore::AppHighlightStorageController::createAppHighlightListData):
(WebCore::AppHighlightStorageController::restoreAppHighlights):
* Modules/highlight/AppHighlightStorageController.h: Added.
* Modules/mediastream/RTCRtpScriptTransform.cpp:
* Modules/mediastream/RTCRtpScriptTransform.h:
* Sources.txt:
* WebCore.xcodeproj/project.pbxproj:
* dom/Document.cpp:
(WebCore::Document::appHighlightStorageController):
(WebCore::Document::updateHighlightPositions):
* dom/Document.h:
(WebCore::Document::appHighlightStorageControllerIfExists const):

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (270728 => 270729)


--- trunk/Source/WebCore/ChangeLog	2020-12-12 03:01:14 UTC (rev 270728)
+++ trunk/Source/WebCore/ChangeLog	2020-12-12 03:19:41 UTC (rev 270729)
@@ -1,3 +1,68 @@
+2020-12-11  Megan Gardner  <[email protected]>
+
+        Infrastructure to store and restore AppHighlights
+        https://bugs.webkit.org/show_bug.cgi?id=219769
+        <rdar://problem/70297699>
+
+        Reviewed by Tim Horton.
+
+        AppHighlights can be pulled out of their HighlightRegister and encoded for storage by the app.
+        Also, an encoded buffer can be unpacked in ranges added back into the HighlightRegister. 
+        Fallbacks for ranges that are not found are not designed yet, so currently they will just be stored
+        with the intention to communicate back to the app that they were not found in the current version of
+        the document. The API to interact with the client is not designed yet, so currently this encoded data does not get
+        shuttle anywhere but that plumbing will be added in a later patch.
+
+        * Modules/highlight/AppHighlightListData.cpp: Added.
+        (WebCore::computePathIndex):
+        (WebCore::createNodePathComponent):
+        (WebCore::makeNodePath):
+        (WebCore::createAppHiglightRangeData):
+        (WebCore::AppHighlightListData::create):
+        (WebCore::AppHighlightListData::toData const):
+        * Modules/highlight/AppHighlightListData.h: Added.
+        (WebCore::AppHighlightRangeData::NodePathComponent::NodePathComponent):
+        (WebCore::AppHighlightRangeData::NodePathComponent::operator== const):
+        (WebCore::AppHighlightRangeData::NodePathComponent::operator!= const):
+        (WebCore::AppHighlightRangeData::AppHighlightRangeData):
+        (WebCore::AppHighlightRangeData::text const):
+        (WebCore::AppHighlightRangeData::startContainer const):
+        (WebCore::AppHighlightRangeData::startOffset const):
+        (WebCore::AppHighlightRangeData::endContainer const):
+        (WebCore::AppHighlightRangeData::endOffset const):
+        (WebCore::AppHighlightListData::AppHighlightListData):
+        (WebCore::AppHighlightListData::ranges const):
+        (WebCore::AppHighlightListData::setRanges):
+        (WebCore::AppHighlightListData::addRanges):
+        (WebCore::AppHighlightListData::size const):
+        (WebCore::AppHighlightListData::isEmpty const):
+        (WebCore::AppHighlightRangeData::NodePathComponent::encode const):
+        (WebCore::AppHighlightRangeData::NodePathComponent::decode):
+        (WebCore::AppHighlightRangeData::encode const):
+        (WebCore::AppHighlightRangeData::decode):
+        (WebCore::AppHighlightListData::encode const):
+        (WebCore::AppHighlightListData::decode):
+        * Modules/highlight/AppHighlightStorageController.cpp: Added.
+        (WebCore::findNodeByPathIndex):
+        (WebCore::findNodeStartingAtPathComponentIndex):
+        (WebCore::findNode):
+        (WebCore::findRangeByIdentifyingStartAndEndPositions):
+        (WebCore::findRangeBySearchingText):
+        (WebCore::findRange):
+        (WebCore::AppHighlightStorageController::AppHighlightStorageController):
+        (WebCore::AppHighlightStorageController::createAppHighlightListData):
+        (WebCore::AppHighlightStorageController::restoreAppHighlights):
+        * Modules/highlight/AppHighlightStorageController.h: Added.
+        * Modules/mediastream/RTCRtpScriptTransform.cpp:
+        * Modules/mediastream/RTCRtpScriptTransform.h:
+        * Sources.txt:
+        * WebCore.xcodeproj/project.pbxproj:
+        * dom/Document.cpp:
+        (WebCore::Document::appHighlightStorageController):
+        (WebCore::Document::updateHighlightPositions):
+        * dom/Document.h:
+        (WebCore::Document::appHighlightStorageControllerIfExists const):
+
 2020-12-11  Jer Noble  <[email protected]>
 
         [WebM] Mark Opus and Vorbis as supported track types.

Added: trunk/Source/WebCore/Modules/highlight/AppHighlightListData.cpp (0 => 270729)


--- trunk/Source/WebCore/Modules/highlight/AppHighlightListData.cpp	                        (rev 0)
+++ trunk/Source/WebCore/Modules/highlight/AppHighlightListData.cpp	2020-12-12 03:19:41 UTC (rev 270729)
@@ -0,0 +1,99 @@
+/*
+ * Copyright (C) 2020 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 "AppHighlightListData.h"
+
+#include "Document.h"
+#include "DocumentMarkerController.h"
+#include "HTMLBodyElement.h"
+#include "Node.h"
+#include "RenderedDocumentMarker.h"
+#include "SharedBuffer.h"
+#include "SimpleRange.h"
+#include "StaticRange.h"
+#include "TextIterator.h"
+#include <wtf/persistence/PersistentCoders.h>
+
+namespace WebCore {
+
+static unsigned computePathIndex(const Node& node)
+{
+    String nodeName = node.nodeName();
+    unsigned index = 0;
+    for (auto* previousSibling = node.previousSibling(); previousSibling; previousSibling = previousSibling->previousSibling()) {
+        if (previousSibling->nodeName() == nodeName)
+            index++;
+    }
+    return index;
+}
+
+static AppHighlightRangeData::NodePathComponent createNodePathComponent(const Node& node)
+{
+    return {
+        is<Element>(node) ? downcast<Element>(node).getIdAttribute().string() : nullString(),
+        node.nodeName(),
+        is<CharacterData>(node) ? downcast<CharacterData>(node).data() : nullString(),
+        computePathIndex(node)
+    };
+}
+
+static AppHighlightRangeData::NodePath makeNodePath(RefPtr<Node>&& node)
+{
+    AppHighlightRangeData::NodePath components;
+    auto body = node->document().body();
+    for (auto ancestor = node; ancestor && ancestor != body; ancestor = ancestor->parentNode())
+        components.append(createNodePathComponent(*ancestor));
+    components.reverse();
+    return { components };
+}
+
+static AppHighlightRangeData createAppHiglightRangeData(const StaticRange& range)
+{
+    return {
+        plainText(range),
+        makeNodePath(&range.startContainer()),
+        range.startOffset(),
+        makeNodePath(&range.endContainer()),
+        range.endOffset()
+    };
+}
+
+AppHighlightListData AppHighlightListData::create(const SharedBuffer& buffer)
+{
+    auto decoder = buffer.decoder();
+    Optional<AppHighlightListData> data;
+    decoder >> data;
+    return data ? *data : AppHighlightListData {{ }};
+}
+
+Ref<SharedBuffer> AppHighlightListData::toData() const
+{
+    WTF::Persistence::Encoder encoder;
+    encoder << *this;
+    return SharedBuffer::create(encoder.buffer(), encoder.bufferSize());
+}
+
+} // namespace WebCore

Added: trunk/Source/WebCore/Modules/highlight/AppHighlightListData.h (0 => 270729)


--- trunk/Source/WebCore/Modules/highlight/AppHighlightListData.h	                        (rev 0)
+++ trunk/Source/WebCore/Modules/highlight/AppHighlightListData.h	2020-12-12 03:19:41 UTC (rev 270729)
@@ -0,0 +1,237 @@
+/*
+ * Copyright (C) 2020 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.
+ */
+
+#pragma once
+
+#include <wtf/Forward.h>
+#include <wtf/OptionSet.h>
+#include <wtf/Optional.h>
+#include <wtf/text/WTFString.h>
+
+namespace WebCore {
+
+class SharedBuffer;
+
+class AppHighlightRangeData {
+public:
+    struct NodePathComponent {
+        String identifier;
+        String nodeName;
+        String textData;
+        unsigned pathIndex { 0 };
+
+        NodePathComponent(String&& elementIdentifier, String&& name, String&& data, unsigned index)
+            : identifier(WTFMove(elementIdentifier))
+            , nodeName(WTFMove(name))
+            , textData(WTFMove(data))
+            , pathIndex(index)
+        {
+        }
+
+        NodePathComponent(const String& elementIdentifier, const String& name, const String& data, unsigned index)
+            : identifier(elementIdentifier)
+            , nodeName(name)
+            , textData(data)
+            , pathIndex(index)
+        {
+        }
+
+        bool operator==(const NodePathComponent& other) const
+        {
+            return identifier == other.identifier && nodeName == other.nodeName && textData == other.textData && pathIndex == other.pathIndex;
+        }
+
+        bool operator!=(const NodePathComponent& other) const
+        {
+            return !(*this == other);
+        }
+
+        template<class Encoder> void encode(Encoder&) const;
+        template<class Decoder> static Optional<NodePathComponent> decode(Decoder&);
+    };
+
+    using NodePath = Vector<NodePathComponent>;
+
+    AppHighlightRangeData(const AppHighlightRangeData&) = default;
+    AppHighlightRangeData() = default;
+    AppHighlightRangeData(String&& text, NodePath&& startContainer, unsigned startOffset, NodePath&& endContainer, unsigned endOffset)
+        : m_text(WTFMove(text))
+        , m_startContainer(WTFMove(startContainer))
+        , m_startOffset(startOffset)
+        , m_endContainer(WTFMove(endContainer))
+        , m_endOffset(endOffset)
+    {
+    }
+
+    AppHighlightRangeData(const String& text, const NodePath& startContainer, unsigned startOffset, const NodePath& endContainer, unsigned endOffset)
+        : m_text(text)
+        , m_startContainer(startContainer)
+        , m_startOffset(startOffset)
+        , m_endContainer(endContainer)
+        , m_endOffset(endOffset)
+    {
+    }
+
+    const String& text() const { return m_text; }
+    const NodePath& startContainer() const { return m_startContainer; }
+    unsigned startOffset() const { return m_startOffset; }
+    const NodePath& endContainer() const { return m_endContainer; }
+    unsigned endOffset() const { return m_endOffset; }
+
+    template<class Encoder> void encode(Encoder&) const;
+    template<class Decoder> static Optional<AppHighlightRangeData> decode(Decoder&);
+
+private:
+    String m_text;
+    NodePath m_startContainer;
+    unsigned m_startOffset { 0 };
+    NodePath m_endContainer;
+    unsigned m_endOffset { 0 };
+};
+
+class AppHighlightListData {
+    WTF_MAKE_FAST_ALLOCATED;
+public:
+    WEBCORE_EXPORT static AppHighlightListData create(const SharedBuffer&);
+
+    AppHighlightListData(const AppHighlightListData&) = default;
+    AppHighlightListData() = default;
+    AppHighlightListData(Vector<AppHighlightRangeData>&& annotationRanges)
+        : m_annotationRanges(WTFMove(annotationRanges))
+    {
+    }
+
+    const Vector<AppHighlightRangeData>& ranges() const { return m_annotationRanges; }
+    void setRanges(Vector<AppHighlightRangeData>&& ranges) { m_annotationRanges = WTFMove(ranges); }
+    void addRanges(Vector<AppHighlightRangeData>&& ranges) { m_annotationRanges.appendVector(WTFMove(ranges)); }
+
+    WEBCORE_EXPORT Ref<SharedBuffer> toData() const;
+
+    size_t size() const { return m_annotationRanges.size(); }
+    bool isEmpty() const { return m_annotationRanges.isEmpty(); }
+
+    template<class Encoder> void encode(Encoder&) const;
+    template<class Decoder> static Optional<AppHighlightListData> decode(Decoder&);
+
+private:
+
+    Vector<AppHighlightRangeData> m_annotationRanges;
+};
+
+template<class Encoder> void AppHighlightRangeData::NodePathComponent::encode(Encoder& encoder) const
+{
+    encoder << identifier;
+    encoder << nodeName;
+    encoder << textData;
+    encoder << static_cast<uint64_t>(pathIndex);
+}
+
+template<class Decoder> Optional<AppHighlightRangeData::NodePathComponent> AppHighlightRangeData::NodePathComponent::decode(Decoder& decoder)
+{
+    Optional<String> identifier;
+    decoder >> identifier;
+    if (!identifier)
+        return WTF::nullopt;
+
+    Optional<String> nodeName;
+    decoder >> nodeName;
+    if (!nodeName)
+        return WTF::nullopt;
+
+    Optional<String> textData;
+    decoder >> textData;
+    if (!textData)
+        return WTF::nullopt;
+
+    Optional<uint64_t> pathIndex;
+    decoder >> pathIndex;
+    if (!pathIndex)
+        return WTF::nullopt;
+
+    return {{ WTFMove(*identifier), WTFMove(*nodeName), WTFMove(*textData), static_cast<unsigned>(*pathIndex) }};
+}
+
+template<class Encoder> void AppHighlightRangeData::encode(Encoder& encoder) const
+{
+    encoder << m_text;
+    encoder << m_startContainer;
+    encoder << static_cast<uint64_t>(m_startOffset);
+    encoder << m_endContainer;
+    encoder << static_cast<uint64_t>(m_endOffset);
+}
+
+template<class Decoder> Optional<AppHighlightRangeData> AppHighlightRangeData::decode(Decoder& decoder)
+{
+    Optional<String> text;
+    decoder >> text;
+    if (!text)
+        return WTF::nullopt;
+
+    Optional<NodePath> startContainer;
+    decoder >> startContainer;
+    if (!startContainer)
+        return WTF::nullopt;
+
+    Optional<uint64_t> startOffset;
+    decoder >> startOffset;
+    if (!startOffset)
+        return WTF::nullopt;
+
+    Optional<NodePath> endContainer;
+    decoder >> endContainer;
+    if (!endContainer)
+        return WTF::nullopt;
+
+    Optional<uint64_t> endOffset;
+    decoder >> endOffset;
+    if (!endOffset)
+        return WTF::nullopt;
+
+
+    return {{
+        WTFMove(*text),
+        WTFMove(*startContainer),
+        static_cast<unsigned>(*startOffset),
+        WTFMove(*endContainer),
+        static_cast<unsigned>(*endOffset),
+    }};
+}
+
+template<class Encoder> void AppHighlightListData::encode(Encoder& encoder) const
+{
+    encoder << m_annotationRanges;
+}
+
+template<class Decoder> Optional<AppHighlightListData> AppHighlightListData::decode(Decoder& decoder)
+{
+    Optional<Vector<AppHighlightRangeData>> annotationRanges;
+    decoder >> annotationRanges;
+    if (!annotationRanges)
+        return WTF::nullopt;
+
+    return {{ WTFMove(*annotationRanges) }};
+}
+
+} // namespace WebCore

Added: trunk/Source/WebCore/Modules/highlight/AppHighlightStorage.cpp (0 => 270729)


--- trunk/Source/WebCore/Modules/highlight/AppHighlightStorage.cpp	                        (rev 0)
+++ trunk/Source/WebCore/Modules/highlight/AppHighlightStorage.cpp	2020-12-12 03:19:41 UTC (rev 270729)
@@ -0,0 +1,209 @@
+/*
+ * Copyright (C) 2020 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 "AppHighlightStorage.h"
+
+#include "AppHighlightListData.h"
+#include "Document.h"
+#include "DocumentMarkerController.h"
+#include "HTMLBodyElement.h"
+#include "HighlightRegister.h"
+#include "Node.h"
+#include "Position.h"
+#include "RenderedDocumentMarker.h"
+#include "SimpleRange.h"
+#include "StaticRange.h"
+#include "TextIterator.h"
+
+
+namespace WebCore {
+
+static RefPtr<Node> findNodeByPathIndex(const Node& parent, unsigned pathIndex, const String& nodeName)
+{
+    if (!is<ContainerNode>(parent))
+        return nullptr;
+
+    for (auto* child = downcast<ContainerNode>(parent).firstChild(); child; child = child->nextSibling()) {
+        if (nodeName != child->nodeName())
+            continue;
+
+        if (!pathIndex--)
+            return child;
+    }
+    return nullptr;
+}
+
+static std::pair<RefPtr<Node>, size_t> findNodeStartingAtPathComponentIndex(const AppHighlightRangeData::NodePath& path, Node& initialNode, size_t initialIndexToFollow)
+{
+    if (initialIndexToFollow >= path.size())
+        return { nullptr, initialIndexToFollow };
+
+    auto currentNode = makeRefPtr(initialNode);
+    size_t currentPathIndex = initialIndexToFollow;
+    for (; currentPathIndex < path.size(); ++currentPathIndex) {
+        auto& component = path[currentPathIndex];
+        auto nextNode = findNodeByPathIndex(*currentNode, component.pathIndex, component.nodeName);
+        if (!nextNode)
+            return { nullptr, currentPathIndex };
+
+        if (is<CharacterData>(*nextNode) && downcast<CharacterData>(*nextNode).data() != component.textData)
+            return { nullptr, currentPathIndex };
+
+        currentNode = WTFMove(nextNode);
+    }
+    return { currentNode, currentPathIndex };
+}
+
+static RefPtr<Node> findNode(const AppHighlightRangeData::NodePath& path, Document& document)
+{
+    if (path.isEmpty() || !document.body())
+        return nullptr;
+
+    auto [foundNode, nextIndex] = findNodeStartingAtPathComponentIndex(path, *document.body(), 0);
+    if (foundNode)
+        return foundNode;
+
+    while (nextIndex < path.size()) {
+        auto& component = path[nextIndex++];
+        if (component.identifier.isEmpty())
+            continue;
+
+        auto elementWithIdentifier = makeRefPtr(document.getElementById(component.identifier));
+        if (!elementWithIdentifier || elementWithIdentifier->nodeName() != component.nodeName)
+            continue;
+
+        std::tie(foundNode, nextIndex) = findNodeStartingAtPathComponentIndex(path, *elementWithIdentifier, nextIndex);
+        if (foundNode)
+            return foundNode;
+    }
+
+    return nullptr;
+}
+
+static Optional<SimpleRange> findRangeByIdentifyingStartAndEndPositions(const AppHighlightRangeData& range, Document& document)
+{
+    auto startContainer = findNode(range.startContainer(), document);
+    if (!startContainer)
+        return WTF::nullopt;
+
+    auto endContainer = findNode(range.endContainer(), document);
+    if (!endContainer)
+        return WTF::nullopt;
+
+    auto start = makeContainerOffsetPosition(startContainer.get(), range.startOffset());
+    auto end = makeContainerOffsetPosition(endContainer.get(), range.endOffset());
+    if (start.isOrphan() || end.isOrphan())
+        return WTF::nullopt;
+
+    return makeSimpleRange(start, end);
+}
+
+static Optional<SimpleRange> findRangeBySearchingText(const AppHighlightRangeData& range, Document& document)
+{
+    HashSet<String> identifiersInStartPath;
+    for (auto& component : range.startContainer()) {
+        if (!component.identifier.isEmpty())
+            identifiersInStartPath.add(component.identifier);
+    }
+
+    RefPtr<Element> foundElement = document.body();
+    for (auto iterator = range.endContainer().rbegin(), end = range.endContainer().rend(); iterator != end; ++iterator) {
+        auto elementIdentifier = iterator->identifier;
+        if (elementIdentifier.isEmpty() || !identifiersInStartPath.contains(elementIdentifier))
+            continue;
+
+        foundElement = document.getElementById(elementIdentifier);
+        if (foundElement)
+            break;
+    }
+
+    if (!foundElement)
+        return WTF::nullopt;
+
+    auto foundElementRange = makeRangeSelectingNodeContents(*foundElement);
+    auto foundText = plainText(foundElementRange);
+    if (auto index = foundText.find(range.text()); index != notFound && index == foundText.reverseFind(range.text()))
+        return resolveCharacterRange(foundElementRange, { index, range.text().length() });
+
+    return WTF::nullopt;
+}
+
+static Optional<SimpleRange> findRange(const AppHighlightRangeData& range, Document& document)
+{
+    if (auto foundRange = findRangeByIdentifyingStartAndEndPositions(range, document))
+        return foundRange;
+
+    return findRangeBySearchingText(range, document);
+}
+
+AppHighlightStorage::AppHighlightStorage(Document& document)
+    : m_document(makeWeakPtr(document))
+{
+}
+
+AppHighlightListData AppHighlightStorage::createAppHighlightListData()
+{
+    Vector<AppHighlightRangeData> data;
+
+    if (!m_document)
+        return { WTFMove(data) };
+
+    if (auto appHighlightRegister = m_document->appHighlightRegisterIfExists()) {
+        for (auto& highlight : appHighlightRegister->map()) {
+            for (auto& rangeData : highlight.value->rangesData())
+                data.append(createAppHiglightRangeData(rangeData->range));
+        }
+    }
+
+    return { WTFMove(data) };
+}
+
+Vector<AppHighlightRangeData> AppHighlightStorage::restoreAppHighlights(Ref<SharedBuffer>&& buffer)
+{
+    auto appHighlightListData = AppHighlightListData::create(buffer);
+    Vector<AppHighlightRangeData> unrestoredRanges;
+    Vector<AppHighlightRangeData> restoredRanges;
+
+    if (!m_document)
+        return unrestoredRanges;
+
+    auto strongDocument = m_document.get();
+
+    for (auto& appHighlightListData : appHighlightListData.ranges()) {
+        auto range = findRange(appHighlightListData, *strongDocument);
+        if (!range) {
+            unrestoredRanges.append(appHighlightListData);
+            continue;
+        }
+
+        restoredRanges.append(appHighlightListData);
+        strongDocument->appHighlightRegister().addAppHighlight(StaticRange::create(*range));
+    }
+    return unrestoredRanges;
+}
+
+} // namespace WebCore

Added: trunk/Source/WebCore/Modules/highlight/AppHighlightStorage.h (0 => 270729)


--- trunk/Source/WebCore/Modules/highlight/AppHighlightStorage.h	                        (rev 0)
+++ trunk/Source/WebCore/Modules/highlight/AppHighlightStorage.h	2020-12-12 03:19:41 UTC (rev 270729)
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2020 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.
+ */
+
+#pragma once
+
+#include <wtf/Forward.h>
+#include <wtf/OptionSet.h>
+#include <wtf/Optional.h>
+#include <wtf/RefCounted.h>
+#include <wtf/WeakPtr.h>
+#include <wtf/text/WTFString.h>
+
+namespace WebCore {
+
+class AppHighlightListData;
+
+class AppHighlightStorage : RefCounted<AppHighlightStorage> {
+    WTF_MAKE_FAST_ALLOCATED;
+public:
+    AppHighlightStorage(Document&);
+
+    AppHighlightListData createAppHighlightListData();
+    Vector<AppHighlightRangeData> restoreAppHighlights(Ref<SharedBuffer>&&);
+
+private:
+    WeakPtr<Document> m_document;
+};
+
+} // namespace WebCore

Modified: trunk/Source/WebCore/Modules/mediastream/RTCRtpScriptTransform.cpp (270728 => 270729)


--- trunk/Source/WebCore/Modules/mediastream/RTCRtpScriptTransform.cpp	2020-12-12 03:01:14 UTC (rev 270728)
+++ trunk/Source/WebCore/Modules/mediastream/RTCRtpScriptTransform.cpp	2020-12-12 03:19:41 UTC (rev 270729)
@@ -35,6 +35,7 @@
 #include "RTCRtpTransformBackend.h"
 #include "Worker.h"
 #include <wtf/IsoMallocInlines.h>
+#include <wtf/WeakPtr.h>
 
 namespace WebCore {
 

Modified: trunk/Source/WebCore/Modules/mediastream/RTCRtpScriptTransform.h (270728 => 270729)


--- trunk/Source/WebCore/Modules/mediastream/RTCRtpScriptTransform.h	2020-12-12 03:01:14 UTC (rev 270728)
+++ trunk/Source/WebCore/Modules/mediastream/RTCRtpScriptTransform.h	2020-12-12 03:19:41 UTC (rev 270729)
@@ -27,7 +27,9 @@
 
 #if ENABLE(WEB_RTC)
 
+#include "ActiveDOMObject.h"
 #include "EventTarget.h"
+#include "MessagePort.h"
 #include <wtf/Lock.h>
 #include <wtf/ThreadSafeRefCounted.h>
 

Modified: trunk/Source/WebCore/Sources.txt (270728 => 270729)


--- trunk/Source/WebCore/Sources.txt	2020-12-12 03:01:14 UTC (rev 270728)
+++ trunk/Source/WebCore/Sources.txt	2020-12-12 03:19:41 UTC (rev 270729)
@@ -64,6 +64,8 @@
 Modules/geolocation/GeolocationController.cpp
 Modules/geolocation/GeolocationCoordinates.cpp
 Modules/geolocation/NavigatorGeolocation.cpp
+Modules/highlight/AppHighlightListData.cpp
+Modules/highlight/AppHighlightStorage.cpp
 Modules/highlight/HighlightRegister.cpp
 Modules/highlight/Highlight.cpp
 Modules/indexeddb/DOMWindowIndexedDatabase.cpp

Modified: trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj (270728 => 270729)


--- trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj	2020-12-12 03:01:14 UTC (rev 270728)
+++ trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj	2020-12-12 03:19:41 UTC (rev 270729)
@@ -1211,6 +1211,7 @@
 		43EDD67F1B485DBF00640E75 /* CombinedFiltersAlphabet.h in Headers */ = {isa = PBXBuildFile; fileRef = 43EDD67D1B485DBF00640E75 /* CombinedFiltersAlphabet.h */; settings = {ATTRIBUTES = (Private, ); }; };
 		440EDBD1255E116C00300BA7 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 446EE4E8255E0D9200454463 /* Localizable.strings */; };
 		4415292E0E1AE8A000C4A2D0 /* HTMLPlugInImageElement.h in Headers */ = {isa = PBXBuildFile; fileRef = 4415292C0E1AE8A000C4A2D0 /* HTMLPlugInImageElement.h */; settings = {ATTRIBUTES = (Private, ); }; };
+		4425070925829A2700C09368 /* AppHighlightStorage.h in Headers */ = {isa = PBXBuildFile; fileRef = 4425070725829A1400C09368 /* AppHighlightStorage.h */; };
 		4430D00B2575A83E0046D401 /* Highlight.h in Headers */ = {isa = PBXBuildFile; fileRef = 44E88E51236A5C8D009B4847 /* Highlight.h */; settings = {ATTRIBUTES = (Private, ); }; };
 		4430D00D2575A8A50046D401 /* HighlightRegister.h in Headers */ = {isa = PBXBuildFile; fileRef = 44E88E50236A56AC009B4847 /* HighlightRegister.h */; settings = {ATTRIBUTES = (Private, ); }; };
 		445775E520472F73008DCE5D /* LocalDefaultSystemAppearance.h in Headers */ = {isa = PBXBuildFile; fileRef = 445775E420472F73008DCE5D /* LocalDefaultSystemAppearance.h */; settings = {ATTRIBUTES = (Private, ); }; };
@@ -1226,6 +1227,7 @@
 		44A20DB90F84166C00B3E1FE /* WebCoreURLResponseIOS.h in Headers */ = {isa = PBXBuildFile; fileRef = 44A20DB80F84166C00B3E1FE /* WebCoreURLResponseIOS.h */; settings = {ATTRIBUTES = (Private, ); }; };
 		44A28AAC12DFB8AC00AE923B /* MathMLElementFactory.h in Headers */ = {isa = PBXBuildFile; fileRef = 44A28AAB12DFB8AC00AE923B /* MathMLElementFactory.h */; };
 		44A28AAF12DFB8BF00AE923B /* MathMLNames.h in Headers */ = {isa = PBXBuildFile; fileRef = 44A28AAE12DFB8BF00AE923B /* MathMLNames.h */; };
+		44AE045C2579CFFB00C42E65 /* AppHighlightListData.h in Headers */ = {isa = PBXBuildFile; fileRef = 44AE045A2579CFFB00C42E65 /* AppHighlightListData.h */; };
 		44C991860F3D1EBE00586670 /* ScrollbarThemeIOS.h in Headers */ = {isa = PBXBuildFile; fileRef = 44C991850F3D1EBE00586670 /* ScrollbarThemeIOS.h */; };
 		44C9919F0F3D210E00586670 /* ThemeIOS.h in Headers */ = {isa = PBXBuildFile; fileRef = 44C9919D0F3D210E00586670 /* ThemeIOS.h */; };
 		44E349F9246F4E190068479C /* LibWebRTCEnumTraits.h in Headers */ = {isa = PBXBuildFile; fileRef = 44E349F7246F4DC70068479C /* LibWebRTCEnumTraits.h */; settings = {ATTRIBUTES = (Private, ); }; };
@@ -8068,6 +8070,8 @@
 		43EDD67D1B485DBF00640E75 /* CombinedFiltersAlphabet.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CombinedFiltersAlphabet.h; sourceTree = "<group>"; };
 		4415292C0E1AE8A000C4A2D0 /* HTMLPlugInImageElement.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HTMLPlugInImageElement.h; sourceTree = "<group>"; };
 		4415292D0E1AE8A000C4A2D0 /* HTMLPlugInImageElement.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = HTMLPlugInImageElement.cpp; sourceTree = "<group>"; };
+		4425070625829A1400C09368 /* AppHighlightStorage.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = AppHighlightStorage.cpp; sourceTree = "<group>"; };
+		4425070725829A1400C09368 /* AppHighlightStorage.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppHighlightStorage.h; sourceTree = "<group>"; };
 		442956CA218A6D300080DB54 /* DictionaryLookupLegacy.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = DictionaryLookupLegacy.mm; sourceTree = "<group>"; };
 		4437C4A724E2AE5F00095128 /* LazyLoadFrameObserver.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LazyLoadFrameObserver.h; sourceTree = "<group>"; };
 		4437C4A924E2AE5F00095128 /* LazyLoadFrameObserver.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = LazyLoadFrameObserver.cpp; sourceTree = "<group>"; };
@@ -8092,6 +8096,8 @@
 		44A20DB80F84166C00B3E1FE /* WebCoreURLResponseIOS.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WebCoreURLResponseIOS.h; sourceTree = "<group>"; };
 		44A28AAB12DFB8AC00AE923B /* MathMLElementFactory.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = MathMLElementFactory.h; path = DerivedSources/WebCore/MathMLElementFactory.h; sourceTree = BUILT_PRODUCTS_DIR; };
 		44A28AAE12DFB8BF00AE923B /* MathMLNames.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = MathMLNames.h; path = DerivedSources/WebCore/MathMLNames.h; sourceTree = BUILT_PRODUCTS_DIR; };
+		44AE04582579CFFB00C42E65 /* AppHighlightListData.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = AppHighlightListData.cpp; sourceTree = "<group>"; };
+		44AE045A2579CFFB00C42E65 /* AppHighlightListData.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AppHighlightListData.h; sourceTree = "<group>"; };
 		44B38BF42536901A00A4458D /* SelectionRangeData.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SelectionRangeData.h; sourceTree = "<group>"; };
 		44B38BF925369A8800A4458D /* SelectionRangeData.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SelectionRangeData.cpp; sourceTree = "<group>"; };
 		44BD2B36252779780055D95A /* GlobalEventHandlers+Selection.idl */ = {isa = PBXFileReference; lastKnownFileType = text; path = "GlobalEventHandlers+Selection.idl"; sourceTree = "<group>"; };
@@ -19744,6 +19750,10 @@
 		44E88E472368DF0F009B4847 /* highlight */ = {
 			isa = PBXGroup;
 			children = (
+				44AE04582579CFFB00C42E65 /* AppHighlightListData.cpp */,
+				44AE045A2579CFFB00C42E65 /* AppHighlightListData.h */,
+				4425070625829A1400C09368 /* AppHighlightStorage.cpp */,
+				4425070725829A1400C09368 /* AppHighlightStorage.h */,
 				44E88E54236A66A1009B4847 /* Highlight.cpp */,
 				44E88E51236A5C8D009B4847 /* Highlight.h */,
 				44E88E4D2369128B009B4847 /* Highlight.idl */,
@@ -31014,6 +31024,8 @@
 				57152B5A21CB3E88000C37CA /* ApduCommand.h in Headers */,
 				57152B5C21CC1902000C37CA /* ApduResponse.h in Headers */,
 				93309DD7099E64920056E581 /* AppendNodeCommand.h in Headers */,
+				44AE045C2579CFFB00C42E65 /* AppHighlightListData.h in Headers */,
+				4425070925829A2700C09368 /* AppHighlightStorage.h in Headers */,
 				A113E02E2331A134009C33A0 /* ApplePayCancelEvent.h in Headers */,
 				A1DF5A941F7EC4320058A477 /* ApplePayContactField.h in Headers */,
 				A12C59EE2035FC9B0012236B /* ApplePayError.h in Headers */,

Modified: trunk/Source/WebCore/dom/Document.cpp (270728 => 270729)


--- trunk/Source/WebCore/dom/Document.cpp	2020-12-12 03:01:14 UTC (rev 270728)
+++ trunk/Source/WebCore/dom/Document.cpp	2020-12-12 03:19:41 UTC (rev 270729)
@@ -29,6 +29,8 @@
 #include "Document.h"
 
 #include "AXObjectCache.h"
+#include "AppHighlightListData.h"
+#include "AppHighlightStorage.h"
 #include "Attr.h"
 #include "BeforeUnloadEvent.h"
 #include "CDATASection.h"
@@ -2786,6 +2788,13 @@
     return *m_appHighlightRegister;
 }
 
+AppHighlightStorage& Document::appHighlightStorage()
+{
+    if (!m_appHighlightStorage)
+        m_appHighlightStorage = makeUnique<AppHighlightStorage>(*this);
+    return *m_appHighlightStorage;
+}
+
 void Document::collectRangeDataFromRegister(Vector<WeakPtr<HighlightRangeData>>& rangesData, const HighlightRegister& highlightRegister)
 {
     for (auto& highlight : highlightRegister.map()) {

Modified: trunk/Source/WebCore/dom/Document.h (270728 => 270729)


--- trunk/Source/WebCore/dom/Document.h	2020-12-12 03:01:14 UTC (rev 270728)
+++ trunk/Source/WebCore/dom/Document.h	2020-12-12 03:19:41 UTC (rev 270729)
@@ -94,6 +94,7 @@
 namespace WebCore {
 
 class AXObjectCache;
+class AppHighlightStorage;
 class Attr;
 class CDATASection;
 class CSSCustomPropertyValue;
@@ -1580,6 +1581,9 @@
     WEBCORE_EXPORT HighlightRegister& appHighlightRegister();
     void updateHighlightPositions();
 
+    AppHighlightStorage& appHighlightStorage();
+    AppHighlightStorage* appHighlightStorageIfExists() const { return m_appHighlightStorage.get(); };
+
     bool allowsContentJavaScript() const;
 
     LazyLoadImageObserver& lazyLoadImageObserver();
@@ -1939,6 +1943,8 @@
     RefPtr<HighlightRegister> m_highlightRegister;
     RefPtr<HighlightRegister> m_appHighlightRegister;
 
+    std::unique_ptr<AppHighlightStorage> m_appHighlightStorage;
+
     Timer m_visualUpdatesSuppressionTimer;
 
     void clearSharedObjectPool();
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to