This source file includes following definitions.
- locateSpecificPasswords
- stripURL
- getElementNameOrId
- assemblePasswordFormResult
#include "config.h"
#include "WebPasswordFormData.h"
#include "HTMLNames.h"
#include "WebPasswordFormUtils.h"
#include "core/dom/Document.h"
#include "core/html/HTMLFormElement.h"
#include "core/html/HTMLInputElement.h"
#include "platform/weborigin/KURL.h"
using namespace WebCore;
namespace blink {
namespace {
bool locateSpecificPasswords(PasswordFormFields* fields,
HTMLInputElement** password,
HTMLInputElement** oldPassword)
{
ASSERT(fields);
ASSERT(password);
ASSERT(oldPassword);
switch (fields->passwords.size()) {
case 1:
*password = fields->passwords[0];
break;
case 2:
if (fields->passwords[0]->value() == fields->passwords[1]->value())
*password = fields->passwords[0];
else {
*oldPassword = fields->passwords[0];
*password = fields->passwords[1];
}
break;
case 3:
if (fields->passwords[0]->value() == fields->passwords[1]->value()
&& fields->passwords[0]->value() == fields->passwords[2]->value()) {
*password = fields->passwords[0];
} else if (fields->passwords[0]->value() == fields->passwords[1]->value()) {
*oldPassword = fields->passwords[0];
*password = fields->passwords[2];
} else if (fields->passwords[1]->value() == fields->passwords[2]->value()) {
*oldPassword = fields->passwords[0];
*password = fields->passwords[1];
} else {
return false;
}
break;
default:
return false;
}
return true;
}
KURL stripURL(const KURL& url)
{
KURL strippedURL = url;
strippedURL.setUser(String());
strippedURL.setPass(String());
strippedURL.setQuery(String());
strippedURL.setFragmentIdentifier(String());
return strippedURL;
}
WebString getElementNameOrId(const HTMLInputElement& element)
{
return element.nameForAutofill();
}
void assemblePasswordFormResult(const KURL& fullOrigin,
const KURL& fullAction,
HTMLFormControlElement* submit,
HTMLInputElement* userName,
const Vector<String>& alternateUserNames,
HTMLInputElement* oldPassword,
HTMLInputElement* password,
WebPasswordFormData* result)
{
result->action = stripURL(fullAction);
result->origin = stripURL(fullOrigin);
KURL signonRealmURL = stripURL(fullOrigin);
signonRealmURL.setPath("");
result->signonRealm = signonRealmURL;
result->possibleUserNames = alternateUserNames;
if (submit)
result->submitElement = submit->name();
if (userName) {
result->userNameElement = getElementNameOrId(*userName);
result->userNameValue = userName->value();
}
if (password) {
result->passwordElement = getElementNameOrId(*password);
result->passwordValue = password->value();
result->passwordShouldAutocomplete = password->shouldAutocomplete();
}
if (oldPassword) {
result->oldPasswordElement = getElementNameOrId(*oldPassword);
result->oldPasswordValue = oldPassword->value();
}
}
}
WebPasswordFormData::WebPasswordFormData(const WebFormElement& webForm)
{
RefPtr<HTMLFormElement> form = webForm.operator PassRefPtr<HTMLFormElement>();
PasswordFormFields fields;
findPasswordFormFields(form.get(), &fields);
KURL fullOrigin = form->document().url();
String action = form->action();
if (action.isNull())
action = "";
KURL fullAction = form->document().completeURL(action);
if (!fullAction.isValid())
return;
HTMLInputElement* password = 0;
HTMLInputElement* oldPassword = 0;
if (!locateSpecificPasswords(&fields, &password, &oldPassword))
return;
assemblePasswordFormResult(fullOrigin, fullAction,
fields.submit, fields.userName,
fields.alternateUserNames,
oldPassword, password, this);
}
}