Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / core / v8 / V8NodeFilterCondition.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/core/v8/V8NodeFilterCondition.h"
33
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"
40
41 namespace blink {
42
43 V8NodeFilterCondition::V8NodeFilterCondition(v8::Handle<v8::Value> filter, v8::Handle<v8::Object> owner, ScriptState* scriptState)
44     : m_scriptState(scriptState)
45 {
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);
53     }
54 }
55
56 V8NodeFilterCondition::~V8NodeFilterCondition()
57 {
58 }
59
60 short V8NodeFilterCondition::acceptNode(Node* node, ExceptionState& exceptionState) const
61 {
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);
66
67     ASSERT(filter.IsEmpty() || filter->IsObject());
68     if (filter.IsEmpty())
69         return NodeFilter::FILTER_ACCEPT;
70
71     v8::TryCatch exceptionCatcher;
72
73     v8::Handle<v8::Function> callback;
74     if (filter->IsFunction()) {
75         callback = v8::Handle<v8::Function>::Cast(filter);
76     } else {
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;
81         }
82         callback = v8::Handle<v8::Function>::Cast(value);
83     }
84
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);
88
89     v8::Handle<v8::Value> result = ScriptController::callFunction(m_scriptState->executionContext(), callback, context, 1, info.get(), isolate);
90
91     if (exceptionCatcher.HasCaught()) {
92         exceptionState.rethrowV8Exception(exceptionCatcher.Exception());
93         return NodeFilter::FILTER_REJECT;
94     }
95
96     ASSERT(!result.IsEmpty());
97
98     return result->Int32Value();
99 }
100
101 void V8NodeFilterCondition::setWeakCallback(const v8::WeakCallbackData<v8::Value, V8NodeFilterCondition>& data)
102 {
103     data.GetParameter()->m_filter.clear();
104 }
105
106 } // namespace blink