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 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 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 "URL.h"
31#include <wtf/Noncopyable.h>
32#include <wtf/Vector.h>
33#include <wtf/text/WTFString.h>
34
35#if PLATFORM(GTK)
36typedef struct _GtkClipboard GtkClipboard;
37#endif
38
39#if PLATFORM(IOS)
40OBJC_CLASS NSArray;
41OBJC_CLASS NSString;
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 uses the DOM and makes calls to Editor.
53// It should be divested of its knowledge of the frame and editor.
54
55namespace WebCore {
56
57class DataObjectGtk;
58class DocumentFragment;
59class DragData;
60class Element;
61class Frame;
62class Range;
63class SharedBuffer;
64
65enum ShouldSerializeSelectedTextForDataTransfer { DefaultSelectedTextType, IncludeImageAltTextForDataTransfer };
66
67// For writing to the pasteboard. Generally sorted with the richest formats on top.
68
69struct PasteboardWebContent {
70#if !(PLATFORM(EFL) || PLATFORM(GTK) || PLATFORM(WIN))
71    PasteboardWebContent();
72    ~PasteboardWebContent();
73    bool canSmartCopyOrDelete;
74    RefPtr<SharedBuffer> dataInWebArchiveFormat;
75    RefPtr<SharedBuffer> dataInRTFDFormat;
76    RefPtr<SharedBuffer> dataInRTFFormat;
77    String dataInStringFormat;
78    Vector<String> clientTypes;
79    Vector<RefPtr<SharedBuffer>> clientData;
80#endif
81};
82
83struct PasteboardURL {
84    URL url;
85    String title;
86#if PLATFORM(MAC)
87    String userVisibleForm;
88#endif
89};
90
91struct PasteboardImage {
92    PasteboardImage();
93    ~PasteboardImage();
94    RefPtr<Image> image;
95#if !(PLATFORM(EFL) || PLATFORM(GTK) || PLATFORM(WIN))
96    PasteboardURL url;
97    RefPtr<SharedBuffer> resourceData;
98    String resourceMIMEType;
99#endif
100};
101
102// For reading from the pasteboard.
103
104class PasteboardWebContentReader {
105public:
106    virtual ~PasteboardWebContentReader() { }
107
108#if !(PLATFORM(EFL) || PLATFORM(GTK) || PLATFORM(WIN))
109    virtual bool readWebArchive(PassRefPtr<SharedBuffer>) = 0;
110    virtual bool readFilenames(const Vector<String>&) = 0;
111    virtual bool readHTML(const String&) = 0;
112    virtual bool readRTFD(PassRefPtr<SharedBuffer>) = 0;
113    virtual bool readRTF(PassRefPtr<SharedBuffer>) = 0;
114    virtual bool readImage(PassRefPtr<SharedBuffer>, const String& type) = 0;
115    virtual bool readURL(const URL&, const String& title) = 0;
116#endif
117    virtual bool readPlainText(const String&) = 0;
118};
119
120struct PasteboardPlainText {
121    String text;
122#if PLATFORM(MAC)
123    bool isURL;
124#endif
125};
126
127class Pasteboard {
128    WTF_MAKE_NONCOPYABLE(Pasteboard); WTF_MAKE_FAST_ALLOCATED;
129public:
130    ~Pasteboard();
131
132    static PassOwnPtr<Pasteboard> createForCopyAndPaste();
133    static PassOwnPtr<Pasteboard> createPrivate(); // Temporary pasteboard. Can put data on this and then write to another pasteboard with writePasteboard.
134
135    bool hasData();
136    Vector<String> types();
137    String readString(const String& type);
138
139    void writeString(const String& type, const String& data);
140    void clear();
141    void clear(const String& type);
142
143    void read(PasteboardPlainText&);
144    void read(PasteboardWebContentReader&);
145
146    void write(const PasteboardURL&);
147    void write(const PasteboardImage&);
148    void write(const PasteboardWebContent&);
149
150    Vector<String> readFilenames();
151    bool canSmartReplace();
152
153    void writeMarkup(const String& markup);
154    enum SmartReplaceOption { CanSmartReplace, CannotSmartReplace };
155    void writePlainText(const String&, SmartReplaceOption); // FIXME: Two separate functions would be clearer than one function with an argument.
156    void writePasteboard(const Pasteboard& sourcePasteboard);
157
158#if ENABLE(DRAG_SUPPORT)
159    static PassOwnPtr<Pasteboard> createForDragAndDrop();
160    static PassOwnPtr<Pasteboard> createForDragAndDrop(const DragData&);
161
162    void setDragImage(DragImageRef, const IntPoint& hotSpot);
163#endif
164
165#if PLATFORM(GTK) || PLATFORM(WIN)
166    PassRefPtr<DocumentFragment> documentFragment(Frame&, Range&, bool allowPlainText, bool& chosePlainText); // FIXME: Layering violation.
167    void writeImage(Element&, const URL&, const String& title); // FIXME: Layering violation.
168    void writeSelection(Range&, bool canSmartCopyOrDelete, Frame&, ShouldSerializeSelectedTextForDataTransfer = DefaultSelectedTextType); // FIXME: Layering violation.
169#endif
170
171#if PLATFORM(GTK)
172    static PassOwnPtr<Pasteboard> create(PassRefPtr<DataObjectGtk>);
173    static PassOwnPtr<Pasteboard> create(GtkClipboard*);
174    PassRefPtr<DataObjectGtk> dataObject() const;
175    static PassOwnPtr<Pasteboard> createForGlobalSelection();
176#endif
177
178#if PLATFORM(IOS)
179    static NSArray* supportedPasteboardTypes();
180    static String resourceMIMEType(const NSString *mimeType);
181#endif
182
183#if PLATFORM(MAC)
184    explicit Pasteboard(const String& pasteboardName);
185    static PassOwnPtr<Pasteboard> create(const String& pasteboardName);
186
187    const String& name() const { return m_pasteboardName; }
188#endif
189
190#if PLATFORM(WIN)
191    COMPtr<IDataObject> dataObject() const { return m_dataObject; }
192    void setExternalDataObject(IDataObject*);
193    void writeURLToWritableDataObject(const URL&, const String&);
194    COMPtr<WCDataObject> writableDataObject() const { return m_writableDataObject; }
195    void writeImageToDataObject(Element&, const URL&); // FIXME: Layering violation.
196#endif
197
198private:
199    Pasteboard();
200
201#if PLATFORM(GTK)
202    Pasteboard(PassRefPtr<DataObjectGtk>);
203    Pasteboard(GtkClipboard*);
204#endif
205
206#if PLATFORM(WIN)
207    explicit Pasteboard(IDataObject*);
208    explicit Pasteboard(WCDataObject*);
209    explicit Pasteboard(const DragDataMap&);
210
211    void finishCreatingPasteboard();
212    void writeRangeToDataObject(Range&, Frame&); // FIXME: Layering violation.
213    void writeURLToDataObject(const URL&, const String&);
214    void writePlainTextToDataObject(const String&, SmartReplaceOption);
215#endif
216
217#if PLATFORM(GTK)
218    RefPtr<DataObjectGtk> m_dataObject;
219    GtkClipboard* m_gtkClipboard;
220#endif
221
222#if PLATFORM(IOS)
223    long m_changeCount;
224#endif
225
226#if PLATFORM(MAC)
227    String m_pasteboardName;
228    long m_changeCount;
229#endif
230
231#if PLATFORM(WIN)
232    HWND m_owner;
233    COMPtr<IDataObject> m_dataObject;
234    COMPtr<WCDataObject> m_writableDataObject;
235    DragDataMap m_dragDataMap;
236#endif
237};
238
239#if PLATFORM(IOS)
240extern NSString *WebArchivePboardType;
241#endif
242
243#if PLATFORM(MAC)
244extern const char* const WebArchivePboardType;
245extern const char* const WebURLNamePboardType;
246#endif
247
248#if !PLATFORM(GTK)
249
250inline Pasteboard::~Pasteboard()
251{
252}
253
254#endif
255
256} // namespace WebCore
257
258#endif // Pasteboard_h
259