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 "V8Clipboard.h"
33
34#include "Clipboard.h"
35#include "HTMLImageElement.h"
36#include "HTMLNames.h"
37#include "IntPoint.h"
38#include "Node.h"
39#include "Element.h"
40
41#include "V8Binding.h"
42#include "V8Node.h"
43#include "V8Proxy.h"
44
45namespace WebCore {
46
47v8::Handle<v8::Value> V8Clipboard::typesAccessorGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
48{
49    INC_STATS("DOM.Clipboard.types()");
50    Clipboard* clipboard = V8Clipboard::toNative(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
65v8::Handle<v8::Value> V8Clipboard::clearDataCallback(const v8::Arguments& args)
66{
67    INC_STATS("DOM.Clipboard.clearData()");
68    Clipboard* clipboard = V8Clipboard::toNative(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
83v8::Handle<v8::Value> V8Clipboard::getDataCallback(const v8::Arguments& args)
84{
85    INC_STATS("DOM.Clipboard.getData()");
86    Clipboard* clipboard = V8Clipboard::toNative(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
99v8::Handle<v8::Value> V8Clipboard::setDragImageCallback(const v8::Arguments& args)
100{
101    INC_STATS("DOM.Clipboard.setDragImage()");
102    Clipboard* clipboard = V8Clipboard::toNative(args.Holder());
103
104    if (!clipboard->isForDragAndDrop())
105        return v8::Undefined();
106
107    if (args.Length() != 3)
108        return throwError("setDragImage: Invalid number of arguments", V8Proxy::SyntaxError);
109
110    int x = toInt32(args[1]);
111    int y = toInt32(args[2]);
112
113    Node* node = 0;
114    if (V8Node::HasInstance(args[0]))
115        node = V8Node::toNative(v8::Handle<v8::Object>::Cast(args[0]));
116
117    if (!node || !node->isElementNode())
118        return throwError("setDragImageFromElement: Invalid first argument");
119
120    if (static_cast<Element*>(node)->hasLocalName(HTMLNames::imgTag) && !node->inDocument())
121        clipboard->setDragImage(static_cast<HTMLImageElement*>(node)->cachedImage(), IntPoint(x, y));
122    else
123        clipboard->setDragImageElement(node, IntPoint(x, y));
124
125    return v8::Undefined();
126}
127
128} // namespace WebCore
129