1 /* 2 * Copyright (C) 2007 Apple Inc. All rights reserved. 3 * Copyright (C) 2008, 2009 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 // Modified from DragDataWin.cpp to not directly call any windows methods as 28 // they may not be available to us in the multiprocess 29 30 #include "config.h" 31 #include "DragData.h" 32 33 #include "ChromiumDataObject.h" 34 #include "ClipboardMimeTypes.h" 35 #include "DocumentFragment.h" 36 #include "FileSystem.h" 37 #include "Frame.h" 38 #include "KURL.h" 39 #include "NotImplemented.h" 40 #include "PlatformBridge.h" 41 #include "PlatformString.h" 42 #include "markup.h" 43 44 namespace WebCore { 45 46 static bool containsHTML(const ChromiumDataObject* dropData) 47 { 48 return dropData->types().contains(mimeTypeTextHTML); 49 } 50 51 bool DragData::containsURL(Frame*, FilenameConversionPolicy filenamePolicy) const 52 { 53 return m_platformDragData->types().contains(mimeTypeURL) 54 || (filenamePolicy == ConvertFilenames && m_platformDragData->containsFilenames()); 55 } 56 57 String DragData::asURL(Frame*, FilenameConversionPolicy filenamePolicy, String* title) const 58 { 59 String url; 60 if (m_platformDragData->types().contains(mimeTypeURL)) { 61 bool ignoredSuccess; 62 url = m_platformDragData->getData(mimeTypeURL, ignoredSuccess); 63 if (title) 64 *title = m_platformDragData->urlTitle(); 65 } else if (filenamePolicy == ConvertFilenames && containsFiles()) { 66 url = PlatformBridge::filePathToURL(PlatformBridge::getAbsolutePath(m_platformDragData->filenames()[0])); 67 } 68 return url; 69 } 70 71 bool DragData::containsFiles() const 72 { 73 return m_platformDragData->containsFilenames(); 74 } 75 76 void DragData::asFilenames(Vector<String>& result) const 77 { 78 const Vector<String>& filenames = m_platformDragData->filenames(); 79 for (size_t i = 0; i < filenames.size(); ++i) 80 result.append(filenames[i]); 81 } 82 83 bool DragData::containsPlainText() const 84 { 85 return m_platformDragData->types().contains(mimeTypeTextPlain); 86 } 87 88 String DragData::asPlainText(Frame*) const 89 { 90 bool ignoredSuccess; 91 return m_platformDragData->getData(mimeTypeTextPlain, ignoredSuccess); 92 } 93 94 bool DragData::containsColor() const 95 { 96 notImplemented(); 97 return false; 98 } 99 100 bool DragData::canSmartReplace() const 101 { 102 // Mimic the situations in which mac allows drag&drop to do a smart replace. 103 // This is allowed whenever the drag data contains a 'range' (ie., 104 // ClipboardWin::writeRange is called). For example, dragging a link 105 // should not result in a space being added. 106 return m_platformDragData->types().contains(mimeTypeTextPlain) 107 && !m_platformDragData->types().contains(mimeTypeURL); 108 } 109 110 bool DragData::containsCompatibleContent() const 111 { 112 return containsPlainText() 113 || containsURL(0) 114 || containsHTML(m_platformDragData) 115 || containsColor() 116 || containsFiles(); 117 } 118 119 PassRefPtr<DocumentFragment> DragData::asFragment(Frame* frame, PassRefPtr<Range>, bool, bool&) const 120 { 121 /* 122 * Order is richest format first. On OSX this is: 123 * * Web Archive 124 * * Filenames 125 * * HTML 126 * * RTF 127 * * TIFF 128 * * PICT 129 */ 130 131 if (containsFiles()) { 132 // FIXME: Implement this. Should be pretty simple to make some HTML 133 // and call createFragmentFromMarkup. 134 //if (RefPtr<DocumentFragment> fragment = createFragmentFromMarkup(doc, 135 // ?, KURL())) 136 // return fragment; 137 } 138 139 if (m_platformDragData->types().contains(mimeTypeTextHTML)) { 140 bool ignoredSuccess; 141 RefPtr<DocumentFragment> fragment = createFragmentFromMarkup(frame->document(), 142 m_platformDragData->getData(mimeTypeTextHTML, ignoredSuccess), m_platformDragData->htmlBaseUrl(), FragmentScriptingNotAllowed); 143 return fragment.release(); 144 } 145 146 return 0; 147 } 148 149 Color DragData::asColor() const 150 { 151 notImplemented(); 152 return Color(); 153 } 154 155 } // namespace WebCore 156