Unreviewed build fix after r242378
[WebKit-https.git] / Source / WebKit / UIProcess / ios / EditableImageController.mm
1 /*
2  * Copyright (C) 2018 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 #import "config.h"
27 #import "EditableImageController.h"
28
29 #if HAVE(PENCILKIT)
30
31 #import "APIAttachment.h"
32 #import "EditableImageControllerMessages.h"
33 #import "PageClientImplIOS.h"
34 #import "PencilKitSPI.h"
35 #import "WKDrawingView.h"
36 #import "WebPageProxy.h"
37 #import "WebProcessProxy.h"
38 #import <WebCore/GraphicsLayer.h>
39 #import <wtf/RetainPtr.h>
40
41 #define MESSAGE_CHECK_VIEWID(embeddedViewID) MESSAGE_CHECK_BASE(m_editableImages.isValidKey(embeddedViewID), m_webPageProxy->process().connection())
42
43 namespace WebKit {
44
45 EditableImageController::EditableImageController(WebPageProxy& webPageProxy)
46     : m_webPageProxy(makeWeakPtr(webPageProxy))
47 {
48     if (auto* webPageProxy = m_webPageProxy.get())
49         webPageProxy->process().addMessageReceiver(Messages::EditableImageController::messageReceiverName(), webPageProxy->pageID(), *this);
50 }
51
52 EditableImageController::~EditableImageController()
53 {
54     if (auto* webPageProxy = m_webPageProxy.get())
55         webPageProxy->process().removeMessageReceiver(Messages::EditableImageController::messageReceiverName(), webPageProxy->pageID());
56 }
57
58 EditableImage& EditableImageController::ensureEditableImage(WebCore::GraphicsLayer::EmbeddedViewID embeddedViewID)
59 {
60     auto result = m_editableImages.ensure(embeddedViewID, [&] {
61         std::unique_ptr<EditableImage> image = std::make_unique<EditableImage>();
62         image->drawingView = m_webPageProxy->pageClient().createDrawingView(embeddedViewID);
63         return image;
64     });
65     return *result.iterator->value;
66 }
67
68 EditableImage* EditableImageController::editableImage(WebCore::GraphicsLayer::EmbeddedViewID embeddedViewID)
69 {
70     auto drawingViewIter = m_editableImages.find(embeddedViewID);
71     if (drawingViewIter == m_editableImages.end())
72         return nil;
73     return drawingViewIter->value.get();
74 }
75
76 void EditableImageController::didCreateEditableImage(WebCore::GraphicsLayer::EmbeddedViewID embeddedViewID)
77 {
78     MESSAGE_CHECK_VIEWID(embeddedViewID);
79     ensureEditableImage(embeddedViewID);
80 }
81
82 void EditableImageController::didDestroyEditableImage(WebCore::GraphicsLayer::EmbeddedViewID embeddedViewID)
83 {
84     MESSAGE_CHECK_VIEWID(embeddedViewID);
85     m_editableImages.remove(embeddedViewID);
86 }
87
88 void EditableImageController::associateWithAttachment(WebCore::GraphicsLayer::EmbeddedViewID embeddedViewID, const String& attachmentID)
89 {
90     if (!m_webPageProxy)
91         return;
92     auto& page = *m_webPageProxy;
93
94     MESSAGE_CHECK_VIEWID(embeddedViewID);
95     page.registerAttachmentIdentifier(attachmentID);
96     auto& attachment = *page.attachmentForIdentifier(attachmentID);
97
98     auto& editableImage = ensureEditableImage(embeddedViewID);
99     WeakObjCPtr<WKDrawingView> drawingView = editableImage.drawingView.get();
100     editableImage.attachmentID = attachmentID;
101
102     loadStrokesFromAttachment(editableImage, attachment);
103
104     attachment.setFileWrapperGenerator([drawingView]() -> RetainPtr<NSFileWrapper> {
105         if (!drawingView)
106             return nil;
107         NSData *data = [drawingView PNGRepresentation];
108         if (!data)
109             return nil;
110         RetainPtr<NSFileWrapper> fileWrapper = adoptNS([[NSFileWrapper alloc] initRegularFileWithContents:data]);
111         [fileWrapper setPreferredFilename:@"drawing.png"];
112         return fileWrapper;
113     });
114     attachment.setContentType("public.png");
115 }
116
117 void EditableImageController::loadStrokesFromAttachment(EditableImage& editableImage, const API::Attachment& attachment)
118 {
119     ASSERT(attachment.identifier() == editableImage.attachmentID);
120     NSFileWrapper *fileWrapper = attachment.fileWrapper();
121     if (!fileWrapper.isRegularFile)
122         return;
123     [editableImage.drawingView loadDrawingFromPNGRepresentation:fileWrapper.regularFileContents];
124 }
125
126 void EditableImageController::invalidateAttachmentForEditableImage(WebCore::GraphicsLayer::EmbeddedViewID embeddedViewID)
127 {
128     if (!m_webPageProxy)
129         return;
130     auto& page = *m_webPageProxy;
131
132     auto editableImage = this->editableImage(embeddedViewID);
133     if (!editableImage)
134         return;
135
136     auto attachment = page.attachmentForIdentifier(editableImage->attachmentID);
137     if (!attachment)
138         return;
139
140     attachment->invalidateGeneratedFileWrapper();
141 }
142
143 WebPageProxy::ShouldUpdateAttachmentAttributes EditableImageController::willUpdateAttachmentAttributes(const API::Attachment& attachment)
144 {
145     for (auto& editableImage : m_editableImages.values()) {
146         if (editableImage->attachmentID != attachment.identifier())
147             continue;
148
149         loadStrokesFromAttachment(*editableImage, attachment);
150         return WebPageProxy::ShouldUpdateAttachmentAttributes::No;
151     }
152
153     return WebPageProxy::ShouldUpdateAttachmentAttributes::Yes;
154 }
155
156 } // namespace WebKit
157
158 #undef MESSAGE_CHECK_VIEWID
159
160 #endif // HAVE(PENCILKIT)