password_generation_popup_controller_impl.h revision 116680a4aac90f2aa7413d9095a592090648e557
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 20namespace content { 21struct NativeWebKeyboardEvent; 22class WebContents; 23} 24 25namespace password_manager { 26class PasswordManager; 27} 28 29namespace autofill { 30 31class PasswordGenerator; 32class PasswordGenerationPopupObserver; 33class 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. 39class 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 virtual ~PasswordGenerationPopupControllerImpl(); 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. 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 const gfx::RectF& element_bounds() { 74 return controller_common_.element_bounds(); 75 } 76 77 private: 78 PasswordGenerationPopupControllerImpl( 79 const gfx::RectF& bounds, 80 const PasswordForm& form, 81 int max_length, 82 password_manager::PasswordManager* password_manager, 83 PasswordGenerationPopupObserver* observer, 84 content::WebContents* web_contents, 85 gfx::NativeView container_view); 86 87 // PasswordGenerationPopupController implementation: 88 virtual void Hide() OVERRIDE; 89 virtual void ViewDestroyed() OVERRIDE; 90 virtual void SetSelectionAtPoint(const gfx::Point& point) OVERRIDE; 91 virtual bool AcceptSelectedLine() OVERRIDE; 92 virtual void SelectionCleared() OVERRIDE; 93 virtual void OnSavedPasswordsLinkClicked() OVERRIDE; 94 virtual int GetMinimumWidth() OVERRIDE; 95 virtual gfx::NativeView container_view() OVERRIDE; 96 virtual const gfx::Rect& popup_bounds() const OVERRIDE; 97 virtual bool display_password() const OVERRIDE; 98 virtual bool password_selected() const OVERRIDE; 99 virtual base::string16 password() const OVERRIDE; 100 virtual base::string16 SuggestedText() OVERRIDE; 101 virtual const base::string16& HelpText() OVERRIDE; 102 virtual const gfx::Range& HelpTextLinkRange() OVERRIDE; 103 104 base::WeakPtr<PasswordGenerationPopupControllerImpl> GetWeakPtr(); 105 106 bool HandleKeyPressEvent(const content::NativeWebKeyboardEvent& event); 107 108 // Set if the password is currently selected. 109 void PasswordSelected(bool selected); 110 111 // Accept the password. Causes the controller to hide itself as the popup 112 // is no longer necessary. 113 void PasswordAccepted(); 114 115 // Accept password if it's selected. 116 bool PossiblyAcceptPassword(); 117 118 // Get desired size of popup. Height depends on width because we do text 119 // wrapping. 120 void CalculateBounds(); 121 122 PasswordForm form_; 123 password_manager::PasswordManager* password_manager_; 124 125 // May be NULL. 126 PasswordGenerationPopupObserver* observer_; 127 128 // Controls how passwords are generated. 129 scoped_ptr<PasswordGenerator> generator_; 130 131 // Contains common popup functionality. 132 PopupControllerCommon controller_common_; 133 134 // Handle to the popup. May be NULL if popup isn't showing. 135 PasswordGenerationPopupView* view_; 136 137 // Help text and the range in the text that corresponds to the saved passwords 138 // link. 139 base::string16 help_text_; 140 gfx::Range link_range_; 141 142 base::string16 current_password_; 143 bool password_selected_; 144 145 // If a password will be shown in this popup. 146 bool display_password_; 147 148 // Bounds for all the elements of the popup. 149 gfx::Rect popup_bounds_; 150 151 base::WeakPtrFactory<PasswordGenerationPopupControllerImpl> weak_ptr_factory_; 152 153 DISALLOW_COPY_AND_ASSIGN(PasswordGenerationPopupControllerImpl); 154}; 155 156} // namespace autofill 157 158#endif // CHROME_BROWSER_UI_AUTOFILL_PASSWORD_GENERATION_POPUP_CONTROLLER_IMPL_H_ 159