Home | History | Annotate | Download | only in browser
      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/password_autofill_manager.h"
      6 #include "components/autofill/core/common/autofill_messages.h"
      7 #include "content/public/browser/render_view_host.h"
      8 #include "content/public/browser/web_contents.h"
      9 #include "ui/base/keycodes/keyboard_codes.h"
     10 
     11 namespace autofill {
     12 
     13 ////////////////////////////////////////////////////////////////////////////////
     14 // PasswordAutofillManager, public:
     15 
     16 PasswordAutofillManager::PasswordAutofillManager(
     17     content::WebContents* web_contents) : web_contents_(web_contents) {
     18 }
     19 
     20 PasswordAutofillManager::~PasswordAutofillManager() {
     21 }
     22 
     23 bool PasswordAutofillManager::DidAcceptAutofillSuggestion(
     24     const FormFieldData& field,
     25     const base::string16& value) {
     26   PasswordFormFillData password;
     27   if (!FindLoginInfo(field, &password))
     28     return false;
     29 
     30   if (WillFillUserNameAndPassword(value, password)) {
     31     if (web_contents_) {
     32       content::RenderViewHost* render_view_host =
     33           web_contents_->GetRenderViewHost();
     34       render_view_host->Send(new AutofillMsg_AcceptPasswordAutofillSuggestion(
     35           render_view_host->GetRoutingID(),
     36           value));
     37     }
     38     return true;
     39   }
     40 
     41   return false;
     42 }
     43 
     44 void PasswordAutofillManager::AddPasswordFormMapping(
     45     const FormFieldData& username_element,
     46     const PasswordFormFillData& password) {
     47   login_to_password_info_[username_element] = password;
     48 }
     49 
     50 void PasswordAutofillManager::Reset() {
     51   login_to_password_info_.clear();
     52 }
     53 
     54 ////////////////////////////////////////////////////////////////////////////////
     55 // PasswordAutofillManager, private:
     56 
     57 bool PasswordAutofillManager::WillFillUserNameAndPassword(
     58     const base::string16& current_username,
     59     const PasswordFormFillData& fill_data) {
     60   // Look for any suitable matches to current field text.
     61   if (fill_data.basic_data.fields[0].value == current_username)
     62     return true;
     63 
     64   // Scan additional logins for a match.
     65    for (PasswordFormFillData::LoginCollection::const_iterator iter =
     66            fill_data.additional_logins.begin();
     67        iter != fill_data.additional_logins.end(); ++iter) {
     68     if (iter->first == current_username)
     69       return true;
     70   }
     71 
     72   for (PasswordFormFillData::UsernamesCollection::const_iterator usernames_iter
     73            = fill_data.other_possible_usernames.begin();
     74        usernames_iter != fill_data.other_possible_usernames.end();
     75        ++usernames_iter) {
     76     for (size_t i = 0; i < usernames_iter->second.size(); ++i) {
     77       if (usernames_iter->second[i] == current_username)
     78         return true;
     79     }
     80   }
     81 
     82   return false;
     83 }
     84 
     85 bool PasswordAutofillManager::FindLoginInfo(
     86     const FormFieldData& field,
     87     PasswordFormFillData* found_password) {
     88   LoginToPasswordInfoMap::iterator iter = login_to_password_info_.find(field);
     89   if (iter == login_to_password_info_.end())
     90     return false;
     91 
     92   *found_password = iter->second;
     93   return true;
     94 }
     95 
     96 }  // namespace autofill
     97