Support File Promise during drag for macOS.
[WebKit-https.git] / Source / WebCore / platform / mac / DragDataMac.mm
index 50b50c0..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 "PasteboardStrategy.h"
 #import "PlatformStrategies.h"
-#import "Range.h"
+#import "WebCoreNSURLExtras.h"
 
 namespace WebCore {
 
@@ -51,7 +44,6 @@ DragData::DragData(DragDataRef data, const IntPoint& clientPosition, const IntPo
     , m_applicationFlags(flags)
     , m_pasteboardName([[m_platformDragData draggingPasteboard] name])
 {
-    ASSERT([data conformsToProtocol:@protocol(NSDraggingInfo)]);
 }
 
 DragData::DragData(const String& dragStorageName, const IntPoint& clientPosition, const IntPoint& globalPosition,
@@ -81,19 +73,23 @@ bool DragData::containsFiles() const
 {
     Vector<String> types;
     platformStrategies()->pasteboardStrategy()->getTypes(types, m_pasteboardName);
-    return types.contains(String(NSFilenamesPboardType));
+    return types.contains(String(NSFilenamesPboardType)) || types.contains(String(NSFilesPromisePboardType));
 }
 
 unsigned DragData::numberOfFiles() const
 {
     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
 {
     platformStrategies()->pasteboardStrategy()->getPathnamesForType(result, String(NSFilenamesPboardType), m_pasteboardName);
+    if (!result.size())
+        result = fileNames();
 }
 
 bool DragData::containsPlainText() const
@@ -108,9 +104,20 @@ bool DragData::containsPlainText() const
         || platformStrategies()->pasteboardStrategy()->stringForType(String(NSURLPboardType), m_pasteboardName).length();
 }
 
-String DragData::asPlainText(Frame *frame) const
+String DragData::asPlainText() const
 {
-    return frame->editor().readPlainTextFromPasteboard(*Pasteboard::create(m_pasteboardName));
+    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
@@ -125,6 +132,7 @@ bool DragData::containsCompatibleContent() const
     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))
@@ -134,13 +142,20 @@ bool DragData::containsCompatibleContent() const
         || types.contains(String(NSColorPboardType))
         || types.contains(String(kUTTypePNG));
 }
-    
-bool DragData::containsURL(Frame* frame, FilenameConversionPolicy filenamePolicy) const
+
+bool DragData::containsPromise() const
+{
+    Vector<String> files;
+    platformStrategies()->pasteboardStrategy()->getPathnamesForType(files, String(NSFilesPromisePboardType), m_pasteboardName);
+    return files.size() == 1;
+}
+
+bool DragData::containsURL(FilenameConversionPolicy filenamePolicy) const
 {
-    return !asURL(frame, filenamePolicy).isEmpty();
+    return !asURL(filenamePolicy).isEmpty();
 }
 
-String DragData::asURL(Frame* frame, FilenameConversionPolicy, String* title) const
+String DragData::asURL(FilenameConversionPolicy, String* title) const
 {
     // FIXME: Use filenamePolicy.
 
@@ -152,17 +167,13 @@ String DragData::asURL(Frame* frame, FilenameConversionPolicy, String* title) co
     
     Vector<String> types;
     platformStrategies()->pasteboardStrategy()->getTypes(types, m_pasteboardName);
-    
-    // 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.contains(String(NSURLPboardType))) {
         NSURL *URLFromPasteboard = [NSURL URLWithString:platformStrategies()->pasteboardStrategy()->stringForType(String(NSURLPboardType), m_pasteboardName)];
         NSString *scheme = [URLFromPasteboard scheme];
         // Cannot drop other schemes unless <rdar://problem/10562662> and <rdar://problem/11187315> are fixed.
         if ([scheme isEqualToString:@"http"] || [scheme isEqualToString:@"https"])
-            return [frame->editor().client()->canonicalizeURL(URLFromPasteboard) absoluteString];
+            return [URLByCanonicalizingURL(URLFromPasteboard) absoluteString];
     }
     
     if (types.contains(String(NSStringPboardType))) {
@@ -172,7 +183,7 @@ String DragData::asURL(Frame* frame, FilenameConversionPolicy, String* title) co
         // 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 [frame->editor().client()->canonicalizeURL(URLFromPasteboard) absoluteString];
+            return [URLByCanonicalizingURL(URLFromPasteboard) absoluteString];
     }
     
     if (types.contains(String(NSFilenamesPboardType))) {
@@ -182,18 +193,16 @@ String DragData::asURL(Frame* frame, FilenameConversionPolicy, String* title) co
             BOOL isDirectory;
             if ([[NSFileManager defaultManager] fileExistsAtPath:files[0] isDirectory:&isDirectory] && isDirectory)
                 return String();
-            return [frame->editor().client()->canonicalizeURL([NSURL fileURLWithPath:files[0]]) 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
-{
-    return Pasteboard(m_pasteboardName).documentFragment(frame, range, allowPlainText, chosePlainText);
-}
-    
 } // namespace WebCore
 
 #endif // ENABLE(DRAG_SUPPORT)