2 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "ClipboardWin.h"
29 #include "CachedImage.h"
30 #include "ClipboardUtilitiesWin.h"
35 #include "EventHandler.h"
38 #include "FrameLoader.h"
39 #include "FrameView.h"
40 #include "HTMLNames.h"
41 #include "HTMLParserIdioms.h"
43 #include "MIMETypeRegistry.h"
44 #include "NotImplemented.h"
46 #include "Pasteboard.h"
47 #include "PlatformMouseEvent.h"
48 #include "PlatformString.h"
50 #include "RenderImage.h"
51 #include "ResourceResponse.h"
52 #include "SharedBuffer.h"
53 #include "WCDataObject.h"
57 #include <wtf/RefPtr.h>
58 #include <wtf/text/CString.h>
59 #include <wtf/text/StringConcatenate.h>
60 #include <wtf/text/StringHash.h>
66 using namespace HTMLNames;
68 // We provide the IE clipboard types (URL and Text), and the clipboard types specified in the WHATWG Web Applications 1.0 draft
69 // see http://www.whatwg.org/specs/web-apps/current-work/ Section 6.3.5.3
71 enum ClipboardDataType { ClipboardDataTypeNone, ClipboardDataTypeURL, ClipboardDataTypeText, ClipboardDataTypeTextHTML };
73 static ClipboardDataType clipboardTypeFromMIMEType(const String& type)
75 String qType = type.stripWhiteSpace().lower();
77 // two special cases for IE compatibility
78 if (qType == "text" || qType == "text/plain" || qType.startsWith("text/plain;"))
79 return ClipboardDataTypeText;
80 if (qType == "url" || qType == "text/uri-list")
81 return ClipboardDataTypeURL;
82 if (qType == "text/html")
83 return ClipboardDataTypeTextHTML;
85 return ClipboardDataTypeNone;
88 static inline FORMATETC* fileDescriptorFormat()
90 static UINT cf = RegisterClipboardFormat(CFSTR_FILEDESCRIPTOR);
91 static FORMATETC fileDescriptorFormat = {cf, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
92 return &fileDescriptorFormat;
95 static inline FORMATETC* fileContentFormatZero()
97 static UINT cf = RegisterClipboardFormat(CFSTR_FILECONTENTS);
98 static FORMATETC fileContentFormat = {cf, 0, DVASPECT_CONTENT, 0, TYMED_HGLOBAL};
99 return &fileContentFormat;
103 static inline void pathRemoveBadFSCharacters(PWSTR psz, size_t length)
107 while (readFrom < length) {
108 UINT type = PathGetCharType(psz[readFrom]);
109 if (!psz[readFrom] || type & (GCT_LFNCHAR | GCT_SHORTCHAR))
110 psz[writeTo++] = psz[readFrom];
118 static String filesystemPathFromUrlOrTitle(const String& url, const String& title, const UChar* extension, bool isLink)
124 static const size_t fsPathMaxLengthExcludingNullTerminator = MAX_PATH - 1;
125 bool usedURL = false;
126 WCHAR fsPathBuffer[MAX_PATH];
128 int extensionLen = extension ? lstrlen(extension) : 0;
129 int fsPathMaxLengthExcludingExtension = fsPathMaxLengthExcludingNullTerminator - extensionLen;
131 if (!title.isEmpty()) {
132 size_t len = min<size_t>(title.length(), fsPathMaxLengthExcludingExtension);
133 CopyMemory(fsPathBuffer, title.characters(), len * sizeof(UChar));
134 fsPathBuffer[len] = 0;
135 pathRemoveBadFSCharacters(fsPathBuffer, len);
138 if (!lstrlen(fsPathBuffer)) {
139 KURL kurl(ParsedURLString, url);
141 // The filename for any content based drag or file url should be the last element of
142 // the path. If we can't find it, or we're coming up with the name for a link
143 // we just use the entire url.
144 DWORD len = fsPathMaxLengthExcludingExtension;
145 String lastComponent = kurl.lastPathComponent();
146 if (kurl.isLocalFile() || (!isLink && !lastComponent.isEmpty())) {
147 len = min<DWORD>(fsPathMaxLengthExcludingExtension, lastComponent.length());
148 CopyMemory(fsPathBuffer, lastComponent.characters(), len * sizeof(UChar));
150 len = min<DWORD>(fsPathMaxLengthExcludingExtension, url.length());
151 CopyMemory(fsPathBuffer, url.characters(), len * sizeof(UChar));
153 fsPathBuffer[len] = 0;
154 pathRemoveBadFSCharacters(fsPathBuffer, len);
158 return String(static_cast<UChar*>(fsPathBuffer));
160 if (!isLink && usedURL) {
161 PathRenameExtension(fsPathBuffer, extension);
162 return String(static_cast<UChar*>(fsPathBuffer));
165 String result(static_cast<UChar*>(fsPathBuffer));
166 result += String(extension);
171 static HGLOBAL createGlobalImageFileContent(SharedBuffer* data)
173 HGLOBAL memObj = GlobalAlloc(GPTR, data->size());
177 char* fileContents = (PSTR)GlobalLock(memObj);
179 CopyMemory(fileContents, data->data(), data->size());
181 GlobalUnlock(memObj);
186 static HGLOBAL createGlobalHDropContent(const KURL& url, String& fileName, SharedBuffer* data)
188 if (fileName.isEmpty() || !data)
191 WCHAR filePath[MAX_PATH];
193 if (url.isLocalFile()) {
194 String localPath = url.path();
195 // windows does not enjoy a leading slash on paths
196 if (localPath[0] == '/')
197 localPath = localPath.substring(1);
198 LPCWSTR localPathStr = localPath.charactersWithNullTermination();
199 if (wcslen(localPathStr) + 1 < MAX_PATH)
200 wcscpy_s(filePath, MAX_PATH, localPathStr);
208 WCHAR tempPath[MAX_PATH];
209 WCHAR extension[MAX_PATH];
210 if (!::GetTempPath(WTF_ARRAY_LENGTH(tempPath), tempPath))
212 if (!::PathAppend(tempPath, fileName.charactersWithNullTermination()))
214 LPCWSTR foundExtension = ::PathFindExtension(tempPath);
215 if (foundExtension) {
216 if (wcscpy_s(extension, MAX_PATH, foundExtension))
220 ::PathRemoveExtension(tempPath);
221 for (int i = 1; i < 10000; i++) {
222 if (swprintf_s(filePath, MAX_PATH, TEXT("%s-%d%s"), tempPath, i, extension) == -1)
224 if (!::PathFileExists(filePath))
227 HANDLE tempFileHandle = CreateFile(filePath, GENERIC_READ | GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
228 if (tempFileHandle == INVALID_HANDLE_VALUE)
231 // Write the data to this temp file.
233 BOOL tempWriteSucceeded = WriteFile(tempFileHandle, data->data(), data->size(), &written, 0);
234 CloseHandle(tempFileHandle);
235 if (!tempWriteSucceeded)
240 SIZE_T dropFilesSize = sizeof(DROPFILES) + (sizeof(WCHAR) * (wcslen(filePath) + 2));
241 HGLOBAL memObj = GlobalAlloc(GHND | GMEM_SHARE, dropFilesSize);
245 DROPFILES* dropFiles = (DROPFILES*) GlobalLock(memObj);
246 dropFiles->pFiles = sizeof(DROPFILES);
247 dropFiles->fWide = TRUE;
248 wcscpy((LPWSTR)(dropFiles + 1), filePath);
249 GlobalUnlock(memObj);
254 static HGLOBAL createGlobalImageFileDescriptor(const String& url, const String& title, CachedImage* image)
256 ASSERT_ARG(image, image);
257 ASSERT(image->image()->data());
262 memObj = GlobalAlloc(GPTR, sizeof(FILEGROUPDESCRIPTOR));
266 FILEGROUPDESCRIPTOR* fgd = (FILEGROUPDESCRIPTOR*)GlobalLock(memObj);
267 memset(fgd, 0, sizeof(FILEGROUPDESCRIPTOR));
269 fgd->fgd[0].dwFlags = FD_FILESIZE;
270 fgd->fgd[0].nFileSizeLow = image->image()->data()->size();
272 const String& preferredTitle = title.isEmpty() ? image->response().suggestedFilename() : title;
273 String extension = image->image()->filenameExtension();
274 if (extension.isEmpty()) {
275 // Do not continue processing in the rare and unusual case where a decoded image is not able
276 // to provide a filename extension. Something tricky (like a bait-n-switch) is going on
279 extension.insert(".", 0);
280 fsPath = filesystemPathFromUrlOrTitle(url, preferredTitle, extension.charactersWithNullTermination(), false);
282 if (fsPath.length() <= 0) {
283 GlobalUnlock(memObj);
288 int maxSize = min(fsPath.length(), WTF_ARRAY_LENGTH(fgd->fgd[0].cFileName));
289 CopyMemory(fgd->fgd[0].cFileName, (LPCWSTR)fsPath.characters(), maxSize * sizeof(UChar));
290 GlobalUnlock(memObj);
296 // writeFileToDataObject takes ownership of fileDescriptor and fileContent
297 static HRESULT writeFileToDataObject(IDataObject* dataObject, HGLOBAL fileDescriptor, HGLOBAL fileContent, HGLOBAL hDropContent)
301 STGMEDIUM medium = {0};
302 medium.tymed = TYMED_HGLOBAL;
304 if (!fileDescriptor || !fileContent)
308 fe = fileDescriptorFormat();
310 medium.hGlobal = fileDescriptor;
312 if (FAILED(hr = dataObject->SetData(fe, &medium, TRUE)))
316 fe = fileContentFormatZero();
317 medium.hGlobal = fileContent;
318 if (FAILED(hr = dataObject->SetData(fe, &medium, TRUE)))
324 medium.hGlobal = hDropContent;
325 hr = dataObject->SetData(cfHDropFormat(), &medium, TRUE);
332 GlobalFree(fileDescriptor);
334 GlobalFree(fileContent);
336 GlobalFree(hDropContent);
341 PassRefPtr<Clipboard> Clipboard::create(ClipboardAccessPolicy policy, DragData* dragData, Frame* frame)
343 if (dragData->platformData())
344 return ClipboardWin::create(DragAndDrop, dragData->platformData(), policy, frame);
345 return ClipboardWin::create(DragAndDrop, dragData->dragDataMap(), policy, frame);
348 ClipboardWin::ClipboardWin(ClipboardType clipboardType, IDataObject* dataObject, ClipboardAccessPolicy policy, Frame* frame)
349 : Clipboard(policy, clipboardType)
350 , m_dataObject(dataObject)
351 , m_writableDataObject(0)
356 ClipboardWin::ClipboardWin(ClipboardType clipboardType, WCDataObject* dataObject, ClipboardAccessPolicy policy, Frame* frame)
357 : Clipboard(policy, clipboardType)
358 , m_dataObject(dataObject)
359 , m_writableDataObject(dataObject)
364 ClipboardWin::ClipboardWin(ClipboardType clipboardType, const DragDataMap& dataMap, ClipboardAccessPolicy policy, Frame* frame)
365 : Clipboard(policy, clipboardType)
367 , m_writableDataObject(0)
369 , m_dragDataMap(dataMap)
373 ClipboardWin::~ClipboardWin()
377 static bool writeURL(WCDataObject *data, const KURL& url, String title, bool withPlainText, bool withHTML)
384 if (title.isEmpty()) {
385 title = url.lastPathComponent();
390 STGMEDIUM medium = {0};
391 medium.tymed = TYMED_HGLOBAL;
393 medium.hGlobal = createGlobalData(url, title);
394 bool success = false;
395 if (medium.hGlobal && FAILED(data->SetData(urlWFormat(), &medium, TRUE)))
396 ::GlobalFree(medium.hGlobal);
401 Vector<char> cfhtmlData;
402 markupToCFHTML(urlToMarkup(url, title), "", cfhtmlData);
403 medium.hGlobal = createGlobalData(cfhtmlData);
404 if (medium.hGlobal && FAILED(data->SetData(htmlFormat(), &medium, TRUE)))
405 ::GlobalFree(medium.hGlobal);
411 medium.hGlobal = createGlobalData(url.string());
412 if (medium.hGlobal && FAILED(data->SetData(plainTextWFormat(), &medium, TRUE)))
413 ::GlobalFree(medium.hGlobal);
421 void ClipboardWin::clearData(const String& type)
423 // FIXME: Need to be able to write to the system clipboard <rdar://problem/5015941>
424 ASSERT(isForDragAndDrop());
425 if (policy() != ClipboardWritable || !m_writableDataObject)
428 ClipboardDataType dataType = clipboardTypeFromMIMEType(type);
430 if (dataType == ClipboardDataTypeURL) {
431 m_writableDataObject->clearData(urlWFormat()->cfFormat);
432 m_writableDataObject->clearData(urlFormat()->cfFormat);
434 if (dataType == ClipboardDataTypeText) {
435 m_writableDataObject->clearData(plainTextFormat()->cfFormat);
436 m_writableDataObject->clearData(plainTextWFormat()->cfFormat);
441 void ClipboardWin::clearAllData()
443 // FIXME: Need to be able to write to the system clipboard <rdar://problem/5015941>
444 ASSERT(isForDragAndDrop());
445 if (policy() != ClipboardWritable)
448 m_writableDataObject = 0;
449 WCDataObject::createInstance(&m_writableDataObject);
450 m_dataObject = m_writableDataObject;
453 String ClipboardWin::getData(const String& type, bool& success) const
456 if (policy() != ClipboardReadable || (!m_dataObject && m_dragDataMap.isEmpty()))
459 ClipboardDataType dataType = clipboardTypeFromMIMEType(type);
460 if (dataType == ClipboardDataTypeText)
461 return m_dataObject ? getPlainText(m_dataObject.get(), success) : getPlainText(&m_dragDataMap);
462 if (dataType == ClipboardDataTypeURL)
463 return m_dataObject ? getURL(m_dataObject.get(), DragData::DoNotConvertFilenames, success) : getURL(&m_dragDataMap, DragData::DoNotConvertFilenames);
464 else if (dataType == ClipboardDataTypeTextHTML) {
465 String data = m_dataObject ? getTextHTML(m_dataObject.get(), success) : getTextHTML(&m_dragDataMap);
468 return m_dataObject ? getCFHTML(m_dataObject.get(), success) : getCFHTML(&m_dragDataMap);
474 bool ClipboardWin::setData(const String& type, const String& data)
476 // FIXME: Need to be able to write to the system clipboard <rdar://problem/5015941>
477 ASSERT(isForDragAndDrop());
478 if (policy() != ClipboardWritable || !m_writableDataObject)
481 ClipboardDataType winType = clipboardTypeFromMIMEType(type);
483 if (winType == ClipboardDataTypeURL)
484 return WebCore::writeURL(m_writableDataObject.get(), KURL(ParsedURLString, data), String(), false, true);
486 if (winType == ClipboardDataTypeText) {
487 STGMEDIUM medium = {0};
488 medium.tymed = TYMED_HGLOBAL;
489 medium.hGlobal = createGlobalData(data);
493 if (FAILED(m_writableDataObject->SetData(plainTextWFormat(), &medium, TRUE))) {
494 ::GlobalFree(medium.hGlobal);
503 static void addMimeTypesForFormat(HashSet<String>& results, const FORMATETC& format)
505 // URL and Text are provided for compatibility with IE's model
506 if (format.cfFormat == urlFormat()->cfFormat || format.cfFormat == urlWFormat()->cfFormat) {
508 results.add("text/uri-list");
511 if (format.cfFormat == plainTextWFormat()->cfFormat || format.cfFormat == plainTextFormat()->cfFormat) {
513 results.add("text/plain");
517 // extensions beyond IE's API
518 HashSet<String> ClipboardWin::types() const
520 HashSet<String> results;
521 if (policy() != ClipboardReadable && policy() != ClipboardTypesReadable)
524 if (!m_dataObject && m_dragDataMap.isEmpty())
528 COMPtr<IEnumFORMATETC> itr;
530 if (FAILED(m_dataObject->EnumFormatEtc(DATADIR_GET, &itr)))
538 // IEnumFORMATETC::Next returns S_FALSE if there are no more items.
539 while (itr->Next(1, &data, 0) == S_OK)
540 addMimeTypesForFormat(results, data);
542 for (DragDataMap::const_iterator it = m_dragDataMap.begin(); it != m_dragDataMap.end(); ++it) {
544 data.cfFormat = (*it).first;
545 addMimeTypesForFormat(results, data);
552 PassRefPtr<FileList> ClipboardWin::files() const
558 RefPtr<FileList> files = FileList::create();
559 if (policy() != ClipboardReadable && policy() != ClipboardTypesReadable)
560 return files.release();
562 if (!m_dataObject && m_dragDataMap.isEmpty())
563 return files.release();
567 if (FAILED(m_dataObject->GetData(cfHDropFormat(), &medium)))
568 return files.release();
570 HDROP hdrop = reinterpret_cast<HDROP>(GlobalLock(medium.hGlobal));
572 return files.release();
574 WCHAR filename[MAX_PATH];
575 UINT fileCount = DragQueryFileW(hdrop, 0xFFFFFFFF, 0, 0);
576 for (UINT i = 0; i < fileCount; i++) {
577 if (!DragQueryFileW(hdrop, i, filename, WTF_ARRAY_LENGTH(filename)))
579 files->append(File::create(reinterpret_cast<UChar*>(filename)));
582 GlobalUnlock(medium.hGlobal);
583 ReleaseStgMedium(&medium);
584 return files.release();
586 if (!m_dragDataMap.contains(cfHDropFormat()->cfFormat))
587 return files.release();
588 Vector<String> filesVector = m_dragDataMap.get(cfHDropFormat()->cfFormat);
589 for (Vector<String>::iterator it = filesVector.begin(); it != filesVector.end(); ++it)
590 files->append(File::create((*it).characters()));
591 return files.release();
595 void ClipboardWin::setDragImage(CachedImage* image, Node *node, const IntPoint &loc)
597 if (policy() != ClipboardImageWritable && policy() != ClipboardWritable)
601 m_dragImage->removeClient(this);
604 m_dragImage->addClient(this);
607 m_dragImageElement = node;
610 void ClipboardWin::setDragImage(CachedImage* img, const IntPoint &loc)
612 setDragImage(img, 0, loc);
615 void ClipboardWin::setDragImageElement(Node *node, const IntPoint &loc)
617 setDragImage(0, node, loc);
620 DragImageRef ClipboardWin::createDragImage(IntPoint& loc) const
624 result = createDragImageFromImage(m_dragImage->image());
626 } else if (m_dragImageElement) {
627 Node* node = m_dragImageElement.get();
628 result = node->document()->frame()->nodeImage(node);
634 static String imageToMarkup(const String& url)
636 String markup("<img src=\"");
638 markup.append("\"/>");
642 static CachedImage* getCachedImage(Element* element)
644 // Attempt to pull CachedImage from element
646 RenderObject* renderer = element->renderer();
647 if (!renderer || !renderer->isImage())
650 RenderImage* image = toRenderImage(renderer);
651 if (image->cachedImage() && !image->cachedImage()->errorOccurred())
652 return image->cachedImage();
657 static void writeImageToDataObject(IDataObject* dataObject, Element* element, const KURL& url)
659 // Shove image data into a DataObject for use as a file
660 CachedImage* cachedImage = getCachedImage(element);
661 if (!cachedImage || !cachedImage->image() || !cachedImage->isLoaded())
664 SharedBuffer* imageBuffer = cachedImage->image()->data();
665 if (!imageBuffer || !imageBuffer->size())
668 HGLOBAL imageFileDescriptor = createGlobalImageFileDescriptor(url.string(), element->getAttribute(altAttr), cachedImage);
669 if (!imageFileDescriptor)
672 HGLOBAL imageFileContent = createGlobalImageFileContent(imageBuffer);
673 if (!imageFileContent) {
674 GlobalFree(imageFileDescriptor);
678 String fileName = cachedImage->response().suggestedFilename();
679 HGLOBAL hDropContent = createGlobalHDropContent(url, fileName, imageBuffer);
681 GlobalFree(hDropContent);
685 writeFileToDataObject(dataObject, imageFileDescriptor, imageFileContent, hDropContent);
688 void ClipboardWin::declareAndWriteDragImage(Element* element, const KURL& url, const String& title, Frame* frame)
690 // Order is important here for Explorer's sake
691 if (!m_writableDataObject)
693 WebCore::writeURL(m_writableDataObject.get(), url, title, true, false);
695 writeImageToDataObject(m_writableDataObject.get(), element, url);
697 AtomicString imageURL = element->getAttribute(srcAttr);
698 if (imageURL.isEmpty())
701 String fullURL = frame->document()->completeURL(stripLeadingAndTrailingHTMLSpaces(imageURL)).string();
702 if (fullURL.isEmpty())
704 STGMEDIUM medium = {0};
705 medium.tymed = TYMED_HGLOBAL;
706 ExceptionCode ec = 0;
708 // Put img tag on the clipboard referencing the image
710 markupToCFHTML(imageToMarkup(fullURL), "", data);
711 medium.hGlobal = createGlobalData(data);
712 if (medium.hGlobal && FAILED(m_writableDataObject->SetData(htmlFormat(), &medium, TRUE)))
713 ::GlobalFree(medium.hGlobal);
716 void ClipboardWin::writeURL(const KURL& kurl, const String& titleStr, Frame*)
718 if (!m_writableDataObject)
720 WebCore::writeURL(m_writableDataObject.get(), kurl, titleStr, true, true);
722 String url = kurl.string();
723 ASSERT(url.containsOnlyASCII()); // KURL::string() is URL encoded.
725 String fsPath = filesystemPathFromUrlOrTitle(url, titleStr, L".URL", true);
726 CString content = makeString("[InternetShortcut]\r\nURL=", url, "\r\n").ascii();
728 if (fsPath.length() <= 0)
731 HGLOBAL urlFileDescriptor = GlobalAlloc(GPTR, sizeof(FILEGROUPDESCRIPTOR));
732 if (!urlFileDescriptor)
735 HGLOBAL urlFileContent = GlobalAlloc(GPTR, content.length());
736 if (!urlFileContent) {
737 GlobalFree(urlFileDescriptor);
741 FILEGROUPDESCRIPTOR* fgd = static_cast<FILEGROUPDESCRIPTOR*>(GlobalLock(urlFileDescriptor));
742 ZeroMemory(fgd, sizeof(FILEGROUPDESCRIPTOR));
744 fgd->fgd[0].dwFlags = FD_FILESIZE;
745 fgd->fgd[0].nFileSizeLow = content.length();
747 unsigned maxSize = min(fsPath.length(), WTF_ARRAY_LENGTH(fgd->fgd[0].cFileName));
748 CopyMemory(fgd->fgd[0].cFileName, fsPath.characters(), maxSize * sizeof(UChar));
749 GlobalUnlock(urlFileDescriptor);
751 char* fileContents = static_cast<char*>(GlobalLock(urlFileContent));
752 CopyMemory(fileContents, content.data(), content.length());
753 GlobalUnlock(urlFileContent);
755 writeFileToDataObject(m_writableDataObject.get(), urlFileDescriptor, urlFileContent, 0);
758 void ClipboardWin::writeRange(Range* selectedRange, Frame* frame)
760 ASSERT(selectedRange);
761 if (!m_writableDataObject)
764 STGMEDIUM medium = {0};
765 medium.tymed = TYMED_HGLOBAL;
766 ExceptionCode ec = 0;
769 markupToCFHTML(createMarkup(selectedRange, 0, AnnotateForInterchange),
770 selectedRange->startContainer(ec)->document()->url().string(), data);
771 medium.hGlobal = createGlobalData(data);
772 if (medium.hGlobal && FAILED(m_writableDataObject->SetData(htmlFormat(), &medium, TRUE)))
773 ::GlobalFree(medium.hGlobal);
775 String str = frame->editor()->selectedText();
776 replaceNewlinesWithWindowsStyleNewlines(str);
777 replaceNBSPWithSpace(str);
778 medium.hGlobal = createGlobalData(str);
779 if (medium.hGlobal && FAILED(m_writableDataObject->SetData(plainTextWFormat(), &medium, TRUE)))
780 ::GlobalFree(medium.hGlobal);
783 if (frame->editor()->canSmartCopyOrDelete())
784 m_writableDataObject->SetData(smartPasteFormat(), &medium, TRUE);
787 void ClipboardWin::writePlainText(const String& text)
789 if (!m_writableDataObject)
792 STGMEDIUM medium = {0};
793 medium.tymed = TYMED_HGLOBAL;
794 ExceptionCode ec = 0;
797 replaceNewlinesWithWindowsStyleNewlines(str);
798 replaceNBSPWithSpace(str);
799 medium.hGlobal = createGlobalData(str);
800 if (medium.hGlobal && FAILED(m_writableDataObject->SetData(plainTextWFormat(), &medium, TRUE)))
801 ::GlobalFree(medium.hGlobal);
806 bool ClipboardWin::hasData()
808 if (!m_dataObject && m_dragDataMap.isEmpty())
812 COMPtr<IEnumFORMATETC> itr;
813 if (FAILED(m_dataObject->EnumFormatEtc(DATADIR_GET, &itr)))
821 // IEnumFORMATETC::Next returns S_FALSE if there are no more items.
822 if (itr->Next(1, &data, 0) == S_OK) {
823 // There is at least one item in the IDataObject
829 return !m_dragDataMap.isEmpty();
832 void ClipboardWin::setExternalDataObject(IDataObject *dataObject)
834 ASSERT(isForDragAndDrop());
836 m_writableDataObject = 0;
837 m_dataObject = dataObject;
840 } // namespace WebCore