initial import
[vuplus_webkit] / Source / WebCore / dom / Clipboard.cpp
1 /*
2  * Copyright (C) 2006, 2007, 2008 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 #include "config.h"
27 #include "Clipboard.h"
28
29 #include "CachedImage.h"
30 #include "FileList.h"
31 #include "Frame.h"
32 #include "FrameLoader.h"
33 #include "Image.h"
34
35 namespace WebCore {
36
37 Clipboard::Clipboard(ClipboardAccessPolicy policy, ClipboardType clipboardType) 
38     : m_policy(policy)
39     , m_dropEffect("uninitialized")
40     , m_effectAllowed("uninitialized")
41     , m_dragStarted(false)
42     , m_clipboardType(clipboardType)
43     , m_dragImage(0)
44 {
45 }
46     
47 void Clipboard::setAccessPolicy(ClipboardAccessPolicy policy)
48 {
49     // once you go numb, can never go back
50     ASSERT(m_policy != ClipboardNumb || policy == ClipboardNumb);
51     m_policy = policy;
52 }
53
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.
56
57 static DragOperation dragOpFromIEOp(const String& op)
58 {
59     // yep, it's really just this fixed set
60     if (op == "uninitialized")
61         return DragOperationEvery;
62     if (op == "none")
63         return DragOperationNone;
64     if (op == "copy")
65         return DragOperationCopy;
66     if (op == "link")
67         return DragOperationLink;
68     if (op == "move")
69         return (DragOperation)(DragOperationGeneric | DragOperationMove);
70     if (op == "copyLink")
71         return (DragOperation)(DragOperationCopy | DragOperationLink);
72     if (op == "copyMove")
73         return (DragOperation)(DragOperationCopy | DragOperationGeneric | DragOperationMove);
74     if (op == "linkMove")
75         return (DragOperation)(DragOperationLink | DragOperationGeneric | DragOperationMove);
76     if (op == "all")
77         return DragOperationEvery;
78     return DragOperationPrivate;  // really a marker for "no conversion"
79 }
80
81 static String IEOpFromDragOp(DragOperation op)
82 {
83     bool moveSet = !!((DragOperationGeneric | DragOperationMove) & op);
84     
85     if ((moveSet && (op & DragOperationCopy) && (op & DragOperationLink))
86         || (op == DragOperationEvery))
87         return "all";
88     if (moveSet && (op & DragOperationCopy))
89         return "copyMove";
90     if (moveSet && (op & DragOperationLink))
91         return "linkMove";
92     if ((op & DragOperationCopy) && (op & DragOperationLink))
93         return "copyLink";
94     if (moveSet)
95         return "move";
96     if (op & DragOperationCopy)
97         return "copy";
98     if (op & DragOperationLink)
99         return "link";
100     return "none";
101 }
102
103 DragOperation Clipboard::sourceOperation() const
104 {
105     DragOperation op = dragOpFromIEOp(m_effectAllowed);
106     ASSERT(op != DragOperationPrivate);
107     return op;
108 }
109
110 DragOperation Clipboard::destinationOperation() const
111 {
112     DragOperation op = dragOpFromIEOp(m_dropEffect);
113     ASSERT(op == DragOperationCopy || op == DragOperationNone || op == DragOperationLink || op == (DragOperation)(DragOperationGeneric | DragOperationMove) || op == DragOperationEvery);
114     return op;
115 }
116
117 void Clipboard::setSourceOperation(DragOperation op)
118 {
119     ASSERT_ARG(op, op != DragOperationPrivate);
120     m_effectAllowed = IEOpFromDragOp(op);
121 }
122
123 void Clipboard::setDestinationOperation(DragOperation op)
124 {
125     ASSERT_ARG(op, op == DragOperationCopy || op == DragOperationNone || op == DragOperationLink || op == DragOperationGeneric || op == DragOperationMove || op == (DragOperation)(DragOperationGeneric | DragOperationMove));
126     m_dropEffect = IEOpFromDragOp(op);
127 }
128
129 bool Clipboard::hasFileOfType(const String& type) const
130 {
131     if (m_policy != ClipboardReadable && m_policy != ClipboardTypesReadable)
132         return false;
133     
134     RefPtr<FileList> fileList = files();
135     if (fileList->isEmpty())
136         return false;
137     
138     for (unsigned int f = 0; f < fileList->length(); f++) {
139         if (equalIgnoringCase(fileList->item(f)->type(), type))
140             return true;
141     }
142     return false;
143 }
144
145 bool Clipboard::hasStringOfType(const String& type) const
146 {
147     if (m_policy != ClipboardReadable && m_policy != ClipboardTypesReadable)
148         return false;
149     
150     return types().contains(type); 
151 }
152     
153 void Clipboard::setDropEffect(const String &effect)
154 {
155     if (!isForDragAndDrop())
156         return;
157
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")
160         return;
161
162     if (m_policy == ClipboardReadable || m_policy == ClipboardTypesReadable)
163         m_dropEffect = effect;
164 }
165
166 void Clipboard::setEffectAllowed(const String &effect)
167 {
168     if (!isForDragAndDrop())
169         return;
170
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.
175
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.
178         return;
179     }
180
181
182     if (m_policy == ClipboardWritable)
183         m_effectAllowed = effect;
184 }
185     
186 DragOperation convertDropZoneOperationToDragOperation(const String& dragOperation)
187 {
188     if (dragOperation == "copy")
189         return DragOperationCopy;
190     if (dragOperation == "move")
191         return DragOperationMove;
192     if (dragOperation == "link")
193         return DragOperationLink;
194     return DragOperationNone;
195 }
196
197 String convertDragOperationToDropZoneOperation(DragOperation operation)
198 {
199     switch (operation) {
200     case DragOperationCopy:
201         return String("copy");
202     case DragOperationMove:
203         return String("move");
204     case DragOperationLink:
205         return String("link");
206     default:
207         return String("copy");
208     }
209 }
210
211 bool Clipboard::hasDropZoneType(const String& keyword)
212 {
213     if (keyword.length() < 3 || keyword[1] != ':')
214         return false;
215         
216     switch (keyword[0]) {
217     case 'f':
218         return hasFileOfType(keyword.substring(2));
219     case 's':
220         return hasStringOfType(keyword.substring(2));
221     default:
222         return false;
223     }
224 }
225
226 } // namespace WebCore