OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/password_manager/content/browser/content_credential_manager
_dispatcher.h" | 5 #include "components/password_manager/content/browser/content_credential_manager
_dispatcher.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/scoped_vector.h" |
8 #include "base/strings/string16.h" | 9 #include "base/strings/string16.h" |
9 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
10 #include "components/autofill/core/common/password_form.h" | 11 #include "components/autofill/core/common/password_form.h" |
11 #include "components/password_manager/content/browser/content_password_manager_d
river.h" | 12 #include "components/password_manager/content/browser/content_password_manager_d
river.h" |
12 #include "components/password_manager/content/browser/content_password_manager_d
river_factory.h" | 13 #include "components/password_manager/content/browser/content_password_manager_d
river_factory.h" |
13 #include "components/password_manager/content/browser/credential_manager_passwor
d_form_manager.h" | 14 #include "components/password_manager/content/browser/credential_manager_passwor
d_form_manager.h" |
14 #include "components/password_manager/content/common/credential_manager_messages
.h" | 15 #include "components/password_manager/content/common/credential_manager_messages
.h" |
15 #include "components/password_manager/content/common/credential_manager_types.h" | 16 #include "components/password_manager/content/common/credential_manager_types.h" |
16 #include "components/password_manager/core/browser/password_manager_client.h" | 17 #include "components/password_manager/core/browser/password_manager_client.h" |
17 #include "components/password_manager/core/browser/password_store.h" | 18 #include "components/password_manager/core/browser/password_store.h" |
18 #include "content/public/browser/render_view_host.h" | 19 #include "content/public/browser/render_view_host.h" |
19 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
20 #include "ipc/ipc_message_macros.h" | 21 #include "ipc/ipc_message_macros.h" |
21 | 22 |
22 namespace password_manager { | 23 namespace password_manager { |
23 | 24 |
| 25 struct ContentCredentialManagerDispatcher::PendingRequestParameters { |
| 26 PendingRequestParameters(int request_id, |
| 27 bool request_zero_click_only, |
| 28 GURL request_origin, |
| 29 const std::vector<GURL>& request_federations) |
| 30 : id(request_id), |
| 31 zero_click_only(request_zero_click_only), |
| 32 origin(request_origin), |
| 33 federations(request_federations) {} |
| 34 |
| 35 int id; |
| 36 bool zero_click_only; |
| 37 GURL origin; |
| 38 std::vector<GURL> federations; |
| 39 }; |
| 40 |
24 ContentCredentialManagerDispatcher::ContentCredentialManagerDispatcher( | 41 ContentCredentialManagerDispatcher::ContentCredentialManagerDispatcher( |
25 content::WebContents* web_contents, | 42 content::WebContents* web_contents, |
26 PasswordManagerClient* client) | 43 PasswordManagerClient* client) |
27 : WebContentsObserver(web_contents), | 44 : WebContentsObserver(web_contents), client_(client) { |
28 client_(client), | |
29 pending_request_id_(0) { | |
30 DCHECK(web_contents); | 45 DCHECK(web_contents); |
31 } | 46 } |
32 | 47 |
33 ContentCredentialManagerDispatcher::~ContentCredentialManagerDispatcher() {} | 48 ContentCredentialManagerDispatcher::~ContentCredentialManagerDispatcher() {} |
34 | 49 |
35 bool ContentCredentialManagerDispatcher::OnMessageReceived( | 50 bool ContentCredentialManagerDispatcher::OnMessageReceived( |
36 const IPC::Message& message) { | 51 const IPC::Message& message) { |
37 bool handled = true; | 52 bool handled = true; |
38 IPC_BEGIN_MESSAGE_MAP(ContentCredentialManagerDispatcher, message) | 53 IPC_BEGIN_MESSAGE_MAP(ContentCredentialManagerDispatcher, message) |
39 IPC_MESSAGE_HANDLER(CredentialManagerHostMsg_NotifyFailedSignIn, | 54 IPC_MESSAGE_HANDLER(CredentialManagerHostMsg_NotifyFailedSignIn, |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 void ContentCredentialManagerDispatcher::OnNotifySignedOut(int request_id) { | 101 void ContentCredentialManagerDispatcher::OnNotifySignedOut(int request_id) { |
87 DCHECK(request_id); | 102 DCHECK(request_id); |
88 // TODO(mkwst): This is a stub. | 103 // TODO(mkwst): This is a stub. |
89 web_contents()->GetRenderViewHost()->Send( | 104 web_contents()->GetRenderViewHost()->Send( |
90 new CredentialManagerMsg_AcknowledgeSignedOut( | 105 new CredentialManagerMsg_AcknowledgeSignedOut( |
91 web_contents()->GetRenderViewHost()->GetRoutingID(), request_id)); | 106 web_contents()->GetRenderViewHost()->GetRoutingID(), request_id)); |
92 } | 107 } |
93 | 108 |
94 void ContentCredentialManagerDispatcher::OnRequestCredential( | 109 void ContentCredentialManagerDispatcher::OnRequestCredential( |
95 int request_id, | 110 int request_id, |
96 bool /* zero_click_only */, | 111 bool zero_click_only, |
97 const std::vector<GURL>& /* federations */) { | 112 const std::vector<GURL>& federations) { |
98 DCHECK(request_id); | 113 DCHECK(request_id); |
99 PasswordStore* store = GetPasswordStore(); | 114 PasswordStore* store = GetPasswordStore(); |
100 if (pending_request_id_ || !store) { | 115 if (pending_request_ || !store) { |
101 web_contents()->GetRenderViewHost()->Send( | 116 web_contents()->GetRenderViewHost()->Send( |
102 new CredentialManagerMsg_RejectCredentialRequest( | 117 new CredentialManagerMsg_RejectCredentialRequest( |
103 web_contents()->GetRenderViewHost()->GetRoutingID(), | 118 web_contents()->GetRenderViewHost()->GetRoutingID(), request_id, |
104 request_id, | 119 pending_request_ |
105 pending_request_id_ | |
106 ? blink::WebCredentialManagerError::ErrorTypePendingRequest | 120 ? blink::WebCredentialManagerError::ErrorTypePendingRequest |
107 : blink::WebCredentialManagerError:: | 121 : blink::WebCredentialManagerError:: |
108 ErrorTypePasswordStoreUnavailable)); | 122 ErrorTypePasswordStoreUnavailable)); |
109 return; | 123 return; |
110 } | 124 } |
111 | 125 |
112 pending_request_id_ = request_id; | 126 pending_request_.reset(new PendingRequestParameters( |
| 127 request_id, zero_click_only, |
| 128 web_contents()->GetLastCommittedURL().GetOrigin(), federations)); |
113 | 129 |
114 // TODO(mkwst): we should deal with federated login types. | 130 store->GetAutofillableLogins(this); |
115 autofill::PasswordForm form; | |
116 form.scheme = autofill::PasswordForm::SCHEME_HTML; | |
117 form.origin = web_contents()->GetLastCommittedURL().GetOrigin(); | |
118 form.signon_realm = form.origin.spec(); | |
119 | |
120 store->GetLogins(form, PasswordStore::DISALLOW_PROMPT, this); | |
121 } | 131 } |
122 | 132 |
123 void ContentCredentialManagerDispatcher::OnGetPasswordStoreResults( | 133 void ContentCredentialManagerDispatcher::OnGetPasswordStoreResults( |
124 const std::vector<autofill::PasswordForm*>& results) { | 134 const std::vector<autofill::PasswordForm*>& results) { |
125 DCHECK(pending_request_id_); | 135 DCHECK(pending_request_); |
126 | 136 |
127 if (results.empty() || | 137 // We own the PasswordForm instances, so we're responsible for cleaning |
128 !client_->PromptUserToChooseCredentials(results, base::Bind( | 138 // up the instances we don't add to |filtered_results|. We'll dump them |
129 &ContentCredentialManagerDispatcher::SendCredential, | 139 // into a ScopedVector and allow it to delete the PasswordForms upon |
130 base::Unretained(this), | 140 // destruction. |
131 pending_request_id_))) { | 141 std::vector<autofill::PasswordForm*> filtered_results; |
132 SendCredential(pending_request_id_, CredentialInfo()); | 142 ScopedVector<autofill::PasswordForm> discarded_results; |
| 143 for (autofill::PasswordForm* form : results) { |
| 144 // TODO(mkwst): Extend this filter to include federations. |
| 145 if (form->origin == pending_request_->origin) { |
| 146 filtered_results.push_back(form); |
| 147 } else { |
| 148 discarded_results.push_back(form); |
| 149 } |
| 150 } |
| 151 |
| 152 if (filtered_results.empty() || |
| 153 web_contents()->GetLastCommittedURL().GetOrigin() != |
| 154 pending_request_->origin) { |
| 155 SendCredential(pending_request_->id, CredentialInfo()); |
| 156 return; |
| 157 } |
| 158 |
| 159 if (!client_->PromptUserToChooseCredentials( |
| 160 filtered_results, |
| 161 base::Bind(&ContentCredentialManagerDispatcher::SendCredential, |
| 162 base::Unretained(this), pending_request_->id))) { |
| 163 SendCredential(pending_request_->id, CredentialInfo()); |
133 } | 164 } |
134 } | 165 } |
135 | 166 |
136 PasswordStore* ContentCredentialManagerDispatcher::GetPasswordStore() { | 167 PasswordStore* ContentCredentialManagerDispatcher::GetPasswordStore() { |
137 return client_ ? client_->GetPasswordStore() : nullptr; | 168 return client_ ? client_->GetPasswordStore() : nullptr; |
138 } | 169 } |
139 | 170 |
140 base::WeakPtr<PasswordManagerDriver> | 171 base::WeakPtr<PasswordManagerDriver> |
141 ContentCredentialManagerDispatcher::GetDriver() { | 172 ContentCredentialManagerDispatcher::GetDriver() { |
142 ContentPasswordManagerDriverFactory* driver_factory = | 173 ContentPasswordManagerDriverFactory* driver_factory = |
143 ContentPasswordManagerDriverFactory::FromWebContents(web_contents()); | 174 ContentPasswordManagerDriverFactory::FromWebContents(web_contents()); |
144 DCHECK(driver_factory); | 175 DCHECK(driver_factory); |
145 PasswordManagerDriver* driver = | 176 PasswordManagerDriver* driver = |
146 driver_factory->GetDriverForFrame(web_contents()->GetMainFrame()); | 177 driver_factory->GetDriverForFrame(web_contents()->GetMainFrame()); |
147 return driver->AsWeakPtr(); | 178 return driver->AsWeakPtr(); |
148 } | 179 } |
149 | 180 |
150 void ContentCredentialManagerDispatcher::SendCredential( | 181 void ContentCredentialManagerDispatcher::SendCredential( |
151 int request_id, const CredentialInfo& info) { | 182 int request_id, const CredentialInfo& info) { |
152 DCHECK(pending_request_id_); | 183 DCHECK(pending_request_); |
153 DCHECK_EQ(pending_request_id_, request_id); | 184 DCHECK_EQ(pending_request_->id, request_id); |
154 web_contents()->GetRenderViewHost()->Send( | 185 web_contents()->GetRenderViewHost()->Send( |
155 new CredentialManagerMsg_SendCredential( | 186 new CredentialManagerMsg_SendCredential( |
156 web_contents()->GetRenderViewHost()->GetRoutingID(), | 187 web_contents()->GetRenderViewHost()->GetRoutingID(), |
157 pending_request_id_, | 188 pending_request_->id, info)); |
158 info)); | 189 pending_request_.reset(); |
159 pending_request_id_ = 0; | |
160 } | 190 } |
161 | 191 |
162 } // namespace password_manager | 192 } // namespace password_manager |
OLD | NEW |