Title: [294830] trunk
Revision
294830
Author
[email protected]
Date
2022-05-25 15:36:19 -0700 (Wed, 25 May 2022)

Log Message

Directories showing up as terminal icons in mail compose
https://bugs.webkit.org/show_bug.cgi?id=240882
rdar://45111300

Reviewed by Wenson Hsieh.

* Source/WebCore/rendering/RenderThemeMac.mm:
(WebCore::RenderThemeMac::iconForAttachment):
We should not try to fetch an icon if all three provided strings are
null. This was causing issues by attempting to fetch an icon after
already rendering a valid icon using the filewrapper.

* Source/WebKit/UIProcess/Cocoa/WebPageProxyCocoa.mm:
(WebKit::WebPageProxy::requestThumbnailWithOperation):
(WebKit::WebPageProxy::requestThumbnailWithPath):
Remove some extra spaces and unnecessary WebCore:: prefix.

(WebKit::WebPageProxy::updateIconForDirectory):
New function to update the icon for directories to use the filewrapper
icon instead of the iconForFileType API. This method flips the icon so
it is right-side-up and converts it to a bitmap to pass to the web
process.

* Source/WebKit/UIProcess/WebPageProxy.cpp:
(WebKit::WebPageProxy::requestAttachmentIcon):
* Source/WebKit/UIProcess/WebPageProxy.h:

* Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* Tools/TestWebKitAPI/Tests/WebKitCocoa/WKAttachmentTests.mm:
(testDirectoryAttachmentFileURL):
(-[TestWKWebView waitForAttachmentElementSizeToBecome:]):
(TestWebKitAPI::_icon):
(TestWebKitAPI::TEST):
Add a test to make sure we hit the new NSFileWrapper icon path to fetch
the correct icon for the bundle.

Canonical link: https://commits.webkit.org/250979@main

Modified Paths

Diff

Modified: trunk/Source/WebCore/rendering/RenderThemeMac.mm (294829 => 294830)


--- trunk/Source/WebCore/rendering/RenderThemeMac.mm	2022-05-25 22:36:14 UTC (rev 294829)
+++ trunk/Source/WebCore/rendering/RenderThemeMac.mm	2022-05-25 22:36:19 UTC (rev 294830)
@@ -2599,6 +2599,9 @@
 
 RetainPtr<NSImage> RenderThemeMac::iconForAttachment(const String& fileName, const String& attachmentType, const String& title)
 {
+    if (fileName.isNull() && attachmentType.isNull() && title.isNull())
+        return nil;
+
     if (auto icon = WebCore::iconForAttachment(fileName, attachmentType, title))
         return icon->nsImage();
     return nil;

Modified: trunk/Source/WebKit/UIProcess/Cocoa/WebPageProxyCocoa.mm (294829 => 294830)


--- trunk/Source/WebKit/UIProcess/Cocoa/WebPageProxyCocoa.mm	2022-05-25 22:36:14 UTC (rev 294829)
+++ trunk/Source/WebKit/UIProcess/Cocoa/WebPageProxyCocoa.mm	2022-05-25 22:36:19 UTC (rev 294830)
@@ -118,6 +118,8 @@
 namespace WebKit {
 using namespace WebCore;
 
+constexpr IntSize iconSize = IntSize(400, 400);
+
 #if ENABLE(DATA_DETECTION)
 
 void WebPageProxy::setDataDetectionResult(const DataDetectionResult& dataDetectionResult)
@@ -602,7 +604,7 @@
     [operation setCompletionBlock:^{
         RunLoop::main().dispatch([this, operation = retainPtr(operation)] {
             auto identifier = [operation identifier];
-            auto convertedImage = convertPlatformImageToBitmap([operation thumbnail], WebCore::IntSize(400, 400));
+            auto convertedImage = convertPlatformImageToBitmap([operation thumbnail], iconSize);
             if (!convertedImage)
                 return;
             this->updateAttachmentThumbnail(identifier, convertedImage);
@@ -612,8 +614,7 @@
     [[WKQLThumbnailQueueManager sharedInstance].queue addOperation:operation];
 }
 
-
-void WebPageProxy::requestThumbnailWithFileWrapper(NSFileWrapper* fileWrapper, const String& identifier)
+void WebPageProxy::requestThumbnailWithFileWrapper(NSFileWrapper *fileWrapper, const String& identifier)
 {
     auto operation = adoptNS([[WKQLThumbnailLoadOperation alloc] initWithAttachment:fileWrapper identifier:identifier]);
     requestThumbnailWithOperation(operation.get());
@@ -623,11 +624,34 @@
 {
     auto operation = adoptNS([[WKQLThumbnailLoadOperation alloc] initWithURL:filePath identifier:identifier]);
     requestThumbnailWithOperation(operation.get());
-    
 }
 
 #endif // HAVE(QUICKLOOK_THUMBNAILING)
 
+#if PLATFORM(MAC)
+
+void WebPageProxy::updateIconForDirectory(NSFileWrapper *fileWrapper, const String& identifier)
+{
+    auto image = [fileWrapper icon];
+    if (!image)
+        return;
+
+    auto flippedIcon = [NSImage imageWithSize:iconSize flipped:YES drawingHandler:^BOOL(NSRect destinationRect) {
+        [image drawInRect:destinationRect fromRect:NSMakeRect(0, 0, [image size].width, [image size].height) operation:NSCompositingOperationSourceOver fraction:1.0f];
+        return YES;
+    }];
+
+    auto convertedImage = convertPlatformImageToBitmap(flippedIcon, iconSize);
+    if (!convertedImage)
+        return;
+
+    ShareableBitmap::Handle handle;
+    convertedImage->createHandle(handle);
+    send(Messages::WebPage::UpdateAttachmentIcon(identifier, handle, iconSize));
+}
+
+#endif
+
 void WebPageProxy::scheduleActivityStateUpdate()
 {
     bool hasScheduledObserver = m_activityStateChangeDispatcher->isScheduled();

Modified: trunk/Source/WebKit/UIProcess/WebPageProxy.cpp (294829 => 294830)


--- trunk/Source/WebKit/UIProcess/WebPageProxy.cpp	2022-05-25 22:36:14 UTC (rev 294829)
+++ trunk/Source/WebKit/UIProcess/WebPageProxy.cpp	2022-05-25 22:36:19 UTC (rev 294830)
@@ -10303,6 +10303,14 @@
 {
     FloatSize size = requestedSize;
     ShareableBitmap::Handle handle;
+#if PLATFORM(MAC)
+    auto attachment = attachmentForIdentifier(identifier);
+    if (attachment && attachment->contentType() == "public.directory") {
+        updateIconForDirectory(attachment->fileWrapper(), attachment->identifier());
+        return;
+    }
+#endif
+
 #if PLATFORM(COCOA)
     if (auto icon = iconForAttachment(fileName, contentType, title, size))
         icon->createHandle(handle);

Modified: trunk/Source/WebKit/UIProcess/WebPageProxy.h (294829 => 294830)


--- trunk/Source/WebKit/UIProcess/WebPageProxy.h	2022-05-25 22:36:14 UTC (rev 294829)
+++ trunk/Source/WebKit/UIProcess/WebPageProxy.h	2022-05-25 22:36:19 UTC (rev 294830)
@@ -2109,6 +2109,10 @@
     void setIsInMultitaskingMode(bool);
 #endif
 
+#if PLATFORM(MAC)
+    void updateIconForDirectory(NSFileWrapper *, const String&);
+#endif
+
 private:
     WebPageProxy(PageClient&, WebProcessProxy&, Ref<API::PageConfiguration>&&);
     void platformInitialize();

Modified: trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKAttachmentTests.mm (294829 => 294830)


--- trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKAttachmentTests.mm	2022-05-25 22:36:14 UTC (rev 294829)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKAttachmentTests.mm	2022-05-25 22:36:19 UTC (rev 294830)
@@ -156,6 +156,20 @@
     return [NSData dataWithContentsOfURL:testiWorkAttachmentFileURL()];
 }
 
+#if PLATFORM(MAC)
+
+static NSURL *testDirectoryAttachmentFileURL()
+{
+    NSString *folderName = [NSString stringWithFormat:@"some.directory-%@", [NSUUID UUID].UUIDString];
+    auto temporaryFolder = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:folderName] isDirectory:YES];
+    NSError *error = nil;
+    [[NSFileManager defaultManager] createDirectoryAtURL:temporaryFolder withIntermediateDirectories:NO attributes:nil error:&error];
+
+    return temporaryFolder;
+}
+
+#endif
+
 @interface AttachmentUIDelegate : NSObject<WKUIDelegatePrivate>
 @end
 
@@ -2062,6 +2076,34 @@
     [webView expectElementCount:2 querySelector:@"IMG"];
 }
 
+static bool didLoadIcon;
+static NSImage *_icon(id, SEL)
+{
+    didLoadIcon = true;
+    return nil;
+}
+
+TEST(WKAttachmentTestsMac, DragDirectoryAttachment)
+{
+    didLoadIcon = false;
+    auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
+    [configuration _setAttachmentElementEnabled:YES];
+    auto simulator = adoptNS([[DragAndDropSimulator alloc] initWithWebViewFrame:NSMakeRect(0, 0, 400, 400) configuration:configuration.get()]);
+    TestWKWebView *webView = [simulator webView];
+    [webView synchronouslyLoadHTMLString:attachmentEditingTestMarkup];
+
+    InstanceMethodSwizzler fileWrapperSwizzler {
+        [NSFileWrapper class],
+        @selector(icon),
+        reinterpret_cast<IMP>(_icon)
+    };
+
+    auto fileWrapper = adoptNS([[NSFileWrapper alloc] initWithURL:testDirectoryAttachmentFileURL() options:0 error:nil]);
+    auto attachment = retainPtr([webView synchronouslyInsertAttachmentWithFileWrapper:fileWrapper.get() contentType:nil]);
+    [simulator runFrom:[webView attachmentElementMidPoint] to:CGPointMake(300, 300)];
+    TestWebKitAPI::Util::run(&didLoadIcon);
+}
+
 #endif // PLATFORM(MAC)
 
 #if PLATFORM(IOS_FAMILY)
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to