| 1 | /* |
| 2 | * Copyright (C) 2019 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "UndoManager.h" |
| 28 | |
| 29 | #include "CustomUndoStep.h" |
| 30 | #include "Document.h" |
| 31 | #include "Editor.h" |
| 32 | #include "Frame.h" |
| 33 | #include "UndoItem.h" |
| 34 | #include <wtf/IsoMallocInlines.h> |
| 35 | |
| 36 | namespace WebCore { |
| 37 | |
| 38 | WTF_MAKE_ISO_ALLOCATED_IMPL(UndoManager); |
| 39 | |
| 40 | UndoManager::UndoManager(Document& document) |
| 41 | : m_document(document) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | UndoManager::~UndoManager() = default; |
| 46 | |
| 47 | ExceptionOr<void> UndoManager::addItem(Ref<UndoItem>&& item) |
| 48 | { |
| 49 | if (item->undoManager()) |
| 50 | return Exception { InvalidModificationError, "This item has already been added to an UndoManager"_s }; |
| 51 | |
| 52 | auto frame = makeRefPtr(m_document.frame()); |
| 53 | if (!frame) |
| 54 | return Exception { SecurityError, "A browsing context is required to add an UndoItem"_s }; |
| 55 | |
| 56 | item->setUndoManager(this); |
| 57 | frame->editor().registerCustomUndoStep(CustomUndoStep::create(item)); |
| 58 | m_items.add(WTFMove(item)); |
| 59 | return { }; |
| 60 | } |
| 61 | |
| 62 | void UndoManager::removeItem(UndoItem& item) |
| 63 | { |
| 64 | if (auto foundItem = m_items.take(&item)) |
| 65 | foundItem->setUndoManager(nullptr); |
| 66 | } |
| 67 | |
| 68 | void UndoManager::removeAllItems() |
| 69 | { |
| 70 | for (auto& item : m_items) |
| 71 | item->setUndoManager(nullptr); |
| 72 | m_items.clear(); |
| 73 | } |
| 74 | |
| 75 | } // namespace WebCore |
| 76 | |