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