1 /* 2 * Copyright (C) 1999 Lars Knoll (knoll (at) kde.org) 3 * (C) 1999 Antti Koivisto (koivisto (at) kde.org) 4 * (C) 2001 Dirk Mueller (mueller (at) kde.org) 5 * Copyright (C) 2004, 2005, 2006, 2010 Apple Inc. All rights reserved. 6 * (C) 2006 Alexey Proskuryakov (ap (at) nypop.com) 7 * 8 * This library is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU Library General Public 10 * License as published by the Free Software Foundation; either 11 * version 2 of the License, or (at your option) any later version. 12 * 13 * This library is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * Library General Public License for more details. 17 * 18 * You should have received a copy of the GNU Library General Public License 19 * along with this library; see the file COPYING.LIB. If not, write to 20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 21 * Boston, MA 02110-1301, USA. 22 * 23 */ 24 25 #include "config.h" 26 #include "core/html/HTMLKeygenElement.h" 27 28 #include "bindings/v8/ExceptionStatePlaceholder.h" 29 #include "core/HTMLNames.h" 30 #include "core/dom/Document.h" 31 #include "core/dom/Text.h" 32 #include "core/dom/shadow/ShadowRoot.h" 33 #include "core/html/FormDataList.h" 34 #include "core/html/HTMLOptionElement.h" 35 #include "core/html/HTMLSelectElement.h" 36 #include "platform/SSLKeyGenerator.h" 37 #include "wtf/StdLibExtras.h" 38 39 using namespace WebCore; 40 41 namespace WebCore { 42 43 using namespace HTMLNames; 44 45 HTMLKeygenElement::HTMLKeygenElement(Document& document, HTMLFormElement* form) 46 : HTMLFormControlElementWithState(keygenTag, document, form) 47 { 48 ScriptWrappable::init(this); 49 } 50 51 PassRefPtrWillBeRawPtr<HTMLKeygenElement> HTMLKeygenElement::create(Document& document, HTMLFormElement* form) 52 { 53 RefPtrWillBeRawPtr<HTMLKeygenElement> keygen = adoptRefWillBeNoop(new HTMLKeygenElement(document, form)); 54 keygen->ensureUserAgentShadowRoot(); 55 return keygen.release(); 56 } 57 58 void HTMLKeygenElement::didAddUserAgentShadowRoot(ShadowRoot& root) 59 { 60 DEFINE_STATIC_LOCAL(AtomicString, keygenSelectPseudoId, ("-webkit-keygen-select", AtomicString::ConstructFromLiteral)); 61 62 Vector<String> keys; 63 getSupportedKeySizes(locale(), keys); 64 65 // Create a select element with one option element for each key size. 66 RefPtrWillBeRawPtr<HTMLSelectElement> select = HTMLSelectElement::create(document()); 67 select->setShadowPseudoId(keygenSelectPseudoId); 68 for (size_t i = 0; i < keys.size(); ++i) { 69 RefPtrWillBeRawPtr<HTMLOptionElement> option = HTMLOptionElement::create(document()); 70 option->appendChild(Text::create(document(), keys[i])); 71 select->appendChild(option); 72 } 73 74 root.appendChild(select); 75 } 76 77 void HTMLKeygenElement::parseAttribute(const QualifiedName& name, const AtomicString& value) 78 { 79 // Reflect disabled attribute on the shadow select element 80 if (name == disabledAttr) 81 shadowSelect()->setAttribute(name, value); 82 83 HTMLFormControlElement::parseAttribute(name, value); 84 } 85 86 bool HTMLKeygenElement::appendFormData(FormDataList& encoding, bool) 87 { 88 // Only RSA is supported at this time. 89 const AtomicString& keyType = fastGetAttribute(keytypeAttr); 90 if (!keyType.isNull() && !equalIgnoringCase(keyType, "rsa")) 91 return false; 92 String value = signedPublicKeyAndChallengeString(shadowSelect()->selectedIndex(), fastGetAttribute(challengeAttr), document().baseURL()); 93 if (value.isNull()) 94 return false; 95 encoding.appendData(name(), value.utf8()); 96 return true; 97 } 98 99 const AtomicString& HTMLKeygenElement::formControlType() const 100 { 101 DEFINE_STATIC_LOCAL(const AtomicString, keygen, ("keygen", AtomicString::ConstructFromLiteral)); 102 return keygen; 103 } 104 105 void HTMLKeygenElement::resetImpl() 106 { 107 shadowSelect()->reset(); 108 } 109 110 HTMLSelectElement* HTMLKeygenElement::shadowSelect() const 111 { 112 ShadowRoot* root = userAgentShadowRoot(); 113 return root ? toHTMLSelectElement(root->firstChild()) : 0; 114 } 115 116 bool HTMLKeygenElement::isInteractiveContent() const 117 { 118 return true; 119 } 120 121 bool HTMLKeygenElement::supportsAutofocus() const 122 { 123 return true; 124 } 125 126 } // namespace 127