Home | History | Annotate | Download | only in autofill
      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/font_list.h"
     16 #include "ui/gfx/native_widget_types.h"
     17 #include "ui/gfx/range/range.h"
     18 #include "ui/gfx/rect.h"
     19 #include "ui/gfx/rect_f.h"
     20 
     21 namespace content {
     22 struct NativeWebKeyboardEvent;
     23 class WebContents;
     24 }
     25 
     26 namespace password_manager {
     27 class PasswordManager;
     28 }
     29 
     30 namespace autofill {
     31 
     32 class PasswordGenerator;
     33 class PasswordGenerationPopupObserver;
     34 class PasswordGenerationPopupView;
     35 
     36 // This class controls a PasswordGenerationPopupView. It is responsible for
     37 // determining the location of the popup, handling keypress events while the
     38 // popup is active, and notifying both the renderer and the password manager
     39 // if the password is accepted.
     40 class PasswordGenerationPopupControllerImpl
     41     : public PasswordGenerationPopupController {
     42  public:
     43   // Create a controller or return |previous| if it is suitable. Will hide
     44   // |previous| if it is not returned. |bounds| is the bounds of the element
     45   // that we are showing the dropdown for in screen space. |form| is the
     46   // identifier for the form that we are filling, and is used to notify
     47   // |password_manager| if the password is generated. |max_length| is used to
     48   // determine the length of the password shown. If not NULL, |observer| will
     49   // be notified of changes of the popup state.
     50   static base::WeakPtr<PasswordGenerationPopupControllerImpl> GetOrCreate(
     51       base::WeakPtr<PasswordGenerationPopupControllerImpl> previous,
     52       const gfx::RectF& bounds,
     53       const PasswordForm& form,
     54       int max_length,
     55       password_manager::PasswordManager* password_manager,
     56       PasswordGenerationPopupObserver* observer,
     57       content::WebContents* web_contents,
     58       gfx::NativeView container_view);
     59   virtual ~PasswordGenerationPopupControllerImpl();
     60 
     61   // Create a PasswordGenerationPopupView if one doesn't already exist.
     62   // If |display_password| is true, a generated password is shown that can be
     63   // selected by the user. Otherwise just the text explaining generated
     64   // passwords is shown.
     65   void Show(bool display_password);
     66 
     67   // Hides the popup and destroys |this|.
     68   void HideAndDestroy();
     69 
     70   // Accessors.
     71   content::WebContents* web_contents() {
     72     return controller_common_.web_contents();
     73   }
     74   const gfx::RectF& element_bounds() {
     75     return controller_common_.element_bounds();
     76   }
     77 
     78  private:
     79   PasswordGenerationPopupControllerImpl(
     80       const gfx::RectF& bounds,
     81       const PasswordForm& form,
     82       int max_length,
     83       password_manager::PasswordManager* password_manager,
     84       PasswordGenerationPopupObserver* observer,
     85       content::WebContents* web_contents,
     86       gfx::NativeView container_view);
     87 
     88   // PasswordGenerationPopupController implementation:
     89   virtual void Hide() OVERRIDE;
     90   virtual void ViewDestroyed() OVERRIDE;
     91   virtual void SetSelectionAtPoint(const gfx::Point& point) OVERRIDE;
     92   virtual bool AcceptSelectedLine() OVERRIDE;
     93   virtual void SelectionCleared() 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   password_manager::PasswordManager* password_manager_;
    130 
    131   // May be NULL.
    132   PasswordGenerationPopupObserver* observer_;
    133 
    134   // Controls how passwords are generated.
    135   scoped_ptr<PasswordGenerator> generator_;
    136 
    137   // Contains common popup functionality.
    138   PopupControllerCommon controller_common_;
    139 
    140   // Handle to the popup. May be NULL if popup isn't showing.
    141   PasswordGenerationPopupView* view_;
    142 
    143   // Font list used in the popup.
    144   const gfx::FontList& font_list_;
    145 
    146   // Help text and the range in the text that corresponds to the saved passwords
    147   // link.
    148   base::string16 help_text_;
    149   gfx::Range link_range_;
    150 
    151   base::string16 current_password_;
    152   bool password_selected_;
    153 
    154   // If a password will be shown in this popup.
    155   bool display_password_;
    156 
    157   // Bounds for all the elements of the popup.
    158   gfx::Rect popup_bounds_;
    159   gfx::Rect password_bounds_;
    160   gfx::Rect divider_bounds_;
    161   gfx::Rect help_bounds_;
    162 
    163   base::WeakPtrFactory<PasswordGenerationPopupControllerImpl> weak_ptr_factory_;
    164 
    165   DISALLOW_COPY_AND_ASSIGN(PasswordGenerationPopupControllerImpl);
    166 };
    167 
    168 }  // namespace autofill
    169 
    170 #endif  // CHROME_BROWSER_UI_AUTOFILL_PASSWORD_GENERATION_POPUP_CONTROLLER_IMPL_H_
    171