Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "components/autofill/core/browser/autofill_manager.h" |
| 6 | |
| 7 | #include <stddef.h> |
| 8 | |
| 9 | #include <limits> |
| 10 | #include <map> |
| 11 | #include <set> |
| 12 | #include <utility> |
| 13 | |
| 14 | #include "base/bind.h" |
| 15 | #include "base/command_line.h" |
| 16 | #include "base/guid.h" |
| 17 | #include "base/logging.h" |
| 18 | #include "base/prefs/pref_service.h" |
| 19 | #include "base/strings/string16.h" |
| 20 | #include "base/strings/string_util.h" |
| 21 | #include "base/strings/utf_string_conversions.h" |
| 22 | #include "base/threading/sequenced_worker_pool.h" |
| 23 | #include "components/autofill/content/browser/autocheckout/whitelist_manager.h" |
| 24 | #include "components/autofill/content/browser/autocheckout_manager.h" |
| 25 | #include "components/autofill/core/browser/autocomplete_history_manager.h" |
| 26 | #include "components/autofill/core/browser/autofill_data_model.h" |
| 27 | #include "components/autofill/core/browser/autofill_driver.h" |
| 28 | #include "components/autofill/core/browser/autofill_external_delegate.h" |
| 29 | #include "components/autofill/core/browser/autofill_field.h" |
| 30 | #include "components/autofill/core/browser/autofill_manager_delegate.h" |
| 31 | #include "components/autofill/core/browser/autofill_manager_test_delegate.h" |
| 32 | #include "components/autofill/core/browser/autofill_metrics.h" |
| 33 | #include "components/autofill/core/browser/autofill_profile.h" |
| 34 | #include "components/autofill/core/browser/autofill_type.h" |
| 35 | #include "components/autofill/core/browser/credit_card.h" |
| 36 | #include "components/autofill/core/browser/form_structure.h" |
| 37 | #include "components/autofill/core/browser/personal_data_manager.h" |
| 38 | #include "components/autofill/core/browser/phone_number.h" |
| 39 | #include "components/autofill/core/browser/phone_number_i18n.h" |
| 40 | #include "components/autofill/core/common/autofill_messages.h" |
| 41 | #include "components/autofill/core/common/autofill_pref_names.h" |
| 42 | #include "components/autofill/core/common/autofill_switches.h" |
| 43 | #include "components/autofill/core/common/form_data.h" |
| 44 | #include "components/autofill/core/common/form_data_predictions.h" |
| 45 | #include "components/autofill/core/common/form_field_data.h" |
| 46 | #include "components/autofill/core/common/password_form_fill_data.h" |
| 47 | #include "components/user_prefs/pref_registry_syncable.h" |
| 48 | #include "content/public/browser/browser_context.h" |
| 49 | #include "content/public/browser/browser_thread.h" |
| 50 | #include "content/public/browser/render_view_host.h" |
| 51 | #include "content/public/browser/web_contents.h" |
| 52 | #include "content/public/browser/web_contents_view.h" |
| 53 | #include "content/public/common/url_constants.h" |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 54 | #include "grit/component_strings.h" |
| 55 | #include "third_party/WebKit/public/web/WebAutofillClient.h" |
| 56 | #include "ui/base/l10n/l10n_util.h" |
| 57 | #include "ui/gfx/rect.h" |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 58 | #include "url/gurl.h" |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 59 | |
| 60 | namespace autofill { |
| 61 | |
| 62 | typedef PersonalDataManager::GUIDPair GUIDPair; |
| 63 | |
| 64 | using base::TimeTicks; |
| 65 | using content::BrowserThread; |
| 66 | using content::RenderViewHost; |
| 67 | using WebKit::WebFormElement; |
| 68 | |
| 69 | namespace { |
| 70 | |
| 71 | // We only send a fraction of the forms to upload server. |
| 72 | // The rate for positive/negative matches potentially could be different. |
| 73 | const double kAutofillPositiveUploadRateDefaultValue = 0.20; |
| 74 | const double kAutofillNegativeUploadRateDefaultValue = 0.20; |
| 75 | |
| 76 | const size_t kMaxRecentFormSignaturesToRemember = 3; |
| 77 | |
| 78 | // Set a conservative upper bound on the number of forms we are willing to |
| 79 | // cache, simply to prevent unbounded memory consumption. |
| 80 | const size_t kMaxFormCacheSize = 100; |
| 81 | |
| 82 | // Removes duplicate suggestions whilst preserving their original order. |
| 83 | void RemoveDuplicateSuggestions(std::vector<base::string16>* values, |
| 84 | std::vector<base::string16>* labels, |
| 85 | std::vector<base::string16>* icons, |
| 86 | std::vector<int>* unique_ids) { |
| 87 | DCHECK_EQ(values->size(), labels->size()); |
| 88 | DCHECK_EQ(values->size(), icons->size()); |
| 89 | DCHECK_EQ(values->size(), unique_ids->size()); |
| 90 | |
| 91 | std::set<std::pair<base::string16, base::string16> > seen_suggestions; |
| 92 | std::vector<base::string16> values_copy; |
| 93 | std::vector<base::string16> labels_copy; |
| 94 | std::vector<base::string16> icons_copy; |
| 95 | std::vector<int> unique_ids_copy; |
| 96 | |
| 97 | for (size_t i = 0; i < values->size(); ++i) { |
| 98 | const std::pair<base::string16, base::string16> suggestion( |
| 99 | (*values)[i], (*labels)[i]); |
| 100 | if (seen_suggestions.insert(suggestion).second) { |
| 101 | values_copy.push_back((*values)[i]); |
| 102 | labels_copy.push_back((*labels)[i]); |
| 103 | icons_copy.push_back((*icons)[i]); |
| 104 | unique_ids_copy.push_back((*unique_ids)[i]); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | values->swap(values_copy); |
| 109 | labels->swap(labels_copy); |
| 110 | icons->swap(icons_copy); |
| 111 | unique_ids->swap(unique_ids_copy); |
| 112 | } |
| 113 | |
| 114 | // Precondition: |form_structure| and |form| should correspond to the same |
| 115 | // logical form. Returns true if any field in the given |section| within |form| |
| 116 | // is auto-filled. |
| 117 | bool SectionIsAutofilled(const FormStructure& form_structure, |
| 118 | const FormData& form, |
| 119 | const std::string& section) { |
| 120 | DCHECK_EQ(form_structure.field_count(), form.fields.size()); |
| 121 | for (size_t i = 0; i < form_structure.field_count(); ++i) { |
| 122 | if (form_structure.field(i)->section() == section && |
| 123 | form.fields[i].is_autofilled) { |
| 124 | return true; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | bool FormIsHTTPS(const FormStructure& form) { |
| 132 | return form.source_url().SchemeIs(chrome::kHttpsScheme); |
| 133 | } |
| 134 | |
| 135 | // Uses the existing personal data in |profiles| and |credit_cards| to determine |
| 136 | // possible field types for the |submitted_form|. This is potentially |
| 137 | // expensive -- on the order of 50ms even for a small set of |stored_data|. |
| 138 | // Hence, it should not run on the UI thread -- to avoid locking up the UI -- |
| 139 | // nor on the IO thread -- to avoid blocking IPC calls. |
| 140 | void DeterminePossibleFieldTypesForUpload( |
| 141 | const std::vector<AutofillProfile>& profiles, |
| 142 | const std::vector<CreditCard>& credit_cards, |
| 143 | const std::string& app_locale, |
| 144 | FormStructure* submitted_form) { |
| 145 | DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| 146 | |
| 147 | // For each field in the |submitted_form|, extract the value. Then for each |
| 148 | // profile or credit card, identify any stored types that match the value. |
| 149 | for (size_t i = 0; i < submitted_form->field_count(); ++i) { |
| 150 | AutofillField* field = submitted_form->field(i); |
| 151 | base::string16 value = CollapseWhitespace(field->value, false); |
| 152 | |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 153 | ServerFieldTypeSet matching_types; |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 154 | for (std::vector<AutofillProfile>::const_iterator it = profiles.begin(); |
| 155 | it != profiles.end(); ++it) { |
| 156 | it->GetMatchingTypes(value, app_locale, &matching_types); |
| 157 | } |
| 158 | for (std::vector<CreditCard>::const_iterator it = credit_cards.begin(); |
| 159 | it != credit_cards.end(); ++it) { |
| 160 | it->GetMatchingTypes(value, app_locale, &matching_types); |
| 161 | } |
| 162 | |
| 163 | if (matching_types.empty()) |
| 164 | matching_types.insert(UNKNOWN_TYPE); |
| 165 | |
| 166 | field->set_possible_types(matching_types); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | // Returns true if server returned known field types to one or more fields in |
| 171 | // this form. |
| 172 | bool HasServerSpecifiedFieldTypes(const FormStructure& form_structure) { |
| 173 | for (size_t i = 0; i < form_structure.field_count(); ++i) { |
| 174 | if (form_structure.field(i)->server_type() != NO_SERVER_DATA) |
| 175 | return true; |
| 176 | } |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | } // namespace |
| 181 | |
| 182 | AutofillManager::AutofillManager( |
| 183 | AutofillDriver* driver, |
| 184 | autofill::AutofillManagerDelegate* delegate, |
| 185 | const std::string& app_locale, |
| 186 | AutofillDownloadManagerState enable_download_manager) |
| 187 | : driver_(driver), |
| 188 | manager_delegate_(delegate), |
| 189 | app_locale_(app_locale), |
| 190 | personal_data_(delegate->GetPersonalDataManager()), |
| 191 | autocomplete_history_manager_( |
| 192 | new AutocompleteHistoryManager(driver, delegate)), |
| 193 | autocheckout_manager_(this), |
| 194 | metric_logger_(new AutofillMetrics), |
| 195 | has_logged_autofill_enabled_(false), |
| 196 | has_logged_address_suggestions_count_(false), |
| 197 | did_show_suggestions_(false), |
| 198 | user_did_type_(false), |
| 199 | user_did_autofill_(false), |
| 200 | user_did_edit_autofilled_field_(false), |
| 201 | external_delegate_(NULL), |
| 202 | test_delegate_(NULL), |
| 203 | weak_ptr_factory_(this) { |
| 204 | if (enable_download_manager == ENABLE_AUTOFILL_DOWNLOAD_MANAGER) { |
| 205 | download_manager_.reset( |
| 206 | new AutofillDownloadManager( |
| 207 | driver->GetWebContents()->GetBrowserContext(), this)); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | AutofillManager::~AutofillManager() {} |
| 212 | |
| 213 | // static |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 214 | void AutofillManager::RegisterProfilePrefs( |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 215 | user_prefs::PrefRegistrySyncable* registry) { |
| 216 | registry->RegisterBooleanPref( |
| 217 | prefs::kAutofillEnabled, |
| 218 | true, |
| 219 | user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| 220 | #if defined(OS_MACOSX) || defined(OS_ANDROID) |
| 221 | registry->RegisterBooleanPref( |
| 222 | prefs::kAutofillAuxiliaryProfilesEnabled, |
| 223 | true, |
| 224 | user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| 225 | #else |
| 226 | registry->RegisterBooleanPref( |
| 227 | prefs::kAutofillAuxiliaryProfilesEnabled, |
| 228 | false, |
| 229 | user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 230 | #endif |
| 231 | registry->RegisterDoublePref( |
| 232 | prefs::kAutofillPositiveUploadRate, |
| 233 | kAutofillPositiveUploadRateDefaultValue, |
| 234 | user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 235 | registry->RegisterDoublePref( |
| 236 | prefs::kAutofillNegativeUploadRate, |
| 237 | kAutofillNegativeUploadRateDefaultValue, |
| 238 | user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 239 | } |
| 240 | |
| 241 | void AutofillManager::SetExternalDelegate(AutofillExternalDelegate* delegate) { |
| 242 | // TODO(jrg): consider passing delegate into the ctor. That won't |
| 243 | // work if the delegate has a pointer to the AutofillManager, but |
| 244 | // future directions may not need such a pointer. |
| 245 | external_delegate_ = delegate; |
| 246 | autocomplete_history_manager_->SetExternalDelegate(delegate); |
| 247 | } |
| 248 | |
| 249 | bool AutofillManager::OnFormSubmitted(const FormData& form, |
| 250 | const TimeTicks& timestamp) { |
| 251 | // Let Autocomplete know as well. |
| 252 | autocomplete_history_manager_->OnFormSubmitted(form); |
| 253 | |
| 254 | if (!IsAutofillEnabled()) |
| 255 | return false; |
| 256 | |
| 257 | if (driver_->GetWebContents()->GetBrowserContext()->IsOffTheRecord()) |
| 258 | return false; |
| 259 | |
| 260 | // Don't save data that was submitted through JavaScript. |
| 261 | if (!form.user_submitted) |
| 262 | return false; |
| 263 | |
| 264 | // Grab a copy of the form data. |
| 265 | scoped_ptr<FormStructure> submitted_form( |
| 266 | new FormStructure(form, GetAutocheckoutURLPrefix())); |
| 267 | |
| 268 | // Disregard forms that we wouldn't ever autofill in the first place. |
| 269 | if (!submitted_form->ShouldBeParsed(true)) |
| 270 | return false; |
| 271 | |
| 272 | // Ignore forms not present in our cache. These are typically forms with |
| 273 | // wonky JavaScript that also makes them not auto-fillable. |
| 274 | FormStructure* cached_submitted_form; |
| 275 | if (!FindCachedForm(form, &cached_submitted_form)) |
| 276 | return false; |
| 277 | |
| 278 | submitted_form->UpdateFromCache(*cached_submitted_form); |
| 279 | // Don't prompt the user to save data entered by Autocheckout. |
| 280 | if (submitted_form->IsAutofillable(true) && |
| 281 | !submitted_form->filled_by_autocheckout()) |
| 282 | ImportFormData(*submitted_form); |
| 283 | |
| 284 | // Only upload server statistics and UMA metrics if at least some local data |
| 285 | // is available to use as a baseline. |
| 286 | const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles(); |
| 287 | const std::vector<CreditCard*>& credit_cards = |
| 288 | personal_data_->GetCreditCards(); |
| 289 | if (!profiles.empty() || !credit_cards.empty()) { |
| 290 | // Copy the profile and credit card data, so that it can be accessed on a |
| 291 | // separate thread. |
| 292 | std::vector<AutofillProfile> copied_profiles; |
| 293 | copied_profiles.reserve(profiles.size()); |
| 294 | for (std::vector<AutofillProfile*>::const_iterator it = profiles.begin(); |
| 295 | it != profiles.end(); ++it) { |
| 296 | copied_profiles.push_back(**it); |
| 297 | } |
| 298 | |
| 299 | std::vector<CreditCard> copied_credit_cards; |
| 300 | copied_credit_cards.reserve(credit_cards.size()); |
| 301 | for (std::vector<CreditCard*>::const_iterator it = credit_cards.begin(); |
| 302 | it != credit_cards.end(); ++it) { |
| 303 | copied_credit_cards.push_back(**it); |
| 304 | } |
| 305 | |
| 306 | // Note that ownership of |submitted_form| is passed to the second task, |
| 307 | // using |base::Owned|. |
| 308 | FormStructure* raw_submitted_form = submitted_form.get(); |
| 309 | BrowserThread::GetBlockingPool()->PostTaskAndReply( |
| 310 | FROM_HERE, |
| 311 | base::Bind(&DeterminePossibleFieldTypesForUpload, |
| 312 | copied_profiles, |
| 313 | copied_credit_cards, |
| 314 | app_locale_, |
| 315 | raw_submitted_form), |
| 316 | base::Bind(&AutofillManager::UploadFormDataAsyncCallback, |
| 317 | weak_ptr_factory_.GetWeakPtr(), |
| 318 | base::Owned(submitted_form.release()), |
| 319 | forms_loaded_timestamp_, |
| 320 | initial_interaction_timestamp_, |
| 321 | timestamp)); |
| 322 | } |
| 323 | |
| 324 | return true; |
| 325 | } |
| 326 | |
| 327 | void AutofillManager::OnFormsSeen(const std::vector<FormData>& forms, |
| 328 | const TimeTicks& timestamp, |
| 329 | autofill::FormsSeenState state) { |
| 330 | bool is_post_document_load = state == autofill::DYNAMIC_FORMS_SEEN; |
| 331 | bool has_more_forms = state == autofill::PARTIAL_FORMS_SEEN; |
| 332 | // If new forms were added dynamically, and the autocheckout manager |
| 333 | // doesn't tell us to ignore ajax on this page, treat as a new page. |
| 334 | if (is_post_document_load) { |
| 335 | if (autocheckout_manager_.ShouldIgnoreAjax()) |
| 336 | return; |
| 337 | |
| 338 | Reset(); |
| 339 | } |
| 340 | |
| 341 | RenderViewHost* host = driver_->GetWebContents()->GetRenderViewHost(); |
| 342 | if (!host) |
| 343 | return; |
| 344 | |
| 345 | if (!GetAutocheckoutURLPrefix().empty()) { |
| 346 | // If whitelisted URL, fetch all the forms. |
| 347 | if (has_more_forms) |
| 348 | host->Send(new AutofillMsg_GetAllForms(host->GetRoutingID())); |
| 349 | if (!is_post_document_load) { |
| 350 | host->Send( |
| 351 | new AutofillMsg_AutocheckoutSupported(host->GetRoutingID())); |
| 352 | } |
| 353 | // Now return early, as OnFormsSeen will get called again with all forms. |
| 354 | if (has_more_forms) |
| 355 | return; |
| 356 | } |
| 357 | |
| 358 | autocheckout_manager_.OnFormsSeen(); |
| 359 | bool enabled = IsAutofillEnabled(); |
| 360 | if (!has_logged_autofill_enabled_) { |
| 361 | metric_logger_->LogIsAutofillEnabledAtPageLoad(enabled); |
| 362 | has_logged_autofill_enabled_ = true; |
| 363 | } |
| 364 | |
| 365 | if (!enabled) |
| 366 | return; |
| 367 | |
| 368 | forms_loaded_timestamp_ = timestamp; |
| 369 | ParseForms(forms); |
| 370 | } |
| 371 | |
| 372 | void AutofillManager::OnTextFieldDidChange(const FormData& form, |
| 373 | const FormFieldData& field, |
| 374 | const TimeTicks& timestamp) { |
| 375 | FormStructure* form_structure = NULL; |
| 376 | AutofillField* autofill_field = NULL; |
| 377 | if (!GetCachedFormAndField(form, field, &form_structure, &autofill_field)) |
| 378 | return; |
| 379 | |
| 380 | if (!user_did_type_) { |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 381 | autocheckout_manager_.set_should_show_bubble(false); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 382 | user_did_type_ = true; |
| 383 | metric_logger_->LogUserHappinessMetric(AutofillMetrics::USER_DID_TYPE); |
| 384 | } |
| 385 | |
| 386 | if (autofill_field->is_autofilled) { |
| 387 | autofill_field->is_autofilled = false; |
| 388 | metric_logger_->LogUserHappinessMetric( |
| 389 | AutofillMetrics::USER_DID_EDIT_AUTOFILLED_FIELD); |
| 390 | |
| 391 | if (!user_did_edit_autofilled_field_) { |
| 392 | user_did_edit_autofilled_field_ = true; |
| 393 | metric_logger_->LogUserHappinessMetric( |
| 394 | AutofillMetrics::USER_DID_EDIT_AUTOFILLED_FIELD_ONCE); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | UpdateInitialInteractionTimestamp(timestamp); |
| 399 | } |
| 400 | |
| 401 | void AutofillManager::OnQueryFormFieldAutofill(int query_id, |
| 402 | const FormData& form, |
| 403 | const FormFieldData& field, |
| 404 | const gfx::RectF& bounding_box, |
| 405 | bool display_warning) { |
| 406 | if (autocheckout_manager_.is_autocheckout_bubble_showing()) |
| 407 | return; |
| 408 | |
| 409 | std::vector<base::string16> values; |
| 410 | std::vector<base::string16> labels; |
| 411 | std::vector<base::string16> icons; |
| 412 | std::vector<int> unique_ids; |
| 413 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 414 | external_delegate_->OnQuery(query_id, |
| 415 | form, |
| 416 | field, |
| 417 | bounding_box, |
| 418 | display_warning); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 419 | |
| 420 | RenderViewHost* host = NULL; |
| 421 | FormStructure* form_structure = NULL; |
| 422 | AutofillField* autofill_field = NULL; |
| 423 | if (GetHost(&host) && |
| 424 | GetCachedFormAndField(form, field, &form_structure, &autofill_field) && |
| 425 | // Don't send suggestions for forms that aren't auto-fillable. |
| 426 | form_structure->IsAutofillable(false)) { |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 427 | AutofillType type = autofill_field->Type(); |
| 428 | bool is_filling_credit_card = (type.group() == CREDIT_CARD); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 429 | if (is_filling_credit_card) { |
| 430 | GetCreditCardSuggestions( |
| 431 | field, type, &values, &labels, &icons, &unique_ids); |
| 432 | } else { |
| 433 | GetProfileSuggestions( |
| 434 | form_structure, field, type, &values, &labels, &icons, &unique_ids); |
| 435 | } |
| 436 | |
| 437 | DCHECK_EQ(values.size(), labels.size()); |
| 438 | DCHECK_EQ(values.size(), icons.size()); |
| 439 | DCHECK_EQ(values.size(), unique_ids.size()); |
| 440 | |
| 441 | if (!values.empty()) { |
| 442 | // Don't provide Autofill suggestions when Autofill is disabled, and don't |
| 443 | // provide credit card suggestions for non-HTTPS pages. However, provide a |
| 444 | // warning to the user in these cases. |
| 445 | int warning = 0; |
| 446 | if (!form_structure->IsAutofillable(true)) |
| 447 | warning = IDS_AUTOFILL_WARNING_FORM_DISABLED; |
| 448 | else if (is_filling_credit_card && !FormIsHTTPS(*form_structure)) |
| 449 | warning = IDS_AUTOFILL_WARNING_INSECURE_CONNECTION; |
| 450 | if (warning) { |
| 451 | values.assign(1, l10n_util::GetStringUTF16(warning)); |
| 452 | labels.assign(1, base::string16()); |
| 453 | icons.assign(1, base::string16()); |
| 454 | unique_ids.assign(1, |
| 455 | WebKit::WebAutofillClient::MenuItemIDWarningMessage); |
| 456 | } else { |
| 457 | bool section_is_autofilled = |
| 458 | SectionIsAutofilled(*form_structure, form, |
| 459 | autofill_field->section()); |
| 460 | if (section_is_autofilled) { |
| 461 | // If the relevant section is auto-filled and the renderer is querying |
| 462 | // for suggestions, then the user is editing the value of a field. |
| 463 | // In this case, mimic autocomplete: don't display labels or icons, |
| 464 | // as that information is redundant. |
| 465 | labels.assign(labels.size(), base::string16()); |
| 466 | icons.assign(icons.size(), base::string16()); |
| 467 | } |
| 468 | |
| 469 | // When filling credit card suggestions, the values and labels are |
| 470 | // typically obfuscated, which makes detecting duplicates hard. Since |
| 471 | // duplicates only tend to be a problem when filling address forms |
| 472 | // anyway, only don't de-dup credit card suggestions. |
| 473 | if (!is_filling_credit_card) |
| 474 | RemoveDuplicateSuggestions(&values, &labels, &icons, &unique_ids); |
| 475 | |
| 476 | // The first time we show suggestions on this page, log the number of |
| 477 | // suggestions shown. |
| 478 | if (!has_logged_address_suggestions_count_ && !section_is_autofilled) { |
| 479 | metric_logger_->LogAddressSuggestionsCount(values.size()); |
| 480 | has_logged_address_suggestions_count_ = true; |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | // Add the results from AutoComplete. They come back asynchronously, so we |
| 487 | // hand off what we generated and they will send the results back to the |
| 488 | // renderer. |
| 489 | autocomplete_history_manager_->OnGetAutocompleteSuggestions( |
| 490 | query_id, field.name, field.value, values, labels, icons, unique_ids); |
| 491 | } |
| 492 | |
| 493 | void AutofillManager::OnFillAutofillFormData(int query_id, |
| 494 | const FormData& form, |
| 495 | const FormFieldData& field, |
| 496 | int unique_id) { |
| 497 | RenderViewHost* host = NULL; |
| 498 | const AutofillDataModel* data_model = NULL; |
| 499 | size_t variant = 0; |
| 500 | FormStructure* form_structure = NULL; |
| 501 | AutofillField* autofill_field = NULL; |
| 502 | // NOTE: GetHost may invalidate |data_model| because it causes the |
| 503 | // PersonalDataManager to reload Mac address book entries. Thus it must |
| 504 | // come before GetProfileOrCreditCard. |
| 505 | if (!GetHost(&host) || |
| 506 | !GetProfileOrCreditCard(unique_id, &data_model, &variant) || |
| 507 | !GetCachedFormAndField(form, field, &form_structure, &autofill_field)) |
| 508 | return; |
| 509 | |
| 510 | DCHECK(host); |
| 511 | DCHECK(form_structure); |
| 512 | DCHECK(autofill_field); |
| 513 | |
| 514 | FormData result = form; |
| 515 | |
| 516 | // If the relevant section is auto-filled, we should fill |field| but not the |
| 517 | // rest of the form. |
| 518 | if (SectionIsAutofilled(*form_structure, form, autofill_field->section())) { |
| 519 | for (std::vector<FormFieldData>::iterator iter = result.fields.begin(); |
| 520 | iter != result.fields.end(); ++iter) { |
| 521 | if ((*iter) == field) { |
| 522 | data_model->FillFormField( |
| 523 | *autofill_field, variant, app_locale_, &(*iter)); |
| 524 | // Mark the cached field as autofilled, so that we can detect when a |
| 525 | // user edits an autofilled field (for metrics). |
| 526 | autofill_field->is_autofilled = true; |
| 527 | break; |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | driver_->SendFormDataToRenderer(query_id, result); |
| 532 | return; |
| 533 | } |
| 534 | |
| 535 | // Cache the field type for the field from which the user initiated autofill. |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 536 | FieldTypeGroup initiating_group_type = autofill_field->Type().group(); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 537 | DCHECK_EQ(form_structure->field_count(), form.fields.size()); |
| 538 | for (size_t i = 0; i < form_structure->field_count(); ++i) { |
| 539 | if (form_structure->field(i)->section() != autofill_field->section()) |
| 540 | continue; |
| 541 | |
| 542 | DCHECK_EQ(*form_structure->field(i), result.fields[i]); |
| 543 | |
| 544 | const AutofillField* cached_field = form_structure->field(i); |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 545 | FieldTypeGroup field_group_type = cached_field->Type().group(); |
Ben Murdoch | 2385ea3 | 2013-08-06 11:01:04 +0100 | [diff] [blame] | 546 | if (field_group_type != NO_GROUP) { |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 547 | // If the field being filled is either |
| 548 | // (a) the field that the user initiated the fill from, or |
| 549 | // (b) part of the same logical unit, e.g. name or phone number, |
| 550 | // then take the multi-profile "variant" into account. |
| 551 | // Otherwise fill with the default (zeroth) variant. |
| 552 | size_t use_variant = 0; |
| 553 | if (result.fields[i] == field || |
| 554 | field_group_type == initiating_group_type) { |
| 555 | use_variant = variant; |
| 556 | } |
| 557 | data_model->FillFormField(*cached_field, |
| 558 | use_variant, |
| 559 | app_locale_, |
| 560 | &result.fields[i]); |
| 561 | // Mark the cached field as autofilled, so that we can detect when a user |
| 562 | // edits an autofilled field (for metrics). |
| 563 | form_structure->field(i)->is_autofilled = true; |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | autofilled_form_signatures_.push_front(form_structure->FormSignature()); |
| 568 | // Only remember the last few forms that we've seen, both to avoid false |
| 569 | // positives and to avoid wasting memory. |
| 570 | if (autofilled_form_signatures_.size() > kMaxRecentFormSignaturesToRemember) |
| 571 | autofilled_form_signatures_.pop_back(); |
| 572 | |
| 573 | driver_->SendFormDataToRenderer(query_id, result); |
| 574 | } |
| 575 | |
| 576 | void AutofillManager::OnShowAutofillDialog() { |
| 577 | manager_delegate_->ShowAutofillSettings(); |
| 578 | } |
| 579 | |
| 580 | void AutofillManager::OnDidPreviewAutofillFormData() { |
| 581 | if (test_delegate_) |
| 582 | test_delegate_->DidPreviewFormData(); |
| 583 | } |
| 584 | |
| 585 | void AutofillManager::OnDidFillAutofillFormData(const TimeTicks& timestamp) { |
| 586 | if (test_delegate_) |
| 587 | test_delegate_->DidFillFormData(); |
| 588 | |
| 589 | metric_logger_->LogUserHappinessMetric(AutofillMetrics::USER_DID_AUTOFILL); |
| 590 | if (!user_did_autofill_) { |
| 591 | user_did_autofill_ = true; |
| 592 | metric_logger_->LogUserHappinessMetric( |
| 593 | AutofillMetrics::USER_DID_AUTOFILL_ONCE); |
| 594 | } |
| 595 | |
| 596 | UpdateInitialInteractionTimestamp(timestamp); |
| 597 | } |
| 598 | |
| 599 | void AutofillManager::OnDidShowAutofillSuggestions(bool is_new_popup) { |
| 600 | if (test_delegate_) |
| 601 | test_delegate_->DidShowSuggestions(); |
| 602 | |
| 603 | if (is_new_popup) { |
| 604 | metric_logger_->LogUserHappinessMetric(AutofillMetrics::SUGGESTIONS_SHOWN); |
| 605 | |
| 606 | if (!did_show_suggestions_) { |
| 607 | did_show_suggestions_ = true; |
| 608 | metric_logger_->LogUserHappinessMetric( |
| 609 | AutofillMetrics::SUGGESTIONS_SHOWN_ONCE); |
| 610 | } |
| 611 | } |
| 612 | } |
| 613 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 614 | void AutofillManager::OnHideAutofillUI() { |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 615 | if (!IsAutofillEnabled()) |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 616 | return; |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 617 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 618 | manager_delegate_->HideAutofillPopup(); |
| 619 | manager_delegate_->HideAutocheckoutBubble(); |
| 620 | } |
| 621 | |
| 622 | void AutofillManager::RemoveAutofillProfileOrCreditCard(int unique_id) { |
| 623 | const AutofillDataModel* data_model = NULL; |
| 624 | size_t variant = 0; |
| 625 | if (!GetProfileOrCreditCard(unique_id, &data_model, &variant)) { |
| 626 | NOTREACHED(); |
| 627 | return; |
| 628 | } |
| 629 | |
| 630 | // TODO(csharp): If we are dealing with a variant only the variant should |
| 631 | // be deleted, instead of doing nothing. |
| 632 | // http://crbug.com/124211 |
| 633 | if (variant != 0) |
| 634 | return; |
| 635 | |
| 636 | personal_data_->RemoveByGUID(data_model->guid()); |
| 637 | } |
| 638 | |
| 639 | void AutofillManager::RemoveAutocompleteEntry(const base::string16& name, |
| 640 | const base::string16& value) { |
| 641 | autocomplete_history_manager_->OnRemoveAutocompleteEntry(name, value); |
| 642 | } |
| 643 | |
| 644 | content::WebContents* AutofillManager::GetWebContents() const { |
| 645 | return driver_->GetWebContents(); |
| 646 | } |
| 647 | |
| 648 | const std::vector<FormStructure*>& AutofillManager::GetFormStructures() { |
| 649 | return form_structures_.get(); |
| 650 | } |
| 651 | |
| 652 | void AutofillManager::ShowRequestAutocompleteDialog( |
| 653 | const FormData& form, |
| 654 | const GURL& source_url, |
| 655 | autofill::DialogType dialog_type, |
| 656 | const base::Callback<void(const FormStructure*, |
| 657 | const std::string&)>& callback) { |
| 658 | manager_delegate_->ShowRequestAutocompleteDialog( |
| 659 | form, source_url, dialog_type, callback); |
| 660 | } |
| 661 | |
| 662 | void AutofillManager::SetTestDelegate( |
| 663 | autofill::AutofillManagerTestDelegate* delegate) { |
| 664 | test_delegate_ = delegate; |
| 665 | } |
| 666 | |
| 667 | void AutofillManager::OnAddPasswordFormMapping( |
| 668 | const FormFieldData& form, |
| 669 | const PasswordFormFillData& fill_data) { |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 670 | external_delegate_->AddPasswordFormMapping(form, fill_data); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | void AutofillManager::OnShowPasswordSuggestions( |
| 674 | const FormFieldData& field, |
| 675 | const gfx::RectF& bounds, |
| 676 | const std::vector<base::string16>& suggestions, |
| 677 | const std::vector<base::string16>& realms) { |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 678 | external_delegate_->OnShowPasswordSuggestions(suggestions, |
| 679 | realms, |
| 680 | field, |
| 681 | bounds); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | void AutofillManager::OnSetDataList(const std::vector<base::string16>& values, |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 685 | const std::vector<base::string16>& labels) { |
| 686 | if (values.size() != labels.size()) |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 687 | return; |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 688 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 689 | external_delegate_->SetCurrentDataListValues(values, labels); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | void AutofillManager::OnRequestAutocomplete( |
| 693 | const FormData& form, |
| 694 | const GURL& frame_url) { |
| 695 | if (!IsAutofillEnabled()) { |
| 696 | ReturnAutocompleteResult(WebFormElement::AutocompleteResultErrorDisabled, |
| 697 | FormData()); |
| 698 | return; |
| 699 | } |
| 700 | |
| 701 | base::Callback<void(const FormStructure*, const std::string&)> callback = |
| 702 | base::Bind(&AutofillManager::ReturnAutocompleteData, |
| 703 | weak_ptr_factory_.GetWeakPtr()); |
| 704 | ShowRequestAutocompleteDialog( |
| 705 | form, frame_url, autofill::DIALOG_TYPE_REQUEST_AUTOCOMPLETE, callback); |
| 706 | } |
| 707 | |
| 708 | void AutofillManager::ReturnAutocompleteResult( |
| 709 | WebFormElement::AutocompleteResult result, const FormData& form_data) { |
| 710 | // driver_->GetWebContents() will be NULL when the interactive autocomplete |
| 711 | // is closed due to a tab or browser window closing. |
| 712 | if (!driver_->GetWebContents()) |
| 713 | return; |
| 714 | |
| 715 | RenderViewHost* host = driver_->GetWebContents()->GetRenderViewHost(); |
| 716 | if (!host) |
| 717 | return; |
| 718 | |
| 719 | host->Send(new AutofillMsg_RequestAutocompleteResult(host->GetRoutingID(), |
| 720 | result, |
| 721 | form_data)); |
| 722 | } |
| 723 | |
| 724 | void AutofillManager::ReturnAutocompleteData( |
| 725 | const FormStructure* result, |
| 726 | const std::string& unused_transaction_id) { |
| 727 | if (!result) { |
| 728 | ReturnAutocompleteResult(WebFormElement::AutocompleteResultErrorCancel, |
| 729 | FormData()); |
| 730 | } else { |
| 731 | ReturnAutocompleteResult(WebFormElement::AutocompleteResultSuccess, |
| 732 | result->ToFormData()); |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | void AutofillManager::OnLoadedServerPredictions( |
| 737 | const std::string& response_xml) { |
| 738 | scoped_ptr<autofill::AutocheckoutPageMetaData> page_meta_data( |
| 739 | new autofill::AutocheckoutPageMetaData()); |
| 740 | |
| 741 | // Parse and store the server predictions. |
| 742 | FormStructure::ParseQueryResponse(response_xml, |
| 743 | form_structures_.get(), |
| 744 | page_meta_data.get(), |
| 745 | *metric_logger_); |
| 746 | |
| 747 | if (page_meta_data->IsInAutofillableFlow()) { |
| 748 | RenderViewHost* host = driver_->GetWebContents()->GetRenderViewHost(); |
| 749 | if (host) |
| 750 | host->Send(new AutofillMsg_AutocheckoutSupported(host->GetRoutingID())); |
| 751 | } |
| 752 | |
| 753 | // TODO(ahutter): Remove this once Autocheckout is implemented on other |
| 754 | // platforms. See http://crbug.com/173416. |
| 755 | #if defined(TOOLKIT_VIEWS) |
| 756 | if (!GetAutocheckoutURLPrefix().empty()) |
| 757 | autocheckout_manager_.OnLoadedPageMetaData(page_meta_data.Pass()); |
| 758 | #endif // #if defined(TOOLKIT_VIEWS) |
| 759 | |
| 760 | // If the corresponding flag is set, annotate forms with the predicted types. |
| 761 | driver_->SendAutofillTypePredictionsToRenderer(form_structures_.get()); |
| 762 | } |
| 763 | |
| 764 | void AutofillManager::OnDidEndTextFieldEditing() { |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 765 | external_delegate_->DidEndTextFieldEditing(); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 766 | } |
| 767 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 768 | void AutofillManager::OnAutocheckoutPageCompleted( |
| 769 | autofill::AutocheckoutStatus status) { |
| 770 | autocheckout_manager_.OnAutocheckoutPageCompleted(status); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | std::string AutofillManager::GetAutocheckoutURLPrefix() const { |
| 774 | if (!driver_->GetWebContents()) |
| 775 | return std::string(); |
| 776 | |
| 777 | autofill::autocheckout::WhitelistManager* whitelist_manager = |
| 778 | manager_delegate_->GetAutocheckoutWhitelistManager(); |
| 779 | |
| 780 | return whitelist_manager ? whitelist_manager->GetMatchedURLPrefix( |
| 781 | driver_->GetWebContents()->GetURL()) : std::string(); |
| 782 | } |
| 783 | |
| 784 | bool AutofillManager::IsAutofillEnabled() const { |
| 785 | return manager_delegate_->GetPrefs()->GetBoolean(prefs::kAutofillEnabled); |
| 786 | } |
| 787 | |
| 788 | void AutofillManager::ImportFormData(const FormStructure& submitted_form) { |
| 789 | const CreditCard* imported_credit_card; |
| 790 | if (!personal_data_->ImportFormData(submitted_form, &imported_credit_card)) |
| 791 | return; |
| 792 | |
| 793 | // If credit card information was submitted, we need to confirm whether to |
| 794 | // save it. |
| 795 | if (imported_credit_card) { |
| 796 | manager_delegate_->ConfirmSaveCreditCard( |
| 797 | *metric_logger_, |
| 798 | *imported_credit_card, |
| 799 | base::Bind(&PersonalDataManager::SaveImportedCreditCard, |
| 800 | base::Unretained(personal_data_), *imported_credit_card)); |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | // Note that |submitted_form| is passed as a pointer rather than as a reference |
| 805 | // so that we can get memory management right across threads. Note also that we |
| 806 | // explicitly pass in all the time stamps of interest, as the cached ones might |
| 807 | // get reset before this method executes. |
| 808 | void AutofillManager::UploadFormDataAsyncCallback( |
| 809 | const FormStructure* submitted_form, |
| 810 | const TimeTicks& load_time, |
| 811 | const TimeTicks& interaction_time, |
| 812 | const TimeTicks& submission_time) { |
| 813 | submitted_form->LogQualityMetrics(*metric_logger_, |
| 814 | load_time, |
| 815 | interaction_time, |
| 816 | submission_time); |
| 817 | |
| 818 | if (submitted_form->ShouldBeCrowdsourced()) |
| 819 | UploadFormData(*submitted_form); |
| 820 | } |
| 821 | |
| 822 | void AutofillManager::OnMaybeShowAutocheckoutBubble( |
| 823 | const FormData& form, |
| 824 | const gfx::RectF& bounding_box) { |
| 825 | if (!IsAutofillEnabled()) |
| 826 | return; |
| 827 | |
| 828 | // Don't show bubble if corresponding FormStructure doesn't have anything to |
| 829 | // autofill. |
| 830 | FormStructure* cached_form; |
| 831 | if (!FindCachedForm(form, &cached_form)) |
| 832 | return; |
| 833 | |
| 834 | // Don't offer Autocheckout bubble if Autofill server is not aware of this |
| 835 | // form in the context of Autocheckout experiment. |
| 836 | if (!HasServerSpecifiedFieldTypes(*cached_form)) |
| 837 | return; |
| 838 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 839 | autocheckout_manager_.MaybeShowAutocheckoutBubble(form.origin, bounding_box); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | void AutofillManager::UploadFormData(const FormStructure& submitted_form) { |
| 843 | if (!download_manager_) |
| 844 | return; |
| 845 | |
| 846 | // Check if the form is among the forms that were recently auto-filled. |
| 847 | bool was_autofilled = false; |
| 848 | std::string form_signature = submitted_form.FormSignature(); |
| 849 | for (std::list<std::string>::const_iterator it = |
| 850 | autofilled_form_signatures_.begin(); |
| 851 | it != autofilled_form_signatures_.end() && !was_autofilled; |
| 852 | ++it) { |
| 853 | if (*it == form_signature) |
| 854 | was_autofilled = true; |
| 855 | } |
| 856 | |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 857 | ServerFieldTypeSet non_empty_types; |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 858 | personal_data_->GetNonEmptyTypes(&non_empty_types); |
| 859 | |
| 860 | download_manager_->StartUploadRequest(submitted_form, was_autofilled, |
| 861 | non_empty_types); |
| 862 | } |
| 863 | |
| 864 | void AutofillManager::Reset() { |
| 865 | form_structures_.clear(); |
| 866 | has_logged_autofill_enabled_ = false; |
| 867 | has_logged_address_suggestions_count_ = false; |
| 868 | did_show_suggestions_ = false; |
| 869 | user_did_type_ = false; |
| 870 | user_did_autofill_ = false; |
| 871 | user_did_edit_autofilled_field_ = false; |
| 872 | forms_loaded_timestamp_ = TimeTicks(); |
| 873 | initial_interaction_timestamp_ = TimeTicks(); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 874 | external_delegate_->Reset(); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | AutofillManager::AutofillManager(AutofillDriver* driver, |
| 878 | autofill::AutofillManagerDelegate* delegate, |
| 879 | PersonalDataManager* personal_data) |
| 880 | : driver_(driver), |
| 881 | manager_delegate_(delegate), |
| 882 | app_locale_("en-US"), |
| 883 | personal_data_(personal_data), |
| 884 | autocomplete_history_manager_( |
| 885 | new AutocompleteHistoryManager(driver, delegate)), |
| 886 | autocheckout_manager_(this), |
| 887 | metric_logger_(new AutofillMetrics), |
| 888 | has_logged_autofill_enabled_(false), |
| 889 | has_logged_address_suggestions_count_(false), |
| 890 | did_show_suggestions_(false), |
| 891 | user_did_type_(false), |
| 892 | user_did_autofill_(false), |
| 893 | user_did_edit_autofilled_field_(false), |
| 894 | external_delegate_(NULL), |
| 895 | test_delegate_(NULL), |
| 896 | weak_ptr_factory_(this) { |
| 897 | DCHECK(driver_); |
| 898 | DCHECK(driver_->GetWebContents()); |
| 899 | DCHECK(manager_delegate_); |
| 900 | } |
| 901 | |
| 902 | void AutofillManager::set_metric_logger(const AutofillMetrics* metric_logger) { |
| 903 | metric_logger_.reset(metric_logger); |
| 904 | } |
| 905 | |
| 906 | bool AutofillManager::GetHost(RenderViewHost** host) const { |
| 907 | if (!IsAutofillEnabled()) |
| 908 | return false; |
| 909 | |
| 910 | // No autofill data to return if the profiles are empty. |
| 911 | if (personal_data_->GetProfiles().empty() && |
| 912 | personal_data_->GetCreditCards().empty()) { |
| 913 | return false; |
| 914 | } |
| 915 | |
| 916 | if (!driver_->RendererIsAvailable()) |
| 917 | return false; |
| 918 | |
| 919 | *host = driver_->GetWebContents()->GetRenderViewHost(); |
| 920 | return true; |
| 921 | } |
| 922 | |
| 923 | bool AutofillManager::GetProfileOrCreditCard( |
| 924 | int unique_id, |
| 925 | const AutofillDataModel** data_model, |
| 926 | size_t* variant) const { |
| 927 | // Unpack the |unique_id| into component parts. |
| 928 | GUIDPair credit_card_guid; |
| 929 | GUIDPair profile_guid; |
| 930 | UnpackGUIDs(unique_id, &credit_card_guid, &profile_guid); |
| 931 | DCHECK(!base::IsValidGUID(credit_card_guid.first) || |
| 932 | !base::IsValidGUID(profile_guid.first)); |
| 933 | |
| 934 | // Find the profile that matches the |profile_guid|, if one is specified. |
| 935 | // Otherwise find the credit card that matches the |credit_card_guid|, |
| 936 | // if specified. |
| 937 | if (base::IsValidGUID(profile_guid.first)) { |
| 938 | *data_model = personal_data_->GetProfileByGUID(profile_guid.first); |
| 939 | *variant = profile_guid.second; |
| 940 | } else if (base::IsValidGUID(credit_card_guid.first)) { |
| 941 | *data_model = personal_data_->GetCreditCardByGUID(credit_card_guid.first); |
| 942 | *variant = credit_card_guid.second; |
| 943 | } |
| 944 | |
| 945 | return !!*data_model; |
| 946 | } |
| 947 | |
| 948 | bool AutofillManager::FindCachedForm(const FormData& form, |
| 949 | FormStructure** form_structure) const { |
| 950 | // Find the FormStructure that corresponds to |form|. |
| 951 | // Scan backward through the cached |form_structures_|, as updated versions of |
| 952 | // forms are added to the back of the list, whereas original versions of these |
| 953 | // forms might appear toward the beginning of the list. The communication |
| 954 | // protocol with the crowdsourcing server does not permit us to discard the |
| 955 | // original versions of the forms. |
| 956 | *form_structure = NULL; |
| 957 | for (std::vector<FormStructure*>::const_reverse_iterator iter = |
| 958 | form_structures_.rbegin(); |
| 959 | iter != form_structures_.rend(); ++iter) { |
| 960 | if (**iter == form) { |
| 961 | *form_structure = *iter; |
| 962 | |
| 963 | // The same form might be cached with multiple field counts: in some |
| 964 | // cases, non-autofillable fields are filtered out, whereas in other cases |
| 965 | // they are not. To avoid thrashing the cache, keep scanning until we |
| 966 | // find a cached version with the same number of fields, if there is one. |
| 967 | if ((*iter)->field_count() == form.fields.size()) |
| 968 | break; |
| 969 | } |
| 970 | } |
| 971 | |
| 972 | if (!(*form_structure)) |
| 973 | return false; |
| 974 | |
| 975 | return true; |
| 976 | } |
| 977 | |
| 978 | bool AutofillManager::GetCachedFormAndField(const FormData& form, |
| 979 | const FormFieldData& field, |
| 980 | FormStructure** form_structure, |
| 981 | AutofillField** autofill_field) { |
| 982 | // Find the FormStructure that corresponds to |form|. |
| 983 | // If we do not have this form in our cache but it is parseable, we'll add it |
| 984 | // in the call to |UpdateCachedForm()|. |
| 985 | if (!FindCachedForm(form, form_structure) && |
| 986 | !FormStructure(form, GetAutocheckoutURLPrefix()).ShouldBeParsed(false)) { |
| 987 | return false; |
| 988 | } |
| 989 | |
| 990 | // Update the cached form to reflect any dynamic changes to the form data, if |
| 991 | // necessary. |
| 992 | if (!UpdateCachedForm(form, *form_structure, form_structure)) |
| 993 | return false; |
| 994 | |
| 995 | // No data to return if there are no auto-fillable fields. |
| 996 | if (!(*form_structure)->autofill_count()) |
| 997 | return false; |
| 998 | |
| 999 | // Find the AutofillField that corresponds to |field|. |
| 1000 | *autofill_field = NULL; |
| 1001 | for (std::vector<AutofillField*>::const_iterator iter = |
| 1002 | (*form_structure)->begin(); |
| 1003 | iter != (*form_structure)->end(); ++iter) { |
| 1004 | if ((**iter) == field) { |
| 1005 | *autofill_field = *iter; |
| 1006 | break; |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | // Even though we always update the cache, the field might not exist if the |
| 1011 | // website disables autocomplete while the user is interacting with the form. |
| 1012 | // See http://crbug.com/160476 |
| 1013 | return *autofill_field != NULL; |
| 1014 | } |
| 1015 | |
| 1016 | bool AutofillManager::UpdateCachedForm(const FormData& live_form, |
| 1017 | const FormStructure* cached_form, |
| 1018 | FormStructure** updated_form) { |
| 1019 | bool needs_update = |
| 1020 | (!cached_form || |
| 1021 | live_form.fields.size() != cached_form->field_count()); |
| 1022 | for (size_t i = 0; !needs_update && i < cached_form->field_count(); ++i) { |
| 1023 | needs_update = *cached_form->field(i) != live_form.fields[i]; |
| 1024 | } |
| 1025 | |
| 1026 | if (!needs_update) |
| 1027 | return true; |
| 1028 | |
| 1029 | if (form_structures_.size() >= kMaxFormCacheSize) |
| 1030 | return false; |
| 1031 | |
| 1032 | // Add the new or updated form to our cache. |
| 1033 | form_structures_.push_back( |
| 1034 | new FormStructure(live_form, GetAutocheckoutURLPrefix())); |
| 1035 | *updated_form = *form_structures_.rbegin(); |
| 1036 | (*updated_form)->DetermineHeuristicTypes(*metric_logger_); |
| 1037 | |
| 1038 | // If we have cached data, propagate it to the updated form. |
| 1039 | if (cached_form) { |
| 1040 | std::map<base::string16, const AutofillField*> cached_fields; |
| 1041 | for (size_t i = 0; i < cached_form->field_count(); ++i) { |
| 1042 | const AutofillField* field = cached_form->field(i); |
| 1043 | cached_fields[field->unique_name()] = field; |
| 1044 | } |
| 1045 | |
| 1046 | for (size_t i = 0; i < (*updated_form)->field_count(); ++i) { |
| 1047 | AutofillField* field = (*updated_form)->field(i); |
| 1048 | std::map<base::string16, const AutofillField*>::iterator cached_field = |
| 1049 | cached_fields.find(field->unique_name()); |
| 1050 | if (cached_field != cached_fields.end()) { |
| 1051 | field->set_server_type(cached_field->second->server_type()); |
| 1052 | field->is_autofilled = cached_field->second->is_autofilled; |
| 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | // Note: We _must not_ remove the original version of the cached form from |
| 1057 | // the list of |form_structures_|. Otherwise, we break parsing of the |
| 1058 | // crowdsourcing server's response to our query. |
| 1059 | } |
| 1060 | |
| 1061 | // Annotate the updated form with its predicted types. |
| 1062 | std::vector<FormStructure*> forms(1, *updated_form); |
| 1063 | driver_->SendAutofillTypePredictionsToRenderer(forms); |
| 1064 | |
| 1065 | return true; |
| 1066 | } |
| 1067 | |
| 1068 | void AutofillManager::GetProfileSuggestions( |
| 1069 | FormStructure* form, |
| 1070 | const FormFieldData& field, |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 1071 | const AutofillType& type, |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 1072 | std::vector<base::string16>* values, |
| 1073 | std::vector<base::string16>* labels, |
| 1074 | std::vector<base::string16>* icons, |
| 1075 | std::vector<int>* unique_ids) const { |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 1076 | std::vector<ServerFieldType> field_types(form->field_count()); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 1077 | for (size_t i = 0; i < form->field_count(); ++i) { |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 1078 | field_types.push_back(form->field(i)->Type().GetStorableType()); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 1079 | } |
| 1080 | std::vector<GUIDPair> guid_pairs; |
| 1081 | |
| 1082 | personal_data_->GetProfileSuggestions( |
| 1083 | type, field.value, field.is_autofilled, field_types, |
| 1084 | values, labels, icons, &guid_pairs); |
| 1085 | |
| 1086 | for (size_t i = 0; i < guid_pairs.size(); ++i) { |
| 1087 | unique_ids->push_back(PackGUIDs(GUIDPair(std::string(), 0), |
| 1088 | guid_pairs[i])); |
| 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | void AutofillManager::GetCreditCardSuggestions( |
| 1093 | const FormFieldData& field, |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 1094 | const AutofillType& type, |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 1095 | std::vector<base::string16>* values, |
| 1096 | std::vector<base::string16>* labels, |
| 1097 | std::vector<base::string16>* icons, |
| 1098 | std::vector<int>* unique_ids) const { |
| 1099 | std::vector<GUIDPair> guid_pairs; |
| 1100 | personal_data_->GetCreditCardSuggestions( |
| 1101 | type, field.value, values, labels, icons, &guid_pairs); |
| 1102 | |
| 1103 | for (size_t i = 0; i < guid_pairs.size(); ++i) { |
| 1104 | unique_ids->push_back(PackGUIDs(guid_pairs[i], GUIDPair(std::string(), 0))); |
| 1105 | } |
| 1106 | } |
| 1107 | |
| 1108 | void AutofillManager::ParseForms(const std::vector<FormData>& forms) { |
| 1109 | std::vector<FormStructure*> non_queryable_forms; |
| 1110 | std::string autocheckout_url_prefix = GetAutocheckoutURLPrefix(); |
| 1111 | for (std::vector<FormData>::const_iterator iter = forms.begin(); |
| 1112 | iter != forms.end(); ++iter) { |
| 1113 | scoped_ptr<FormStructure> form_structure( |
| 1114 | new FormStructure(*iter, autocheckout_url_prefix)); |
| 1115 | if (!form_structure->ShouldBeParsed(false)) |
| 1116 | continue; |
| 1117 | |
| 1118 | form_structure->DetermineHeuristicTypes(*metric_logger_); |
| 1119 | |
| 1120 | // Set aside forms with method GET or author-specified types, so that they |
| 1121 | // are not included in the query to the server. |
| 1122 | if (form_structure->ShouldBeCrowdsourced()) |
| 1123 | form_structures_.push_back(form_structure.release()); |
| 1124 | else |
| 1125 | non_queryable_forms.push_back(form_structure.release()); |
| 1126 | } |
| 1127 | |
| 1128 | if (form_structures_.empty()) { |
| 1129 | // Call OnLoadedPageMetaData with no page metadata immediately if there is |
| 1130 | // no form in the page. This give |autocheckout_manager| a chance to |
| 1131 | // terminate Autocheckout and send Autocheckout status. |
| 1132 | autocheckout_manager_.OnLoadedPageMetaData( |
| 1133 | scoped_ptr<autofill::AutocheckoutPageMetaData>()); |
| 1134 | } else if (download_manager_) { |
| 1135 | // Query the server if we have at least one of the forms were parsed. |
| 1136 | download_manager_->StartQueryRequest(form_structures_.get(), |
| 1137 | *metric_logger_); |
| 1138 | } |
| 1139 | |
| 1140 | for (std::vector<FormStructure*>::const_iterator iter = |
| 1141 | non_queryable_forms.begin(); |
| 1142 | iter != non_queryable_forms.end(); ++iter) { |
| 1143 | form_structures_.push_back(*iter); |
| 1144 | } |
| 1145 | |
| 1146 | if (!form_structures_.empty()) |
| 1147 | metric_logger_->LogUserHappinessMetric(AutofillMetrics::FORMS_LOADED); |
| 1148 | |
| 1149 | // For the |non_queryable_forms|, we have all the field type info we're ever |
| 1150 | // going to get about them. For the other forms, we'll wait until we get a |
| 1151 | // response from the server. |
| 1152 | driver_->SendAutofillTypePredictionsToRenderer(non_queryable_forms); |
| 1153 | } |
| 1154 | |
| 1155 | int AutofillManager::GUIDToID(const GUIDPair& guid) const { |
| 1156 | if (!base::IsValidGUID(guid.first)) |
| 1157 | return 0; |
| 1158 | |
| 1159 | std::map<GUIDPair, int>::const_iterator iter = guid_id_map_.find(guid); |
| 1160 | if (iter == guid_id_map_.end()) { |
| 1161 | int id = guid_id_map_.size() + 1; |
| 1162 | guid_id_map_[guid] = id; |
| 1163 | id_guid_map_[id] = guid; |
| 1164 | return id; |
| 1165 | } else { |
| 1166 | return iter->second; |
| 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | const GUIDPair AutofillManager::IDToGUID(int id) const { |
| 1171 | if (id == 0) |
| 1172 | return GUIDPair(std::string(), 0); |
| 1173 | |
| 1174 | std::map<int, GUIDPair>::const_iterator iter = id_guid_map_.find(id); |
| 1175 | if (iter == id_guid_map_.end()) { |
| 1176 | NOTREACHED(); |
| 1177 | return GUIDPair(std::string(), 0); |
| 1178 | } |
| 1179 | |
| 1180 | return iter->second; |
| 1181 | } |
| 1182 | |
| 1183 | // When sending IDs (across processes) to the renderer we pack credit card and |
| 1184 | // profile IDs into a single integer. Credit card IDs are sent in the high |
| 1185 | // word and profile IDs are sent in the low word. |
| 1186 | int AutofillManager::PackGUIDs(const GUIDPair& cc_guid, |
| 1187 | const GUIDPair& profile_guid) const { |
| 1188 | int cc_id = GUIDToID(cc_guid); |
| 1189 | int profile_id = GUIDToID(profile_guid); |
| 1190 | |
| 1191 | DCHECK(cc_id <= std::numeric_limits<unsigned short>::max()); |
| 1192 | DCHECK(profile_id <= std::numeric_limits<unsigned short>::max()); |
| 1193 | |
| 1194 | return cc_id << std::numeric_limits<unsigned short>::digits | profile_id; |
| 1195 | } |
| 1196 | |
| 1197 | // When receiving IDs (across processes) from the renderer we unpack credit card |
| 1198 | // and profile IDs from a single integer. Credit card IDs are stored in the |
| 1199 | // high word and profile IDs are stored in the low word. |
| 1200 | void AutofillManager::UnpackGUIDs(int id, |
| 1201 | GUIDPair* cc_guid, |
| 1202 | GUIDPair* profile_guid) const { |
| 1203 | int cc_id = id >> std::numeric_limits<unsigned short>::digits & |
| 1204 | std::numeric_limits<unsigned short>::max(); |
| 1205 | int profile_id = id & std::numeric_limits<unsigned short>::max(); |
| 1206 | |
| 1207 | *cc_guid = IDToGUID(cc_id); |
| 1208 | *profile_guid = IDToGUID(profile_id); |
| 1209 | } |
| 1210 | |
| 1211 | void AutofillManager::UpdateInitialInteractionTimestamp( |
| 1212 | const TimeTicks& interaction_timestamp) { |
| 1213 | if (initial_interaction_timestamp_.is_null() || |
| 1214 | interaction_timestamp < initial_interaction_timestamp_) { |
| 1215 | initial_interaction_timestamp_ = interaction_timestamp; |
| 1216 | } |
| 1217 | } |
| 1218 | |
| 1219 | } // namespace autofill |