Home | History | Annotate | Download | only in platform
      1 /*
      2  * Copyright (C) 2007 Apple Inc.  All rights reserved.
      3  * Copyright (C) 2013 Google Inc.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  *
     14  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  */
     26 
     27 #include "config.h"
     28 #include "core/platform/DragData.h"
     29 
     30 #include "core/dom/Document.h"
     31 #include "core/dom/DocumentFragment.h"
     32 #include "core/dom/Range.h"
     33 #include "core/editing/markup.h"
     34 #include "core/page/Frame.h"
     35 #include "core/platform/FileSystem.h"
     36 #include "core/platform/chromium/ChromiumDataObject.h"
     37 #include "core/platform/chromium/ClipboardMimeTypes.h"
     38 #include "modules/filesystem/DraggedIsolatedFileSystem.h"
     39 #include "weborigin/KURL.h"
     40 #include "wtf/text/WTFString.h"
     41 
     42 #include "public/platform/Platform.h"
     43 #include "public/platform/WebFileUtilities.h"
     44 
     45 namespace WebCore {
     46 
     47 DragData::DragData(ChromiumDataObject* data, const IntPoint& clientPosition, const IntPoint& globalPosition,
     48     DragOperation sourceOperationMask, DragApplicationFlags flags)
     49     : m_clientPosition(clientPosition)
     50     , m_globalPosition(globalPosition)
     51     , m_platformDragData(data)
     52     , m_draggingSourceOperationMask(sourceOperationMask)
     53     , m_applicationFlags(flags)
     54 {
     55 }
     56 
     57 DragData::DragData(const String&, const IntPoint& clientPosition, const IntPoint& globalPosition,
     58     DragOperation sourceOperationMask, DragApplicationFlags flags)
     59     : m_clientPosition(clientPosition)
     60     , m_globalPosition(globalPosition)
     61     , m_platformDragData(0)
     62     , m_draggingSourceOperationMask(sourceOperationMask)
     63     , m_applicationFlags(flags)
     64 {
     65 }
     66 
     67 static bool containsHTML(const ChromiumDataObject* dropData)
     68 {
     69     return dropData->types().contains(mimeTypeTextHTML);
     70 }
     71 
     72 bool DragData::containsURL(Frame*, FilenameConversionPolicy filenamePolicy) const
     73 {
     74     return m_platformDragData->types().contains(mimeTypeTextURIList)
     75         || (filenamePolicy == ConvertFilenames && m_platformDragData->containsFilenames());
     76 }
     77 
     78 String DragData::asURL(Frame*, FilenameConversionPolicy filenamePolicy, String* title) const
     79 {
     80     String url;
     81     if (m_platformDragData->types().contains(mimeTypeTextURIList))
     82         m_platformDragData->urlAndTitle(url, title);
     83     else if (filenamePolicy == ConvertFilenames && containsFiles())
     84         url = KURL(WebKit::Platform::current()->fileUtilities()->filePathToURL(m_platformDragData->filenames()[0]));
     85     return url;
     86 }
     87 
     88 bool DragData::containsFiles() const
     89 {
     90     return m_platformDragData->containsFilenames();
     91 }
     92 
     93 unsigned DragData::numberOfFiles() const
     94 {
     95     return m_platformDragData->filenames().size();
     96 }
     97 
     98 int DragData::modifierKeyState() const
     99 {
    100     return m_platformDragData->modifierKeyState();
    101 }
    102 
    103 void DragData::asFilenames(Vector<String>& result) const
    104 {
    105     const Vector<String>& filenames = m_platformDragData->filenames();
    106     for (size_t i = 0; i < filenames.size(); ++i)
    107         result.append(filenames[i]);
    108 }
    109 
    110 bool DragData::containsPlainText() const
    111 {
    112     return m_platformDragData->types().contains(mimeTypeTextPlain);
    113 }
    114 
    115 String DragData::asPlainText(Frame*) const
    116 {
    117     return m_platformDragData->getData(mimeTypeTextPlain);
    118 }
    119 
    120 bool DragData::canSmartReplace() const
    121 {
    122     // Mimic the situations in which mac allows drag&drop to do a smart replace.
    123     // This is allowed whenever the drag data contains a 'range' (ie.,
    124     // ClipboardWin::writeRange is called). For example, dragging a link
    125     // should not result in a space being added.
    126     return m_platformDragData->types().contains(mimeTypeTextPlain)
    127         && !m_platformDragData->types().contains(mimeTypeTextURIList);
    128 }
    129 
    130 bool DragData::containsCompatibleContent() const
    131 {
    132     return containsPlainText()
    133         || containsURL(0)
    134         || containsHTML(m_platformDragData)
    135         || containsFiles();
    136 }
    137 
    138 PassRefPtr<DocumentFragment> DragData::asFragment(Frame* frame, PassRefPtr<Range>, bool, bool&) const
    139 {
    140     /*
    141      * Order is richest format first. On OSX this is:
    142      * * Web Archive
    143      * * Filenames
    144      * * HTML
    145      * * RTF
    146      * * TIFF
    147      * * PICT
    148      */
    149 
    150     if (containsFiles()) {
    151         // FIXME: Implement this. Should be pretty simple to make some HTML
    152         // and call createFragmentFromMarkup.
    153     }
    154 
    155     if (m_platformDragData->types().contains(mimeTypeTextHTML)) {
    156         String html;
    157         KURL baseURL;
    158         m_platformDragData->htmlAndBaseURL(html, baseURL);
    159         if (RefPtr<DocumentFragment> fragment = createFragmentFromMarkup(frame->document(), html, baseURL, DisallowScriptingAndPluginContent))
    160             return fragment.release();
    161     }
    162 
    163     return 0;
    164 }
    165 
    166 String DragData::droppedFileSystemId() const
    167 {
    168     DraggedIsolatedFileSystem* filesystem = DraggedIsolatedFileSystem::from(m_platformDragData);
    169     if (!filesystem)
    170         return String();
    171     return filesystem->filesystemId();
    172 }
    173 
    174 } // namespace WebCore
    175