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 "ElementShadow.h"
31#include "FormDataList.h"
32#include "HTMLNames.h"
33#include "HTMLSelectElement.h"
34#include "HTMLOptionElement.h"
35#include "SSLKeyGenerator.h"
36#include "ShadowRoot.h"
37#include "Text.h"
38#include <wtf/StdLibExtras.h>
39
40using namespace WebCore;
41
42namespace WebCore {
43
44using namespace HTMLNames;
45
46class KeygenSelectElement FINAL : public HTMLSelectElement {
47public:
48    static PassRefPtr<KeygenSelectElement> create(Document* document)
49    {
50        return adoptRef(new KeygenSelectElement(document));
51    }
52
53protected:
54    KeygenSelectElement(Document* document)
55        : HTMLSelectElement(selectTag, document, 0)
56    {
57        DEFINE_STATIC_LOCAL(AtomicString, pseudoId, ("-webkit-keygen-select", AtomicString::ConstructFromLiteral));
58        setPseudo(pseudoId);
59    }
60
61private:
62    virtual PassRefPtr<Element> cloneElementWithoutAttributesAndChildren()
63    {
64        return create(document());
65    }
66};
67
68inline HTMLKeygenElement::HTMLKeygenElement(const QualifiedName& tagName, Document* document, HTMLFormElement* form)
69    : HTMLFormControlElementWithState(tagName, document, form)
70{
71    ASSERT(hasTagName(keygenTag));
72
73    // Create a select element with one option element for each key size.
74    Vector<String> keys;
75    getSupportedKeySizes(keys);
76
77    RefPtr<HTMLSelectElement> select = KeygenSelectElement::create(document);
78    for (size_t i = 0; i < keys.size(); ++i) {
79        RefPtr<HTMLOptionElement> option = HTMLOptionElement::create(document);
80        select->appendChild(option, IGNORE_EXCEPTION);
81        option->appendChild(Text::create(document, keys[i]), IGNORE_EXCEPTION);
82    }
83
84    ensureUserAgentShadowRoot()->appendChild(select, IGNORE_EXCEPTION);
85}
86
87PassRefPtr<HTMLKeygenElement> HTMLKeygenElement::create(const QualifiedName& tagName, Document* document, HTMLFormElement* form)
88{
89    return adoptRef(new HTMLKeygenElement(tagName, document, form));
90}
91
92void HTMLKeygenElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
93{
94    // Reflect disabled attribute on the shadow select element
95    if (name == disabledAttr)
96        shadowSelect()->setAttribute(name, value);
97
98    HTMLFormControlElement::parseAttribute(name, value);
99}
100
101bool HTMLKeygenElement::appendFormData(FormDataList& encoded_values, bool)
102{
103    // Only RSA is supported at this time.
104    const AtomicString& keyType = fastGetAttribute(keytypeAttr);
105    if (!keyType.isNull() && !equalIgnoringCase(keyType, "rsa"))
106        return false;
107    String value = signedPublicKeyAndChallengeString(shadowSelect()->selectedIndex(), fastGetAttribute(challengeAttr), document()->baseURL());
108    if (value.isNull())
109        return false;
110    encoded_values.appendData(name(), value.utf8());
111    return true;
112}
113
114const AtomicString& HTMLKeygenElement::formControlType() const
115{
116    DEFINE_STATIC_LOCAL(const AtomicString, keygen, ("keygen", AtomicString::ConstructFromLiteral));
117    return keygen;
118}
119
120void HTMLKeygenElement::reset()
121{
122    static_cast<HTMLFormControlElement*>(shadowSelect())->reset();
123}
124
125bool HTMLKeygenElement::shouldSaveAndRestoreFormControlState() const
126{
127    return false;
128}
129
130HTMLSelectElement* HTMLKeygenElement::shadowSelect() const
131{
132    ShadowRoot* root = userAgentShadowRoot();
133    return root ? toHTMLSelectElement(root->firstChild()) : 0;
134}
135
136} // namespace
137