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 "DOMImplementation.h"
31 #include "Frame.h"
32 #include "FrameLoader.h"
33 #include "Image.h"
34
35 namespace WebCore {
36
Clipboard(ClipboardAccessPolicy policy,bool isForDragging)37 Clipboard::Clipboard(ClipboardAccessPolicy policy, bool isForDragging)
38 : m_policy(policy)
39 , m_dragStarted(false)
40 , m_forDragging(isForDragging)
41 , m_dragImage(0)
42 {
43 }
44
setAccessPolicy(ClipboardAccessPolicy policy)45 void Clipboard::setAccessPolicy(ClipboardAccessPolicy policy)
46 {
47 // once you go numb, can never go back
48 ASSERT(m_policy != ClipboardNumb || policy == ClipboardNumb);
49 m_policy = policy;
50 }
51
52 // These "conversion" methods are called by both WebCore and WebKit, and never make sense to JS, so we don't
53 // worry about security for these. They don't allow access to the pasteboard anyway.
54
dragOpFromIEOp(const String & op)55 static DragOperation dragOpFromIEOp(const String& op)
56 {
57 // yep, it's really just this fixed set
58 if (op == "none")
59 return DragOperationNone;
60 if (op == "copy")
61 return DragOperationCopy;
62 if (op == "link")
63 return DragOperationLink;
64 if (op == "move")
65 return DragOperationGeneric;
66 if (op == "copyLink")
67 return (DragOperation)(DragOperationCopy | DragOperationLink);
68 if (op == "copyMove")
69 return (DragOperation)(DragOperationCopy | DragOperationGeneric | DragOperationMove);
70 if (op == "linkMove")
71 return (DragOperation)(DragOperationLink | DragOperationGeneric | DragOperationMove);
72 if (op == "all")
73 return DragOperationEvery;
74 return DragOperationPrivate; // really a marker for "no conversion"
75 }
76
IEOpFromDragOp(DragOperation op)77 static String IEOpFromDragOp(DragOperation op)
78 {
79 bool moveSet = !!((DragOperationGeneric | DragOperationMove) & op);
80
81 if ((moveSet && (op & DragOperationCopy) && (op & DragOperationLink))
82 || (op == DragOperationEvery))
83 return "all";
84 if (moveSet && (op & DragOperationCopy))
85 return "copyMove";
86 if (moveSet && (op & DragOperationLink))
87 return "linkMove";
88 if ((op & DragOperationCopy) && (op & DragOperationLink))
89 return "copyLink";
90 if (moveSet)
91 return "move";
92 if (op & DragOperationCopy)
93 return "copy";
94 if (op & DragOperationLink)
95 return "link";
96 return "none";
97 }
98
sourceOperation(DragOperation & op) const99 bool Clipboard::sourceOperation(DragOperation& op) const
100 {
101 if (m_effectAllowed.isNull())
102 return false;
103 op = dragOpFromIEOp(m_effectAllowed);
104 return true;
105 }
106
destinationOperation(DragOperation & op) const107 bool Clipboard::destinationOperation(DragOperation& op) const
108 {
109 if (m_dropEffect.isNull())
110 return false;
111 op = dragOpFromIEOp(m_dropEffect);
112 return true;
113 }
114
setSourceOperation(DragOperation op)115 void Clipboard::setSourceOperation(DragOperation op)
116 {
117 m_effectAllowed = IEOpFromDragOp(op);
118 }
119
setDestinationOperation(DragOperation op)120 void Clipboard::setDestinationOperation(DragOperation op)
121 {
122 m_dropEffect = IEOpFromDragOp(op);
123 }
124
setDropEffect(const String & effect)125 void Clipboard::setDropEffect(const String &effect)
126 {
127 if (!m_forDragging)
128 return;
129
130 if (m_policy == ClipboardReadable || m_policy == ClipboardTypesReadable)
131 m_dropEffect = effect;
132 }
133
setEffectAllowed(const String & effect)134 void Clipboard::setEffectAllowed(const String &effect)
135 {
136 if (!m_forDragging)
137 return;
138
139 if (m_policy == ClipboardWritable)
140 m_effectAllowed = effect;
141 }
142
143 } // namespace WebCore
144