This source file includes following definitions.
- GetAccountCreationPasswordFields
- ContainsURL
- FormsAreEqual
- ContainsForm
- enabled_
- DidFinishDocumentLoad
- DidFinishLoad
- ShouldAnalyzeDocument
- OnMessageReceived
- OnFormNotBlacklisted
- OnPasswordAccepted
- OnAccountCreationFormsDetected
- DetermineGenerationElement
- FocusedNodeChanged
- TextDidChangeInTextField
- ShowGenerationPopup
- ShowEditingPopup
- HidePopup
#include "components/autofill/content/renderer/password_generation_agent.h"
#include "base/command_line.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "components/autofill/content/common/autofill_messages.h"
#include "components/autofill/content/renderer/form_autofill_util.h"
#include "components/autofill/content/renderer/password_form_conversion_utils.h"
#include "components/autofill/core/common/autofill_switches.h"
#include "components/autofill/core/common/form_data.h"
#include "components/autofill/core/common/password_form.h"
#include "components/autofill/core/common/password_generation_util.h"
#include "content/public/renderer/render_view.h"
#include "google_apis/gaia/gaia_urls.h"
#include "third_party/WebKit/public/platform/WebCString.h"
#include "third_party/WebKit/public/platform/WebRect.h"
#include "third_party/WebKit/public/platform/WebVector.h"
#include "third_party/WebKit/public/web/WebDocument.h"
#include "third_party/WebKit/public/web/WebFormElement.h"
#include "third_party/WebKit/public/web/WebFrame.h"
#include "third_party/WebKit/public/web/WebInputElement.h"
#include "third_party/WebKit/public/web/WebSecurityOrigin.h"
#include "third_party/WebKit/public/web/WebView.h"
#include "ui/gfx/rect.h"
namespace autofill {
namespace {
bool GetAccountCreationPasswordFields(
    const blink::WebFormElement& form,
    std::vector<blink::WebInputElement>* passwords) {
  
  blink::WebVector<blink::WebFormControlElement> control_elements;
  form.getFormControlElements(control_elements);
  size_t num_input_elements = 0;
  for (size_t i = 0; i < control_elements.size(); i++) {
    blink::WebInputElement* input_element =
        toWebInputElement(&control_elements[i]);
    
    if (input_element &&
        input_element->isTextField() &&
        input_element->hasNonEmptyBoundingBox()) {
      num_input_elements++;
      if (input_element->isPasswordField())
        passwords->push_back(*input_element);
    }
  }
  
  
  
  if (!passwords->empty() && num_input_elements >= 3) {
    
    
    
    if (passwords->size() > 2)
      passwords->resize(2);
    return true;
  }
  return false;
}
bool ContainsURL(const std::vector<GURL>& urls, const GURL& url) {
  return std::find(urls.begin(), urls.end(), url) != urls.end();
}
bool FormsAreEqual(const autofill::FormData& form1,
                   const PasswordForm& form2) {
  
  
  
  
  return form1.origin.GetOrigin() == form2.origin.GetOrigin();
}
bool ContainsForm(const std::vector<autofill::FormData>& forms,
                  const PasswordForm& form) {
  for (std::vector<autofill::FormData>::const_iterator it =
           forms.begin(); it != forms.end(); ++it) {
    if (FormsAreEqual(*it, form))
      return true;
  }
  return false;
}
}  
PasswordGenerationAgent::PasswordGenerationAgent(
    content::RenderView* render_view)
    : content::RenderViewObserver(render_view),
      render_view_(render_view),
      password_is_generated_(false),
      password_edited_(false),
      enabled_(password_generation::IsPasswordGenerationEnabled()) {
  DVLOG(2) << "Password Generation is " << (enabled_ ? "Enabled" : "Disabled");
}
PasswordGenerationAgent::~PasswordGenerationAgent() {}
void PasswordGenerationAgent::DidFinishDocumentLoad(blink::WebFrame* frame) {
  
  
  
  
  
  
  
  if (!frame->parent()) {
    not_blacklisted_password_form_origins_.clear();
    generation_enabled_forms_.clear();
    generation_element_.reset();
    possible_account_creation_form_.reset(new PasswordForm());
    password_elements_.clear();
    password_is_generated_ = false;
    if (password_edited_) {
      password_generation::LogPasswordGenerationEvent(
          password_generation::PASSWORD_EDITED);
    }
    password_edited_ = false;
  }
}
void PasswordGenerationAgent::DidFinishLoad(blink::WebFrame* frame) {
  if (!enabled_)
    return;
  
  
  if (!ShouldAnalyzeDocument(frame->document()))
    return;
  blink::WebVector<blink::WebFormElement> forms;
  frame->document().forms(forms);
  for (size_t i = 0; i < forms.size(); ++i) {
    if (forms[i].isNull())
      continue;
    
    
    scoped_ptr<PasswordForm> password_form(
        CreatePasswordForm(forms[i]));
    if (!password_form.get()) {
      DVLOG(2) << "Skipping form as it would not be saved";
      continue;
    }
    
    
    GURL realm(password_form->signon_realm);
    if (realm == GaiaUrls::GetInstance()->gaia_login_form_realm())
      continue;
    std::vector<blink::WebInputElement> passwords;
    if (GetAccountCreationPasswordFields(forms[i], &passwords)) {
      DVLOG(2) << "Account creation form detected";
      password_generation::LogPasswordGenerationEvent(
          password_generation::SIGN_UP_DETECTED);
      password_elements_ = passwords;
      possible_account_creation_form_.swap(password_form);
      DetermineGenerationElement();
      
      return;
    }
  }
  password_generation::LogPasswordGenerationEvent(
      password_generation::NO_SIGN_UP_DETECTED);
}
bool PasswordGenerationAgent::ShouldAnalyzeDocument(
    const blink::WebDocument& document) const {
  
  
  blink::WebSecurityOrigin origin = document.securityOrigin();
  if (!origin.canAccessPasswordManager()) {
    DVLOG(1) << "No PasswordManager access";
    return false;
  }
  return true;
}
bool PasswordGenerationAgent::OnMessageReceived(const IPC::Message& message) {
  bool handled = true;
  IPC_BEGIN_MESSAGE_MAP(PasswordGenerationAgent, message)
    IPC_MESSAGE_HANDLER(AutofillMsg_FormNotBlacklisted,
                        OnFormNotBlacklisted)
    IPC_MESSAGE_HANDLER(AutofillMsg_GeneratedPasswordAccepted,
                        OnPasswordAccepted)
    IPC_MESSAGE_HANDLER(AutofillMsg_AccountCreationFormsDetected,
                        OnAccountCreationFormsDetected)
    IPC_MESSAGE_UNHANDLED(handled = false)
  IPC_END_MESSAGE_MAP()
  return handled;
}
void PasswordGenerationAgent::OnFormNotBlacklisted(const PasswordForm& form) {
  not_blacklisted_password_form_origins_.push_back(form.origin);
  DetermineGenerationElement();
}
void PasswordGenerationAgent::OnPasswordAccepted(
    const base::string16& password) {
  password_is_generated_ = true;
  password_generation::LogPasswordGenerationEvent(
      password_generation::PASSWORD_ACCEPTED);
  for (std::vector<blink::WebInputElement>::iterator it =
           password_elements_.begin();
       it != password_elements_.end(); ++it) {
    it->setValue(password);
    it->setAutofilled(true);
    
    
    render_view_->GetWebView()->advanceFocus(false);
  }
}
void PasswordGenerationAgent::OnAccountCreationFormsDetected(
    const std::vector<autofill::FormData>& forms) {
  generation_enabled_forms_.insert(
      generation_enabled_forms_.end(), forms.begin(), forms.end());
  DetermineGenerationElement();
}
void PasswordGenerationAgent::DetermineGenerationElement() {
  
  
  if (!possible_account_creation_form_.get() || password_elements_.empty()) {
    DVLOG(2) << "Local hueristics have not detected a possible account "
             << "creation form";
    return;
  }
  if (CommandLine::ForCurrentProcess()->HasSwitch(
          switches::kLocalHeuristicsOnlyForPasswordGeneration)) {
    DVLOG(2) << "Bypassing additional checks.";
  } else if (not_blacklisted_password_form_origins_.empty() ||
             !ContainsURL(not_blacklisted_password_form_origins_,
                          possible_account_creation_form_->origin)) {
    DVLOG(2) << "Have not received confirmation that password form isn't "
             << "blacklisted";
    return;
  } else if (generation_enabled_forms_.empty() ||
             !ContainsForm(generation_enabled_forms_,
                           *possible_account_creation_form_)) {
    
    
    DVLOG(2) << "Have not received confirmation from Autofill that form is "
             << "used for account creation";
    return;
  }
  DVLOG(2) << "Password generation eligible form found";
  generation_element_ = password_elements_[0];
  password_generation::LogPasswordGenerationEvent(
      password_generation::GENERATION_AVAILABLE);
}
void PasswordGenerationAgent::FocusedNodeChanged(const blink::WebNode& node) {
  if (!generation_element_.isNull())
    generation_element_.setShouldRevealPassword(false);
  if (node.isNull() || !node.isElementNode())
    return;
  const blink::WebElement web_element = node.toConst<blink::WebElement>();
  if (!web_element.document().frame())
    return;
  const blink::WebInputElement* element = toWebInputElement(&web_element);
  if (!element || *element != generation_element_)
    return;
  if (password_is_generated_) {
    generation_element_.setShouldRevealPassword(true);
    ShowEditingPopup();
  }
  
  if (!element->isReadOnly() &&
      element->isEnabled() &&
      element->value().isEmpty()) {
    ShowGenerationPopup();
  }
}
bool PasswordGenerationAgent::TextDidChangeInTextField(
    const blink::WebInputElement& element) {
  if (element != generation_element_)
    return false;
  if (element.value().isEmpty()) {
    if (password_is_generated_) {
      
      password_generation::LogPasswordGenerationEvent(
          password_generation::PASSWORD_DELETED);
    }
    
    
    password_is_generated_ = false;
    generation_element_.setShouldRevealPassword(false);
    
    ShowGenerationPopup();
  } else if (!password_is_generated_) {
    
    HidePopup();
  } else {
    password_edited_ = true;
    
    for (std::vector<blink::WebInputElement>::iterator it =
             password_elements_.begin();
         it != password_elements_.end(); ++it) {
      it->setValue(element.value());
    }
  }
  return true;
}
void PasswordGenerationAgent::ShowGenerationPopup() {
  gfx::RectF bounding_box_scaled =
      GetScaledBoundingBox(render_view_->GetWebView()->pageScaleFactor(),
                           &generation_element_);
  Send(new AutofillHostMsg_ShowPasswordGenerationPopup(
      routing_id(),
      bounding_box_scaled,
      generation_element_.maxLength(),
      *possible_account_creation_form_));
  password_generation::LogPasswordGenerationEvent(
      password_generation::GENERATION_POPUP_SHOWN);
}
void PasswordGenerationAgent::ShowEditingPopup() {
  gfx::RectF bounding_box_scaled =
      GetScaledBoundingBox(render_view_->GetWebView()->pageScaleFactor(),
                           &generation_element_);
  Send(new AutofillHostMsg_ShowPasswordEditingPopup(
      routing_id(),
      bounding_box_scaled,
      *possible_account_creation_form_));
  password_generation::LogPasswordGenerationEvent(
      password_generation::EDITING_POPUP_SHOWN);
}
void PasswordGenerationAgent::HidePopup() {
  Send(new AutofillHostMsg_HidePasswordGenerationPopup(routing_id()));
}
}