2 * Copyright (C) 2008, 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "bindings/v8/V8Utilities.h"
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"
53 // Use an array to hold dependents. It works like a ref-counted scheme.
54 // A value can be added more than once to the DOM object.
55 void createHiddenDependency(v8::Handle<v8::Object> object, v8::Local<v8::Value> value, int cacheIndex, v8::Isolate* isolate)
57 v8::Local<v8::Value> cache = object->GetInternalField(cacheIndex);
58 if (cache->IsNull() || cache->IsUndefined()) {
59 cache = v8::Array::New(isolate);
60 object->SetInternalField(cacheIndex, cache);
63 v8::Local<v8::Array> cacheArray = v8::Local<v8::Array>::Cast(cache);
64 cacheArray->Set(v8::Integer::New(isolate, cacheArray->Length()), value);
67 bool extractTransferables(v8::Local<v8::Value> value, int argumentIndex, MessagePortArray& ports, ArrayBufferArray& arrayBuffers, ExceptionState& exceptionState, v8::Isolate* isolate)
69 if (isUndefinedOrNull(value)) {
71 arrayBuffers.resize(0);
76 if (value->IsArray()) {
77 v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(value);
78 length = array->Length();
79 } else if (toV8Sequence(value, length, isolate).IsEmpty()) {
80 exceptionState.throwTypeError(ExceptionMessages::notAnArrayTypeArgumentOrValue(argumentIndex + 1));
84 v8::Local<v8::Object> transferrables = v8::Local<v8::Object>::Cast(value);
86 // Validate the passed array of transferrables.
87 for (unsigned int i = 0; i < length; ++i) {
88 v8::Local<v8::Value> transferrable = transferrables->Get(i);
89 // Validation of non-null objects, per HTML5 spec 10.3.3.
90 if (isUndefinedOrNull(transferrable)) {
91 exceptionState.throwDOMException(DataCloneError, "Value at index " + String::number(i) + " is an untransferable " + (transferrable->IsUndefined() ? "'undefined'" : "'null'") + " value.");
94 // Validation of Objects implementing an interface, per WebIDL spec 4.1.15.
95 if (V8MessagePort::hasInstance(transferrable, isolate)) {
96 RefPtr<MessagePort> port = V8MessagePort::toNative(v8::Handle<v8::Object>::Cast(transferrable));
97 // Check for duplicate MessagePorts.
98 if (ports.contains(port)) {
99 exceptionState.throwDOMException(DataCloneError, "Message port at index " + String::number(i) + " is a duplicate of an earlier port.");
102 ports.append(port.release());
103 } else if (V8ArrayBuffer::hasInstance(transferrable, isolate)) {
104 RefPtr<ArrayBuffer> arrayBuffer = V8ArrayBuffer::toNative(v8::Handle<v8::Object>::Cast(transferrable));
105 if (arrayBuffers.contains(arrayBuffer)) {
106 exceptionState.throwDOMException(DataCloneError, "ArrayBuffer at index " + String::number(i) + " is a duplicate of an earlier ArrayBuffer.");
109 arrayBuffers.append(arrayBuffer.release());
111 exceptionState.throwDOMException(DataCloneError, "Value at index " + String::number(i) + " does not have a transferable type.");
118 bool getMessagePortArray(v8::Local<v8::Value> value, const String& propertyName, MessagePortArray& ports, v8::Isolate* isolate)
120 if (isUndefinedOrNull(value)) {
124 bool success = false;
125 ports = toRefPtrNativeArray<MessagePort, V8MessagePort>(value, propertyName, isolate, &success);
129 bool getMessagePortArray(v8::Local<v8::Value> value, int argumentIndex, MessagePortArray& ports, v8::Isolate* isolate)
131 if (isUndefinedOrNull(value)) {
135 bool success = false;
136 ports = toRefPtrNativeArray<MessagePort, V8MessagePort>(value, argumentIndex, isolate, &success);
140 void removeHiddenDependency(v8::Handle<v8::Object> object, v8::Local<v8::Value> value, int cacheIndex, v8::Isolate* isolate)
142 v8::Local<v8::Value> cache = object->GetInternalField(cacheIndex);
143 if (!cache->IsArray())
145 v8::Local<v8::Array> cacheArray = v8::Local<v8::Array>::Cast(cache);
146 for (int i = cacheArray->Length() - 1; i >= 0; --i) {
147 v8::Local<v8::Value> cached = cacheArray->Get(v8::Integer::New(isolate, i));
148 if (cached->StrictEquals(value)) {
149 cacheArray->Delete(i);
155 void transferHiddenDependency(v8::Handle<v8::Object> object, EventListener* oldValue, v8::Local<v8::Value> newValue, int cacheIndex, v8::Isolate* isolate)
158 V8AbstractEventListener* oldListener = V8AbstractEventListener::cast(oldValue);
160 v8::Local<v8::Object> oldListenerObject = oldListener->getExistingListenerObject();
161 if (!oldListenerObject.IsEmpty())
162 removeHiddenDependency(object, oldListenerObject, cacheIndex, isolate);
165 // Non-callable input is treated as null and ignored
166 if (newValue->IsFunction())
167 createHiddenDependency(object, newValue, cacheIndex, isolate);
170 ExecutionContext* getExecutionContext()
172 if (WorkerScriptController* controller = WorkerScriptController::controllerForContext())
173 return &controller->workerGlobalScope();
175 return currentDocument();
178 v8::Handle<v8::Function> getBoundFunction(v8::Handle<v8::Function> function)
180 v8::Handle<v8::Value> boundFunction = function->GetBoundFunction();
181 if (boundFunction->IsFunction()) {
182 return v8::Handle<v8::Function>::Cast(boundFunction);
188 } // namespace WebCore