tizen beta release
[profile/ivi/webkit-efl.git] / Source / WebCore / html / HTMLKeygenElement.cpp
1 /*
2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3  *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4  *           (C) 2001 Dirk Mueller (mueller@kde.org)
5  * Copyright (C) 2004, 2005, 2006, 2010 Apple Inc. All rights reserved.
6  *           (C) 2006 Alexey Proskuryakov (ap@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 "HTMLKeygenElement.h"
27
28 #include "Attribute.h"
29 #include "Document.h"
30 #include "FormDataList.h"
31 #include "HTMLNames.h"
32 #include "HTMLSelectElement.h"
33 #include "HTMLOptionElement.h"
34 #include "SSLKeyGenerator.h"
35 #include "ShadowRoot.h"
36 #include "Text.h"
37 #include <wtf/StdLibExtras.h>
38
39 using namespace WebCore;
40
41 namespace WebCore {
42
43 using namespace HTMLNames;
44
45 class KeygenSelectElement : public HTMLSelectElement {
46 public:
47     static PassRefPtr<KeygenSelectElement> create(Document* document)
48     {
49         return adoptRef(new KeygenSelectElement(document));
50     }
51
52     virtual const AtomicString& shadowPseudoId() const
53     {
54         DEFINE_STATIC_LOCAL(AtomicString, pseudoId, ("-webkit-keygen-select"));
55         return pseudoId;
56     }
57
58 protected:
59     KeygenSelectElement(Document* document)
60         : HTMLSelectElement(selectTag, document, 0)
61     {
62     }
63
64 private:
65     virtual PassRefPtr<Element> cloneElementWithoutAttributesAndChildren()
66     {
67         return create(document());
68     }
69 };
70
71 inline HTMLKeygenElement::HTMLKeygenElement(const QualifiedName& tagName, Document* document, HTMLFormElement* form)
72     : HTMLFormControlElementWithState(tagName, document, form)
73 {
74     ASSERT(hasTagName(keygenTag));
75
76     // Create a select element with one option element for each key size.
77     Vector<String> keys;
78     getSupportedKeySizes(keys);
79
80     RefPtr<HTMLSelectElement> select = KeygenSelectElement::create(document);
81     ExceptionCode ec = 0;
82     for (size_t i = 0; i < keys.size(); ++i) {
83         RefPtr<HTMLOptionElement> option = HTMLOptionElement::create(document, this->form());
84         select->appendChild(option, ec);
85         option->appendChild(Text::create(document, keys[i]), ec);
86     }
87
88     ensureShadowRoot()->appendChild(select, ec);
89 }
90
91 PassRefPtr<HTMLKeygenElement> HTMLKeygenElement::create(const QualifiedName& tagName, Document* document, HTMLFormElement* form)
92 {
93     return adoptRef(new HTMLKeygenElement(tagName, document, form));
94 }
95
96 void HTMLKeygenElement::parseMappedAttribute(Attribute* attr)
97 {
98     // Reflect disabled attribute on the shadow select element
99     if (attr->name() == disabledAttr)
100         shadowSelect()->setAttribute(attr->name(), attr->value());
101
102     HTMLFormControlElement::parseMappedAttribute(attr);
103 }
104
105 bool HTMLKeygenElement::appendFormData(FormDataList& encoded_values, bool)
106 {
107     // Only RSA is supported at this time.
108     const AtomicString& keyType = fastGetAttribute(keytypeAttr);
109     if (!keyType.isNull() && !equalIgnoringCase(keyType, "rsa"))
110         return false;
111     String value = signedPublicKeyAndChallengeString(shadowSelect()->selectedIndex(), fastGetAttribute(challengeAttr), document()->baseURL());
112     if (value.isNull())
113         return false;
114     encoded_values.appendData(name(), value.utf8());
115     return true;
116 }
117
118 const AtomicString& HTMLKeygenElement::formControlType() const
119 {
120     DEFINE_STATIC_LOCAL(const AtomicString, keygen, ("keygen"));
121     return keygen;
122 }
123
124 void HTMLKeygenElement::reset()
125 {
126     static_cast<HTMLFormControlElement*>(shadowSelect())->reset();
127 }
128
129 HTMLSelectElement* HTMLKeygenElement::shadowSelect() const
130 {
131     ShadowRoot* shadow = shadowRoot();
132     ASSERT(shadow);
133     return shadow ? toHTMLSelectElement(shadow->firstChild()) : 0;
134 }
135
136 } // namespace