1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
/*
* Copyright 2010, The Android Open Source Project
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "WebAutoFill.h"
#if ENABLE(WEB_AUTOFILL)
#include "AutoFillHostAndroid.h"
#include "Frame.h"
#include "FormData.h"
#include "FormManagerAndroid.h"
#include "FrameLoader.h"
#include "HTMLFormControlElement.h"
#include "MainThreadProxy.h"
#include "Node.h"
#include "Page.h"
#include "Settings.h"
#include "WebFrame.h"
#include "WebRequestContext.h"
#include "WebUrlLoaderClient.h"
#include "WebViewCore.h"
#define NO_PROFILE_SET 0
#define FORM_NOT_AUTOFILLABLE -1
namespace android
{
WebAutoFill::WebAutoFill()
: mQueryId(1)
, mWebViewCore(0)
, mLastSearchDomVersion(0)
, mParsingForms(false)
{
mTabContents = new TabContents();
setEmptyProfile();
}
void WebAutoFill::init()
{
if (mAutoFillManager)
return;
mFormManager = new FormManager();
// We use the WebView's WebRequestContext, which may be a private browsing context.
ASSERT(mWebViewCore);
mAutoFillManager = new AutoFillManager(mTabContents.get());
mAutoFillHost = new AutoFillHostAndroid(this);
mTabContents->SetProfileRequestContext(new AndroidURLRequestContextGetter(mWebViewCore->webRequestContext(), WebUrlLoaderClient::ioThread()));
mTabContents->SetAutoFillHost(mAutoFillHost.get());
}
WebAutoFill::~WebAutoFill()
{
cleanUpQueryMap();
mUniqueIdMap.clear();
}
void WebAutoFill::cleanUpQueryMap()
{
for (AutoFillQueryFormDataMap::iterator it = mQueryMap.begin(); it != mQueryMap.end(); it++)
delete it->second;
mQueryMap.clear();
}
void WebAutoFill::searchDocument(WebCore::Frame* frame)
{
if (!enabled())
return;
MutexLocker lock(mFormsSeenMutex);
init();
cleanUpQueryMap();
mUniqueIdMap.clear();
mForms.clear();
mQueryId = 1;
ASSERT(mFormManager);
ASSERT(mAutoFillManager);
mAutoFillManager->Reset();
mFormManager->Reset();
mFormManager->ExtractForms(frame);
mFormManager->GetFormsInFrame(frame, FormManager::REQUIRE_AUTOCOMPLETE, &mForms);
// Needs to be done on a Chrome thread as it will make a URL request to the AutoFill server.
// TODO: Use our own Autofill thread instead of the IO thread.
// TODO: For now, block here. Would like to make this properly async.
base::Thread* thread = WebUrlLoaderClient::ioThread();
mParsingForms = true;
thread->message_loop()->PostTask(FROM_HERE, NewRunnableMethod(this, &WebAutoFill::formsSeenImpl));
while (mParsingForms)
mFormsSeenCondition.wait(mFormsSeenMutex);
}
// Called on the Chromium IO thread.
void WebAutoFill::formsSeenImpl()
{
MutexLocker lock(mFormsSeenMutex);
mAutoFillManager->FormsSeen(mForms);
mParsingForms = false;
mFormsSeenCondition.signal();
}
void WebAutoFill::formFieldFocused(WebCore::HTMLFormControlElement* formFieldElement)
{
ASSERT(formFieldElement);
Document* doc = formFieldElement->document();
Frame* frame = doc->frame();
// FIXME: AutoFill only works in main frame for now. Should consider
// child frames.
if (frame != frame->page()->mainFrame())
return;
unsigned domVersion = doc->domTreeVersion();
ASSERT(domVersion > 0);
if (mLastSearchDomVersion != domVersion) {
// Need to extract forms as DOM version has changed since the last time
// we searched.
searchDocument(formFieldElement->document()->frame());
mLastSearchDomVersion = domVersion;
}
if (!enabled()) {
// In case that we've just been disabled and the last time we got autofill
// suggestions and told Java about them, clear that bit Java side now
// we're disabled.
mWebViewCore->setWebTextViewAutoFillable(FORM_NOT_AUTOFILLABLE, string16());
return;
}
// Get the FormField from the Node.
webkit_glue::FormField* formField = new webkit_glue::FormField;
FormManager::HTMLFormControlElementToFormField(formFieldElement, FormManager::EXTRACT_NONE, formField);
formField->set_label(FormManager::LabelForElement(*formFieldElement));
webkit_glue::FormData* form = new webkit_glue::FormData;
mFormManager->FindFormWithFormControlElement(formFieldElement, FormManager::REQUIRE_AUTOCOMPLETE, form);
mQueryMap[mQueryId] = new FormDataAndField(form, formField);
bool suggestions = mAutoFillManager->GetAutoFillSuggestions(*form, *formField);
mQueryId++;
if (!suggestions) {
ASSERT(mWebViewCore);
// Tell Java no autofill suggestions for this form.
mWebViewCore->setWebTextViewAutoFillable(FORM_NOT_AUTOFILLABLE, string16());
return;
}
}
void WebAutoFill::querySuccessful(const string16& value, const string16& label, int uniqueId)
{
if (!enabled())
return;
// Store the unique ID for the query and inform java that autofill suggestions for this form are available.
// Pass java the queryId so that it can pass it back if the user decides to use autofill.
mUniqueIdMap[mQueryId] = uniqueId;
ASSERT(mWebViewCore);
mWebViewCore->setWebTextViewAutoFillable(mQueryId, mAutoFillProfile->Label());
}
void WebAutoFill::fillFormFields(int queryId)
{
if (!enabled())
return;
webkit_glue::FormData* form = mQueryMap[queryId]->form();
webkit_glue::FormField* field = mQueryMap[queryId]->field();
ASSERT(form);
ASSERT(field);
AutoFillQueryToUniqueIdMap::iterator iter = mUniqueIdMap.find(queryId);
if (iter == mUniqueIdMap.end()) {
// The user has most likely tried to AutoFill the form again without
// refocussing the form field. The UI should protect against this
// but stop here to be certain.
return;
}
mAutoFillManager->FillAutoFillFormData(queryId, *form, *field, iter->second);
mUniqueIdMap.erase(iter);
}
void WebAutoFill::fillFormInPage(int queryId, const webkit_glue::FormData& form)
{
if (!enabled())
return;
// FIXME: Pass a pointer to the Node that triggered the AutoFill flow here instead of 0.
// The consquence of passing 0 is that we should always fail the test in FormManader::ForEachMathcingFormField():169
// that says "only overwrite an elements current value if the user triggered autofill through that element"
// for elements that have a value already. But by a quirk of Android text views we are OK. We should still
// fix this though.
mFormManager->FillForm(form, 0);
}
bool WebAutoFill::enabled() const
{
Page* page = mWebViewCore->mainFrame()->page();
return page ? page->settings()->autoFillEnabled() : false;
}
void WebAutoFill::setProfile(const string16& fullName, const string16& emailAddress, const string16& companyName,
const string16& addressLine1, const string16& addressLine2, const string16& city,
const string16& state, const string16& zipCode, const string16& country, const string16& phoneNumber)
{
if (!mAutoFillProfile)
mAutoFillProfile.set(new AutoFillProfile());
// Update the profile.
// Constants for AutoFill field types are found in external/chromium/chrome/browser/autofill/field_types.h.
mAutoFillProfile->SetInfo(AutoFillType(NAME_FULL), fullName);
mAutoFillProfile->SetInfo(AutoFillType(EMAIL_ADDRESS), emailAddress);
mAutoFillProfile->SetInfo(AutoFillType(COMPANY_NAME), companyName);
mAutoFillProfile->SetInfo(AutoFillType(ADDRESS_HOME_LINE1), addressLine1);
mAutoFillProfile->SetInfo(AutoFillType(ADDRESS_HOME_LINE2), addressLine2);
mAutoFillProfile->SetInfo(AutoFillType(ADDRESS_HOME_CITY), city);
mAutoFillProfile->SetInfo(AutoFillType(ADDRESS_HOME_STATE), state);
mAutoFillProfile->SetInfo(AutoFillType(ADDRESS_HOME_ZIP), zipCode);
mAutoFillProfile->SetInfo(AutoFillType(ADDRESS_HOME_COUNTRY), country);
mAutoFillProfile->SetInfo(AutoFillType(PHONE_HOME_WHOLE_NUMBER), phoneNumber);
std::vector<AutoFillProfile> profiles;
profiles.push_back(*mAutoFillProfile);
updateProfileLabel();
mTabContents->profile()->GetPersonalDataManager()->SetProfiles(&profiles);
}
bool WebAutoFill::updateProfileLabel()
{
std::vector<AutoFillProfile*> profiles;
profiles.push_back(mAutoFillProfile.get());
return AutoFillProfile::AdjustInferredLabels(&profiles);
}
void WebAutoFill::clearProfiles()
{
if (!mAutoFillProfile)
return;
// For now Chromium only ever knows about one profile, so we can just
// remove it. If we support multiple profiles in the future
// we need to remove them all here.
std::string profileGuid = mAutoFillProfile->guid();
mTabContents->profile()->GetPersonalDataManager()->RemoveProfile(profileGuid);
setEmptyProfile();
}
void WebAutoFill::setEmptyProfile()
{
// Set an empty profile. This will ensure that when autofill is enabled,
// we will still search the document for autofillable forms and inform
// java of their presence so we can invite the user to set up
// their own profile.
// Chromium code will strip the values sent into the profile so we need them to be
// at least one non-whitespace character long. We need to set all fields of the
// profile to a non-empty string so that any field type can trigger the autofill
// suggestion. AutoFill will not detect form fields if the profile value for that
// field is an empty string.
static const string16 empty = string16(ASCIIToUTF16("a"));
setProfile(empty, empty, empty, empty, empty, empty, empty, empty, empty, empty);
}
}
#endif
|