1 /*
2 * Copyright (C) 2007-2009 Google 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 are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "config.h"
32 #include "Clipboard.h"
33
34 #include "HTMLImageElement.h"
35 #include "HTMLNames.h"
36 #include "IntPoint.h"
37 #include "Node.h"
38 #include "Element.h"
39
40 #include "V8Binding.h"
41 #include "V8CustomBinding.h"
42 #include "V8Node.h"
43 #include "V8Proxy.h"
44
45 namespace WebCore {
46
ACCESSOR_GETTER(ClipboardTypes)47 ACCESSOR_GETTER(ClipboardTypes)
48 {
49 INC_STATS("DOM.Clipboard.types()");
50 Clipboard* clipboard = V8DOMWrapper::convertToNativeObject<Clipboard>(V8ClassIndex::CLIPBOARD, info.Holder());
51
52 HashSet<String> types = clipboard->types();
53 if (types.isEmpty())
54 return v8::Null();
55
56 v8::Local<v8::Array> result = v8::Array::New(types.size());
57 HashSet<String>::const_iterator end = types.end();
58 int index = 0;
59 for (HashSet<String>::const_iterator it = types.begin(); it != end; ++it, ++index)
60 result->Set(v8::Integer::New(index), v8String(*it));
61
62 return result;
63 }
64
CALLBACK_FUNC_DECL(ClipboardClearData)65 CALLBACK_FUNC_DECL(ClipboardClearData)
66 {
67 INC_STATS("DOM.Clipboard.clearData()");
68 Clipboard* clipboard = V8DOMWrapper::convertToNativeObject<Clipboard>(V8ClassIndex::CLIPBOARD, args.Holder());
69
70 if (!args.Length()) {
71 clipboard->clearAllData();
72 return v8::Undefined();
73 }
74
75 if (args.Length() != 1)
76 return throwError("clearData: Invalid number of arguments", V8Proxy::SyntaxError);
77
78 String type = toWebCoreString(args[0]);
79 clipboard->clearData(type);
80 return v8::Undefined();
81 }
82
CALLBACK_FUNC_DECL(ClipboardGetData)83 CALLBACK_FUNC_DECL(ClipboardGetData)
84 {
85 INC_STATS("DOM.Clipboard.getData()");
86 Clipboard* clipboard = V8DOMWrapper::convertToNativeObject<Clipboard>(V8ClassIndex::CLIPBOARD, args.Holder());
87
88 if (args.Length() != 1)
89 return throwError("getData: Invalid number of arguments", V8Proxy::SyntaxError);
90
91 bool success;
92 String result = clipboard->getData(toWebCoreString(args[0]), success);
93 if (success)
94 return v8String(result);
95
96 return v8::Undefined();
97 }
98
CALLBACK_FUNC_DECL(ClipboardSetData)99 CALLBACK_FUNC_DECL(ClipboardSetData)
100 {
101 INC_STATS("DOM.Clipboard.setData()");
102 Clipboard* clipboard = V8DOMWrapper::convertToNativeObject<Clipboard>(V8ClassIndex::CLIPBOARD, args.Holder());
103
104 if (args.Length() != 2)
105 return throwError("setData: Invalid number of arguments", V8Proxy::SyntaxError);
106
107 String type = toWebCoreString(args[0]);
108 String data = toWebCoreString(args[1]);
109 return v8Boolean(clipboard->setData(type, data));
110 }
111
CALLBACK_FUNC_DECL(ClipboardSetDragImage)112 CALLBACK_FUNC_DECL(ClipboardSetDragImage)
113 {
114 INC_STATS("DOM.Clipboard.setDragImage()");
115 Clipboard* clipboard = V8DOMWrapper::convertToNativeObject<Clipboard>(V8ClassIndex::CLIPBOARD, args.Holder());
116
117 if (!clipboard->isForDragging())
118 return v8::Undefined();
119
120 if (args.Length() != 3)
121 return throwError("setDragImage: Invalid number of arguments", V8Proxy::SyntaxError);
122
123 int x = toInt32(args[1]);
124 int y = toInt32(args[2]);
125
126 Node* node = 0;
127 if (V8Node::HasInstance(args[0]))
128 node = V8DOMWrapper::convertDOMWrapperToNode<Node>(v8::Handle<v8::Object>::Cast(args[0]));
129
130 if (!node || !node->isElementNode())
131 return throwError("setDragImageFromElement: Invalid first argument");
132
133 if (static_cast<Element*>(node)->hasLocalName(HTMLNames::imgTag) && !node->inDocument())
134 clipboard->setDragImage(static_cast<HTMLImageElement*>(node)->cachedImage(), IntPoint(x, y));
135 else
136 clipboard->setDragImageElement(node, IntPoint(x, y));
137
138 return v8::Undefined();
139 }
140
141 } // namespace WebCore
142