Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / autofill / password_generation_popup_controller_impl.h
1 // Copyright 2014 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 CHROME_BROWSER_UI_AUTOFILL_PASSWORD_GENERATION_POPUP_CONTROLLER_IMPL_H_
6 #define CHROME_BROWSER_UI_AUTOFILL_PASSWORD_GENERATION_POPUP_CONTROLLER_IMPL_H_
7
8 #include <string>
9
10 #include "base/basictypes.h"
11 #include "base/memory/weak_ptr.h"
12 #include "chrome/browser/ui/autofill/password_generation_popup_controller.h"
13 #include "chrome/browser/ui/autofill/popup_controller_common.h"
14 #include "components/autofill/core/common/password_form.h"
15 #include "ui/gfx/native_widget_types.h"
16 #include "ui/gfx/range/range.h"
17 #include "ui/gfx/rect.h"
18 #include "ui/gfx/rect_f.h"
19
20 namespace content {
21 struct NativeWebKeyboardEvent;
22 class WebContents;
23 }
24
25 namespace password_manager {
26 class PasswordManager;
27 }
28
29 namespace autofill {
30
31 class PasswordGenerator;
32 class PasswordGenerationPopupObserver;
33 class PasswordGenerationPopupView;
34
35 // This class controls a PasswordGenerationPopupView. It is responsible for
36 // determining the location of the popup, handling keypress events while the
37 // popup is active, and notifying both the renderer and the password manager
38 // if the password is accepted.
39 class PasswordGenerationPopupControllerImpl
40     : public PasswordGenerationPopupController {
41  public:
42   // Create a controller or return |previous| if it is suitable. Will hide
43   // |previous| if it is not returned. |bounds| is the bounds of the element
44   // that we are showing the dropdown for in screen space. |form| is the
45   // identifier for the form that we are filling, and is used to notify
46   // |password_manager| if the password is generated. |max_length| is used to
47   // determine the length of the password shown. If not NULL, |observer| will
48   // be notified of changes of the popup state.
49   static base::WeakPtr<PasswordGenerationPopupControllerImpl> GetOrCreate(
50       base::WeakPtr<PasswordGenerationPopupControllerImpl> previous,
51       const gfx::RectF& bounds,
52       const PasswordForm& form,
53       int max_length,
54       password_manager::PasswordManager* password_manager,
55       PasswordGenerationPopupObserver* observer,
56       content::WebContents* web_contents,
57       gfx::NativeView container_view);
58   ~PasswordGenerationPopupControllerImpl() override;
59
60   // Create a PasswordGenerationPopupView if one doesn't already exist.
61   // If |display_password| is true, a generated password is shown that can be
62   // selected by the user. Otherwise just the text explaining generated
63   // passwords is shown. Idempotent.
64   void Show(bool display_password);
65
66   // Hides the popup and destroys |this|.
67   void HideAndDestroy();
68
69   // Accessors.
70   content::WebContents* web_contents() {
71     return controller_common_.web_contents();
72   }
73
74  protected:
75   PasswordGenerationPopupControllerImpl(
76       const gfx::RectF& bounds,
77       const PasswordForm& form,
78       int max_length,
79       password_manager::PasswordManager* password_manager,
80       PasswordGenerationPopupObserver* observer,
81       content::WebContents* web_contents,
82       gfx::NativeView container_view);
83
84   // Handle to the popup. May be NULL if popup isn't showing.
85   PasswordGenerationPopupView* view_;
86
87  private:
88   // PasswordGenerationPopupController implementation:
89   void Hide() override;
90   void ViewDestroyed() override;
91   void SetSelectionAtPoint(const gfx::Point& point) override;
92   bool AcceptSelectedLine() override;
93   void SelectionCleared() override;
94   void PasswordAccepted() override;
95   void OnSavedPasswordsLinkClicked() override;
96   int GetMinimumWidth() override;
97   gfx::NativeView container_view() override;
98   const gfx::Rect& popup_bounds() const override;
99   const gfx::RectF& element_bounds() const override;
100   bool IsRTL() const override;
101   bool display_password() const override;
102   bool password_selected() const override;
103   base::string16 password() const override;
104   base::string16 SuggestedText() override;
105   const base::string16& HelpText() override;
106   base::string16 AccessibleName() override;
107   const gfx::Range& HelpTextLinkRange() override;
108
109   base::WeakPtr<PasswordGenerationPopupControllerImpl> GetWeakPtr();
110
111   bool HandleKeyPressEvent(const content::NativeWebKeyboardEvent& event);
112
113   // Set if the password is currently selected.
114   void PasswordSelected(bool selected);
115
116   // Accept password if it's selected.
117   bool PossiblyAcceptPassword();
118
119   // Get desired size of popup. Height depends on width because we do text
120   // wrapping.
121   void CalculateBounds();
122
123   PasswordForm form_;
124   password_manager::PasswordManager* password_manager_;
125
126   // May be NULL.
127   PasswordGenerationPopupObserver* observer_;
128
129   // Controls how passwords are generated.
130   scoped_ptr<PasswordGenerator> generator_;
131
132   // Contains common popup functionality.
133   PopupControllerCommon controller_common_;
134
135   // Help text and the range in the text that corresponds to the saved passwords
136   // link.
137   base::string16 help_text_;
138   gfx::Range link_range_;
139
140   base::string16 current_password_;
141   bool password_selected_;
142
143   // If a password will be shown in this popup.
144   bool display_password_;
145
146   // Bounds for all the elements of the popup.
147   gfx::Rect popup_bounds_;
148
149   base::WeakPtrFactory<PasswordGenerationPopupControllerImpl> weak_ptr_factory_;
150
151   DISALLOW_COPY_AND_ASSIGN(PasswordGenerationPopupControllerImpl);
152 };
153
154 }  // namespace autofill
155
156 #endif  // CHROME_BROWSER_UI_AUTOFILL_PASSWORD_GENERATION_POPUP_CONTROLLER_IMPL_H_