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.
5 #include "chrome/browser/password_manager/password_generation_manager.h"
7 #include "chrome/browser/password_manager/password_manager.h"
8 #include "chrome/browser/password_manager/password_manager_client.h"
9 #include "chrome/browser/password_manager/password_manager_driver.h"
10 #include "chrome/browser/ui/autofill/password_generation_popup_controller_impl.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/browser_finder.h"
13 #include "chrome/browser/ui/browser_window.h"
14 #include "components/autofill/content/common/autofill_messages.h"
15 #include "components/autofill/core/browser/autofill_field.h"
16 #include "components/autofill/core/browser/field_types.h"
17 #include "components/autofill/core/browser/form_structure.h"
18 #include "components/autofill/core/browser/password_generator.h"
19 #include "components/autofill/core/common/form_data.h"
20 #include "components/autofill/core/common/password_form.h"
21 #include "content/public/browser/render_view_host.h"
22 #include "content/public/browser/web_contents.h"
23 #include "content/public/browser/web_contents_view.h"
24 #include "ui/gfx/rect.h"
26 PasswordGenerationManager::PasswordGenerationManager(
27 content::WebContents
* contents
,
28 PasswordManagerClient
* client
)
29 : web_contents_(contents
),
32 driver_(client
->GetDriver()) {}
34 PasswordGenerationManager::~PasswordGenerationManager() {}
36 void PasswordGenerationManager::SetTestObserver(
37 autofill::PasswordGenerationPopupObserver
* observer
) {
41 void PasswordGenerationManager::DetectAccountCreationForms(
42 const std::vector
<autofill::FormStructure
*>& forms
) {
43 std::vector
<autofill::FormData
> account_creation_forms
;
44 for (std::vector
<autofill::FormStructure
*>::const_iterator form_it
=
45 forms
.begin(); form_it
!= forms
.end(); ++form_it
) {
46 autofill::FormStructure
* form
= *form_it
;
47 for (std::vector
<autofill::AutofillField
*>::const_iterator field_it
=
48 form
->begin(); field_it
!= form
->end(); ++field_it
) {
49 autofill::AutofillField
* field
= *field_it
;
50 if (field
->server_type() == autofill::ACCOUNT_CREATION_PASSWORD
) {
51 account_creation_forms
.push_back(form
->ToFormData());
56 if (!account_creation_forms
.empty() && IsGenerationEnabled()) {
57 SendAccountCreationFormsToRenderer(web_contents_
->GetRenderViewHost(),
58 account_creation_forms
);
62 // In order for password generation to be enabled, we need to make sure:
63 // (1) Password sync is enabled, and
64 // (2) Password saving is enabled.
65 bool PasswordGenerationManager::IsGenerationEnabled() const {
66 if (!driver_
->GetPasswordManager()->IsSavingEnabled()) {
67 DVLOG(2) << "Generation disabled because password saving is disabled";
71 if (!client_
->IsPasswordSyncEnabled()) {
72 DVLOG(2) << "Generation disabled because passwords are not being synced";
79 void PasswordGenerationManager::SendAccountCreationFormsToRenderer(
80 content::RenderViewHost
* host
,
81 const std::vector
<autofill::FormData
>& forms
) {
82 host
->Send(new AutofillMsg_AccountCreationFormsDetected(
83 host
->GetRoutingID(), forms
));
86 gfx::RectF
PasswordGenerationManager::GetBoundsInScreenSpace(
87 const gfx::RectF
& bounds
) {
88 gfx::Rect client_area
;
89 web_contents_
->GetView()->GetContainerBounds(&client_area
);
90 return bounds
+ client_area
.OffsetFromOrigin();
93 void PasswordGenerationManager::OnShowPasswordGenerationPopup(
94 const gfx::RectF
& bounds
,
96 const autofill::PasswordForm
& form
) {
97 // TODO(gcasto): Validate data in PasswordForm.
99 // Only implemented for Aura right now.
100 #if defined(USE_AURA)
101 // Convert element_bounds to be in screen space.
102 gfx::RectF element_bounds_in_screen_space
= GetBoundsInScreenSpace(bounds
);
104 password_generator_
.reset(new autofill::PasswordGenerator(max_length
));
107 autofill::PasswordGenerationPopupControllerImpl::GetOrCreate(
109 element_bounds_in_screen_space
,
111 password_generator_
.get(),
112 driver_
->GetPasswordManager(),
115 web_contents_
->GetView()->GetNativeView());
116 popup_controller_
->Show(true /* display_password */);
117 #endif // #if defined(USE_AURA)
120 void PasswordGenerationManager::OnShowPasswordEditingPopup(
121 const gfx::RectF
& bounds
,
122 const autofill::PasswordForm
& form
) {
123 // Only implemented for Aura right now.
124 #if defined(USE_AURA)
125 gfx::RectF element_bounds_in_screen_space
= GetBoundsInScreenSpace(bounds
);
128 autofill::PasswordGenerationPopupControllerImpl::GetOrCreate(
130 element_bounds_in_screen_space
,
132 password_generator_
.get(),
133 driver_
->GetPasswordManager(),
136 web_contents_
->GetView()->GetNativeView());
137 popup_controller_
->Show(false /* display_password */);
138 #endif // #if defined(USE_AURA)
141 void PasswordGenerationManager::OnHidePasswordGenerationPopup() {
145 void PasswordGenerationManager::HidePopup() {
146 if (popup_controller_
)
147 popup_controller_
->HideAndDestroy();