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#ifndef COMPONENTS_AUTOFILL_CONTENT_RENDERER_PASSWORD_GENERATION_AGENT_H_
6#define COMPONENTS_AUTOFILL_CONTENT_RENDERER_PASSWORD_GENERATION_AGENT_H_
7
8#include <map>
9#include <utility>
10#include <vector>
11
12#include "base/memory/scoped_ptr.h"
13#include "content/public/renderer/render_view_observer.h"
14#include "third_party/WebKit/public/web/WebInputElement.h"
15#include "url/gurl.h"
16
17namespace blink {
18class WebCString;
19class WebDocument;
20}
21
22namespace autofill {
23
24struct FormData;
25struct PasswordForm;
26
27// This class is responsible for controlling communication for password
28// generation between the browser (which shows the popup and generates
29// passwords) and WebKit (shows the generation icon in the password field).
30class PasswordGenerationAgent : public content::RenderViewObserver {
31 public:
32  explicit PasswordGenerationAgent(content::RenderView* render_view);
33  virtual ~PasswordGenerationAgent();
34
35  // Returns true if the field being changed is one where a generated password
36  // is being offered. Updates the state of the popup if necessary.
37  bool TextDidChangeInTextField(const blink::WebInputElement& element);
38
39  // Returns true if the newly focused node caused the generation UI to show.
40  bool FocusedNodeHasChanged(const blink::WebNode& node);
41
42  // The length that a password can be before the UI is hidden.
43  static const size_t kMaximumOfferSize = 5;
44
45 protected:
46  // Returns true if this document is one that we should consider analyzing.
47  // Virtual so that it can be overriden during testing.
48  virtual bool ShouldAnalyzeDocument(const blink::WebDocument& document) const;
49
50  // RenderViewObserver:
51  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
52
53  // Use to force enable during testing.
54  void set_enabled(bool enabled) { enabled_ = enabled; }
55
56 private:
57  // RenderViewObserver:
58  virtual void DidFinishDocumentLoad(blink::WebLocalFrame* frame) OVERRIDE;
59  virtual void DidFinishLoad(blink::WebLocalFrame* frame) OVERRIDE;
60
61  // Message handlers.
62  void OnFormNotBlacklisted(const PasswordForm& form);
63  void OnPasswordAccepted(const base::string16& password);
64  void OnAccountCreationFormsDetected(
65      const std::vector<autofill::FormData>& forms);
66
67  // Helper function to decide if |passwords_| contains password fields for
68  // an account creation form. Sets |generation_element_| to the field that
69  // we want to trigger the generation UI on.
70  void DetermineGenerationElement();
71
72  // Show password generation UI anchored at |generation_element_|.
73  void ShowGenerationPopup();
74
75  // Show UI for editing a generated password at |generation_element_|.
76  void ShowEditingPopup();
77
78  // Hides a password generation popup if one exists.
79  void HidePopup();
80
81  content::RenderView* render_view_;
82
83  // Stores the origin of the account creation form we detected.
84  scoped_ptr<PasswordForm> possible_account_creation_form_;
85
86  // Stores the origins of the password forms confirmed not to be blacklisted
87  // by the browser. A form can be blacklisted if a user chooses "never save
88  // passwords for this site".
89  std::vector<GURL> not_blacklisted_password_form_origins_;
90
91  // Stores each password form for which the Autofill server classifies one of
92  // the form's fields as an ACCOUNT_CREATION_PASSWORD. These forms will
93  // not be sent if the feature is disabled.
94  std::vector<autofill::FormData> generation_enabled_forms_;
95
96  // Password elements that may be part of an account creation form.
97  std::vector<blink::WebInputElement> password_elements_;
98
99  // Element where we want to trigger password generation UI.
100  blink::WebInputElement generation_element_;
101
102  // If the password field at |generation_element_| contains a generated
103  // password.
104  bool password_is_generated_;
105
106  // True if a password was generated and the user edited it. Used for UMA
107  // stats.
108  bool password_edited_;
109
110  // If this feature is enabled. Controlled by Finch.
111  bool enabled_;
112
113  DISALLOW_COPY_AND_ASSIGN(PasswordGenerationAgent);
114};
115
116}  // namespace autofill
117
118#endif  // COMPONENTS_AUTOFILL_CONTENT_RENDERER_PASSWORD_GENERATION_AGENT_H_
119