1/*
2 * Copyright (C) 2006, 2013 Apple 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
6 * are met:
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.
12 *
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.
24 */
25
26#ifndef Pasteboard_h
27#define Pasteboard_h
28
29#include "DragImage.h"
30#include <wtf/ListHashSet.h>
31#include <wtf/Noncopyable.h>
32#include <wtf/Vector.h>
33#include <wtf/text/WTFString.h>
34
35#if PLATFORM(GTK)
36#include "DragData.h"
37typedef struct _GtkClipboard GtkClipboard;
38#endif
39
40#if PLATFORM(QT)
41#include <QMimeData>
42#endif
43
44#if PLATFORM(WIN)
45#include "COMPtr.h"
46#include "WCDataObject.h"
47#include <objidl.h>
48#include <windows.h>
49typedef struct HWND__* HWND;
50#endif
51
52// FIXME: This class is too high-level to be in the platform directory, since it
53// uses the DOM and makes calls to Editor. It should either be divested of its
54// knowledge of the frame and editor or moved into the editing directory.
55
56namespace WebCore {
57
58#if PLATFORM(MAC)
59#if PLATFORM(IOS)
60// FIXME: This is only temporary until Pasteboard is refactored for iOS.
61extern NSString *WebArchivePboardType;
62#else
63extern const char* WebArchivePboardType;
64#endif
65extern const char* WebSmartPastePboardType;
66extern const char* WebURLNamePboardType;
67extern const char* WebURLPboardType;
68extern const char* WebURLsWithTitlesPboardType;
69#endif
70
71class Clipboard;
72class DocumentFragment;
73class DragData;
74class Element;
75class Frame;
76class HitTestResult;
77class KURL;
78class Node;
79class Range;
80class SharedBuffer;
81
82enum ShouldSerializeSelectedTextForClipboard { DefaultSelectedTextType, IncludeImageAltTextForClipboard };
83
84class Pasteboard {
85    WTF_MAKE_NONCOPYABLE(Pasteboard); WTF_MAKE_FAST_ALLOCATED;
86public:
87    enum SmartReplaceOption {
88        CanSmartReplace,
89        CannotSmartReplace
90    };
91
92#if PLATFORM(MAC) && !PLATFORM(IOS)
93    static PassOwnPtr<Pasteboard> create(const String& pasteboardName);
94    String name() const { return m_pasteboardName; }
95
96    void writeSelectionForTypes(const Vector<String>& pasteboardTypes, bool canSmartCopyOrDelete, Frame*, ShouldSerializeSelectedTextForClipboard);
97    explicit Pasteboard(const String& pasteboardName);
98    static PassRefPtr<SharedBuffer> getDataSelection(Frame*, const String& pasteboardType);
99#endif
100
101#if PLATFORM(GTK)
102    static PassOwnPtr<Pasteboard> create(PassRefPtr<DataObjectGtk>);
103    static PassOwnPtr<Pasteboard> create(GtkClipboard*);
104    PassRefPtr<DataObjectGtk> dataObject() const;
105#endif
106
107#if PLATFORM(QT)
108    static PassOwnPtr<Pasteboard> create(const QMimeData* readableClipboard = 0, bool isForDragAndDrop = false);
109    QMimeData* clipboardData() const { return m_writableData; }
110    void invalidateWritableData() const { m_writableData = 0; }
111    bool isForDragAndDrop() const { return m_isForDragAndDrop; }
112    bool isForCopyAndPaste() const { return !m_isForDragAndDrop; }
113#endif
114
115    // Deprecated. Use createForCopyAndPaste instead.
116    static Pasteboard* generalPasteboard();
117
118    static PassOwnPtr<Pasteboard> createForCopyAndPaste();
119    static PassOwnPtr<Pasteboard> createPrivate(); // Corresponds to the "unique pasteboard" concept on Mac. Used in editing, not sure exactly for what purpose.
120
121#if ENABLE(DRAG_SUPPORT)
122    static PassOwnPtr<Pasteboard> createForDragAndDrop();
123    static PassOwnPtr<Pasteboard> createForDragAndDrop(const DragData&);
124#endif
125
126    bool hasData();
127    ListHashSet<String> types();
128
129    String readString(const String& type);
130    Vector<String> readFilenames();
131
132    bool writeString(const String& type, const String& data);
133    void writeSelection(Range*, bool canSmartCopyOrDelete, Frame*, ShouldSerializeSelectedTextForClipboard = DefaultSelectedTextType);
134    void writePlainText(const String&, SmartReplaceOption);
135#if !PLATFORM(IOS)
136    void writeURL(const KURL&, const String&, Frame* = 0);
137    void writeImage(Node*, const KURL&, const String& title);
138#else
139    void writeImage(Node*, Frame*);
140    void writePlainText(const String&, Frame*);
141    static NSArray* supportedPasteboardTypes();
142#endif
143    void writePasteboard(const Pasteboard& sourcePasteboard);
144    void writeClipboard(Clipboard*);
145
146    void clear();
147    void clear(const String& type);
148
149    bool canSmartReplace();
150
151#if ENABLE(DRAG_SUPPORT)
152    void setDragImage(DragImageRef, const IntPoint& hotSpot);
153#endif
154
155    // FIXME: Having these functions here is a layering violation.
156    // These functions need to move to the editing directory even if they have platform-specific aspects.
157    PassRefPtr<DocumentFragment> documentFragment(Frame*, PassRefPtr<Range>, bool allowPlainText, bool& chosePlainText);
158    String plainText(Frame* = 0);
159
160#if PLATFORM(QT) || PLATFORM(GTK)
161    bool isSelectionMode() const;
162    void setSelectionMode(bool);
163#else
164    bool isSelectionMode() const { return false; }
165    void setSelectionMode(bool) { }
166#endif
167
168#if PLATFORM(WIN)
169    COMPtr<IDataObject> dataObject() const { return m_dataObject; }
170    void setExternalDataObject(IDataObject*);
171    void writeURLToWritableDataObject(const KURL&, const String&);
172    COMPtr<WCDataObject> writableDataObject() const { return m_writableDataObject; }
173    void writeImageToDataObject(Element*, const KURL&);
174#endif
175
176#if PLATFORM(GTK) || PLATFORM(QT)
177    ~Pasteboard();
178#endif
179
180private:
181    Pasteboard();
182
183#if PLATFORM(GTK)
184    Pasteboard(PassRefPtr<DataObjectGtk>);
185    Pasteboard(GtkClipboard*);
186#endif
187
188#if PLATFORM(QT)
189    Pasteboard(const QMimeData* , bool);
190#endif
191
192#if PLATFORM(WIN)
193    explicit Pasteboard(IDataObject*);
194    explicit Pasteboard(WCDataObject*);
195    explicit Pasteboard(const DragDataMap&);
196#endif
197
198#if PLATFORM(MAC) && !PLATFORM(IOS)
199    String m_pasteboardName;
200    long m_changeCount;
201#endif
202
203#if PLATFORM(IOS)
204    PassRefPtr<DocumentFragment> documentFragmentForPasteboardItemAtIndex(Frame*, int index, bool allowPlainText, bool& chosePlainText);
205#endif
206
207#if PLATFORM(WIN)
208    void finishCreatingPasteboard();
209    void writeRangeToDataObject(Range*, Frame*);
210    void writeURLToDataObject(const KURL&, const String&, Frame*);
211    void writePlainTextToDataObject(const String&, SmartReplaceOption);
212
213    HWND m_owner;
214    COMPtr<IDataObject> m_dataObject;
215    COMPtr<WCDataObject> m_writableDataObject;
216    DragDataMap m_dragDataMap;
217#endif
218
219#if PLATFORM(QT)
220    const QMimeData* readData() const;
221
222    bool m_selectionMode;
223    const QMimeData* m_readableData;
224    mutable QMimeData* m_writableData;
225    bool m_isForDragAndDrop;
226#endif
227
228#if PLATFORM(GTK)
229    RefPtr<DataObjectGtk> m_dataObject;
230    GtkClipboard* m_gtkClipboard;
231#endif
232
233};
234
235} // namespace WebCore
236
237#endif // Pasteboard_h
238