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.
5 #include "components/password_manager/content/browser/credential_manager_dispatcher.h"
8 #include "base/memory/scoped_vector.h"
9 #include "base/strings/string16.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "components/autofill/core/common/password_form.h"
12 #include "components/password_manager/content/browser/content_password_manager_driver.h"
13 #include "components/password_manager/content/browser/content_password_manager_driver_factory.h"
14 #include "components/password_manager/content/common/credential_manager_messages.h"
15 #include "components/password_manager/core/browser/password_manager_client.h"
16 #include "components/password_manager/core/browser/password_store.h"
17 #include "components/password_manager/core/common/credential_manager_types.h"
18 #include "components/password_manager/core/common/password_manager_pref_names.h"
19 #include "content/public/browser/render_view_host.h"
20 #include "content/public/browser/web_contents.h"
21 #include "ipc/ipc_message_macros.h"
23 namespace password_manager
{
25 // CredentialManagerDispatcher -------------------------------------------------
27 CredentialManagerDispatcher::CredentialManagerDispatcher(
28 content::WebContents
* web_contents
,
29 PasswordManagerClient
* client
)
30 : WebContentsObserver(web_contents
), client_(client
) {
32 auto_signin_enabled_
.Init(prefs::kPasswordManagerAutoSignin
,
36 CredentialManagerDispatcher::~CredentialManagerDispatcher() {
39 bool CredentialManagerDispatcher::OnMessageReceived(
40 const IPC::Message
& message
) {
42 IPC_BEGIN_MESSAGE_MAP(CredentialManagerDispatcher
, message
)
43 IPC_MESSAGE_HANDLER(CredentialManagerHostMsg_NotifyFailedSignIn
,
44 OnNotifyFailedSignIn
);
45 IPC_MESSAGE_HANDLER(CredentialManagerHostMsg_NotifySignedIn
,
47 IPC_MESSAGE_HANDLER(CredentialManagerHostMsg_RequireUserMediation
,
48 OnRequireUserMediation
);
49 IPC_MESSAGE_HANDLER(CredentialManagerHostMsg_RequestCredential
,
51 IPC_MESSAGE_UNHANDLED(handled
= false)
56 void CredentialManagerDispatcher::OnNotifyFailedSignIn(int request_id
,
57 const CredentialInfo
&) {
59 // TODO(mkwst): This is a stub.
60 web_contents()->GetRenderViewHost()->Send(
61 new CredentialManagerMsg_AcknowledgeFailedSignIn(
62 web_contents()->GetRenderViewHost()->GetRoutingID(), request_id
));
65 void CredentialManagerDispatcher::OnNotifySignedIn(
67 const password_manager::CredentialInfo
& credential
) {
68 DCHECK(credential
.type
!= CredentialType::CREDENTIAL_TYPE_EMPTY
);
70 web_contents()->GetRenderViewHost()->Send(
71 new CredentialManagerMsg_AcknowledgeSignedIn(
72 web_contents()->GetRenderViewHost()->GetRoutingID(), request_id
));
74 if (!client_
->IsSavingEnabledForCurrentPage())
77 scoped_ptr
<autofill::PasswordForm
> form(CreatePasswordFormFromCredentialInfo(
78 credential
, web_contents()->GetLastCommittedURL().GetOrigin()));
79 form
->skip_zero_click
= !IsZeroClickAllowed();
81 // TODO(mkwst): This is a stub; we should be checking the PasswordStore to
82 // determine whether or not the credential exists, and calling UpdateLogin
84 form_manager_
.reset(new CredentialManagerPasswordFormManager(
85 client_
, GetDriver(), *form
, this));
88 void CredentialManagerDispatcher::OnProvisionalSaveComplete() {
89 DCHECK(form_manager_
);
90 if (client_
->IsSavingEnabledForCurrentPage() &&
91 !form_manager_
->IsBlacklisted()) {
92 client_
->PromptUserToSavePassword(
93 form_manager_
.Pass(), CredentialSourceType::CREDENTIAL_SOURCE_API
);
97 void CredentialManagerDispatcher::OnRequireUserMediation(int request_id
) {
100 PasswordStore
* store
= GetPasswordStore();
102 if (!pending_require_user_mediation_
) {
103 pending_require_user_mediation_
.reset(
104 new CredentialManagerPendingRequireUserMediationTask(
105 this, web_contents()->GetLastCommittedURL().GetOrigin()));
107 // This will result in a callback to
108 // CredentialManagerPendingRequireUserMediationTask::OnGetPasswordStoreResults().
109 store
->GetAutofillableLogins(pending_require_user_mediation_
.get());
111 pending_require_user_mediation_
->AddOrigin(
112 web_contents()->GetLastCommittedURL().GetOrigin());
116 web_contents()->GetRenderViewHost()->Send(
117 new CredentialManagerMsg_AcknowledgeRequireUserMediation(
118 web_contents()->GetRenderViewHost()->GetRoutingID(), request_id
));
121 void CredentialManagerDispatcher::OnRequestCredential(
123 bool zero_click_only
,
124 const std::vector
<GURL
>& federations
) {
126 PasswordStore
* store
= GetPasswordStore();
127 if (pending_request_
|| !store
) {
128 web_contents()->GetRenderViewHost()->Send(
129 new CredentialManagerMsg_RejectCredentialRequest(
130 web_contents()->GetRenderViewHost()->GetRoutingID(), request_id
,
132 ? blink::WebCredentialManagerError::ErrorTypePendingRequest
133 : blink::WebCredentialManagerError::
134 ErrorTypePasswordStoreUnavailable
));
138 if (zero_click_only
&& !IsZeroClickAllowed()) {
139 web_contents()->GetRenderViewHost()->Send(
140 new CredentialManagerMsg_SendCredential(
141 web_contents()->GetRenderViewHost()->GetRoutingID(), request_id
,
146 pending_request_
.reset(new CredentialManagerPendingRequestTask(
147 this, request_id
, zero_click_only
,
148 web_contents()->GetLastCommittedURL().GetOrigin(), federations
));
150 // This will result in a callback to
151 // PendingRequestTask::OnGetPasswordStoreResults().
152 store
->GetAutofillableLogins(pending_request_
.get());
155 PasswordStore
* CredentialManagerDispatcher::GetPasswordStore() {
156 return client_
? client_
->GetPasswordStore() : nullptr;
159 bool CredentialManagerDispatcher::IsZeroClickAllowed() const {
160 return *auto_signin_enabled_
&& !client_
->IsOffTheRecord();
163 GURL
CredentialManagerDispatcher::GetOrigin() const {
164 return web_contents()->GetLastCommittedURL().GetOrigin();
167 base::WeakPtr
<PasswordManagerDriver
> CredentialManagerDispatcher::GetDriver() {
168 ContentPasswordManagerDriverFactory
* driver_factory
=
169 ContentPasswordManagerDriverFactory::FromWebContents(web_contents());
170 DCHECK(driver_factory
);
171 PasswordManagerDriver
* driver
=
172 driver_factory
->GetDriverForFrame(web_contents()->GetMainFrame());
173 return driver
->AsWeakPtr();
176 void CredentialManagerDispatcher::SendCredential(int request_id
,
177 const CredentialInfo
& info
) {
178 DCHECK(pending_request_
);
179 DCHECK_EQ(pending_request_
->id(), request_id
);
181 if (PasswordStore
* store
= GetPasswordStore()) {
182 if (info
.type
!= CredentialType::CREDENTIAL_TYPE_EMPTY
&&
183 IsZeroClickAllowed()) {
184 scoped_ptr
<autofill::PasswordForm
> form(
185 CreatePasswordFormFromCredentialInfo(info
,
186 pending_request_
->origin()));
187 form
->skip_zero_click
= false;
188 store
->UpdateLogin(*form
);
192 web_contents()->GetRenderViewHost()->Send(
193 new CredentialManagerMsg_SendCredential(
194 web_contents()->GetRenderViewHost()->GetRoutingID(),
195 pending_request_
->id(), info
));
196 pending_request_
.reset();
199 PasswordManagerClient
* CredentialManagerDispatcher::client() const {
203 void CredentialManagerDispatcher::DoneRequiringUserMediation() {
204 DCHECK(pending_require_user_mediation_
);
205 pending_require_user_mediation_
.reset();
208 } // namespace password_manager