Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / components / autofill / content / renderer / password_generation_agent.cc
index b777c9c..147b773 100644 (file)
@@ -16,8 +16,6 @@
 #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"
@@ -94,6 +92,15 @@ bool ContainsForm(const std::vector<autofill::FormData>& forms,
   return false;
 }
 
+void CopyValueToAllInputElements(
+    const blink::WebString value,
+    std::vector<blink::WebInputElement>* elements) {
+  for (std::vector<blink::WebInputElement>::iterator it = elements->begin();
+       it != elements->end(); ++it) {
+    it->setValue(value, true /* sendEvents */);
+  }
+}
+
 }  // namespace
 
 PasswordGenerationAgent::PasswordGenerationAgent(
@@ -102,6 +109,8 @@ PasswordGenerationAgent::PasswordGenerationAgent(
       render_view_(render_view),
       password_is_generated_(false),
       password_edited_(false),
+      generation_popup_shown_(false),
+      editing_popup_shown_(false),
       enabled_(password_generation::IsPasswordGenerationEnabled()) {
   DVLOG(2) << "Password Generation is " << (enabled_ ? "Enabled" : "Disabled");
 }
@@ -121,6 +130,15 @@ void PasswordGenerationAgent::DidFinishDocumentLoad(
     generation_enabled_forms_.clear();
     generation_element_.reset();
     possible_account_creation_form_.reset(new PasswordForm());
+
+    // Log statistics after navigation so that we only log once per page.
+    if (password_elements_.empty()) {
+      password_generation::LogPasswordGenerationEvent(
+          password_generation::NO_SIGN_UP_DETECTED);
+    } else {
+      password_generation::LogPasswordGenerationEvent(
+          password_generation::SIGN_UP_DETECTED);
+    }
     password_elements_.clear();
     password_is_generated_ = false;
     if (password_edited_) {
@@ -128,10 +146,31 @@ void PasswordGenerationAgent::DidFinishDocumentLoad(
           password_generation::PASSWORD_EDITED);
     }
     password_edited_ = false;
+
+    if (generation_popup_shown_) {
+      password_generation::LogPasswordGenerationEvent(
+          password_generation::GENERATION_POPUP_SHOWN);
+    }
+    generation_popup_shown_ = false;
+
+    if (editing_popup_shown_) {
+      password_generation::LogPasswordGenerationEvent(
+          password_generation::EDITING_POPUP_SHOWN);
+    }
+    editing_popup_shown_ = false;
   }
 }
 
+void PasswordGenerationAgent::OnDynamicFormsSeen(blink::WebLocalFrame* frame) {
+  FindPossibleGenerationForm(frame);
+}
+
 void PasswordGenerationAgent::DidFinishLoad(blink::WebLocalFrame* frame) {
+  FindPossibleGenerationForm(frame);
+}
+
+void PasswordGenerationAgent::FindPossibleGenerationForm(
+    blink::WebLocalFrame* frame) {
   if (!enabled_)
     return;
 
@@ -140,6 +179,10 @@ void PasswordGenerationAgent::DidFinishLoad(blink::WebLocalFrame* frame) {
   if (!ShouldAnalyzeDocument(frame->document()))
     return;
 
+  // If we have already found a signup form for this page, no need to continue.
+  if (!password_elements_.empty())
+    return;
+
   blink::WebVector<blink::WebFormElement> forms;
   frame->document().forms(forms);
   for (size_t i = 0; i < forms.size(); ++i) {
@@ -164,8 +207,6 @@ void PasswordGenerationAgent::DidFinishLoad(blink::WebLocalFrame* frame) {
     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();
@@ -173,8 +214,6 @@ void PasswordGenerationAgent::DidFinishLoad(blink::WebLocalFrame* frame) {
       return;
     }
   }
-  password_generation::LogPasswordGenerationEvent(
-      password_generation::NO_SIGN_UP_DETECTED);
 }
 
 bool PasswordGenerationAgent::ShouldAnalyzeDocument(
@@ -217,7 +256,7 @@ void PasswordGenerationAgent::OnPasswordAccepted(
   for (std::vector<blink::WebInputElement>::iterator it =
            password_elements_.begin();
        it != password_elements_.end(); ++it) {
-    it->setValue(password);
+    it->setValue(password, true /* sendEvents */);
     it->setAutofilled(true);
     // Advance focus to the next input field. We assume password fields in
     // an account creation form are always adjacent.
@@ -311,6 +350,7 @@ bool PasswordGenerationAgent::TextDidChangeInTextField(
       // User generated a password and then deleted it.
       password_generation::LogPasswordGenerationEvent(
           password_generation::PASSWORD_DELETED);
+      CopyValueToAllInputElements(element.value(), &password_elements_);
     }
 
     // Do not treat the password as generated.
@@ -323,11 +363,7 @@ bool PasswordGenerationAgent::TextDidChangeInTextField(
   } else if (password_is_generated_) {
     password_edited_ = true;
     // Mirror edits to any confirmation password fields.
-    for (std::vector<blink::WebInputElement>::iterator it =
-             password_elements_.begin();
-         it != password_elements_.end(); ++it) {
-      it->setValue(element.value());
-    }
+    CopyValueToAllInputElements(element.value(), &password_elements_);
   } else if (element.value().length() > kMaximumOfferSize) {
     // User has rejected the feature and has started typing a password.
     HidePopup();
@@ -352,8 +388,7 @@ void PasswordGenerationAgent::ShowGenerationPopup() {
       generation_element_.maxLength(),
       *possible_account_creation_form_));
 
-  password_generation::LogPasswordGenerationEvent(
-      password_generation::GENERATION_POPUP_SHOWN);
+  generation_popup_shown_ = true;
 }
 
 void PasswordGenerationAgent::ShowEditingPopup() {
@@ -366,8 +401,7 @@ void PasswordGenerationAgent::ShowEditingPopup() {
       bounding_box_scaled,
       *possible_account_creation_form_));
 
-  password_generation::LogPasswordGenerationEvent(
-      password_generation::EDITING_POPUP_SHOWN);
+  editing_popup_shown_ = true;
 }
 
 void PasswordGenerationAgent::HidePopup() {