Filter files from dataTransfer.getData on Mac
authorjpfau@apple.com <jpfau@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 9 Apr 2012 18:19:00 +0000 (18:19 +0000)
committerjpfau@apple.com <jpfau@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 9 Apr 2012 18:19:00 +0000 (18:19 +0000)
https://bugs.webkit.org/show_bug.cgi?id=38876

Reviewed by Enrica Casucci.

Source/WebCore:

The ClipboardMac class now keeps track of whether it was created for copy and paste, dragging and dropping
files or dragging and dropping generic data. This enables the class to block calls to set-/getData when the
clipboard is not operating on generic data, and vice-versa.

Test: fast/events/drop-with-file-paths.html

* editing/mac/EditorMac.mm:
(WebCore::Editor::newGeneralClipboard):
* page/mac/EventHandlerMac.mm:
(WebCore::EventHandler::createDraggingClipboard):
* platform/mac/ClipboardMac.h:
(WebCore::ClipboardMac::create):
(ClipboardMac):
* platform/mac/ClipboardMac.mm:
(WebCore::Clipboard::create):
(WebCore::ClipboardMac::ClipboardMac):
(WebCore::ClipboardMac::getData):
(WebCore::ClipboardMac::setData):
(WebCore::ClipboardMac::files):

LayoutTests:

* fast/events/drop-with-file-paths-expected.txt: Added.
* fast/events/drop-with-file-paths.html: Added.
* fast/events/resources/file-for-drop-with-file-paths.html: Added.

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@113596 268f45cc-cd09-0410-ab3c-d52691b4dbfc

LayoutTests/ChangeLog
LayoutTests/fast/events/drop-with-file-paths-expected.txt [new file with mode: 0644]
LayoutTests/fast/events/drop-with-file-paths.html [new file with mode: 0644]
LayoutTests/fast/events/resources/file-for-drop-with-file-paths.html [new file with mode: 0644]
Source/WebCore/ChangeLog
Source/WebCore/editing/mac/EditorMac.mm
Source/WebCore/page/mac/EventHandlerMac.mm
Source/WebCore/platform/mac/ClipboardMac.h
Source/WebCore/platform/mac/ClipboardMac.mm

index ffdf7e3..ea3439b 100644 (file)
@@ -1,3 +1,14 @@
+2012-04-09  Jeffrey Pfau  <jpfau@apple.com>
+
+        Filter files from dataTransfer.getData on Mac
+        https://bugs.webkit.org/show_bug.cgi?id=38876
+
+        Reviewed by Enrica Casucci.
+
+        * fast/events/drop-with-file-paths-expected.txt: Added.
+        * fast/events/drop-with-file-paths.html: Added.
+        * fast/events/resources/file-for-drop-with-file-paths.html: Added.
+
 2012-04-09  Robert Hogan  <robert@webkit.org>
 
         Fix layout test for r113584 on Qt and Mac
diff --git a/LayoutTests/fast/events/drop-with-file-paths-expected.txt b/LayoutTests/fast/events/drop-with-file-paths-expected.txt
new file mode 100644 (file)
index 0000000..7ef22e9
--- /dev/null
@@ -0,0 +1 @@
+PASS
diff --git a/LayoutTests/fast/events/drop-with-file-paths.html b/LayoutTests/fast/events/drop-with-file-paths.html
new file mode 100644 (file)
index 0000000..a1ed04c
--- /dev/null
@@ -0,0 +1,39 @@
+<html>
+<body>
+<script>
+
+function runTest() {
+    if (!window.layoutTestController)
+        return;
+
+    window.layoutTestController.dumpAsText();
+
+    eventSender.beginDragWithFiles(['resources/file-for-drop-with-file-paths.html']);
+    eventSender.mouseMoveTo(10, 10);
+    eventSender.mouseUp();
+}
+
+document.body.addEventListener('dragenter',
+    function (e) {
+      e.preventDefault();
+    });
+document.body.addEventListener('dragover',
+    function (e) {
+      e.preventDefault();
+    });
+document.body.addEventListener('drop',
+    function (e) {
+      data = e.dataTransfer.getData('text/uri-list');
+      if (data) {
+        document.body.innerHTML += '<p>Dropped file: ' + data + '</p>';
+        document.body.innerHTML += 'Dropped types: ' + e.dataTransfer.types;
+      } else
+        document.body.innerHTML = 'PASS';
+      e.preventDefault();
+    });
+
+window.onload = runTest;
+</script>
+<p>Drop files anywhere on this page. The page will try to intercept the drop--it should succeed but not be able to use dataTransfer.getData</p>
+</body>
+</html>
diff --git a/LayoutTests/fast/events/resources/file-for-drop-with-file-paths.html b/LayoutTests/fast/events/resources/file-for-drop-with-file-paths.html
new file mode 100644 (file)
index 0000000..94e1707
--- /dev/null
@@ -0,0 +1 @@
+FAIL
index 00ce364..63fe4ac 100644 (file)
@@ -1,3 +1,30 @@
+2012-04-09  Jeffrey Pfau  <jpfau@apple.com>
+
+        Filter files from dataTransfer.getData on Mac
+        https://bugs.webkit.org/show_bug.cgi?id=38876
+
+        Reviewed by Enrica Casucci.
+
+        The ClipboardMac class now keeps track of whether it was created for copy and paste, dragging and dropping
+        files or dragging and dropping generic data. This enables the class to block calls to set-/getData when the
+        clipboard is not operating on generic data, and vice-versa.
+
+        Test: fast/events/drop-with-file-paths.html
+
+        * editing/mac/EditorMac.mm:
+        (WebCore::Editor::newGeneralClipboard):
+        * page/mac/EventHandlerMac.mm:
+        (WebCore::EventHandler::createDraggingClipboard):
+        * platform/mac/ClipboardMac.h:
+        (WebCore::ClipboardMac::create):
+        (ClipboardMac):
+        * platform/mac/ClipboardMac.mm:
+        (WebCore::Clipboard::create):
+        (WebCore::ClipboardMac::ClipboardMac):
+        (WebCore::ClipboardMac::getData):
+        (WebCore::ClipboardMac::setData):
+        (WebCore::ClipboardMac::files):
+
 2012-04-09  Sheriff Bot  <webkit.review.bot@gmail.com>
 
         Unreviewed, rolling out r113561.
index 85133dd..8eac82b 100644 (file)
@@ -51,7 +51,7 @@ using namespace HTMLNames;
 PassRefPtr<Clipboard> Editor::newGeneralClipboard(ClipboardAccessPolicy policy, Frame* frame)
 {
     return ClipboardMac::create(Clipboard::CopyAndPaste,
-        policy == ClipboardWritable ? platformStrategies()->pasteboardStrategy()->uniqueName() : String(NSGeneralPboard), policy, frame);
+        policy == ClipboardWritable ? platformStrategies()->pasteboardStrategy()->uniqueName() : String(NSGeneralPboard), policy, ClipboardMac::CopyAndPasteGeneric, frame);
 }
 
 void Editor::showFontPanel()
index 55dd816..6dc5111 100644 (file)
@@ -673,7 +673,7 @@ PassRefPtr<Clipboard> EventHandler::createDraggingClipboard() const
     // also done for security, as it erases data from the last drag
     Pasteboard pasteboard(NSDragPboard);
     pasteboard.clear();
-    return ClipboardMac::create(Clipboard::DragAndDrop, String(NSDragPboard), ClipboardWritable, m_frame);
+    return ClipboardMac::create(Clipboard::DragAndDrop, String(NSDragPboard), ClipboardWritable, ClipboardMac::DragAndDropData, m_frame);
 }
 
 #endif
index 7f6ad85..1fe7398 100644 (file)
@@ -40,9 +40,15 @@ class FileList;
 class ClipboardMac : public Clipboard, public CachedImageClient {
     WTF_MAKE_FAST_ALLOCATED;
 public:
-    static PassRefPtr<ClipboardMac> create(ClipboardType clipboardType, const String& pasteboardName, ClipboardAccessPolicy policy, Frame* frame)
+    enum ClipboardContents {
+        DragAndDropData,
+        DragAndDropFiles,
+        CopyAndPasteGeneric
+    };
+
+    static PassRefPtr<ClipboardMac> create(ClipboardType clipboardType, const String& pasteboardName, ClipboardAccessPolicy policy, ClipboardContents clipboardContents, Frame* frame)
     {
-        return adoptRef(new ClipboardMac(clipboardType, pasteboardName, policy, frame));
+        return adoptRef(new ClipboardMac(clipboardType, pasteboardName, policy, clipboardContents, frame));
     }
 
     virtual ~ClipboardMac();
@@ -74,12 +80,13 @@ public:
     const String& pasteboardName() { return m_pasteboardName; }
 
 private:
-    ClipboardMac(ClipboardType, const String& pasteboardName, ClipboardAccessPolicy, Frame*);
+    ClipboardMac(ClipboardType, const String& pasteboardName, ClipboardAccessPolicy, ClipboardContents, Frame*);
 
     void setDragImage(CachedImage*, Node*, const IntPoint&);
 
     String m_pasteboardName;
     int m_changeCount;
+    ClipboardContents m_clipboardContents;
     Frame* m_frame; // used on the source side to generate dragging images
 };
 
index e006bdb..4d0b877 100644 (file)
@@ -48,12 +48,13 @@ namespace WebCore {
 
 PassRefPtr<Clipboard> Clipboard::create(ClipboardAccessPolicy policy, DragData* dragData, Frame* frame)
 {
-    return ClipboardMac::create(DragAndDrop, dragData->pasteboardName(), policy, frame);
+    return ClipboardMac::create(DragAndDrop, dragData->pasteboardName(), policy, dragData->containsFiles() ? ClipboardMac::DragAndDropFiles : ClipboardMac::DragAndDropData, frame);
 }
 
-ClipboardMac::ClipboardMac(ClipboardType clipboardType, const String& pasteboardName, ClipboardAccessPolicy policy, Frame *frame)
+ClipboardMac::ClipboardMac(ClipboardType clipboardType, const String& pasteboardName, ClipboardAccessPolicy policy, ClipboardContents clipboardContents, Frame *frame)
     : Clipboard(policy, clipboardType)
     , m_pasteboardName(pasteboardName)
+    , m_clipboardContents(clipboardContents)
     , m_frame(frame)
 {
     m_changeCount = platformStrategies()->pasteboardStrategy()->changeCount(m_pasteboardName);
@@ -212,7 +213,7 @@ static Vector<String> absoluteURLsFromPasteboard(const String& pasteboardName, b
 
 String ClipboardMac::getData(const String& type) const
 {
-    if (policy() != ClipboardReadable)
+    if (policy() != ClipboardReadable || m_clipboardContents == DragAndDropFiles)
         return String();
 
     const String& cocoaType = cocoaTypeFromHTMLClipboardType(type);
@@ -241,7 +242,7 @@ String ClipboardMac::getData(const String& type) const
 
 bool ClipboardMac::setData(const String &type, const String &data)
 {
-    if (policy() != ClipboardWritable)
+    if (policy() != ClipboardWritable || m_clipboardContents == DragAndDropFiles)
         return false;
     // note NSPasteboard enforces changeCount itself on writing - can't write if not the owner
 
@@ -311,7 +312,7 @@ HashSet<String> ClipboardMac::types() const
 // clipboard are not reflected in any FileList objects the page has accessed and stored
 PassRefPtr<FileList> ClipboardMac::files() const
 {
-    if (policy() != ClipboardReadable)
+    if (policy() != ClipboardReadable || m_clipboardContents == DragAndDropData)
         return FileList::create();
 
     Vector<String> absoluteURLs = absoluteURLsFromPasteboardFilenames(m_pasteboardName);