Title: [240315] trunk/Source/WebCore
Revision
240315
Author
wenson_hs...@apple.com
Date
2019-01-22 18:06:53 -0800 (Tue, 22 Jan 2019)

Log Message

Add some bindings-related bookkeeping to UndoManager and UndoItem
https://bugs.webkit.org/show_bug.cgi?id=193111
<rdar://problem/44807048>

Reviewed by Ryosuke Niwa.

This patch is work in progress towards supporting `UndoManager.addItem()`. Here, we add helper methods to
UndoItem and UndoManager which later patches will exercise, as well as introduce some custom bindings to
properly handle the case where UndoItems are given anonymous _javascript_ functions (see below for more details).

No new tests, because there is no script-observable change in behavior yet. When `addItems()` is hooked up, I
will write a test to verify that the undo and redo _javascript_ functions survive garbage collection.

* Sources.txt:
* WebCore.xcodeproj/project.pbxproj:
* bindings/js/JSUndoItemCustom.cpp:
(WebCore::JSUndoItem::visitAdditionalChildren):

Have each JSUndoItem visit its undo and redo callback functions to ensure that the _javascript_ wrapper objects
for these functions are not garbage collected underneath the item.

(WebCore::JSUndoItemOwner::isReachableFromOpaqueRoots):

Consider the undo item wrapper reachable from opaque roots if it is associated with its UndoManager's Document.
This ensures that if script isn't holding on to a reference to the wrapper (for instance, by calling
`UndoManager.addItem(new UndoItem({ ... }))`), we still protect the corresponding JSUndoItem as long as the
UndoManager's Document is alive. In the case where the undo item is not associated with a document, either (1)
script is keeping a reference to it, in which case it will be trivially reachable, or (2) script won't be able
to observe the destruction of the wrapper anyways (e.g. calling `new UndoItem({ ... })` by itself).

* dom/Document.cpp:
(WebCore::Document::prepareForDestruction):

Invalidate all undo items when the document is about to go away.

* page/UndoItem.cpp:
(WebCore::UndoItem::setUndoManager):
(WebCore::UndoItem::invalidate):
(WebCore::UndoItem::isValid const):

Add a few helpers, to be used in a future patch. We consider an UndoItem valid if it has been added to an
UndoManager, and is thus associated with a document.

(WebCore::UndoItem::document const):
* page/UndoItem.h:
* page/UndoItem.idl:
* page/UndoManager.cpp:
(WebCore::UndoManager::UndoManager):
(WebCore::UndoManager::addItem):

Have an UndoManager keep its UndoItems alive. These UndoItems remain in this set until either the document will
be destroyed, or the corresponding undo action is no longer needed because the platform undo stack has changed
(this latter behavior is yet to be implemented).

(WebCore::UndoManager::removeItem):
(WebCore::UndoManager::removeAllItems):
* page/UndoManager.h:
(WebCore::UndoManager::UndoManager): Deleted.
* page/scrolling/ScrollingTreeScrollingNode.cpp:

Unified build fix.

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (240314 => 240315)


--- trunk/Source/WebCore/ChangeLog	2019-01-23 01:46:01 UTC (rev 240314)
+++ trunk/Source/WebCore/ChangeLog	2019-01-23 02:06:53 UTC (rev 240315)
@@ -1,3 +1,67 @@
+2019-01-22  Wenson Hsieh  <wenson_hs...@apple.com>
+
+        Add some bindings-related bookkeeping to UndoManager and UndoItem
+        https://bugs.webkit.org/show_bug.cgi?id=193111
+        <rdar://problem/44807048>
+
+        Reviewed by Ryosuke Niwa.
+
+        This patch is work in progress towards supporting `UndoManager.addItem()`. Here, we add helper methods to
+        UndoItem and UndoManager which later patches will exercise, as well as introduce some custom bindings to
+        properly handle the case where UndoItems are given anonymous _javascript_ functions (see below for more details).
+
+        No new tests, because there is no script-observable change in behavior yet. When `addItems()` is hooked up, I
+        will write a test to verify that the undo and redo _javascript_ functions survive garbage collection.
+
+        * Sources.txt:
+        * WebCore.xcodeproj/project.pbxproj:
+        * bindings/js/JSUndoItemCustom.cpp:
+        (WebCore::JSUndoItem::visitAdditionalChildren):
+
+        Have each JSUndoItem visit its undo and redo callback functions to ensure that the _javascript_ wrapper objects
+        for these functions are not garbage collected underneath the item.
+
+        (WebCore::JSUndoItemOwner::isReachableFromOpaqueRoots):
+
+        Consider the undo item wrapper reachable from opaque roots if it is associated with its UndoManager's Document.
+        This ensures that if script isn't holding on to a reference to the wrapper (for instance, by calling
+        `UndoManager.addItem(new UndoItem({ ... }))`), we still protect the corresponding JSUndoItem as long as the
+        UndoManager's Document is alive. In the case where the undo item is not associated with a document, either (1)
+        script is keeping a reference to it, in which case it will be trivially reachable, or (2) script won't be able
+        to observe the destruction of the wrapper anyways (e.g. calling `new UndoItem({ ... })` by itself).
+
+        * dom/Document.cpp:
+        (WebCore::Document::prepareForDestruction):
+
+        Invalidate all undo items when the document is about to go away.
+
+        * page/UndoItem.cpp:
+        (WebCore::UndoItem::setUndoManager):
+        (WebCore::UndoItem::invalidate):
+        (WebCore::UndoItem::isValid const):
+
+        Add a few helpers, to be used in a future patch. We consider an UndoItem valid if it has been added to an
+        UndoManager, and is thus associated with a document.
+
+        (WebCore::UndoItem::document const):
+        * page/UndoItem.h:
+        * page/UndoItem.idl:
+        * page/UndoManager.cpp:
+        (WebCore::UndoManager::UndoManager):
+        (WebCore::UndoManager::addItem):
+
+        Have an UndoManager keep its UndoItems alive. These UndoItems remain in this set until either the document will
+        be destroyed, or the corresponding undo action is no longer needed because the platform undo stack has changed
+        (this latter behavior is yet to be implemented).
+
+        (WebCore::UndoManager::removeItem):
+        (WebCore::UndoManager::removeAllItems):
+        * page/UndoManager.h:
+        (WebCore::UndoManager::UndoManager): Deleted.
+        * page/scrolling/ScrollingTreeScrollingNode.cpp:
+
+        Unified build fix.
+
 2019-01-22  Fujii Hironori  <hironori.fu...@sony.com>
 
         [WinCairo][WebKitTestRunner] Null dereference of GraphicsContext::m_data in GraphicsContext::releaseWindowsContext

Modified: trunk/Source/WebCore/Sources.txt (240314 => 240315)


--- trunk/Source/WebCore/Sources.txt	2019-01-23 01:46:01 UTC (rev 240314)
+++ trunk/Source/WebCore/Sources.txt	2019-01-23 02:06:53 UTC (rev 240315)
@@ -526,6 +526,7 @@
 bindings/js/JSTrackCustom.cpp
 bindings/js/JSTreeWalkerCustom.cpp
 bindings/js/JSTypedOMCSSStyleValueCustom.cpp
+bindings/js/JSUndoItemCustom.cpp
 bindings/js/JSVideoTrackCustom.cpp
 bindings/js/JSVideoTrackListCustom.cpp
 bindings/js/JSWebAnimationCustom.cpp
@@ -1536,6 +1537,7 @@
 page/SpatialNavigation.cpp
 page/SuspendableTimer.cpp
 page/TextIndicator.cpp
+page/UndoItem.cpp
 page/UndoManager.cpp
 page/UserContentProvider.cpp
 page/UserContentController.cpp

Modified: trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj (240314 => 240315)


--- trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj	2019-01-23 01:46:01 UTC (rev 240314)
+++ trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj	2019-01-23 02:06:53 UTC (rev 240315)
@@ -7098,8 +7098,6 @@
 		319BDE531E7A86C100BA296C /* JSRTCIceTransportState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSRTCIceTransportState.h; sourceTree = "<group>"; };
 		319BDE531E7A86C100BA296D /* JSRTCPeerConnectionState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSRTCPeerConnectionState.h; sourceTree = "<group>"; };
 		319FBD5D15D2F444009640A6 /* CachedImageClient.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CachedImageClient.h; sourceTree = "<group>"; };
-		31A088C41E737B2C003B6609 /* JSWebGPURenderingContextCustom.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSWebGPURenderingContextCustom.cpp; sourceTree = "<group>"; };
-		31A088C51E737B2C003B6609 /* JSWebGPURenderPassAttachmentDescriptorCustom.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSWebGPURenderPassAttachmentDescriptorCustom.cpp; sourceTree = "<group>"; };
 		31A0891B1E738D59003B6609 /* JSWebGPUBuffer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSWebGPUBuffer.cpp; sourceTree = "<group>"; };
 		31A0891D1E738D59003B6609 /* JSWebGPUBuffer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSWebGPUBuffer.h; sourceTree = "<group>"; };
 		31A0891E1E738D59003B6609 /* JSWebGPUCommandBuffer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSWebGPUCommandBuffer.cpp; sourceTree = "<group>"; };
@@ -15131,6 +15129,8 @@
 		F4D43D64218802E600ECECAC /* SerializedAttachmentData.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SerializedAttachmentData.h; sourceTree = "<group>"; };
 		F4D9817D2195FBF6008230FC /* ChangeListTypeCommand.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ChangeListTypeCommand.h; sourceTree = "<group>"; };
 		F4D9817E2195FBF6008230FC /* ChangeListTypeCommand.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = ChangeListTypeCommand.cpp; sourceTree = "<group>"; };
+		F4E1965A21F2395000285078 /* JSUndoItemCustom.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSUndoItemCustom.cpp; sourceTree = "<group>"; };
+		F4E1965F21F26E4E00285078 /* UndoItem.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = UndoItem.cpp; sourceTree = "<group>"; };
 		F4E57EDA213F3F5F004EA98E /* FontAttributeChanges.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FontAttributeChanges.h; sourceTree = "<group>"; };
 		F4E57EDF213F434A004EA98E /* WebCoreNSFontManagerExtras.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WebCoreNSFontManagerExtras.h; sourceTree = "<group>"; };
 		F4E57EE0213F434A004EA98E /* WebCoreNSFontManagerExtras.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = WebCoreNSFontManagerExtras.mm; sourceTree = "<group>"; };
@@ -17169,10 +17169,10 @@
 				1CECB3B621F50AC700F44542 /* WHLSLMetalCodeGenerator.h */,
 				1CECB3B821F50D1000F44542 /* WHLSLNativeFunctionWriter.cpp */,
 				1CECB3B921F50D1000F44542 /* WHLSLNativeFunctionWriter.h */,
+				1CECB3C621F59C8700F44542 /* WHLSLNativeTypeWriter.cpp */,
+				1CECB3C721F59C8700F44542 /* WHLSLNativeTypeWriter.h */,
 				1CECB3B121F2B98500F44542 /* WHLSLTypeNamer.cpp */,
 				1CECB3B021F2B98500F44542 /* WHLSLTypeNamer.h */,
-				1CECB3C621F59C8700F44542 /* WHLSLNativeTypeWriter.cpp */,
-				1CECB3C721F59C8700F44542 /* WHLSLNativeTypeWriter.h */,
 			);
 			path = Metal;
 			sourceTree = "<group>";
@@ -20434,6 +20434,7 @@
 				62C1217B11AB9E77003C462C /* SuspendableTimer.h */,
 				2D4F96F11A1ECC240098BF88 /* TextIndicator.cpp */,
 				2D4F96F21A1ECC240098BF88 /* TextIndicator.h */,
+				F4E1965F21F26E4E00285078 /* UndoItem.cpp */,
 				2ECDBAD521D8906300F00ECD /* UndoItem.h */,
 				2ECDBAD721D8906300F00ECD /* UndoItem.idl */,
 				2ECDBACF21D8903400F00ECD /* UndoManager.cpp */,
@@ -20802,13 +20803,12 @@
 				07846384145B1B8E00A58DF1 /* JSTrackCustom.h */,
 				516BB7920CE91E6800512F79 /* JSTreeWalkerCustom.cpp */,
 				4BAFD0E22192604D00C0AB64 /* JSTypedOMCSSStyleValueCustom.cpp */,
+				F4E1965A21F2395000285078 /* JSUndoItemCustom.cpp */,
 				BE6DF708171CA2C500DD52B8 /* JSVideoTrackCustom.cpp */,
 				BE6DF70A171CA2C500DD52B8 /* JSVideoTrackListCustom.cpp */,
 				715DA5D3201BB902002EF2B0 /* JSWebAnimationCustom.cpp */,
 				D3F3D3591A69A3B00059FC2B /* JSWebGL2RenderingContextCustom.cpp */,
 				49EED14C1051971A00099FAB /* JSWebGLRenderingContextCustom.cpp */,
-				31A088C41E737B2C003B6609 /* JSWebGPURenderingContextCustom.cpp */,
-				31A088C51E737B2C003B6609 /* JSWebGPURenderPassAttachmentDescriptorCustom.cpp */,
 				E18258AB0EF3CD7000933242 /* JSWorkerGlobalScopeCustom.cpp */,
 				46F91BC91FCDD0FE001599C3 /* JSWorkerNavigatorCustom.cpp */,
 				83A4A9F81CE7FD7E00709B00 /* JSXMLDocumentCustom.cpp */,

Copied: trunk/Source/WebCore/bindings/js/JSUndoItemCustom.cpp (from rev 240314, trunk/Source/WebCore/page/UndoManager.cpp) (0 => 240315)


--- trunk/Source/WebCore/bindings/js/JSUndoItemCustom.cpp	                        (rev 0)
+++ trunk/Source/WebCore/bindings/js/JSUndoItemCustom.cpp	2019-01-23 02:06:53 UTC (rev 240315)
@@ -0,0 +1,46 @@
+/*
+ * 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 "JSUndoItem.h"
+
+namespace WebCore {
+
+void JSUndoItem::visitAdditionalChildren(JSC::SlotVisitor& visitor)
+{
+    wrapped().undoHandler().visitJSFunction(visitor);
+    wrapped().redoHandler().visitJSFunction(visitor);
+}
+
+bool JSUndoItemOwner::isReachableFromOpaqueRoots(JSC::Handle<JSC::Unknown> handle, void*, SlotVisitor& visitor, const char** reason)
+{
+    if (UNLIKELY(reason))
+        *reason = "Document is an opaque root.";
+
+    auto* documentForUndoItem = jsCast<JSUndoItem*>(handle.slot()->asCell())->wrapped().document();
+    return documentForUndoItem && visitor.containsOpaqueRoot(documentForUndoItem);
+}
+
+} // namespace WebCore

Modified: trunk/Source/WebCore/dom/Document.cpp (240314 => 240315)


--- trunk/Source/WebCore/dom/Document.cpp	2019-01-23 01:46:01 UTC (rev 240314)
+++ trunk/Source/WebCore/dom/Document.cpp	2019-01-23 02:06:53 UTC (rev 240315)
@@ -2501,6 +2501,8 @@
     clearTouchEventHandlersAndListeners();
 #endif
 
+    m_undoManager->removeAllItems();
+
 #if HAVE(ACCESSIBILITY)
     if (this != &topDocument()) {
         // Let the ax cache know that this subframe goes out of scope.

Copied: trunk/Source/WebCore/page/UndoItem.cpp (from rev 240314, trunk/Source/WebCore/page/UndoManager.cpp) (0 => 240315)


--- trunk/Source/WebCore/page/UndoItem.cpp	                        (rev 0)
+++ trunk/Source/WebCore/page/UndoItem.cpp	2019-01-23 02:06:53 UTC (rev 240315)
@@ -0,0 +1,62 @@
+/*
+ * 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 "UndoItem.h"
+
+#include "UndoManager.h"
+#include <wtf/IsoMallocInlines.h>
+
+namespace WebCore {
+
+WTF_MAKE_ISO_ALLOCATED_IMPL(UndoItem);
+
+void UndoItem::setUndoManager(UndoManager* undoManager)
+{
+    m_undoManager = makeWeakPtr(undoManager);
+}
+
+void UndoItem::invalidate()
+{
+    if (auto* undoManager = m_undoManager.get()) {
+        undoManager->removeItem(*this);
+        m_undoManager = nullptr;
+    }
+}
+
+bool UndoItem::isValid() const
+{
+    return !!m_undoManager;
+}
+
+Document* UndoItem::document() const
+{
+    if (!isValid())
+        return nullptr;
+
+    return &m_undoManager->document();
+}
+
+} // namespace WebCore

Modified: trunk/Source/WebCore/page/UndoItem.h (240314 => 240315)


--- trunk/Source/WebCore/page/UndoItem.h	2019-01-23 01:46:01 UTC (rev 240314)
+++ trunk/Source/WebCore/page/UndoItem.h	2019-01-23 02:06:53 UTC (rev 240315)
@@ -26,15 +26,18 @@
 #pragma once
 
 #include "VoidCallback.h"
-#include <wtf/Function.h>
-#include <wtf/IsoMallocInlines.h>
+#include <wtf/IsoMalloc.h>
 #include <wtf/RefCounted.h>
+#include <wtf/WeakPtr.h>
 #include <wtf/text/WTFString.h>
 
 namespace WebCore {
 
+class Document;
+class UndoManager;
+
 class UndoItem : public RefCounted<UndoItem> {
-    WTF_MAKE_ISO_ALLOCATED_INLINE(UndoItem);
+    WTF_MAKE_ISO_ALLOCATED(UndoItem);
 public:
     struct Init {
         String label;
@@ -47,6 +50,13 @@
         return adoptRef(*new UndoItem(WTFMove(init)));
     }
 
+    bool isValid() const;
+    void invalidate();
+
+    Document* document() const;
+
+    void setUndoManager(UndoManager*);
+
     const String& label() const { return m_label; }
     VoidCallback& undoHandler() const { return m_undoHandler.get(); }
     VoidCallback& redoHandler() const { return m_redoHandler.get(); }
@@ -62,6 +72,7 @@
     String m_label;
     Ref<VoidCallback> m_undoHandler;
     Ref<VoidCallback> m_redoHandler;
+    WeakPtr<UndoManager> m_undoManager;
 };
 
 } // namespace WebCore

Modified: trunk/Source/WebCore/page/UndoItem.idl (240314 => 240315)


--- trunk/Source/WebCore/page/UndoItem.idl	2019-01-23 01:46:01 UTC (rev 240314)
+++ trunk/Source/WebCore/page/UndoItem.idl	2019-01-23 02:06:53 UTC (rev 240315)
@@ -34,6 +34,8 @@
 [
     EnabledAtRuntime=UndoManagerAPI,
     ImplementationLacksVTable,
+    JSCustomMarkFunction,
+    CustomIsReachable,
     Constructor(UndoItemInit initDict),
 ] interface UndoItem {
     readonly attribute DOMString label;

Modified: trunk/Source/WebCore/page/UndoManager.cpp (240314 => 240315)


--- trunk/Source/WebCore/page/UndoManager.cpp	2019-01-23 01:46:01 UTC (rev 240314)
+++ trunk/Source/WebCore/page/UndoManager.cpp	2019-01-23 02:06:53 UTC (rev 240315)
@@ -33,10 +33,32 @@
 
 WTF_MAKE_ISO_ALLOCATED_IMPL(UndoManager);
 
+UndoManager::UndoManager(Document& document)
+    : m_document(document)
+{
+}
+
+UndoManager::~UndoManager() = default;
+
 void UndoManager::addItem(Ref<UndoItem>&& item)
 {
-    UNUSED_PARAM(item);
     UNUSED_PARAM(m_document);
+
+    item->setUndoManager(this);
+    m_items.add(WTFMove(item));
 }
 
+void UndoManager::removeItem(UndoItem& item)
+{
+    if (auto foundItem = m_items.take(&item))
+        foundItem->setUndoManager(nullptr);
+}
+
+void UndoManager::removeAllItems()
+{
+    for (auto& item : m_items)
+        item->setUndoManager(nullptr);
+    m_items.clear();
+}
+
 } // namespace WebCore

Modified: trunk/Source/WebCore/page/UndoManager.h (240314 => 240315)


--- trunk/Source/WebCore/page/UndoManager.h	2019-01-23 01:46:01 UTC (rev 240314)
+++ trunk/Source/WebCore/page/UndoManager.h	2019-01-23 02:06:53 UTC (rev 240315)
@@ -28,6 +28,8 @@
 #include <wtf/IsoMalloc.h>
 #include <wtf/Ref.h>
 #include <wtf/RefCounted.h>
+#include <wtf/RefPtr.h>
+#include <wtf/WeakPtr.h>
 
 namespace WebCore {
 
@@ -34,7 +36,7 @@
 class Document;
 class UndoItem;
 
-class UndoManager : public RefCounted<UndoManager> {
+class UndoManager : public RefCounted<UndoManager>, public CanMakeWeakPtr<UndoManager> {
     WTF_MAKE_ISO_ALLOCATED(UndoManager);
 public:
     static Ref<UndoManager> create(Document& document)
@@ -42,16 +44,18 @@
         return adoptRef(*new UndoManager(document));
     }
 
+    ~UndoManager();
+
+    void removeItem(UndoItem&);
+    void removeAllItems();
     void addItem(Ref<UndoItem>&&);
     Document& document() { return m_document; }
 
 private:
-    UndoManager(Document& document)
-        : m_document(document)
-    {
-    }
+    UndoManager(Document&);
 
     Document& m_document;
+    HashSet<RefPtr<UndoItem>> m_items;
 };
 
 } // namespace WebCore
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to