2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "WebPasswordFormData.h"
35 #include "DocumentLoader.h"
37 #include "FrameLoader.h"
38 #include "HTMLFormElement.h"
39 #include "HTMLInputElement.h"
40 #include "HTMLNames.h"
43 #include "DOMUtilitiesPrivate.h"
44 #include "WebPasswordFormUtils.h"
46 using namespace WebCore;
52 // Helper to determine which password is the main one, and which is
53 // an old password (e.g on a "make new password" form), if any.
54 bool locateSpecificPasswords(PasswordFormFields* fields,
55 HTMLInputElement** password,
56 HTMLInputElement** oldPassword)
61 switch (fields->passwords.size()) {
63 // Single password, easy.
64 *password = fields->passwords[0];
67 if (fields->passwords[0]->value() == fields->passwords[1]->value())
68 // Treat two identical passwords as a single password.
69 *password = fields->passwords[0];
71 // Assume first is old password, second is new (no choice but to guess).
72 *oldPassword = fields->passwords[0];
73 *password = fields->passwords[1];
77 if (fields->passwords[0]->value() == fields->passwords[1]->value()
78 && fields->passwords[0]->value() == fields->passwords[2]->value()) {
79 // All three passwords the same? Just treat as one and hope.
80 *password = fields->passwords[0];
81 } else if (fields->passwords[0]->value() == fields->passwords[1]->value()) {
82 // Two the same and one different -> old password is duplicated one.
83 *oldPassword = fields->passwords[0];
84 *password = fields->passwords[2];
85 } else if (fields->passwords[1]->value() == fields->passwords[2]->value()) {
86 *oldPassword = fields->passwords[0];
87 *password = fields->passwords[1];
89 // Three different passwords, or first and last match with middle
90 // different. No idea which is which, so no luck.
100 // Helped method to clear url of unneeded parts.
101 KURL stripURL(const KURL& url)
103 KURL strippedURL = url;
104 strippedURL.setUser(String());
105 strippedURL.setPass(String());
106 strippedURL.setQuery(String());
107 strippedURL.setFragmentIdentifier(String());
111 // Helper to gather up the final form data and create a PasswordForm.
112 void assemblePasswordFormResult(const KURL& fullOrigin,
113 const KURL& fullAction,
114 HTMLFormControlElement* submit,
115 HTMLInputElement* userName,
116 HTMLInputElement* oldPassword,
117 HTMLInputElement* password,
118 WebPasswordFormData* result)
120 // We want to keep the path but strip any authentication data, as well as
121 // query and ref portions of URL, for the form action and form origin.
122 result->action = stripURL(fullAction);
123 result->origin = stripURL(fullOrigin);
125 // Naming is confusing here because we have both the HTML form origin URL
126 // the page where the form was seen), and the "origin" components of the url
127 // (scheme, host, and port).
128 KURL signonRealmURL = stripURL(fullOrigin);
129 signonRealmURL.setPath("");
130 result->signonRealm = signonRealmURL;
133 result->submitElement = submit->name();
135 result->userNameElement = userName->name();
136 result->userNameValue = userName->value();
139 result->passwordElement = password->name();
140 result->passwordValue = password->value();
143 result->oldPasswordElement = oldPassword->name();
144 result->oldPasswordValue = oldPassword->value();
150 WebPasswordFormData::WebPasswordFormData(const WebFormElement& webForm)
152 RefPtr<HTMLFormElement> form = webForm.operator PassRefPtr<HTMLFormElement>();
153 PasswordFormFields fields;
154 findPasswordFormFields(form.get(), &fields);
156 // Get the document URL
157 KURL fullOrigin(ParsedURLString, form->document()->documentURI());
159 // Calculate the canonical action URL
160 String action = form->action();
162 action = ""; // missing 'action' attribute implies current URL
163 KURL fullAction = form->document()->completeURL(action);
164 if (!fullAction.isValid())
167 // Determine the types of the password fields
168 HTMLInputElement* password = 0;
169 HTMLInputElement* oldPassword = 0;
170 if (!locateSpecificPasswords(&fields, &password, &oldPassword))
173 assemblePasswordFormResult(fullOrigin, fullAction,
174 fields.submit, fields.userName,
175 oldPassword, password, this);
178 } // namespace WebKit