Changeset 167784 in webkit


Ignore:
Timestamp:
Apr 24, 2014 5:48:38 PM (10 years ago)
Author:
ap@apple.com
Message:

Dropzone effects don't work in non-file documents
https://bugs.webkit.org/show_bug.cgi?id=131770

Reviewed by Darin Adler.

File documents have two quirks that were making dropzone work in these before:

  1. An ancient hack for Dashboard allows pasteboard access from JS.
  2. On Mac, sandbox doesn't prevent File object creation, as we already have the access.
  • dom/DataTransfer.cpp:

(WebCore::DataTransfer::hasFileOfType):
(WebCore::DataTransfer::hasStringOfType):

  • dom/DataTransfer.h:

Moved these functions from EventHandler to DataTransfer. We can't create a DataTransfer
with Files while dragging, security doesn't permit us to. But we can get the file name.

  • fileapi/File.cpp:

(WebCore::createBlobDataForFile):
(WebCore::createBlobDataForFileWithName):
(WebCore::File::contentTypeFromFilePath):
(WebCore::getContentTypeFromFileName): Deleted.

  • fileapi/File.h:

Exposed a function to get file type from path without creating a File first.
This is much cheaper than creating a File, and works even when sandbox disallows
read access to content, such as when dragging over a target.

  • page/EventHandler.cpp:

(WebCore::hasDropZoneType):
(WebCore::hasFileOfType): Deleted.
(WebCore::hasStringOfType): Deleted.

Location:
trunk/Source/WebCore
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r167780 r167784  
     12014-04-24  Alexey Proskuryakov  <ap@apple.com>
     2
     3        Dropzone effects don't work in non-file documents
     4        https://bugs.webkit.org/show_bug.cgi?id=131770
     5
     6        Reviewed by Darin Adler.
     7
     8        File documents have two quirks that were making dropzone work in these before:
     9        1. An ancient hack for Dashboard allows pasteboard access from JS.
     10        2. On Mac, sandbox doesn't prevent File object creation, as we already have the access.
     11
     12        * dom/DataTransfer.cpp:
     13        (WebCore::DataTransfer::hasFileOfType):
     14        (WebCore::DataTransfer::hasStringOfType):
     15        * dom/DataTransfer.h:
     16        Moved these functions from EventHandler to DataTransfer. We can't create a DataTransfer
     17        with Files while dragging, security doesn't permit us to. But we can get the file name.
     18
     19        * fileapi/File.cpp:
     20        (WebCore::createBlobDataForFile):
     21        (WebCore::createBlobDataForFileWithName):
     22        (WebCore::File::contentTypeFromFilePath):
     23        (WebCore::getContentTypeFromFileName): Deleted.
     24        * fileapi/File.h:
     25        Exposed a function to get file type from path without creating a File first.
     26        This is much cheaper than creating a File, and works even when sandbox disallows
     27        read access to content, such as when dragging over a target.
     28
     29        * page/EventHandler.cpp:
     30        (WebCore::hasDropZoneType):
     31        (WebCore::hasFileOfType): Deleted.
     32        (WebCore::hasStringOfType): Deleted.
     33
    1342014-04-24  Commit Queue  <commit-queue@webkit.org>
    235
  • trunk/Source/WebCore/dom/DataTransfer.cpp

    r167368 r167784  
    185185}
    186186
     187bool DataTransfer::hasFileOfType(const String& type)
     188{
     189    ASSERT_WITH_SECURITY_IMPLICATION(canReadTypes());
     190
     191    for (const String& filename : m_pasteboard->readFilenames()) {
     192        if (equalIgnoringCase(File::contentTypeFromFilePath(filename, File::AllContentTypes), type))
     193            return true;
     194    }
     195
     196    return false;
     197}
     198
     199bool DataTransfer::hasStringOfType(const String& type)
     200{
     201    ASSERT_WITH_SECURITY_IMPLICATION(canReadTypes());
     202
     203    return !type.isNull() && types().contains(type);
     204}
     205
    187206#if !ENABLE(DRAG_SUPPORT)
    188207
  • trunk/Source/WebCore/dom/DataTransfer.h

    r167368 r167784  
    7777        bool canWriteData() const;
    7878
     79        bool hasFileOfType(const String&);
     80        bool hasStringOfType(const String&);
     81
    7982        Pasteboard& pasteboard() { return *m_pasteboard; }
    8083
  • trunk/Source/WebCore/fileapi/File.cpp

    r163483 r167784  
    3636namespace WebCore {
    3737
    38 static String getContentTypeFromFileName(const String& name, File::ContentTypeLookupPolicy policy)
    39 {
    40     String type;
    41     int index = name.reverseFind('.');
    42     if (index != -1) {
    43         if (policy == File::WellKnownContentTypes)
    44             type = MIMETypeRegistry::getWellKnownMIMETypeForExtension(name.substring(index + 1));
    45         else {
    46             ASSERT(policy == File::AllContentTypes);
    47             type = MIMETypeRegistry::getMIMETypeForExtension(name.substring(index + 1));
    48         }
    49     }
    50     return type;
    51 }
    52 
    5338static std::unique_ptr<BlobData> createBlobDataForFileWithType(const String& path, const String& contentType)
    5439{
     
    6247static std::unique_ptr<BlobData> createBlobDataForFile(const String& path, File::ContentTypeLookupPolicy policy)
    6348{
    64     return createBlobDataForFileWithType(path, getContentTypeFromFileName(path, policy));
     49    return createBlobDataForFileWithType(path, File::contentTypeFromFilePath(path, policy));
    6550}
    6651
    6752static std::unique_ptr<BlobData> createBlobDataForFileWithName(const String& path, const String& fileSystemName, File::ContentTypeLookupPolicy policy)
    6853{
    69     return createBlobDataForFileWithType(path, getContentTypeFromFileName(fileSystemName, policy));
     54    return createBlobDataForFileWithType(path, File::contentTypeFromFilePath(fileSystemName, policy));
    7055}
    7156
     
    128113}
    129114
     115String File::contentTypeFromFilePath(const String& name, File::ContentTypeLookupPolicy policy)
     116{
     117    String type;
     118    int index = name.reverseFind('.');
     119    if (index != -1) {
     120        if (policy == File::WellKnownContentTypes)
     121            type = MIMETypeRegistry::getWellKnownMIMETypeForExtension(name.substring(index + 1));
     122        else {
     123            ASSERT(policy == File::AllContentTypes);
     124            type = MIMETypeRegistry::getMIMETypeForExtension(name.substring(index + 1));
     125        }
     126    }
     127    return type;
     128}
     129
    130130} // namespace WebCore
  • trunk/Source/WebCore/fileapi/File.h

    r163483 r167784  
    7676    void captureSnapshot(long long& snapshotSize, double& snapshotModificationTime) const;
    7777
     78    static String contentTypeFromFilePath(const String&, ContentTypeLookupPolicy);
     79
    7880private:
    7981    File(const String& path, ContentTypeLookupPolicy);
  • trunk/Source/WebCore/page/EventHandler.cpp

    r167700 r167784  
    20602060}
    20612061
    2062 static inline bool hasFileOfType(DataTransfer& dataTransfer, const String& type)
    2063 {
    2064     RefPtr<FileList> fileList = dataTransfer.files();
    2065     for (unsigned i = 0; i < fileList->length(); i++) {
    2066         if (equalIgnoringCase(fileList->item(i)->type(), type))
    2067             return true;
    2068     }
    2069     return false;
    2070 }
    2071 
    2072 static inline bool hasStringOfType(DataTransfer& dataTransfer, const String& type)
    2073 {
    2074     return !type.isNull() && dataTransfer.types().contains(type);
    2075 }
    2076 
    20772062static bool hasDropZoneType(DataTransfer& dataTransfer, const String& keyword)
    20782063{
    20792064    if (keyword.startsWith("file:"))
    2080         return hasFileOfType(dataTransfer, keyword.substring(5));
     2065        return dataTransfer.hasFileOfType(keyword.substring(5));
    20812066
    20822067    if (keyword.startsWith("string:"))
    2083         return hasStringOfType(dataTransfer, keyword.substring(7));
     2068        return dataTransfer.hasStringOfType(keyword.substring(7));
    20842069
    20852070    return false;
Note: See TracChangeset for help on using the changeset viewer.