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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
// 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.
#include "chrome/browser/password_manager/password_generation_manager.h"
#include "chrome/browser/password_manager/password_manager.h"
#include "chrome/browser/password_manager/password_manager_delegate.h"
#include "chrome/browser/password_manager/password_manager_driver.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sync/profile_sync_service.h"
#include "chrome/browser/sync/profile_sync_service_factory.h"
#include "chrome/browser/ui/autofill/password_generation_popup_controller_impl.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window.h"
#include "components/autofill/content/common/autofill_messages.h"
#include "components/autofill/core/browser/autofill_field.h"
#include "components/autofill/core/browser/field_types.h"
#include "components/autofill/core/browser/form_structure.h"
#include "components/autofill/core/browser/password_generator.h"
#include "components/autofill/core/common/form_data.h"
#include "components/autofill/core/common/password_form.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_view.h"
#include "ui/gfx/rect.h"
PasswordGenerationManager::PasswordGenerationManager(
content::WebContents* contents,
PasswordManagerDelegate* delegate)
: web_contents_(contents),
observer_(NULL),
delegate_(delegate),
driver_(delegate->GetDriver()) {}
PasswordGenerationManager::~PasswordGenerationManager() {}
void PasswordGenerationManager::SetTestObserver(
autofill::PasswordGenerationPopupObserver* observer) {
observer_ = observer;
}
void PasswordGenerationManager::DetectAccountCreationForms(
const std::vector<autofill::FormStructure*>& forms) {
std::vector<autofill::FormData> account_creation_forms;
for (std::vector<autofill::FormStructure*>::const_iterator form_it =
forms.begin(); form_it != forms.end(); ++form_it) {
autofill::FormStructure* form = *form_it;
for (std::vector<autofill::AutofillField*>::const_iterator field_it =
form->begin(); field_it != form->end(); ++field_it) {
autofill::AutofillField* field = *field_it;
if (field->server_type() == autofill::ACCOUNT_CREATION_PASSWORD) {
account_creation_forms.push_back(form->ToFormData());
break;
}
}
}
if (!account_creation_forms.empty() && IsGenerationEnabled()) {
SendAccountCreationFormsToRenderer(web_contents_->GetRenderViewHost(),
account_creation_forms);
}
}
// In order for password generation to be enabled, we need to make sure:
// (1) Password sync is enabled, and
// (2) Password saving is enabled.
bool PasswordGenerationManager::IsGenerationEnabled() const {
if (!web_contents_)
return false;
Profile* profile =
Profile::FromBrowserContext(web_contents_->GetBrowserContext());
if (!driver_->GetPasswordManager()->IsSavingEnabled()) {
DVLOG(2) << "Generation disabled because password saving is disabled";
return false;
}
bool password_sync_enabled = false;
ProfileSyncService* sync_service =
ProfileSyncServiceFactory::GetForProfile(profile);
if (sync_service) {
syncer::ModelTypeSet sync_set = sync_service->GetActiveDataTypes();
password_sync_enabled = (sync_service->HasSyncSetupCompleted() &&
sync_set.Has(syncer::PASSWORDS));
}
if (!password_sync_enabled) {
DVLOG(2) << "Generation disabled because passwords are not being synced";
return false;
}
return true;
}
void PasswordGenerationManager::SendAccountCreationFormsToRenderer(
content::RenderViewHost* host,
const std::vector<autofill::FormData>& forms) {
host->Send(new AutofillMsg_AccountCreationFormsDetected(
host->GetRoutingID(), forms));
}
gfx::RectF PasswordGenerationManager::GetBoundsInScreenSpace(
const gfx::RectF& bounds) {
gfx::Rect client_area;
web_contents_->GetView()->GetContainerBounds(&client_area);
return bounds + client_area.OffsetFromOrigin();
}
void PasswordGenerationManager::OnShowPasswordGenerationPopup(
const gfx::RectF& bounds,
int max_length,
const autofill::PasswordForm& form) {
// TODO(gcasto): Validate data in PasswordForm.
// Only implemented for Aura right now.
#if defined(USE_AURA)
// Convert element_bounds to be in screen space.
gfx::RectF element_bounds_in_screen_space = GetBoundsInScreenSpace(bounds);
password_generator_.reset(new autofill::PasswordGenerator(max_length));
popup_controller_ =
autofill::PasswordGenerationPopupControllerImpl::GetOrCreate(
popup_controller_,
element_bounds_in_screen_space,
form,
password_generator_.get(),
driver_->GetPasswordManager(),
observer_,
web_contents_,
web_contents_->GetView()->GetNativeView());
popup_controller_->Show();
#endif // #if defined(USE_AURA)
}
void PasswordGenerationManager::OnShowPasswordEditingPopup(
const gfx::RectF& bounds) {
// TODO(gcasto): Enable this.
}
void PasswordGenerationManager::OnHidePasswordGenerationPopup() {
HidePopup();
}
void PasswordGenerationManager::HidePopup() {
if (popup_controller_)
popup_controller_->HideAndDestroy();
}
|