1/*
2 * Copyright (c) 2010 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "config.h"
32#include "ReadableDataObject.h"
33
34#include "ClipboardMimeTypes.h"
35#include "Pasteboard.h"
36#include "PasteboardPrivate.h"
37#include "PlatformBridge.h"
38
39namespace WebCore {
40
41static PasteboardPrivate::ClipboardBuffer clipboardBuffer(Clipboard::ClipboardType clipboardType)
42{
43    return clipboardType == Clipboard::DragAndDrop ? PasteboardPrivate::DragBuffer : PasteboardPrivate::StandardBuffer;
44}
45
46PassRefPtr<ReadableDataObject> ReadableDataObject::create(Clipboard::ClipboardType clipboardType)
47{
48    return adoptRef(new ReadableDataObject(clipboardType));
49}
50
51ReadableDataObject::ReadableDataObject(Clipboard::ClipboardType clipboardType)
52    : m_clipboardType(clipboardType)
53    , m_containsFilenames(false)
54    , m_isTypeCacheInitialized(false)
55{
56}
57
58bool ReadableDataObject::hasData() const
59{
60    ensureTypeCacheInitialized();
61    return !m_types.isEmpty() || m_containsFilenames;
62}
63
64HashSet<String> ReadableDataObject::types() const
65{
66    ensureTypeCacheInitialized();
67    return m_types;
68}
69
70String ReadableDataObject::getData(const String& type, bool& succeeded) const
71{
72    String data;
73    String ignoredMetadata;
74    // Since the Chromium-side bridge isn't complete yet, we special case this
75    // for copy-and-paste, since that code path no longer uses
76    // ChromiumDataObjectLegacy.
77    if (m_clipboardType == Clipboard::CopyAndPaste) {
78        if (type == mimeTypeTextPlain) {
79            PasteboardPrivate::ClipboardBuffer buffer =
80                Pasteboard::generalPasteboard()->isSelectionMode() ?
81                PasteboardPrivate::SelectionBuffer :
82                PasteboardPrivate::StandardBuffer;
83            data = PlatformBridge::clipboardReadPlainText(buffer);
84        } else if (type == mimeTypeTextHTML) {
85            PasteboardPrivate::ClipboardBuffer buffer =
86                Pasteboard::generalPasteboard()->isSelectionMode() ?
87                PasteboardPrivate::SelectionBuffer :
88                PasteboardPrivate::StandardBuffer;
89            KURL ignoredSourceURL;
90            PlatformBridge::clipboardReadHTML(buffer, &data, &ignoredSourceURL);
91        }
92        succeeded = !data.isEmpty();
93        return data;
94    }
95    succeeded = PlatformBridge::clipboardReadData(
96        clipboardBuffer(m_clipboardType), type, data, ignoredMetadata);
97    return data;
98}
99
100String ReadableDataObject::urlTitle() const
101{
102    String ignoredData;
103    String urlTitle;
104    PlatformBridge::clipboardReadData(
105        clipboardBuffer(m_clipboardType), mimeTypeTextURIList, ignoredData, urlTitle);
106    return urlTitle;
107}
108
109KURL ReadableDataObject::htmlBaseUrl() const
110{
111    String ignoredData;
112    String htmlBaseUrl;
113    PlatformBridge::clipboardReadData(
114        clipboardBuffer(m_clipboardType), mimeTypeTextHTML, ignoredData, htmlBaseUrl);
115    return KURL(ParsedURLString, htmlBaseUrl);
116}
117
118bool ReadableDataObject::containsFilenames() const
119{
120    ensureTypeCacheInitialized();
121    return m_containsFilenames;
122}
123
124Vector<String> ReadableDataObject::filenames() const
125{
126    return PlatformBridge::clipboardReadFilenames(clipboardBuffer(m_clipboardType));
127}
128
129void ReadableDataObject::ensureTypeCacheInitialized() const
130{
131    if (m_isTypeCacheInitialized)
132        return;
133
134    m_types = PlatformBridge::clipboardReadAvailableTypes(
135        clipboardBuffer(m_clipboardType), &m_containsFilenames);
136    m_isTypeCacheInitialized = true;
137}
138
139} // namespace WebCore
140