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