Home | History | Annotate | Download | only in password_manager
      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 CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_GENERATION_MANAGER_H_
      6 #define CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_GENERATION_MANAGER_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "base/memory/scoped_ptr.h"
     10 #include "base/memory/weak_ptr.h"
     11 #include "base/prefs/pref_change_registrar.h"
     12 #include "chrome/browser/sync/profile_sync_service_observer.h"
     13 #include "content/public/browser/web_contents_observer.h"
     14 #include "content/public/browser/web_contents_user_data.h"
     15 
     16 namespace autofill {
     17 class PasswordGenerator;
     18 }
     19 
     20 namespace content {
     21 struct PasswordForm;
     22 }
     23 
     24 namespace user_prefs {
     25 class PrefRegistrySyncable;
     26 }
     27 
     28 // Per-tab manager for password generation. Will enable this feature only if
     29 //
     30 // -  Password manager is enabled
     31 // -  Password sync is enabled
     32 // -  Password generation pref is enabled
     33 //
     34 // NOTE: At the moment, the creation of the renderer PasswordGenerationManager
     35 // is controlled by a switch (--enable-password-generation) so this feature will
     36 // not be enabled regardless of the above criteria without the switch being
     37 // present.
     38 //
     39 // When enabled we will send a message enabling this feature in the renderer,
     40 // which will show an icon next to password fields which we think are associated
     41 // with account creation. This class also manages the popup which is created
     42 // if the user chooses to generate a password.
     43 class PasswordGenerationManager
     44     : public ProfileSyncServiceObserver,
     45       public content::WebContentsObserver,
     46       public content::WebContentsUserData<PasswordGenerationManager> {
     47  public:
     48   static void CreateForWebContents(content::WebContents* contents);
     49   static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
     50   virtual ~PasswordGenerationManager();
     51 
     52  protected:
     53   explicit PasswordGenerationManager(content::WebContents* contents);
     54 
     55  private:
     56   friend class content::WebContentsUserData<PasswordGenerationManager>;
     57   friend class PasswordGenerationManagerTest;
     58 
     59   // WebContentsObserver:
     60   virtual void RenderViewCreated(content::RenderViewHost* host) OVERRIDE;
     61   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
     62   virtual void WebContentsDestroyed(content::WebContents* contents) OVERRIDE;
     63 
     64   // ProfileSyncServiceObserver:
     65   virtual void OnStateChanged() OVERRIDE;
     66 
     67   // Add ourselves as an observer to the sync service to be informed of changes
     68   // to the password sync state.
     69   void RegisterWithSyncService();
     70 
     71   // Start watching for changes to the password generation enabled pref.
     72   void SetUpPrefChangeRegistrar();
     73   void OnPrefStateChanged();
     74 
     75   // Determines current state of password generation and sends this information
     76   // to the renderer if it is different from |enabled_| or if |new_renderer|
     77   // is true.
     78   void UpdateState(content::RenderViewHost* host, bool new_renderer);
     79 
     80   // Sends a message to the renderer enabling or disabling this feature. This
     81   // is a separate function to aid in testing.
     82   virtual void SendStateToRenderer(content::RenderViewHost* host, bool enabled);
     83 
     84   // Causes the password generation bubble UI to be shown for the specified
     85   // form. The popup will be anchored at |icon_bounds|. The generated
     86   // password will be no longer than |max_length|.
     87   void OnShowPasswordGenerationPopup(const gfx::Rect& icon_bounds,
     88                                      int max_length,
     89                                      const content::PasswordForm& form);
     90 
     91   // Whether password generation is enabled.
     92   bool enabled_;
     93 
     94   // Listens for changes to the state of the password generation pref.
     95   PrefChangeRegistrar registrar_;
     96 
     97   // For vending a weak_ptr for |registrar_|.
     98   base::WeakPtrFactory<PasswordGenerationManager> weak_factory_;
     99 
    100   // Controls how passwords are generated.
    101   scoped_ptr<autofill::PasswordGenerator> password_generator_;
    102 
    103   DISALLOW_COPY_AND_ASSIGN(PasswordGenerationManager);
    104 };
    105 
    106 #endif  // CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_GENERATION_MANAGER_H_
    107