summaryrefslogtreecommitdiffstats
path: root/chrome/browser/password_manager/password_generation_manager.h
blob: 62d827a79d37f33237ea2bcdf3c76ce33f4ab8b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_GENERATION_MANAGER_H_
#define CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_GENERATION_MANAGER_H_

#include <vector>

#include "base/basictypes.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "ui/gfx/rect.h"

class PasswordManager;
class PasswordManagerDelegate;
class PasswordManagerDriver;

namespace autofill {
struct FormData;
class FormStructure;
class PasswordGenerator;
class PasswordGenerationPopupControllerImpl;
class PasswordGenerationPopupObserver;
struct PasswordForm;
}

namespace content {
class RenderViewHost;
class WebContents;
}

namespace user_prefs {
class PrefRegistrySyncable;
}

// Per-tab manager for password generation. Will enable this feature only if
//
// -  Password manager is enabled
// -  Password sync is enabled
//
// NOTE: At the moment, the creation of the renderer PasswordGenerationManager
// is controlled by a switch (--enable-password-generation) so this feature will
// not be enabled regardless of the above criteria without the switch being
// present.
//
// This class is used to determine what forms we should offer to generate
// passwords for and manages the popup which is created if the user chooses to
// generate a password.
class PasswordGenerationManager {
 public:
  PasswordGenerationManager(content::WebContents* contents,
                            PasswordManagerDelegate* delegate);
  virtual ~PasswordGenerationManager();

  // Detect account creation forms from forms with autofill type annotated.
  // Will send a message to the renderer if we find a correctly annotated form
  // and the feature is enabled.
  void DetectAccountCreationForms(
      const std::vector<autofill::FormStructure*>& forms);

  // Hide any visible password generation related popups.
  void HidePopup();

  // Observer for PasswordGenerationPopup events. Used for testing.
  void SetTestObserver(autofill::PasswordGenerationPopupObserver* observer);

  // Causes the password generation UI to be shown for the specified form.
  // The popup will be anchored at |element_bounds|. The generated password
  // will be no longer than |max_length|.
  void OnShowPasswordGenerationPopup(const gfx::RectF& element_bounds,
                                     int max_length,
                                     const autofill::PasswordForm& form);

  // Causes the password editing UI to be shown anchored at |element_bounds|.
  void OnShowPasswordEditingPopup(const gfx::RectF& element_bounds);

  // Hides any visible UI.
  void OnHidePasswordGenerationPopup();

 private:
  friend class PasswordGenerationManagerTest;

  // Determines current state of password generation
  bool IsGenerationEnabled() const;

  // Sends a message to the renderer specifying form(s) that we should enable
  // password generation on. This is a separate function to aid in testing.
  virtual void SendAccountCreationFormsToRenderer(
      content::RenderViewHost* host,
      const std::vector<autofill::FormData>& forms);

  // Given |bounds| in the renderers coordinate system, return the same bounds
  // in the screens coordinate system.
  gfx::RectF GetBoundsInScreenSpace(const gfx::RectF& bounds);

  // The WebContents instance associated with this instance. Scoped to the
  // lifetime of this class, as this class is indirectly a WCUD via
  // PasswordManagerDelegateImpl.
  // TODO(blundell): Eliminate this ivar. crbug.com/340675
  content::WebContents* web_contents_;

  // Observer for password generation popup.
  autofill::PasswordGenerationPopupObserver* observer_;

  // Controls how passwords are generated.
  scoped_ptr<autofill::PasswordGenerator> password_generator_;

  // Controls the popup
  base::WeakPtr<
    autofill::PasswordGenerationPopupControllerImpl> popup_controller_;

  // The PasswordManagerDelegate instance associated with this instance. Must
  // outlive this instance.
  PasswordManagerDelegate* delegate_;

  // The PasswordManagerDriver instance associated with this instance. Must
  // outlive this instance.
  PasswordManagerDriver* driver_;

  DISALLOW_COPY_AND_ASSIGN(PasswordGenerationManager);
};

#endif  // CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_GENERATION_MANAGER_H_