Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / v8 / V8Utilities.cpp
1 /*
2  * Copyright (C) 2008, 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 "bindings/v8/V8Utilities.h"
33
34 #include "V8MessagePort.h"
35 #include "bindings/v8/ExceptionMessages.h"
36 #include "bindings/v8/ScriptState.h"
37 #include "bindings/v8/V8AbstractEventListener.h"
38 #include "bindings/v8/V8Binding.h"
39 #include "bindings/v8/custom/V8ArrayBufferCustom.h"
40 #include "core/dom/Document.h"
41 #include "core/dom/ExceptionCode.h"
42 #include "core/dom/ExecutionContext.h"
43 #include "core/dom/MessagePort.h"
44 #include "core/frame/Frame.h"
45 #include "core/workers/WorkerGlobalScope.h"
46 #include "wtf/ArrayBuffer.h"
47 #include "wtf/text/WTFString.h"
48 #include <v8.h>
49
50
51 namespace WebCore {
52
53 bool extractTransferables(v8::Local<v8::Value> value, int argumentIndex, MessagePortArray& ports, ArrayBufferArray& arrayBuffers, ExceptionState& exceptionState, v8::Isolate* isolate)
54 {
55     if (isUndefinedOrNull(value)) {
56         ports.resize(0);
57         arrayBuffers.resize(0);
58         return true;
59     }
60
61     uint32_t length = 0;
62     if (value->IsArray()) {
63         v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(value);
64         length = array->Length();
65     } else if (toV8Sequence(value, length, isolate).IsEmpty()) {
66         exceptionState.throwTypeError(ExceptionMessages::notAnArrayTypeArgumentOrValue(argumentIndex + 1));
67         return false;
68     }
69
70     v8::Local<v8::Object> transferrables = v8::Local<v8::Object>::Cast(value);
71
72     // Validate the passed array of transferrables.
73     for (unsigned int i = 0; i < length; ++i) {
74         v8::Local<v8::Value> transferrable = transferrables->Get(i);
75         // Validation of non-null objects, per HTML5 spec 10.3.3.
76         if (isUndefinedOrNull(transferrable)) {
77             exceptionState.throwDOMException(DataCloneError, "Value at index " + String::number(i) + " is an untransferable " + (transferrable->IsUndefined() ? "'undefined'" : "'null'") + " value.");
78             return false;
79         }
80         // Validation of Objects implementing an interface, per WebIDL spec 4.1.15.
81         if (V8MessagePort::hasInstance(transferrable, isolate)) {
82             RefPtr<MessagePort> port = V8MessagePort::toNative(v8::Handle<v8::Object>::Cast(transferrable));
83             // Check for duplicate MessagePorts.
84             if (ports.contains(port)) {
85                 exceptionState.throwDOMException(DataCloneError, "Message port at index " + String::number(i) + " is a duplicate of an earlier port.");
86                 return false;
87             }
88             ports.append(port.release());
89         } else if (V8ArrayBuffer::hasInstance(transferrable, isolate)) {
90             RefPtr<ArrayBuffer> arrayBuffer = V8ArrayBuffer::toNative(v8::Handle<v8::Object>::Cast(transferrable));
91             if (arrayBuffers.contains(arrayBuffer)) {
92                 exceptionState.throwDOMException(DataCloneError, "ArrayBuffer at index " + String::number(i) + " is a duplicate of an earlier ArrayBuffer.");
93                 return false;
94             }
95             arrayBuffers.append(arrayBuffer.release());
96         } else {
97             exceptionState.throwDOMException(DataCloneError, "Value at index " + String::number(i) + " does not have a transferable type.");
98             return false;
99         }
100     }
101     return true;
102 }
103
104 } // namespace WebCore