2 * Copyright (C) 2006, 2007, 2008 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 "Clipboard.h"
29 #include "CachedImage.h"
32 #include "FrameLoader.h"
37 Clipboard::Clipboard(ClipboardAccessPolicy policy, ClipboardType clipboardType)
39 , m_dropEffect("uninitialized")
40 , m_effectAllowed("uninitialized")
41 , m_dragStarted(false)
42 , m_clipboardType(clipboardType)
47 void Clipboard::setAccessPolicy(ClipboardAccessPolicy policy)
49 // once you go numb, can never go back
50 ASSERT(m_policy != ClipboardNumb || policy == ClipboardNumb);
54 // These "conversion" methods are called by both WebCore and WebKit, and never make sense to JS, so we don't
55 // worry about security for these. They don't allow access to the pasteboard anyway.
57 static DragOperation dragOpFromIEOp(const String& op)
59 // yep, it's really just this fixed set
60 if (op == "uninitialized")
61 return DragOperationEvery;
63 return DragOperationNone;
65 return DragOperationCopy;
67 return DragOperationLink;
69 return (DragOperation)(DragOperationGeneric | DragOperationMove);
71 return (DragOperation)(DragOperationCopy | DragOperationLink);
73 return (DragOperation)(DragOperationCopy | DragOperationGeneric | DragOperationMove);
75 return (DragOperation)(DragOperationLink | DragOperationGeneric | DragOperationMove);
77 return DragOperationEvery;
78 return DragOperationPrivate; // really a marker for "no conversion"
81 static String IEOpFromDragOp(DragOperation op)
83 bool moveSet = !!((DragOperationGeneric | DragOperationMove) & op);
85 if ((moveSet && (op & DragOperationCopy) && (op & DragOperationLink))
86 || (op == DragOperationEvery))
88 if (moveSet && (op & DragOperationCopy))
90 if (moveSet && (op & DragOperationLink))
92 if ((op & DragOperationCopy) && (op & DragOperationLink))
96 if (op & DragOperationCopy)
98 if (op & DragOperationLink)
103 DragOperation Clipboard::sourceOperation() const
105 DragOperation op = dragOpFromIEOp(m_effectAllowed);
106 ASSERT(op != DragOperationPrivate);
110 DragOperation Clipboard::destinationOperation() const
112 DragOperation op = dragOpFromIEOp(m_dropEffect);
113 ASSERT(op == DragOperationCopy || op == DragOperationNone || op == DragOperationLink || op == (DragOperation)(DragOperationGeneric | DragOperationMove) || op == DragOperationEvery);
117 void Clipboard::setSourceOperation(DragOperation op)
119 ASSERT_ARG(op, op != DragOperationPrivate);
120 m_effectAllowed = IEOpFromDragOp(op);
123 void Clipboard::setDestinationOperation(DragOperation op)
125 ASSERT_ARG(op, op == DragOperationCopy || op == DragOperationNone || op == DragOperationLink || op == DragOperationGeneric || op == DragOperationMove || op == (DragOperation)(DragOperationGeneric | DragOperationMove));
126 m_dropEffect = IEOpFromDragOp(op);
129 bool Clipboard::hasFileOfType(const String& type) const
131 if (m_policy != ClipboardReadable && m_policy != ClipboardTypesReadable)
134 RefPtr<FileList> fileList = files();
135 if (fileList->isEmpty())
138 for (unsigned int f = 0; f < fileList->length(); f++) {
139 if (equalIgnoringCase(fileList->item(f)->type(), type))
145 bool Clipboard::hasStringOfType(const String& type) const
147 if (m_policy != ClipboardReadable && m_policy != ClipboardTypesReadable)
150 return types().contains(type);
153 void Clipboard::setDropEffect(const String &effect)
155 if (!isForDragAndDrop())
158 // The attribute must ignore any attempts to set it to a value other than none, copy, link, and move.
159 if (effect != "none" && effect != "copy" && effect != "link" && effect != "move")
162 if (m_policy == ClipboardReadable || m_policy == ClipboardTypesReadable)
163 m_dropEffect = effect;
166 void Clipboard::setEffectAllowed(const String &effect)
168 if (!isForDragAndDrop())
171 if (dragOpFromIEOp(effect) == DragOperationPrivate) {
172 // This means that there was no conversion, and the effectAllowed that
173 // we are passed isn't a valid effectAllowed, so we should ignore it,
174 // and not set m_effectAllowed.
176 // The attribute must ignore any attempts to set it to a value other than
177 // none, copy, copyLink, copyMove, link, linkMove, move, all, and uninitialized.
182 if (m_policy == ClipboardWritable)
183 m_effectAllowed = effect;
186 DragOperation convertDropZoneOperationToDragOperation(const String& dragOperation)
188 if (dragOperation == "copy")
189 return DragOperationCopy;
190 if (dragOperation == "move")
191 return DragOperationMove;
192 if (dragOperation == "link")
193 return DragOperationLink;
194 return DragOperationNone;
197 String convertDragOperationToDropZoneOperation(DragOperation operation)
200 case DragOperationCopy:
201 return String("copy");
202 case DragOperationMove:
203 return String("move");
204 case DragOperationLink:
205 return String("link");
207 return String("copy");
211 bool Clipboard::hasDropZoneType(const String& keyword)
213 if (keyword.length() < 3 || keyword[1] != ':')
216 switch (keyword[0]) {
218 return hasFileOfType(keyword.substring(2));
220 return hasStringOfType(keyword.substring(2));
226 } // namespace WebCore