password_generation_popup_controller_impl.h revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
16f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org// Copyright 2014 The Chromium Authors. All rights reserved.
26f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org// Use of this source code is governed by a BSD-style license that can be
36f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org// found in the LICENSE file.
46f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
56f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#ifndef CHROME_BROWSER_UI_AUTOFILL_PASSWORD_GENERATION_POPUP_CONTROLLER_IMPL_H_
66f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#define CHROME_BROWSER_UI_AUTOFILL_PASSWORD_GENERATION_POPUP_CONTROLLER_IMPL_H_
76f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
86f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include <string>
96f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
106f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include "base/basictypes.h"
116f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include "base/memory/weak_ptr.h"
126f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include "chrome/browser/ui/autofill/password_generation_popup_controller.h"
136f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include "chrome/browser/ui/autofill/popup_controller_common.h"
146f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include "components/autofill/core/common/password_form.h"
156f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include "ui/gfx/font_list.h"
166f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include "ui/gfx/native_widget_types.h"
176f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include "ui/gfx/range/range.h"
186f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include "ui/gfx/rect.h"
196f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include "ui/gfx/rect_f.h"
206f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
216f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgnamespace content {
226f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgstruct NativeWebKeyboardEvent;
236f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgclass WebContents;
246f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
256f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
266f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgclass PasswordManager;
276f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
286f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgnamespace autofill {
296f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
306f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgclass PasswordGenerator;
316f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgclass PasswordGenerationPopupObserver;
326f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgclass PasswordGenerationPopupView;
336f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
346f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org// This class controls a PasswordGenerationPopupView. It is responsible for
356f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org// determining the location of the popup, handling keypress events while the
366f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org// popup is active, and notifying both the renderer and the password manager
376f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org// if the password is accepted.
386f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgclass PasswordGenerationPopupControllerImpl
396f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    : public PasswordGenerationPopupController {
406f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org public:
416f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  // Create a controller or return |previous| if it is suitable. Will hide
426f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  // |previous| if it is not returned. |bounds| is the bounds of the element
436f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  // that we are showing the dropdown for in screen space. |form| is the
446f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  // identifier for the form that we are filling, and is used to notify
456f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  // |password_manager| if the password is generated. |generator| is used to
466f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  // create the password shown. If not NULL, |observer| will be notified of
476f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  // changes of the popup state.
486f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  static base::WeakPtr<PasswordGenerationPopupControllerImpl> GetOrCreate(
496f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org      base::WeakPtr<PasswordGenerationPopupControllerImpl> previous,
506f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org      const gfx::RectF& bounds,
516f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org      const PasswordForm& form,
526f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org      PasswordGenerator* generator,
536f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org      PasswordManager* password_manager,
546f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org      PasswordGenerationPopupObserver* observer,
556f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org      content::WebContents* web_contents,
566f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org      gfx::NativeView container_view);
576f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  virtual ~PasswordGenerationPopupControllerImpl();
586f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
596f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  // Create a PasswordGenerationPopupView if one doesn't already exist.
606f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  // If |display_password| is true, a generated password is shown that can be
616f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  // selected by the user. Otherwise just the text explaining generated
626f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  // passwords is shown.
636f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  void Show(bool display_password);
646f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
656f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  // Hides the popup and destroys |this|.
666f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  void HideAndDestroy();
676f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
686f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  // Accessors.
696f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  content::WebContents* web_contents() {
706f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return controller_common_.web_contents();
716f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  }
726f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  const gfx::RectF& element_bounds() {
736f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return controller_common_.element_bounds();
746f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  }
756f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
766f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org private:
776f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  PasswordGenerationPopupControllerImpl(
786f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org      const gfx::RectF& bounds,
796f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org      const PasswordForm& form,
806f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org      PasswordGenerator* generator,
816f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org      PasswordManager* password_manager,
826f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org      PasswordGenerationPopupObserver* observer,
836f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org      content::WebContents* web_contents,
846f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org      gfx::NativeView container_view);
856f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
866f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  // PasswordGenerationPopupController implementation:
876f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  virtual void Hide() OVERRIDE;
886f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  virtual void ViewDestroyed() OVERRIDE;
896f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  virtual void SetSelectionAtPoint(const gfx::Point& point) OVERRIDE;
90  virtual void AcceptSelectionAtPoint(const gfx::Point& point) OVERRIDE;
91  virtual void SelectionCleared() OVERRIDE;
92  virtual bool ShouldRepostEvent(const ui::MouseEvent& event) OVERRIDE;
93  virtual bool ShouldHideOnOutsideClick() const OVERRIDE;
94  virtual void OnSavedPasswordsLinkClicked() OVERRIDE;
95  virtual gfx::NativeView container_view() OVERRIDE;
96  virtual const gfx::FontList& font_list() const OVERRIDE;
97  virtual const gfx::Rect& popup_bounds() const OVERRIDE;
98  virtual const gfx::Rect& password_bounds() const OVERRIDE;
99  virtual const gfx::Rect& divider_bounds() const OVERRIDE;
100  virtual const gfx::Rect& help_bounds() const OVERRIDE;
101  virtual bool display_password() const OVERRIDE;
102  virtual bool password_selected() const OVERRIDE;
103  virtual base::string16 password() const OVERRIDE;
104  virtual base::string16 SuggestedText() OVERRIDE;
105  virtual const base::string16& HelpText() OVERRIDE;
106  virtual const gfx::Range& HelpTextLinkRange() OVERRIDE;
107
108  base::WeakPtr<PasswordGenerationPopupControllerImpl> GetWeakPtr();
109
110  bool HandleKeyPressEvent(const content::NativeWebKeyboardEvent& event);
111
112  // Set if the password is currently selected.
113  void PasswordSelected(bool selected);
114
115  // Accept the password. Causes the controller to hide itself as the popup
116  // is no longer necessary.
117  void PasswordAccepted();
118
119  // Accept password if it's selected.
120  bool PossiblyAcceptPassword();
121
122  // Get desired size of popup. Height depends on width because we do text
123  // wrapping.
124  int GetDesiredWidth();
125  int GetDesiredHeight(int width);
126  void CalculateBounds();
127
128  PasswordForm form_;
129  PasswordGenerator* generator_;
130  PasswordManager* password_manager_;
131  // May be NULL.
132  PasswordGenerationPopupObserver* observer_;
133
134  // Contains common popup functionality.
135  PopupControllerCommon controller_common_;
136
137  // Handle to the popup. May be NULL if popup isn't showing.
138  PasswordGenerationPopupView* view_;
139
140  // Font list used in the popup.
141  const gfx::FontList& font_list_;
142
143  // Help text and the range in the text that corresponds to the saved passwords
144  // link.
145  base::string16 help_text_;
146  gfx::Range link_range_;
147
148  base::string16 current_password_;
149  bool password_selected_;
150
151  // If a password will be shown in this popup.
152  bool display_password_;
153
154  // Bounds for all the elements of the popup.
155  gfx::Rect popup_bounds_;
156  gfx::Rect password_bounds_;
157  gfx::Rect divider_bounds_;
158  gfx::Rect help_bounds_;
159
160  base::WeakPtrFactory<PasswordGenerationPopupControllerImpl> weak_ptr_factory_;
161
162  DISALLOW_COPY_AND_ASSIGN(PasswordGenerationPopupControllerImpl);
163};
164
165}  // namespace autofill
166
167#endif  // CHROME_BROWSER_UI_AUTOFILL_PASSWORD_GENERATION_POPUP_CONTROLLER_IMPL_H_
168