Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / 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 "core/html/HTMLKeygenElement.h"
27
28 #include "bindings/core/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/text/PlatformLocale.h"
37 #include "public/platform/Platform.h"
38 #include "public/platform/WebLocalizedString.h"
39 #include "wtf/StdLibExtras.h"
40
41 using namespace blink;
42
43 namespace blink {
44
45 using namespace HTMLNames;
46
47 HTMLKeygenElement::HTMLKeygenElement(Document& document, HTMLFormElement* form)
48     : HTMLFormControlElementWithState(keygenTag, document, form)
49 {
50 }
51
52 PassRefPtrWillBeRawPtr<HTMLKeygenElement> HTMLKeygenElement::create(Document& document, HTMLFormElement* form)
53 {
54     RefPtrWillBeRawPtr<HTMLKeygenElement> keygen = adoptRefWillBeNoop(new HTMLKeygenElement(document, form));
55     keygen->ensureUserAgentShadowRoot();
56     return keygen.release();
57 }
58
59 void HTMLKeygenElement::didAddUserAgentShadowRoot(ShadowRoot& root)
60 {
61     DEFINE_STATIC_LOCAL(AtomicString, keygenSelectPseudoId, ("-webkit-keygen-select", AtomicString::ConstructFromLiteral));
62
63     Vector<String> keys;
64     keys.reserveCapacity(2);
65     keys.append(locale().queryString(WebLocalizedString::KeygenMenuHighGradeKeySize));
66     keys.append(locale().queryString(WebLocalizedString::KeygenMenuMediumGradeKeySize));
67
68     // Create a select element with one option element for each key size.
69     RefPtrWillBeRawPtr<HTMLSelectElement> select = HTMLSelectElement::create(document());
70     select->setShadowPseudoId(keygenSelectPseudoId);
71     for (const String& key : keys) {
72         RefPtrWillBeRawPtr<HTMLOptionElement> option = HTMLOptionElement::create(document());
73         option->appendChild(Text::create(document(), key));
74         select->appendChild(option);
75     }
76
77     root.appendChild(select);
78 }
79
80 void HTMLKeygenElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
81 {
82     // Reflect disabled attribute on the shadow select element
83     if (name == disabledAttr)
84         shadowSelect()->setAttribute(name, value);
85
86     HTMLFormControlElement::parseAttribute(name, value);
87 }
88
89 bool HTMLKeygenElement::appendFormData(FormDataList& encoding, bool)
90 {
91     // Only RSA is supported at this time.
92     const AtomicString& keyType = fastGetAttribute(keytypeAttr);
93     if (!keyType.isNull() && !equalIgnoringCase(keyType, "rsa"))
94         return false;
95     String value = Platform::current()->signedPublicKeyAndChallengeString(shadowSelect()->selectedIndex(), fastGetAttribute(challengeAttr), document().baseURL());
96     if (value.isNull())
97         return false;
98     encoding.appendData(name(), value.utf8());
99     return true;
100 }
101
102 const AtomicString& HTMLKeygenElement::formControlType() const
103 {
104     DEFINE_STATIC_LOCAL(const AtomicString, keygen, ("keygen", AtomicString::ConstructFromLiteral));
105     return keygen;
106 }
107
108 void HTMLKeygenElement::resetImpl()
109 {
110     shadowSelect()->reset();
111 }
112
113 HTMLSelectElement* HTMLKeygenElement::shadowSelect() const
114 {
115     ShadowRoot* root = userAgentShadowRoot();
116     return root ? toHTMLSelectElement(root->firstChild()) : 0;
117 }
118
119 bool HTMLKeygenElement::isInteractiveContent() const
120 {
121     return true;
122 }
123
124 bool HTMLKeygenElement::supportsAutofocus() const
125 {
126     return true;
127 }
128
129 } // namespace