1 /*
   2  * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #undef IMPL
  27 
  28 #include "config.h"
  29 #include <wtf/RefPtr.h>
  30 
  31 #include <WebCore/Document.h>
  32 #include <WebCore/Element.h>
  33 #include <WebCore/Event.h>
  34 #include <WebCore/EventListener.h>
  35 #include <WebCore/EventTarget.h>
  36 #include <WebCore/NamedNodeMap.h>
  37 #include <WebCore/Node.h>
  38 #include <WebCore/NodeList.h>
  39 #include <WebCore/JSMainThreadExecState.h>
  40 #include <WebCore/SVGTests.h>
  41 #include <JavaScriptCore/APICast.h>
  42 
  43 #include "DOMException.h"
  44 #include "com_sun_webkit_dom_JSObject.h"
  45 #include "JavaDOMUtils.h"
  46 #include <wtf/java/JavaEnv.h>
  47 
  48 using namespace WebCore;
  49 
  50 extern "C" {
  51 
  52 #define IMPL (static_cast<Node*>(jlong_to_ptr(peer)))
  53 
  54 JNIEXPORT void JNICALL Java_com_sun_webkit_dom_NodeImpl_dispose(JNIEnv*, jclass, jlong peer) {
  55     IMPL->deref();
  56 }
  57 
  58 
  59 // Attributes
  60 JNIEXPORT jstring JNICALL Java_com_sun_webkit_dom_NodeImpl_getNodeNameImpl(JNIEnv* env, jclass, jlong peer) {
  61     WebCore::JSMainThreadNullState state;
  62     return JavaReturn<String>(env, IMPL->nodeName());
  63 }
  64 
  65 JNIEXPORT jstring JNICALL Java_com_sun_webkit_dom_NodeImpl_getNodeValueImpl(JNIEnv* env, jclass, jlong peer) {
  66     WebCore::JSMainThreadNullState state;
  67     return JavaReturn<String>(env, IMPL->nodeValue());
  68 }
  69 JNIEXPORT void JNICALL Java_com_sun_webkit_dom_NodeImpl_setNodeValueImpl(JNIEnv* env, jclass, jlong peer, jstring value) {
  70     WebCore::JSMainThreadNullState state;
  71     IMPL->setNodeValue(String(env, value));
  72 }
  73 
  74 JNIEXPORT jshort JNICALL Java_com_sun_webkit_dom_NodeImpl_getNodeTypeImpl(JNIEnv*, jclass, jlong peer) {
  75     WebCore::JSMainThreadNullState state;
  76     return IMPL->nodeType();
  77 }
  78 
  79 JNIEXPORT jlong JNICALL Java_com_sun_webkit_dom_NodeImpl_getParentNodeImpl(JNIEnv* env, jclass, jlong peer) {
  80     WebCore::JSMainThreadNullState state;
  81     return JavaReturn<Node>(env, WTF::getPtr(IMPL->parentNode()));
  82 }
  83 
  84 JNIEXPORT jlong JNICALL Java_com_sun_webkit_dom_NodeImpl_getChildNodesImpl(JNIEnv* env, jclass, jlong peer) {
  85     WebCore::JSMainThreadNullState state;
  86     return JavaReturn<NodeList>(env, WTF::getPtr(IMPL->childNodes()));
  87 }
  88 
  89 JNIEXPORT jlong JNICALL Java_com_sun_webkit_dom_NodeImpl_getFirstChildImpl(JNIEnv* env, jclass, jlong peer) {
  90     WebCore::JSMainThreadNullState state;
  91     return JavaReturn<Node>(env, WTF::getPtr(IMPL->firstChild()));
  92 }
  93 
  94 JNIEXPORT jlong JNICALL Java_com_sun_webkit_dom_NodeImpl_getLastChildImpl(JNIEnv* env, jclass, jlong peer) {
  95     WebCore::JSMainThreadNullState state;
  96     return JavaReturn<Node>(env, WTF::getPtr(IMPL->lastChild()));
  97 }
  98 
  99 JNIEXPORT jlong JNICALL Java_com_sun_webkit_dom_NodeImpl_getPreviousSiblingImpl(JNIEnv* env, jclass, jlong peer) {
 100     WebCore::JSMainThreadNullState state;
 101     return JavaReturn<Node>(env, WTF::getPtr(IMPL->previousSibling()));
 102 }
 103 
 104 JNIEXPORT jlong JNICALL Java_com_sun_webkit_dom_NodeImpl_getNextSiblingImpl(JNIEnv* env, jclass, jlong peer) {
 105     WebCore::JSMainThreadNullState state;
 106     return JavaReturn<Node>(env, WTF::getPtr(IMPL->nextSibling()));
 107 }
 108 
 109 JNIEXPORT jlong JNICALL Java_com_sun_webkit_dom_NodeImpl_getOwnerDocumentImpl(JNIEnv* env, jclass, jlong peer) {
 110     WebCore::JSMainThreadNullState state;
 111     return JavaReturn<Document>(env, WTF::getPtr(IMPL->ownerDocument()));
 112 }
 113 
 114 JNIEXPORT jstring JNICALL Java_com_sun_webkit_dom_NodeImpl_getNamespaceURIImpl(JNIEnv* env, jclass, jlong peer) {
 115     WebCore::JSMainThreadNullState state;
 116     return JavaReturn<String>(env, IMPL->namespaceURI());
 117 }
 118 
 119 JNIEXPORT jstring JNICALL Java_com_sun_webkit_dom_NodeImpl_getPrefixImpl(JNIEnv* env, jclass, jlong peer) {
 120     WebCore::JSMainThreadNullState state;
 121     return JavaReturn<String>(env, IMPL->prefix());
 122 }
 123 JNIEXPORT void JNICALL Java_com_sun_webkit_dom_NodeImpl_setPrefixImpl(JNIEnv* env, jclass, jlong peer, jstring value) {
 124     WebCore::JSMainThreadNullState state;
 125     IMPL->setPrefix(String(env, value));
 126 }
 127 
 128 JNIEXPORT jstring JNICALL Java_com_sun_webkit_dom_NodeImpl_getLocalNameImpl(JNIEnv* env, jclass, jlong peer) {
 129     WebCore::JSMainThreadNullState state;
 130     return JavaReturn<String>(env, IMPL->localName());
 131 }
 132 
 133 JNIEXPORT jlong JNICALL Java_com_sun_webkit_dom_NodeImpl_getAttributesImpl(JNIEnv* env, jclass, jlong peer) {
 134     WebCore::JSMainThreadNullState state;
 135     return JavaReturn<NamedNodeMap>(env, WTF::getPtr(IMPL->attributes()));
 136 }
 137 
 138 JNIEXPORT jstring JNICALL Java_com_sun_webkit_dom_NodeImpl_getBaseURIImpl(JNIEnv* env, jclass, jlong peer) {
 139     WebCore::JSMainThreadNullState state;
 140     return JavaReturn<String>(env, IMPL->baseURI());
 141 }
 142 
 143 JNIEXPORT jstring JNICALL Java_com_sun_webkit_dom_NodeImpl_getTextContentImpl(JNIEnv* env, jclass, jlong peer) {
 144     WebCore::JSMainThreadNullState state;
 145     return JavaReturn<String>(env, IMPL->textContent());
 146 }
 147 JNIEXPORT void JNICALL Java_com_sun_webkit_dom_NodeImpl_setTextContentImpl(JNIEnv* env, jclass, jlong peer, jstring value) {
 148     WebCore::JSMainThreadNullState state;
 149     IMPL->setTextContent(String(env, value));
 150 }
 151 
 152 JNIEXPORT jlong JNICALL Java_com_sun_webkit_dom_NodeImpl_getParentElementImpl(JNIEnv* env, jclass, jlong peer) {
 153     WebCore::JSMainThreadNullState state;
 154     return JavaReturn<Element>(env, WTF::getPtr(IMPL->parentElement()));
 155 }
 156 
 157 
 158 // Functions
 159 JNIEXPORT jlong JNICALL Java_com_sun_webkit_dom_NodeImpl_insertBeforeImpl(JNIEnv* env, jclass, jlong peer
 160     , jlong newChild
 161     , jlong refChild)
 162 {
 163     WebCore::JSMainThreadNullState state;
 164     if (!newChild) {
 165         raiseTypeErrorException(env);
 166         return 0;
 167     }
 168     Node* pnewChild = static_cast<Node*>(jlong_to_ptr(newChild));
 169     raiseOnDOMError(env, IMPL->insertBefore(*pnewChild, static_cast<Node*>(jlong_to_ptr(refChild))));
 170     return JavaReturn<Node>(env, pnewChild );
 171 }
 172 
 173 
 174 JNIEXPORT jlong JNICALL Java_com_sun_webkit_dom_NodeImpl_replaceChildImpl(JNIEnv* env, jclass, jlong peer
 175     , jlong newChild
 176     , jlong oldChild)
 177 {
 178     WebCore::JSMainThreadNullState state;
 179     if (!newChild) {
 180         raiseTypeErrorException(env);
 181         return 0;
 182     }
 183 
 184     if (!oldChild) {
 185         raiseTypeErrorException(env);
 186         return 0;
 187     }
 188 
 189     Node* poldChild = static_cast<Node*>(jlong_to_ptr(oldChild));
 190     raiseOnDOMError(env, IMPL->replaceChild(*static_cast<Node*>(jlong_to_ptr(newChild)), *poldChild));
 191     return JavaReturn<Node>(env, poldChild);
 192 }
 193 
 194 
 195 JNIEXPORT jlong JNICALL Java_com_sun_webkit_dom_NodeImpl_removeChildImpl(JNIEnv* env, jclass, jlong peer
 196     , jlong oldChild)
 197 {
 198     WebCore::JSMainThreadNullState state;
 199     if (!oldChild) {
 200         raiseTypeErrorException(env);
 201         return 0;
 202     }
 203     Node* poldChild = static_cast<Node*>(jlong_to_ptr(oldChild));
 204     raiseOnDOMError(env, IMPL->removeChild(*poldChild));
 205     return JavaReturn<Node>(env, poldChild);
 206 }
 207 
 208 
 209 JNIEXPORT jlong JNICALL Java_com_sun_webkit_dom_NodeImpl_appendChildImpl(JNIEnv* env, jclass, jlong peer
 210     , jlong newChild)
 211 {
 212     WebCore::JSMainThreadNullState state;
 213     if (!newChild) {
 214         raiseTypeErrorException(env);
 215         return 0;
 216     }
 217     Node* pnewChild = static_cast<Node*>(jlong_to_ptr(newChild));
 218     raiseOnDOMError(env, IMPL->appendChild(*pnewChild));
 219     return JavaReturn<Node>(env, pnewChild);
 220 }
 221 
 222 
 223 JNIEXPORT jboolean JNICALL Java_com_sun_webkit_dom_NodeImpl_hasChildNodesImpl(JNIEnv*, jclass, jlong peer)
 224 {
 225     WebCore::JSMainThreadNullState state;
 226     return IMPL->hasChildNodes();
 227 }
 228 
 229 
 230 JNIEXPORT jlong JNICALL Java_com_sun_webkit_dom_NodeImpl_cloneNodeImpl(JNIEnv* env, jclass, jlong peer
 231     , jboolean deep)
 232 {
 233     WebCore::JSMainThreadNullState state;
 234     return JavaReturn<Node>(env, WTF::getPtr(raiseOnDOMError(env, IMPL->cloneNodeForBindings(deep))));
 235 }
 236 
 237 
 238 JNIEXPORT void JNICALL Java_com_sun_webkit_dom_NodeImpl_normalizeImpl(JNIEnv*, jclass, jlong peer)
 239 {
 240     WebCore::JSMainThreadNullState state;
 241     IMPL->normalize();
 242 }
 243 
 244 
 245 JNIEXPORT jboolean JNICALL Java_com_sun_webkit_dom_NodeImpl_isSupportedImpl(JNIEnv* env, jclass, jlong
 246     , jstring feature
 247     , jstring version)
 248 {
 249     WebCore::JSMainThreadNullState state;
 250     return SVGTests::hasFeatureForLegacyBindings(String(env, feature), String(env, version));
 251 }
 252 
 253 
 254 JNIEXPORT jboolean JNICALL Java_com_sun_webkit_dom_NodeImpl_hasAttributesImpl(JNIEnv*, jclass, jlong peer)
 255 {
 256     WebCore::JSMainThreadNullState state;
 257     return IMPL->hasAttributes();
 258 }
 259 
 260 
 261 JNIEXPORT jboolean JNICALL Java_com_sun_webkit_dom_NodeImpl_isSameNodeImpl(JNIEnv*, jclass, jlong peer
 262     , jlong other)
 263 {
 264     WebCore::JSMainThreadNullState state;
 265     return IMPL->isSameNode(static_cast<Node*>(jlong_to_ptr(other)));
 266 }
 267 
 268 
 269 JNIEXPORT jboolean JNICALL Java_com_sun_webkit_dom_NodeImpl_isEqualNodeImpl(JNIEnv*, jclass, jlong peer
 270     , jlong other)
 271 {
 272     WebCore::JSMainThreadNullState state;
 273     return IMPL->isEqualNode(static_cast<Node*>(jlong_to_ptr(other)));
 274 }
 275 
 276 
 277 JNIEXPORT jstring JNICALL Java_com_sun_webkit_dom_NodeImpl_lookupPrefixImpl(JNIEnv* env, jclass, jlong peer
 278     , jstring namespaceURI)
 279 {
 280     WebCore::JSMainThreadNullState state;
 281     return JavaReturn<String>(env, IMPL->lookupPrefix(String(env, namespaceURI)));
 282 }
 283 
 284 
 285 JNIEXPORT jboolean JNICALL Java_com_sun_webkit_dom_NodeImpl_isDefaultNamespaceImpl(JNIEnv* env, jclass, jlong peer
 286     , jstring namespaceURI)
 287 {
 288     WebCore::JSMainThreadNullState state;
 289     return IMPL->isDefaultNamespace(String(env, namespaceURI));
 290 }
 291 
 292 
 293 JNIEXPORT jstring JNICALL Java_com_sun_webkit_dom_NodeImpl_lookupNamespaceURIImpl(JNIEnv* env, jclass, jlong peer
 294     , jstring prefix)
 295 {
 296     WebCore::JSMainThreadNullState state;
 297     return JavaReturn<String>(env, IMPL->lookupNamespaceURI(String(env, prefix)));
 298 }
 299 
 300 
 301 JNIEXPORT jshort JNICALL Java_com_sun_webkit_dom_NodeImpl_compareDocumentPositionImpl(JNIEnv*, jclass, jlong peer
 302     , jlong other)
 303 {
 304     WebCore::JSMainThreadNullState state;
 305     if (!other)
 306         return Node::DOCUMENT_POSITION_DISCONNECTED;
 307     return IMPL->compareDocumentPosition(*static_cast<Node*>(jlong_to_ptr(other)));
 308 }
 309 
 310 
 311 JNIEXPORT jboolean JNICALL Java_com_sun_webkit_dom_NodeImpl_containsImpl(JNIEnv*, jclass, jlong peer
 312     , jlong other)
 313 {
 314     WebCore::JSMainThreadNullState state;
 315     return IMPL->contains(static_cast<Node*>(jlong_to_ptr(other)));
 316 }
 317 
 318 
 319 JNIEXPORT void JNICALL Java_com_sun_webkit_dom_NodeImpl_addEventListenerImpl(JNIEnv* env, jclass, jlong peer
 320     , jstring type
 321     , jlong listener
 322     , jboolean useCapture)
 323 {
 324     WebCore::JSMainThreadNullState state;
 325     IMPL->addEventListenerForBindings(String(env, type)
 326             , static_cast<EventListener*>(jlong_to_ptr(listener))
 327             , static_cast<bool>(useCapture));
 328 }
 329 
 330 
 331 JNIEXPORT void JNICALL Java_com_sun_webkit_dom_NodeImpl_removeEventListenerImpl(JNIEnv* env, jclass, jlong peer
 332     , jstring type
 333     , jlong listener
 334     , jboolean useCapture)
 335 {
 336     WebCore::JSMainThreadNullState state;
 337     IMPL->removeEventListenerForBindings(String(env, type)
 338             , static_cast<EventListener*>(jlong_to_ptr(listener))
 339             , static_cast<bool>(useCapture));
 340 }
 341 
 342 
 343 JNIEXPORT jboolean JNICALL Java_com_sun_webkit_dom_NodeImpl_dispatchEventImpl(JNIEnv* env, jclass, jlong peer
 344     , jlong event)
 345 {
 346     WebCore::JSMainThreadNullState state;
 347     if (!event) {
 348         raiseTypeErrorException(env);
 349         return JNI_FALSE;
 350     }
 351 
 352     return raiseOnDOMError(env, IMPL->dispatchEventForBindings(*static_cast<Event*>(jlong_to_ptr(event))));
 353 }
 354 
 355 
 356 }