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