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/core/v8/V8NodeFilterCondition.h"
34 #include "bindings/core/v8/ScriptController.h"
35 #include "bindings/core/v8/V8HiddenValue.h"
36 #include "bindings/core/v8/V8Node.h"
37 #include "core/dom/Node.h"
38 #include "core/dom/NodeFilter.h"
39 #include "wtf/OwnPtr.h"
43 V8NodeFilterCondition::V8NodeFilterCondition(v8::Handle<v8::Value> filter, v8::Handle<v8::Object> owner, ScriptState* scriptState)
44 : m_scriptState(scriptState)
46 // ..acceptNode(..) will only dispatch m_filter if m_filter->IsObject().
47 // We'll make sure m_filter is either usable by acceptNode or empty.
48 // (See the fast/dom/node-filter-gc test for a case where 'empty' happens.)
49 if (!filter.IsEmpty() && filter->IsObject()) {
50 V8HiddenValue::setHiddenValue(scriptState->isolate(), owner, V8HiddenValue::condition(scriptState->isolate()), filter);
51 m_filter.set(scriptState->isolate(), filter);
52 m_filter.setWeak(this, &setWeakCallback);
56 V8NodeFilterCondition::~V8NodeFilterCondition()
60 short V8NodeFilterCondition::acceptNode(Node* node, ExceptionState& exceptionState) const
62 v8::Isolate* isolate = m_scriptState->isolate();
63 ASSERT(!m_scriptState->context().IsEmpty());
64 v8::HandleScope handleScope(isolate);
65 v8::Handle<v8::Value> filter = m_filter.newLocal(isolate);
67 ASSERT(filter.IsEmpty() || filter->IsObject());
69 return NodeFilter::FILTER_ACCEPT;
71 v8::TryCatch exceptionCatcher;
73 v8::Handle<v8::Function> callback;
74 if (filter->IsFunction()) {
75 callback = v8::Handle<v8::Function>::Cast(filter);
77 v8::Local<v8::Value> value = filter->ToObject()->Get(v8AtomicString(isolate, "acceptNode"));
78 if (value.IsEmpty() || !value->IsFunction()) {
79 exceptionState.throwTypeError("NodeFilter object does not have an acceptNode function");
80 return NodeFilter::FILTER_REJECT;
82 callback = v8::Handle<v8::Function>::Cast(value);
85 OwnPtr<v8::Handle<v8::Value>[]> info = adoptArrayPtr(new v8::Handle<v8::Value>[1]);
86 v8::Handle<v8::Object> context = m_scriptState->context()->Global();
87 info[0] = toV8(node, context, isolate);
89 v8::Handle<v8::Value> result = ScriptController::callFunction(m_scriptState->executionContext(), callback, context, 1, info.get(), isolate);
91 if (exceptionCatcher.HasCaught()) {
92 exceptionState.rethrowV8Exception(exceptionCatcher.Exception());
93 return NodeFilter::FILTER_REJECT;
96 ASSERT(!result.IsEmpty());
98 return result->Int32Value();
101 void V8NodeFilterCondition::setWeakCallback(const v8::WeakCallbackData<v8::Value, V8NodeFilterCondition>& data)
103 data.GetParameter()->m_filter.clear();