Home | History | Annotate | Download | only in page
      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/page/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/frame/Frame.h"
     35 #include "core/platform/chromium/ChromiumDataObject.h"
     36 #include "modules/filesystem/DraggedIsolatedFileSystem.h"
     37 #include "platform/FileMetadata.h"
     38 #include "platform/clipboard/ClipboardMimeTypes.h"
     39 #include "platform/weborigin/KURL.h"
     40 #include "wtf/text/WTFString.h"
     41 
     42 namespace WebCore {
     43 
     44 DragData::DragData(ChromiumDataObject* data, const IntPoint& clientPosition, const IntPoint& globalPosition,
     45     DragOperation sourceOperationMask, DragApplicationFlags flags)
     46     : m_clientPosition(clientPosition)
     47     , m_globalPosition(globalPosition)
     48     , m_platformDragData(data)
     49     , m_draggingSourceOperationMask(sourceOperationMask)
     50     , m_applicationFlags(flags)
     51 {
     52 }
     53 
     54 DragData::DragData(const String&, const IntPoint& clientPosition, const IntPoint& globalPosition,
     55     DragOperation sourceOperationMask, DragApplicationFlags flags)
     56     : m_clientPosition(clientPosition)
     57     , m_globalPosition(globalPosition)
     58     , m_platformDragData(0)
     59     , m_draggingSourceOperationMask(sourceOperationMask)
     60     , m_applicationFlags(flags)
     61 {
     62 }
     63 
     64 static bool containsHTML(const ChromiumDataObject* dropData)
     65 {
     66     return dropData->types().contains(mimeTypeTextHTML);
     67 }
     68 
     69 bool DragData::containsURL(FilenameConversionPolicy filenamePolicy) const
     70 {
     71     return m_platformDragData->types().contains(mimeTypeTextURIList)
     72         || (filenamePolicy == ConvertFilenames && m_platformDragData->containsFilenames());
     73 }
     74 
     75 String DragData::asURL(FilenameConversionPolicy filenamePolicy, String* title) const
     76 {
     77     String url;
     78     if (m_platformDragData->types().contains(mimeTypeTextURIList))
     79         m_platformDragData->urlAndTitle(url, title);
     80     else if (filenamePolicy == ConvertFilenames && containsFiles())
     81         url = filePathToURL(m_platformDragData->filenames()[0]);
     82     return url;
     83 }
     84 
     85 bool DragData::containsFiles() const
     86 {
     87     return m_platformDragData->containsFilenames();
     88 }
     89 
     90 unsigned DragData::numberOfFiles() const
     91 {
     92     return m_platformDragData->filenames().size();
     93 }
     94 
     95 int DragData::modifierKeyState() const
     96 {
     97     return m_platformDragData->modifierKeyState();
     98 }
     99 
    100 void DragData::asFilenames(Vector<String>& result) const
    101 {
    102     const Vector<String>& filenames = m_platformDragData->filenames();
    103     for (size_t i = 0; i < filenames.size(); ++i)
    104         result.append(filenames[i]);
    105 }
    106 
    107 bool DragData::containsPlainText() const
    108 {
    109     return m_platformDragData->types().contains(mimeTypeTextPlain);
    110 }
    111 
    112 String DragData::asPlainText() const
    113 {
    114     return m_platformDragData->getData(mimeTypeTextPlain);
    115 }
    116 
    117 bool DragData::canSmartReplace() const
    118 {
    119     // Mimic the situations in which mac allows drag&drop to do a smart replace.
    120     // This is allowed whenever the drag data contains a 'range' (ie.,
    121     // ClipboardWin::writeRange is called). For example, dragging a link
    122     // should not result in a space being added.
    123     return m_platformDragData->types().contains(mimeTypeTextPlain)
    124         && !m_platformDragData->types().contains(mimeTypeTextURIList);
    125 }
    126 
    127 bool DragData::containsCompatibleContent() const
    128 {
    129     return containsPlainText()
    130         || containsURL()
    131         || containsHTML(m_platformDragData)
    132         || containsFiles();
    133 }
    134 
    135 PassRefPtr<DocumentFragment> DragData::asFragment(Frame* frame, PassRefPtr<Range>, bool, bool&) const
    136 {
    137     /*
    138      * Order is richest format first. On OSX this is:
    139      * * Web Archive
    140      * * Filenames
    141      * * HTML
    142      * * RTF
    143      * * TIFF
    144      * * PICT
    145      */
    146 
    147     if (containsFiles()) {
    148         // FIXME: Implement this. Should be pretty simple to make some HTML
    149         // and call createFragmentFromMarkup.
    150     }
    151 
    152     if (m_platformDragData->types().contains(mimeTypeTextHTML)) {
    153         String html;
    154         KURL baseURL;
    155         m_platformDragData->htmlAndBaseURL(html, baseURL);
    156         ASSERT(frame->document());
    157         if (RefPtr<DocumentFragment> fragment = createFragmentFromMarkup(*frame->document(), html, baseURL, DisallowScriptingAndPluginContent))
    158             return fragment.release();
    159     }
    160 
    161     return 0;
    162 }
    163 
    164 String DragData::droppedFileSystemId() const
    165 {
    166     DraggedIsolatedFileSystem* filesystem = DraggedIsolatedFileSystem::from(m_platformDragData);
    167     if (!filesystem)
    168         return String();
    169     return filesystem->filesystemId();
    170 }
    171 
    172 } // namespace WebCore
    173