1 /*
   2  * Copyright (c) 2013, 2017, 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 
  30 #include "DOMException.h"
  31 #include <WebCore/HTMLCollection.h>
  32 #include <WebCore/HTMLElement.h>
  33 #include <WebCore/HTMLFormElement.h>
  34 #include <WebCore/HTMLNames.h>
  35 #include <WebCore/HTMLOptGroupElement.h>
  36 #include <WebCore/HTMLOptionsCollection.h>
  37 #include <WebCore/HTMLSelectElement.h>
  38 #include <WebCore/JSMainThreadExecState.h>
  39 #include <WebCore/NameNodeList.h>
  40 #include <WebCore/Node.h>
  41 #include <WebCore/NodeList.h>
  42 #include <WebCore/ThreadCheck.h>
  43 #include <WebCore/URL.h>
  44 
  45 #include <wtf/RefPtr.h>
  46 #include <wtf/GetPtr.h>
  47 
  48 #include "JavaDOMUtils.h"
  49 #include <wtf/java/JavaEnv.h>
  50 
  51 using namespace WebCore;
  52 
  53 extern "C" {
  54 
  55 #define IMPL (static_cast<HTMLSelectElement*>(jlong_to_ptr(peer)))
  56 
  57 // Attributes
  58 JNIEXPORT jboolean JNICALL Java_com_sun_webkit_dom_HTMLSelectElementImpl_getAutofocusImpl(JNIEnv*, jclass, jlong peer)
  59 {
  60     WebCore::JSMainThreadNullState state;
  61     return IMPL->hasAttribute(WebCore::HTMLNames::autofocusAttr);
  62 }
  63 
  64 JNIEXPORT void JNICALL Java_com_sun_webkit_dom_HTMLSelectElementImpl_setAutofocusImpl(JNIEnv*, jclass, jlong peer, jboolean value)
  65 {
  66     WebCore::JSMainThreadNullState state;
  67     IMPL->setBooleanAttribute(WebCore::HTMLNames::autofocusAttr, value);
  68 }
  69 
  70 JNIEXPORT jboolean JNICALL Java_com_sun_webkit_dom_HTMLSelectElementImpl_getDisabledImpl(JNIEnv*, jclass, jlong peer)
  71 {
  72     WebCore::JSMainThreadNullState state;
  73     return IMPL->hasAttribute(WebCore::HTMLNames::disabledAttr);
  74 }
  75 
  76 JNIEXPORT void JNICALL Java_com_sun_webkit_dom_HTMLSelectElementImpl_setDisabledImpl(JNIEnv*, jclass, jlong peer, jboolean value)
  77 {
  78     WebCore::JSMainThreadNullState state;
  79     IMPL->setBooleanAttribute(WebCore::HTMLNames::disabledAttr, value);
  80 }
  81 
  82 JNIEXPORT jlong JNICALL Java_com_sun_webkit_dom_HTMLSelectElementImpl_getFormImpl(JNIEnv* env, jclass, jlong peer)
  83 {
  84     WebCore::JSMainThreadNullState state;
  85     return JavaReturn<HTMLFormElement>(env, WTF::getPtr(IMPL->form()));
  86 }
  87 
  88 JNIEXPORT jboolean JNICALL Java_com_sun_webkit_dom_HTMLSelectElementImpl_getMultipleImpl(JNIEnv*, jclass, jlong peer)
  89 {
  90     WebCore::JSMainThreadNullState state;
  91     return IMPL->multiple();
  92 }
  93 
  94 JNIEXPORT void JNICALL Java_com_sun_webkit_dom_HTMLSelectElementImpl_setMultipleImpl(JNIEnv*, jclass, jlong peer, jboolean value)
  95 {
  96     WebCore::JSMainThreadNullState state;
  97     IMPL->setMultiple(value);
  98 }
  99 
 100 JNIEXPORT jstring JNICALL Java_com_sun_webkit_dom_HTMLSelectElementImpl_getNameImpl(JNIEnv* env, jclass, jlong peer)
 101 {
 102     WebCore::JSMainThreadNullState state;
 103     return JavaReturn<String>(env, IMPL->getNameAttribute());
 104 }
 105 
 106 JNIEXPORT void JNICALL Java_com_sun_webkit_dom_HTMLSelectElementImpl_setNameImpl(JNIEnv* env, jclass, jlong peer, jstring value)
 107 {
 108     WebCore::JSMainThreadNullState state;
 109     IMPL->setAttributeWithoutSynchronization(WebCore::HTMLNames::nameAttr, String(env, value));
 110 }
 111 
 112 JNIEXPORT jboolean JNICALL Java_com_sun_webkit_dom_HTMLSelectElementImpl_getRequiredImpl(JNIEnv*, jclass, jlong peer)
 113 {
 114     WebCore::JSMainThreadNullState state;
 115     return IMPL->hasAttribute(WebCore::HTMLNames::requiredAttr);
 116 }
 117 
 118 JNIEXPORT void JNICALL Java_com_sun_webkit_dom_HTMLSelectElementImpl_setRequiredImpl(JNIEnv*, jclass, jlong peer, jboolean value)
 119 {
 120     WebCore::JSMainThreadNullState state;
 121     IMPL->setBooleanAttribute(WebCore::HTMLNames::requiredAttr, value);
 122 }
 123 
 124 JNIEXPORT jint JNICALL Java_com_sun_webkit_dom_HTMLSelectElementImpl_getSizeImpl(JNIEnv*, jclass, jlong peer)
 125 {
 126     WebCore::JSMainThreadNullState state;
 127     return IMPL->size();
 128 }
 129 
 130 JNIEXPORT void JNICALL Java_com_sun_webkit_dom_HTMLSelectElementImpl_setSizeImpl(JNIEnv*, jclass, jlong peer, jint value)
 131 {
 132     WebCore::JSMainThreadNullState state;
 133     IMPL->setSize(value);
 134 }
 135 
 136 JNIEXPORT jstring JNICALL Java_com_sun_webkit_dom_HTMLSelectElementImpl_getTypeImpl(JNIEnv* env, jclass, jlong peer)
 137 {
 138     WebCore::JSMainThreadNullState state;
 139     return JavaReturn<String>(env, IMPL->type());
 140 }
 141 
 142 JNIEXPORT jlong JNICALL Java_com_sun_webkit_dom_HTMLSelectElementImpl_getOptionsImpl(JNIEnv* env, jclass, jlong peer)
 143 {
 144     WebCore::JSMainThreadNullState state;
 145     return JavaReturn<HTMLOptionsCollection>(env, WTF::getPtr(IMPL->options()));
 146 }
 147 
 148 JNIEXPORT jint JNICALL Java_com_sun_webkit_dom_HTMLSelectElementImpl_getLengthImpl(JNIEnv*, jclass, jlong peer)
 149 {
 150     WebCore::JSMainThreadNullState state;
 151     return IMPL->length();
 152 }
 153 
 154 JNIEXPORT jlong JNICALL Java_com_sun_webkit_dom_HTMLSelectElementImpl_getSelectedOptionsImpl(JNIEnv* env, jclass, jlong peer)
 155 {
 156     WebCore::JSMainThreadNullState state;
 157     return JavaReturn<HTMLCollection>(env, WTF::getPtr(IMPL->selectedOptions()));
 158 }
 159 
 160 JNIEXPORT jint JNICALL Java_com_sun_webkit_dom_HTMLSelectElementImpl_getSelectedIndexImpl(JNIEnv*, jclass, jlong peer)
 161 {
 162     WebCore::JSMainThreadNullState state;
 163     return IMPL->selectedIndex();
 164 }
 165 
 166 JNIEXPORT void JNICALL Java_com_sun_webkit_dom_HTMLSelectElementImpl_setSelectedIndexImpl(JNIEnv*, jclass, jlong peer, jint value)
 167 {
 168     WebCore::JSMainThreadNullState state;
 169     IMPL->setSelectedIndex(value);
 170 }
 171 
 172 JNIEXPORT jstring JNICALL Java_com_sun_webkit_dom_HTMLSelectElementImpl_getValueImpl(JNIEnv* env, jclass, jlong peer)
 173 {
 174     WebCore::JSMainThreadNullState state;
 175     return JavaReturn<String>(env, IMPL->value());
 176 }
 177 
 178 JNIEXPORT void JNICALL Java_com_sun_webkit_dom_HTMLSelectElementImpl_setValueImpl(JNIEnv* env, jclass, jlong peer, jstring value)
 179 {
 180     WebCore::JSMainThreadNullState state;
 181     IMPL->setValue(String(env, value));
 182 }
 183 
 184 JNIEXPORT jboolean JNICALL Java_com_sun_webkit_dom_HTMLSelectElementImpl_getWillValidateImpl(JNIEnv*, jclass, jlong peer)
 185 {
 186     WebCore::JSMainThreadNullState state;
 187     return IMPL->willValidate();
 188 }
 189 
 190 JNIEXPORT jstring JNICALL Java_com_sun_webkit_dom_HTMLSelectElementImpl_getValidationMessageImpl(JNIEnv* env, jclass, jlong peer)
 191 {
 192     WebCore::JSMainThreadNullState state;
 193     return JavaReturn<String>(env, IMPL->validationMessage());
 194 }
 195 
 196 JNIEXPORT jlong JNICALL Java_com_sun_webkit_dom_HTMLSelectElementImpl_getLabelsImpl(JNIEnv* env, jclass, jlong peer)
 197 {
 198     WebCore::JSMainThreadNullState state;
 199     return JavaReturn<NodeList>(env, WTF::getPtr(IMPL->labels()));
 200 }
 201 
 202 JNIEXPORT jstring JNICALL Java_com_sun_webkit_dom_HTMLSelectElementImpl_getAutocompleteImpl(JNIEnv* env, jclass, jlong peer)
 203 {
 204     WebCore::JSMainThreadNullState state;
 205     return JavaReturn<String>(env, IMPL->autocomplete());
 206 }
 207 
 208 JNIEXPORT void JNICALL Java_com_sun_webkit_dom_HTMLSelectElementImpl_setAutocompleteImpl(JNIEnv* env, jclass, jlong peer, jstring value)
 209 {
 210     WebCore::JSMainThreadNullState state;
 211     IMPL->setAutocomplete(String(env, value));
 212 }
 213 
 214 
 215 // Functions
 216 JNIEXPORT jlong JNICALL Java_com_sun_webkit_dom_HTMLSelectElementImpl_itemImpl(JNIEnv* env, jclass, jlong peer
 217     , jint index)
 218 {
 219     WebCore::JSMainThreadNullState state;
 220     return JavaReturn<Node>(env, WTF::getPtr(IMPL->item(index)));
 221 }
 222 
 223 
 224 JNIEXPORT jlong JNICALL Java_com_sun_webkit_dom_HTMLSelectElementImpl_namedItemImpl(JNIEnv* env, jclass, jlong peer
 225     , jstring name)
 226 {
 227     WebCore::JSMainThreadNullState state;
 228     return JavaReturn<Node>(env, WTF::getPtr(IMPL->namedItem(String(env, name))));
 229 }
 230 
 231 
 232 JNIEXPORT void JNICALL Java_com_sun_webkit_dom_HTMLSelectElementImpl_addImpl(JNIEnv* env, jclass, jlong peer
 233     , jlong element
 234     , jlong before)
 235 {
 236     WebCore::JSMainThreadNullState state;
 237     if (!element) {
 238         raiseTypeErrorException(env);
 239         return;
 240     }
 241 
 242     auto& coreElement = *static_cast<HTMLElement*>(jlong_to_ptr(element));
 243     Variant<RefPtr<WebCore::HTMLOptionElement>, RefPtr<WebCore::HTMLOptGroupElement>> variantElement;
 244     if (is<WebCore::HTMLOptionElement>(coreElement))
 245         variantElement = &downcast<WebCore::HTMLOptionElement>(coreElement);
 246     else if (is<WebCore::HTMLOptGroupElement>(coreElement))
 247         variantElement = &downcast<WebCore::HTMLOptGroupElement>(coreElement);
 248     else {
 249         raiseTypeErrorException(env);
 250         return;
 251     }
 252     raiseOnDOMError(env, IMPL->add(WTFMove(variantElement), WebCore::HTMLSelectElement::HTMLElementOrInt(static_cast<HTMLElement*>(jlong_to_ptr(before)))));
 253 }
 254 
 255 
 256 JNIEXPORT void JNICALL Java_com_sun_webkit_dom_HTMLSelectElementImpl_removeImpl(JNIEnv*, jclass, jlong peer
 257     , jint index)
 258 {
 259     WebCore::JSMainThreadNullState state;
 260     IMPL->remove(index);
 261 }
 262 
 263 
 264 JNIEXPORT jboolean JNICALL Java_com_sun_webkit_dom_HTMLSelectElementImpl_checkValidityImpl(JNIEnv*, jclass, jlong peer)
 265 {
 266     WebCore::JSMainThreadNullState state;
 267     return IMPL->checkValidity();
 268 }
 269 
 270 
 271 JNIEXPORT void JNICALL Java_com_sun_webkit_dom_HTMLSelectElementImpl_setCustomValidityImpl(JNIEnv* env, jclass, jlong peer
 272     , jstring error)
 273 {
 274     WebCore::JSMainThreadNullState state;
 275     IMPL->setCustomValidity(String(env, error));
 276 }
 277 
 278 
 279 }