Support File Promise during drag for macOS.
[WebKit-https.git] / Source / WebCore / platform / mac / DragDataMac.mm
index 96c24ed..75d50bc 100644 (file)
  *    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 COMPUTER, INC. ``AS IS'' AND ANY
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``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 COMPUTER, INC. OR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
  * 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
 #import "DragData.h"
 
 #if ENABLE(DRAG_SUPPORT)
-#import "Document.h"
-#import "DocumentFragment.h"
-#import "DOMDocumentFragment.h"
-#import "DOMDocumentFragmentInternal.h"
-#import "Editor.h"
-#import "EditorClient.h"
-#import "Frame.h"
 #import "MIMETypeRegistry.h"
 #import "Pasteboard.h"
-#import "Range.h"
+#import "PasteboardStrategy.h"
+#import "PlatformStrategies.h"
+#import "WebCoreNSURLExtras.h"
 
 namespace WebCore {
 
@@ -47,7 +42,7 @@ DragData::DragData(DragDataRef data, const IntPoint& clientPosition, const IntPo
     , m_platformDragData(data)
     , m_draggingSourceOperationMask(sourceOperationMask)
     , m_applicationFlags(flags)
-    , m_pasteboard([m_platformDragData draggingPasteboard])
+    , m_pasteboardName([[m_platformDragData draggingPasteboard] name])
 {
 }
 
@@ -58,151 +53,156 @@ DragData::DragData(const String& dragStorageName, const IntPoint& clientPosition
     , m_platformDragData(0)
     , m_draggingSourceOperationMask(sourceOperationMask)
     , m_applicationFlags(flags)
-    , m_pasteboard([NSPasteboard pasteboardWithName:dragStorageName])
+    , m_pasteboardName(dragStorageName)
 {
 }
     
 bool DragData::canSmartReplace() const
 {
-    //Need to call this so that the various Pasteboard type strings are intialised
-    Pasteboard::generalPasteboard();
-    return [[m_pasteboard.get() types] containsObject:String(WebSmartPastePboardType)];
+    return Pasteboard(m_pasteboardName).canSmartReplace();
 }
 
 bool DragData::containsColor() const
 {
-    return [[m_pasteboard.get() types] containsObject:NSColorPboardType];
+    Vector<String> types;
+    platformStrategies()->pasteboardStrategy()->getTypes(types, m_pasteboardName);
+    return types.contains(String(NSColorPboardType));
 }
 
 bool DragData::containsFiles() const
 {
-    return [[m_pasteboard.get() types] containsObject:NSFilenamesPboardType];
+    Vector<String> types;
+    platformStrategies()->pasteboardStrategy()->getTypes(types, m_pasteboardName);
+    return types.contains(String(NSFilenamesPboardType)) || types.contains(String(NSFilesPromisePboardType));
 }
 
 unsigned DragData::numberOfFiles() const
 {
-    if (![[m_pasteboard.get() types] containsObject:NSFilenamesPboardType])
-        return 0;
-    return [[m_pasteboard.get() propertyListForType:NSFilenamesPboardType] count];
+    Vector<String> files;
+    platformStrategies()->pasteboardStrategy()->getPathnamesForType(files, String(NSFilenamesPboardType), m_pasteboardName);
+    if (!files.size())
+        platformStrategies()->pasteboardStrategy()->getPathnamesForType(files, String(NSFilesPromisePboardType), m_pasteboardName);
+    return files.size();
 }
 
 void DragData::asFilenames(Vector<String>& result) const
 {
-    NSArray *filenames = [m_pasteboard.get() propertyListForType:NSFilenamesPboardType];
-    NSEnumerator *fileEnumerator = [filenames objectEnumerator];
-    
-    while (NSString *filename = [fileEnumerator nextObject])
-        result.append(filename);
+    platformStrategies()->pasteboardStrategy()->getPathnamesForType(result, String(NSFilenamesPboardType), m_pasteboardName);
+    if (!result.size())
+        result = fileNames();
 }
 
 bool DragData::containsPlainText() const
 {
-    NSArray *types = [m_pasteboard.get() types];
-    
-    return [types containsObject:NSStringPboardType] 
-        || [types containsObject:NSRTFDPboardType]
-        || [types containsObject:NSRTFPboardType]
-        || [types containsObject:NSFilenamesPboardType]
-        || [NSURL URLFromPasteboard:m_pasteboard.get()];
+    Vector<String> types;
+    platformStrategies()->pasteboardStrategy()->getTypes(types, m_pasteboardName);
+
+    return types.contains(String(NSStringPboardType))
+        || types.contains(String(NSRTFDPboardType))
+        || types.contains(String(NSRTFPboardType))
+        || types.contains(String(NSFilenamesPboardType))
+        || platformStrategies()->pasteboardStrategy()->stringForType(String(NSURLPboardType), m_pasteboardName).length();
 }
 
-String DragData::asPlainText(Frame *frame) const
+String DragData::asPlainText() const
 {
-    Pasteboard pasteboard([m_pasteboard.get() name]);
-    return pasteboard.plainText(frame);
+    Pasteboard pasteboard(m_pasteboardName);
+    PasteboardPlainText text;
+    pasteboard.read(text);
+    String string = text.text;
+
+    // FIXME: It's not clear this is 100% correct since we know -[NSURL URLWithString:] does not handle
+    // all the same cases we handle well in the URL code for creating an NSURL.
+    if (text.isURL)
+        return userVisibleString([NSURL URLWithString:string]);
+
+    // FIXME: WTF should offer a non-Mac-specific way to convert string to precomposed form so we can do it for all platforms.
+    return [(NSString *)string precomposedStringWithCanonicalMapping];
 }
 
 Color DragData::asColor() const
 {
-    NSColor *color = [NSColor colorFromPasteboard:m_pasteboard.get()];
-    
-    // The color may not be in an RGB colorspace. This commonly occurs when a color is 
-    // dragged from the NSColorPanel grayscale picker.
-    if ([[color colorSpace] colorSpaceModel] != NSRGBColorSpaceModel)
-        color = [color colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
-    
-    return makeRGBA((int)([color redComponent] * 255.0 + 0.5), (int)([color greenComponent] * 255.0 + 0.5), 
-                    (int)([color blueComponent] * 255.0 + 0.5), (int)([color alphaComponent] * 255.0 + 0.5));
+    return platformStrategies()->pasteboardStrategy()->color(m_pasteboardName);
 }
 
-static NSArray *insertablePasteboardTypes()
+bool DragData::containsCompatibleContent() const
 {
-    static NSArray *types = nil;
-    if (!types) {
-        types = [[NSArray alloc] initWithObjects:String(WebArchivePboardType), NSHTMLPboardType, NSFilenamesPboardType, NSTIFFPboardType, NSPDFPboardType,
-#ifdef BUILDING_ON_LEOPARD
-                 NSPICTPboardType,
-#endif
-                 NSURLPboardType, NSRTFDPboardType, NSRTFPboardType, NSStringPboardType, NSColorPboardType, kUTTypePNG, nil];
-        CFRetain(types);
-    }
-    return types;
+    Vector<String> types;
+    platformStrategies()->pasteboardStrategy()->getTypes(types, m_pasteboardName);
+    return types.contains(String(WebArchivePboardType))
+        || types.contains(String(NSHTMLPboardType))
+        || types.contains(String(NSFilenamesPboardType))
+        || types.contains(String(NSFilesPromisePboardType))
+        || types.contains(String(NSTIFFPboardType))
+        || types.contains(String(NSPDFPboardType))
+        || types.contains(String(NSURLPboardType))
+        || types.contains(String(NSRTFDPboardType))
+        || types.contains(String(NSRTFPboardType))
+        || types.contains(String(NSStringPboardType))
+        || types.contains(String(NSColorPboardType))
+        || types.contains(String(kUTTypePNG));
 }
-    
-bool DragData::containsCompatibleContent() const
+
+bool DragData::containsPromise() const
 {
-    NSMutableSet *types = [NSMutableSet setWithArray:[m_pasteboard.get() types]];
-    [types intersectSet:[NSSet setWithArray:insertablePasteboardTypes()]];
-    return [types count] != 0;
+    Vector<String> files;
+    platformStrategies()->pasteboardStrategy()->getPathnamesForType(files, String(NSFilesPromisePboardType), m_pasteboardName);
+    return files.size() == 1;
 }
-    
-bool DragData::containsURL(Frame* frame, FilenameConversionPolicy filenamePolicy) const
+
+bool DragData::containsURL(FilenameConversionPolicy filenamePolicy) const
 {
-    return !asURL(frame, filenamePolicy).isEmpty();
+    return !asURL(filenamePolicy).isEmpty();
 }
 
-String DragData::asURL(Frame* frame, FilenameConversionPolicy filenamePolicy, String* title) const
+String DragData::asURL(FilenameConversionPolicy, String* title) const
 {
     // FIXME: Use filenamePolicy.
-    (void)filenamePolicy;
 
     if (title) {
-        if (NSString *URLTitleString = [m_pasteboard.get() stringForType:String(WebURLNamePboardType)])
+        String URLTitleString = platformStrategies()->pasteboardStrategy()->stringForType(String(WebURLNamePboardType), m_pasteboardName);
+        if (!URLTitleString.isEmpty())
             *title = URLTitleString;
     }
     
-    NSArray *types = [m_pasteboard.get() types];
-    
-    // FIXME: using the editorClient to call into WebKit, for now, since 
-    // calling webkit_canonicalize from WebCore involves migrating a sizable amount of 
-    // helper code that should either be done in a separate patch or figured out in another way.
-    
-    if ([types containsObject:NSURLPboardType]) {
-        NSURL *URLFromPasteboard = [NSURL URLFromPasteboard:m_pasteboard.get()];
+    Vector<String> types;
+    platformStrategies()->pasteboardStrategy()->getTypes(types, m_pasteboardName);
+
+    if (types.contains(String(NSURLPboardType))) {
+        NSURL *URLFromPasteboard = [NSURL URLWithString:platformStrategies()->pasteboardStrategy()->stringForType(String(NSURLPboardType), m_pasteboardName)];
         NSString *scheme = [URLFromPasteboard scheme];
-        if ([scheme isEqualToString:@"http"] || [scheme isEqualToString:@"https"]) {
-            return [frame->editor()->client()->canonicalizeURL(URLFromPasteboard) absoluteString];
-        }
+        // Cannot drop other schemes unless <rdar://problem/10562662> and <rdar://problem/11187315> are fixed.
+        if ([scheme isEqualToString:@"http"] || [scheme isEqualToString:@"https"])
+            return [URLByCanonicalizingURL(URLFromPasteboard) absoluteString];
     }
     
-    if ([types containsObject:NSStringPboardType]) {
-        NSString *URLString = [m_pasteboard.get() stringForType:NSStringPboardType];
-        NSURL *URL = frame->editor()->client()->canonicalizeURLString(URLString);
-        if (URL)
-            return [URL absoluteString];
+    if (types.contains(String(NSStringPboardType))) {
+        NSURL *URLFromPasteboard = [NSURL URLWithString:platformStrategies()->pasteboardStrategy()->stringForType(String(NSStringPboardType), m_pasteboardName)];
+        NSString *scheme = [URLFromPasteboard scheme];
+        // Pasteboard content is not trusted, because JavaScript code can modify it. We can sanitize it for URLs and other typed content, but not for strings.
+        // The result of this function is used to initiate navigation, so we shouldn't allow arbitrary file URLs.
+        // FIXME: Should we allow only http family schemes, or anything non-local?
+        if ([scheme isEqualToString:@"http"] || [scheme isEqualToString:@"https"])
+            return [URLByCanonicalizingURL(URLFromPasteboard) absoluteString];
     }
     
-    if ([types containsObject:NSFilenamesPboardType]) {
-        NSArray *files = [m_pasteboard.get() propertyListForType:NSFilenamesPboardType];
-        if ([files count] == 1) {
-            NSString *file = [files objectAtIndex:0];
+    if (types.contains(String(NSFilenamesPboardType))) {
+        Vector<String> files;
+        platformStrategies()->pasteboardStrategy()->getPathnamesForType(files, String(NSFilenamesPboardType), m_pasteboardName);
+        if (files.size() == 1) {
             BOOL isDirectory;
-            if ([[NSFileManager defaultManager] fileExistsAtPath:file isDirectory:&isDirectory] && isDirectory)
+            if ([[NSFileManager defaultManager] fileExistsAtPath:files[0] isDirectory:&isDirectory] && isDirectory)
                 return String();
-            return [frame->editor()->client()->canonicalizeURL([NSURL fileURLWithPath:file]) absoluteString];
+            return [URLByCanonicalizingURL([NSURL fileURLWithPath:files[0]]) absoluteString];
         }
     }
-    
+
+    if (types.contains(String(NSFilesPromisePboardType)) && fileNames().size() == 1)
+        return [URLByCanonicalizingURL([NSURL fileURLWithPath:fileNames()[0]]) absoluteString];
+
     return String();        
 }
 
-PassRefPtr<DocumentFragment> DragData::asFragment(Frame* frame, PassRefPtr<Range> range, bool allowPlainText, bool& chosePlainText) const
-{
-    Pasteboard pasteboard([m_pasteboard.get() name]);
-    
-    return pasteboard.documentFragment(frame, range, allowPlainText, chosePlainText);
-}
-    
 } // namespace WebCore
 
 #endif // ENABLE(DRAG_SUPPORT)