Unreviewed, rolling out r240630.
[WebKit-https.git] / Source / WebKit / UIProcess / API / Cocoa / _WKAttachment.mm
index d1b146f..ffffd22 100644 (file)
 #if WK_API_ENABLED
 
 #import "APIAttachment.h"
+#import "WKErrorPrivate.h"
 #import "_WKAttachmentInternal.h"
+#import <WebCore/MIMETypeRegistry.h>
+#import <WebCore/SharedBuffer.h>
+#import <wtf/BlockPtr.h>
 
-using namespace WebKit;
+#if PLATFORM(IOS_FAMILY)
+#import <MobileCoreServices/MobileCoreServices.h>
+#endif
+
+static const NSInteger UnspecifiedAttachmentErrorCode = 1;
+static const NSInteger InvalidAttachmentErrorCode = 2;
 
 @implementation _WKAttachmentDisplayOptions : NSObject
+@end
 
-- (instancetype)init
+@implementation _WKAttachmentInfo {
+    RetainPtr<NSFileWrapper> _fileWrapper;
+    RetainPtr<NSString> _mimeType;
+    RetainPtr<NSString> _utiType;
+    RetainPtr<NSString> _filePath;
+}
+
+- (instancetype)initWithFileWrapper:(NSFileWrapper *)fileWrapper filePath:(NSString *)filePath mimeType:(NSString *)mimeType utiType:(NSString *)utiType
 {
-    if (self = [super init]) {
-        _mode = _WKAttachmentDisplayModeAuto;
-        _expandsImageToMaximumWidth = NO;
-    }
+    if (!(self = [super init]))
+        return nil;
+
+    _fileWrapper = fileWrapper;
+    _filePath = filePath;
+    _mimeType = mimeType;
+    _utiType = utiType;
     return self;
 }
 
+- (NSData *)data
+{
+    if (![_fileWrapper isRegularFile]) {
+        // FIXME: Handle attachments backed by NSFileWrappers that represent directories.
+        return nil;
+    }
+
+    return [_fileWrapper regularFileContents];
+}
+
+- (NSString *)name
+{
+    if ([_fileWrapper filename].length)
+        return [_fileWrapper filename];
+
+    return [_fileWrapper preferredFilename];
+}
+
+- (NSString *)filePath
+{
+    return _filePath.get();
+}
+
+- (NSFileWrapper *)fileWrapper
+{
+    return _fileWrapper.get();
+}
+
+- (NSString *)contentType
+{
+    if ([_mimeType length])
+        return _mimeType.get();
+
+    return _utiType.get();
+}
+
 @end
 
 @implementation _WKAttachment
@@ -53,9 +109,47 @@ using namespace WebKit;
     return *_attachment;
 }
 
-- (BOOL)isEqual:(id)object
+- (_WKAttachmentInfo *)info
+{
+    if (!_attachment->isValid())
+        return nil;
+
+    return [[[_WKAttachmentInfo alloc] initWithFileWrapper:_attachment->fileWrapper() filePath:_attachment->filePath() mimeType:_attachment->mimeType() utiType:_attachment->utiType()] autorelease];
+}
+
+- (void)requestInfo:(void(^)(_WKAttachmentInfo *, NSError *))completionHandler
+{
+    completionHandler(self.info, nil);
+}
+
+- (void)setFileWrapper:(NSFileWrapper *)fileWrapper contentType:(NSString *)contentType completion:(void (^)(NSError *))completionHandler
+{
+    if (!_attachment->isValid()) {
+        completionHandler([NSError errorWithDomain:WKErrorDomain code:InvalidAttachmentErrorCode userInfo:nil]);
+        return;
+    }
+
+    // This file path member is only populated when the attachment is generated upon dropping files. When data is specified via NSFileWrapper
+    // from the SPI client, the corresponding file path of the data is unknown, if it even exists at all.
+    _attachment->setFilePath({ });
+    _attachment->setFileWrapperAndUpdateContentType(fileWrapper, contentType);
+    _attachment->updateAttributes([capturedBlock = makeBlockPtr(completionHandler)] (auto error) {
+        if (!capturedBlock)
+            return;
+
+        if (error == WebKit::CallbackBase::Error::None)
+            capturedBlock(nil);
+        else
+            capturedBlock([NSError errorWithDomain:WKErrorDomain code:UnspecifiedAttachmentErrorCode userInfo:nil]);
+    });
+}
+
+- (void)setData:(NSData *)data newContentType:(NSString *)newContentType newFilename:(NSString *)newFilename completion:(void(^)(NSError *))completionHandler
 {
-    return [object isKindOfClass:[_WKAttachment class]] && [self.uniqueIdentifier isEqual:[(_WKAttachment *)object uniqueIdentifier]];
+    auto fileWrapper = adoptNS([[NSFileWrapper alloc] initRegularFileWithContents:data]);
+    if (newFilename)
+        [fileWrapper setPreferredFilename:newFilename];
+    [self setFileWrapper:fileWrapper.get() contentType:newContentType completion:completionHandler];
 }
 
 - (NSString *)uniqueIdentifier
@@ -63,14 +157,14 @@ using namespace WebKit;
     return _attachment->identifier();
 }
 
-- (NSUInteger)hash
+- (NSString *)description
 {
-    return [self.uniqueIdentifier hash];
+    return [NSString stringWithFormat:@"<%@ %p id='%@'>", [self class], self, self.uniqueIdentifier];
 }
 
-- (NSString *)description
+- (BOOL)isConnected
 {
-    return [NSString stringWithFormat:@"<%@ id='%@'>", [self class], self.uniqueIdentifier];
+    return _attachment->insertionState() == API::Attachment::InsertionState::Inserted;
 }
 
 @end