Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(18)

Side by Side Diff: components/password_manager/content/browser/credential_manager_impl.cc

Issue 1762603002: Switch components/password_manager code from IPC messages to Mojo. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase only Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/credential_manager_dispatc her.h" 5 #include "components/password_manager/content/browser/credential_manager_impl.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/strings/string16.h" 10 #include "base/strings/string16.h"
11 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
12 #include "components/autofill/core/common/password_form.h" 12 #include "components/autofill/core/common/password_form.h"
13 #include "components/password_manager/content/browser/content_password_manager_d river.h" 13 #include "components/password_manager/content/browser/content_password_manager_d river.h"
14 #include "components/password_manager/content/browser/content_password_manager_d river_factory.h" 14 #include "components/password_manager/content/browser/content_password_manager_d river_factory.h"
15 #include "components/password_manager/content/common/credential_manager_messages .h" 15 #include "components/password_manager/content/public/cpp/type_converters.h"
16 #include "components/password_manager/core/browser/affiliated_match_helper.h" 16 #include "components/password_manager/core/browser/affiliated_match_helper.h"
17 #include "components/password_manager/core/browser/password_manager_client.h" 17 #include "components/password_manager/core/browser/password_manager_client.h"
18 #include "components/password_manager/core/browser/password_store.h" 18 #include "components/password_manager/core/browser/password_store.h"
19 #include "components/password_manager/core/common/credential_manager_types.h" 19 #include "components/password_manager/core/common/credential_manager_types.h"
20 #include "components/password_manager/core/common/password_manager_pref_names.h" 20 #include "components/password_manager/core/common/password_manager_pref_names.h"
21 #include "content/public/browser/render_view_host.h"
22 #include "content/public/browser/web_contents.h" 21 #include "content/public/browser/web_contents.h"
23 #include "ipc/ipc_message_macros.h" 22 #include "mojo/common/url_type_converters.h"
24 23
25 namespace password_manager { 24 namespace password_manager {
26 25
27 // CredentialManagerDispatcher ------------------------------------------------- 26 namespace {
28 27
29 CredentialManagerDispatcher::CredentialManagerDispatcher( 28 void RunMojoGetCallback(const mojom::CredentialManager::GetCallback& callback,
30 content::WebContents* web_contents, 29 const CredentialInfo& info) {
31 PasswordManagerClient* client) 30 mojom::CredentialInfoPtr credential = mojom::CredentialInfo::From(info);
31 callback.Run(mojom::CredentialManagerError::SUCCESS, std::move(credential));
32 }
33
34 } // namespace
35
36 // CredentialManagerImpl -------------------------------------------------
37
38 CredentialManagerImpl::CredentialManagerImpl(content::WebContents* web_contents,
39 PasswordManagerClient* client)
32 : WebContentsObserver(web_contents), client_(client), weak_factory_(this) { 40 : WebContentsObserver(web_contents), client_(client), weak_factory_(this) {
33 DCHECK(web_contents); 41 DCHECK(web_contents);
34 auto_signin_enabled_.Init(prefs::kCredentialsEnableAutosignin, 42 auto_signin_enabled_.Init(prefs::kCredentialsEnableAutosignin,
35 client_->GetPrefs()); 43 client_->GetPrefs());
36 } 44 }
37 45
38 CredentialManagerDispatcher::~CredentialManagerDispatcher() { 46 CredentialManagerImpl::~CredentialManagerImpl() {}
47
48 void CredentialManagerImpl::BindRequest(
49 mojom::CredentialManagerRequest request) {
50 bindings_.AddBinding(this, std::move(request));
39 } 51 }
40 52
41 bool CredentialManagerDispatcher::OnMessageReceived( 53 void CredentialManagerImpl::Store(mojom::CredentialInfoPtr credential,
42 const IPC::Message& message) { 54 const StoreCallback& callback) {
43 bool handled = true; 55 CredentialInfo info = credential.To<CredentialInfo>();
44 IPC_BEGIN_MESSAGE_MAP(CredentialManagerDispatcher, message) 56 DCHECK_NE(CredentialType::CREDENTIAL_TYPE_EMPTY, info.type);
45 IPC_MESSAGE_HANDLER(CredentialManagerHostMsg_Store, OnStore);
46 IPC_MESSAGE_HANDLER(CredentialManagerHostMsg_RequireUserMediation,
47 OnRequireUserMediation);
48 IPC_MESSAGE_HANDLER(CredentialManagerHostMsg_RequestCredential,
49 OnRequestCredential);
50 IPC_MESSAGE_UNHANDLED(handled = false)
51 IPC_END_MESSAGE_MAP()
52 return handled;
53 }
54 57
55 void CredentialManagerDispatcher::OnStore( 58 // Send acknowledge response back.
56 int request_id, 59 callback.Run();
57 const password_manager::CredentialInfo& credential) {
58 DCHECK(credential.type != CredentialType::CREDENTIAL_TYPE_EMPTY);
59 DCHECK(request_id);
60 web_contents()->GetRenderViewHost()->Send(
61 new CredentialManagerMsg_AcknowledgeStore(
62 web_contents()->GetRenderViewHost()->GetRoutingID(), request_id));
63 60
64 if (!client_->IsSavingAndFillingEnabledForCurrentPage()) 61 if (!client_->IsSavingAndFillingEnabledForCurrentPage())
65 return; 62 return;
66 63
67 std::unique_ptr<autofill::PasswordForm> form( 64 std::unique_ptr<autofill::PasswordForm> form(
68 CreatePasswordFormFromCredentialInfo( 65 CreatePasswordFormFromCredentialInfo(
69 credential, web_contents()->GetLastCommittedURL().GetOrigin())); 66 info, web_contents()->GetLastCommittedURL().GetOrigin()));
70 form->skip_zero_click = !IsZeroClickAllowed(); 67 form->skip_zero_click = !IsZeroClickAllowed();
71 68
72 form_manager_.reset(new CredentialManagerPasswordFormManager( 69 form_manager_.reset(new CredentialManagerPasswordFormManager(
73 client_, GetDriver(), *form, this)); 70 client_, GetDriver(), *form, this));
74 } 71 }
75 72
76 void CredentialManagerDispatcher::OnProvisionalSaveComplete() { 73 void CredentialManagerImpl::OnProvisionalSaveComplete() {
77 DCHECK(form_manager_); 74 DCHECK(form_manager_);
78 DCHECK(client_->IsSavingAndFillingEnabledForCurrentPage()); 75 DCHECK(client_->IsSavingAndFillingEnabledForCurrentPage());
79 76
80 if (form_manager_->IsNewLogin()) { 77 if (form_manager_->IsNewLogin()) {
81 // If the PasswordForm we were given does not match an existing 78 // If the PasswordForm we were given does not match an existing
82 // PasswordForm, ask the user if they'd like to save. 79 // PasswordForm, ask the user if they'd like to save.
83 client_->PromptUserToSaveOrUpdatePassword( 80 client_->PromptUserToSaveOrUpdatePassword(
84 std::move(form_manager_), CredentialSourceType::CREDENTIAL_SOURCE_API, 81 std::move(form_manager_), CredentialSourceType::CREDENTIAL_SOURCE_API,
85 false); 82 false);
86 } else { 83 } else {
87 // Otherwise, update the existing form, as we've been told by the site 84 // Otherwise, update the existing form, as we've been told by the site
88 // that the new PasswordForm is a functioning credential for the user. 85 // that the new PasswordForm is a functioning credential for the user.
89 // We use 'PasswordFormManager::Update(PasswordForm&)' here rather than 86 // We use 'PasswordFormManager::Update(PasswordForm&)' here rather than
90 // 'PasswordFormManager::UpdateLogin', as we need to port over the 87 // 'PasswordFormManager::UpdateLogin', as we need to port over the
91 // 'skip_zero_click' state to ensure that we don't accidentally start 88 // 'skip_zero_click' state to ensure that we don't accidentally start
92 // signing users in just because the site asks us to. The simplest way 89 // signing users in just because the site asks us to. The simplest way
93 // to do so is simply to update the password field of the existing 90 // to do so is simply to update the password field of the existing
94 // credential. 91 // credential.
95 form_manager_->Update(*form_manager_->preferred_match()); 92 form_manager_->Update(*form_manager_->preferred_match());
96 } 93 }
97 } 94 }
98 95
99 void CredentialManagerDispatcher::OnRequireUserMediation(int request_id) { 96 void CredentialManagerImpl::RequireUserMediation(
100 DCHECK(request_id); 97 const RequireUserMediationCallback& callback) {
101
102 PasswordStore* store = GetPasswordStore(); 98 PasswordStore* store = GetPasswordStore();
103 if (!store || !IsUpdatingCredentialAllowed()) { 99 if (!store || !IsUpdatingCredentialAllowed()) {
104 web_contents()->GetRenderViewHost()->Send( 100 callback.Run();
105 new CredentialManagerMsg_AcknowledgeRequireUserMediation(
106 web_contents()->GetRenderViewHost()->GetRoutingID(), request_id));
107 return; 101 return;
108 } 102 }
109 103
110 if (store->affiliated_match_helper()) { 104 if (store->affiliated_match_helper()) {
111 store->affiliated_match_helper()->GetAffiliatedAndroidRealms( 105 store->affiliated_match_helper()->GetAffiliatedAndroidRealms(
112 GetSynthesizedFormForOrigin(), 106 GetSynthesizedFormForOrigin(),
113 base::Bind(&CredentialManagerDispatcher::ScheduleRequireMediationTask, 107 base::Bind(&CredentialManagerImpl::ScheduleRequireMediationTask,
114 weak_factory_.GetWeakPtr(), request_id)); 108 weak_factory_.GetWeakPtr(), callback));
115 } else { 109 } else {
116 std::vector<std::string> no_affiliated_realms; 110 std::vector<std::string> no_affiliated_realms;
117 ScheduleRequireMediationTask(request_id, no_affiliated_realms); 111 ScheduleRequireMediationTask(callback, no_affiliated_realms);
118 } 112 }
119 } 113 }
120 114
121 void CredentialManagerDispatcher::ScheduleRequireMediationTask( 115 void CredentialManagerImpl::ScheduleRequireMediationTask(
122 int request_id, 116 const RequireUserMediationCallback& callback,
123 const std::vector<std::string>& android_realms) { 117 const std::vector<std::string>& android_realms) {
124 DCHECK(GetPasswordStore()); 118 DCHECK(GetPasswordStore());
125 if (!pending_require_user_mediation_) { 119 if (!pending_require_user_mediation_) {
126 pending_require_user_mediation_.reset( 120 pending_require_user_mediation_.reset(
127 new CredentialManagerPendingRequireUserMediationTask( 121 new CredentialManagerPendingRequireUserMediationTask(
128 this, web_contents()->GetLastCommittedURL().GetOrigin(), 122 this, web_contents()->GetLastCommittedURL().GetOrigin(),
129 android_realms)); 123 android_realms));
130 124
131 // This will result in a callback to 125 // This will result in a callback to
132 // CredentialManagerPendingRequireUserMediationTask::OnGetPasswordStoreResul ts(). 126 // CredentialManagerPendingRequireUserMediationTask::
127 // OnGetPasswordStoreResults().
133 GetPasswordStore()->GetAutofillableLogins( 128 GetPasswordStore()->GetAutofillableLogins(
134 pending_require_user_mediation_.get()); 129 pending_require_user_mediation_.get());
135 } else { 130 } else {
136 pending_require_user_mediation_->AddOrigin( 131 pending_require_user_mediation_->AddOrigin(
137 web_contents()->GetLastCommittedURL().GetOrigin()); 132 web_contents()->GetLastCommittedURL().GetOrigin());
138 } 133 }
139 134
140 web_contents()->GetRenderViewHost()->Send( 135 // Send acknowledge response back.
141 new CredentialManagerMsg_AcknowledgeRequireUserMediation( 136 callback.Run();
142 web_contents()->GetRenderViewHost()->GetRoutingID(), request_id));
143 } 137 }
144 138
145 void CredentialManagerDispatcher::OnRequestCredential( 139 void CredentialManagerImpl::Get(bool zero_click_only,
146 int request_id, 140 bool include_passwords,
147 bool zero_click_only, 141 mojo::Array<mojo::String> federations,
148 bool include_passwords, 142 const GetCallback& callback) {
149 const std::vector<GURL>& federations) {
150 DCHECK(request_id);
151 PasswordStore* store = GetPasswordStore(); 143 PasswordStore* store = GetPasswordStore();
152 if (pending_request_ || !store) { 144 if (pending_request_ || !store) {
153 web_contents()->GetRenderViewHost()->Send( 145 // Callback error.
154 new CredentialManagerMsg_RejectCredentialRequest( 146 callback.Run(pending_request_
155 web_contents()->GetRenderViewHost()->GetRoutingID(), request_id, 147 ? mojom::CredentialManagerError::PENDINGREQUEST
156 pending_request_ 148 : mojom::CredentialManagerError::PASSWORDSTOREUNAVAILABLE,
157 ? blink::WebCredentialManagerPendingRequestError 149 nullptr);
158 : blink::WebCredentialManagerPasswordStoreUnavailableError));
159 return; 150 return;
160 } 151 }
161 152
162 // Return an empty credential if zero-click is required but disabled, or if 153 // Return an empty credential if zero-click is required but disabled, or if
163 // the current page has TLS errors. 154 // the current page has TLS errors.
164 if ((zero_click_only && !IsZeroClickAllowed()) || 155 if ((zero_click_only && !IsZeroClickAllowed()) ||
165 client_->DidLastPageLoadEncounterSSLErrors()) { 156 client_->DidLastPageLoadEncounterSSLErrors()) {
166 web_contents()->GetRenderViewHost()->Send( 157 // Callback with empty credential info.
167 new CredentialManagerMsg_SendCredential( 158 callback.Run(mojom::CredentialManagerError::SUCCESS,
168 web_contents()->GetRenderViewHost()->GetRoutingID(), request_id, 159 mojom::CredentialInfo::New());
169 CredentialInfo()));
170 return; 160 return;
171 } 161 }
172 162
173 if (store->affiliated_match_helper()) { 163 if (store->affiliated_match_helper()) {
174 store->affiliated_match_helper()->GetAffiliatedAndroidRealms( 164 store->affiliated_match_helper()->GetAffiliatedAndroidRealms(
175 GetSynthesizedFormForOrigin(), 165 GetSynthesizedFormForOrigin(),
176 base::Bind(&CredentialManagerDispatcher::ScheduleRequestTask, 166 base::Bind(&CredentialManagerImpl::ScheduleRequestTask,
177 weak_factory_.GetWeakPtr(), request_id, zero_click_only, 167 weak_factory_.GetWeakPtr(), callback, zero_click_only,
178 include_passwords, federations)); 168 include_passwords, federations.To<std::vector<GURL>>()));
179 } else { 169 } else {
180 std::vector<std::string> no_affiliated_realms; 170 std::vector<std::string> no_affiliated_realms;
181 ScheduleRequestTask(request_id, zero_click_only, include_passwords, 171 ScheduleRequestTask(callback, zero_click_only, include_passwords,
182 federations, no_affiliated_realms); 172 federations.To<std::vector<GURL>>(),
173 no_affiliated_realms);
183 } 174 }
184 } 175 }
185 176
186 void CredentialManagerDispatcher::ScheduleRequestTask( 177 void CredentialManagerImpl::ScheduleRequestTask(
187 int request_id, 178 const GetCallback& callback,
188 bool zero_click_only, 179 bool zero_click_only,
189 bool include_passwords, 180 bool include_passwords,
190 const std::vector<GURL>& federations, 181 const std::vector<GURL>& federations,
191 const std::vector<std::string>& android_realms) { 182 const std::vector<std::string>& android_realms) {
192 DCHECK(GetPasswordStore()); 183 DCHECK(GetPasswordStore());
193 pending_request_.reset(new CredentialManagerPendingRequestTask( 184 pending_request_.reset(new CredentialManagerPendingRequestTask(
194 this, request_id, zero_click_only, 185 this, base::Bind(&RunMojoGetCallback, callback), zero_click_only,
195 web_contents()->GetLastCommittedURL().GetOrigin(), include_passwords, 186 web_contents()->GetLastCommittedURL().GetOrigin(), include_passwords,
196 federations, android_realms)); 187 federations, android_realms));
197 188
198 // This will result in a callback to 189 // This will result in a callback to
199 // PendingRequestTask::OnGetPasswordStoreResults(). 190 // PendingRequestTask::OnGetPasswordStoreResults().
200 GetPasswordStore()->GetAutofillableLogins(pending_request_.get()); 191 GetPasswordStore()->GetAutofillableLogins(pending_request_.get());
201 } 192 }
202 193
203 PasswordStore* CredentialManagerDispatcher::GetPasswordStore() { 194 PasswordStore* CredentialManagerImpl::GetPasswordStore() {
204 return client_ ? client_->GetPasswordStore() : nullptr; 195 return client_ ? client_->GetPasswordStore() : nullptr;
205 } 196 }
206 197
207 bool CredentialManagerDispatcher::IsZeroClickAllowed() const { 198 bool CredentialManagerImpl::IsZeroClickAllowed() const {
208 return *auto_signin_enabled_ && !client_->IsOffTheRecord(); 199 return *auto_signin_enabled_ && !client_->IsOffTheRecord();
209 } 200 }
210 201
211 GURL CredentialManagerDispatcher::GetOrigin() const { 202 GURL CredentialManagerImpl::GetOrigin() const {
212 return web_contents()->GetLastCommittedURL().GetOrigin(); 203 return web_contents()->GetLastCommittedURL().GetOrigin();
213 } 204 }
214 205
215 base::WeakPtr<PasswordManagerDriver> CredentialManagerDispatcher::GetDriver() { 206 base::WeakPtr<PasswordManagerDriver> CredentialManagerImpl::GetDriver() {
216 ContentPasswordManagerDriverFactory* driver_factory = 207 ContentPasswordManagerDriverFactory* driver_factory =
217 ContentPasswordManagerDriverFactory::FromWebContents(web_contents()); 208 ContentPasswordManagerDriverFactory::FromWebContents(web_contents());
218 DCHECK(driver_factory); 209 DCHECK(driver_factory);
219 PasswordManagerDriver* driver = 210 PasswordManagerDriver* driver =
220 driver_factory->GetDriverForFrame(web_contents()->GetMainFrame()); 211 driver_factory->GetDriverForFrame(web_contents()->GetMainFrame());
221 return driver->AsWeakPtr(); 212 return driver->AsWeakPtr();
222 } 213 }
223 214
224 void CredentialManagerDispatcher::SendCredential(int request_id, 215 void CredentialManagerImpl::SendCredential(
225 const CredentialInfo& info) { 216 const SendCredentialCallback& send_callback,
217 const CredentialInfo& info) {
226 DCHECK(pending_request_); 218 DCHECK(pending_request_);
227 DCHECK_EQ(pending_request_->id(), request_id); 219 DCHECK(send_callback.Equals(pending_request_->send_callback()));
228 220
229 web_contents()->GetRenderViewHost()->Send( 221 send_callback.Run(info);
230 new CredentialManagerMsg_SendCredential(
231 web_contents()->GetRenderViewHost()->GetRoutingID(),
232 pending_request_->id(), info));
233 pending_request_.reset(); 222 pending_request_.reset();
234 } 223 }
235 224
236 void CredentialManagerDispatcher::SendPasswordForm( 225 void CredentialManagerImpl::SendPasswordForm(
237 int request_id, 226 const SendCredentialCallback& send_callback,
238 const autofill::PasswordForm* form) { 227 const autofill::PasswordForm* form) {
239 CredentialInfo info; 228 CredentialInfo info;
240 if (form) { 229 if (form) {
241 password_manager::CredentialType type_to_return = 230 password_manager::CredentialType type_to_return =
242 form->federation_origin.unique() 231 form->federation_origin.unique()
243 ? CredentialType::CREDENTIAL_TYPE_PASSWORD 232 ? CredentialType::CREDENTIAL_TYPE_PASSWORD
244 : CredentialType::CREDENTIAL_TYPE_FEDERATED; 233 : CredentialType::CREDENTIAL_TYPE_FEDERATED;
245 info = CredentialInfo(*form, type_to_return); 234 info = CredentialInfo(*form, type_to_return);
246 if (PasswordStore* store = GetPasswordStore()) { 235 if (PasswordStore* store = GetPasswordStore()) {
247 if (form->skip_zero_click && IsZeroClickAllowed()) { 236 if (form->skip_zero_click && IsZeroClickAllowed()) {
248 DCHECK(IsUpdatingCredentialAllowed()); 237 DCHECK(IsUpdatingCredentialAllowed());
249 autofill::PasswordForm update_form = *form; 238 autofill::PasswordForm update_form = *form;
250 update_form.skip_zero_click = false; 239 update_form.skip_zero_click = false;
251 store->UpdateLogin(update_form); 240 store->UpdateLogin(update_form);
252 } 241 }
253 } 242 }
254 } 243 }
255 SendCredential(request_id, info); 244 SendCredential(send_callback, info);
256 } 245 }
257 246
258 PasswordManagerClient* CredentialManagerDispatcher::client() const { 247 PasswordManagerClient* CredentialManagerImpl::client() const {
259 return client_; 248 return client_;
260 } 249 }
261 250
262 autofill::PasswordForm 251 autofill::PasswordForm CredentialManagerImpl::GetSynthesizedFormForOrigin()
263 CredentialManagerDispatcher::GetSynthesizedFormForOrigin() const { 252 const {
264 autofill::PasswordForm synthetic_form; 253 autofill::PasswordForm synthetic_form;
265 synthetic_form.origin = web_contents()->GetLastCommittedURL().GetOrigin(); 254 synthetic_form.origin = web_contents()->GetLastCommittedURL().GetOrigin();
266 synthetic_form.signon_realm = synthetic_form.origin.spec(); 255 synthetic_form.signon_realm = synthetic_form.origin.spec();
267 synthetic_form.scheme = autofill::PasswordForm::SCHEME_HTML; 256 synthetic_form.scheme = autofill::PasswordForm::SCHEME_HTML;
268 synthetic_form.ssl_valid = synthetic_form.origin.SchemeIsCryptographic() && 257 synthetic_form.ssl_valid = synthetic_form.origin.SchemeIsCryptographic() &&
269 !client_->DidLastPageLoadEncounterSSLErrors(); 258 !client_->DidLastPageLoadEncounterSSLErrors();
270 return synthetic_form; 259 return synthetic_form;
271 } 260 }
272 261
273 void CredentialManagerDispatcher::DoneRequiringUserMediation() { 262 void CredentialManagerImpl::DoneRequiringUserMediation() {
274 DCHECK(pending_require_user_mediation_); 263 DCHECK(pending_require_user_mediation_);
275 pending_require_user_mediation_.reset(); 264 pending_require_user_mediation_.reset();
276 } 265 }
277 266
278 bool CredentialManagerDispatcher::IsUpdatingCredentialAllowed() const { 267 bool CredentialManagerImpl::IsUpdatingCredentialAllowed() const {
279 return !client_->DidLastPageLoadEncounterSSLErrors() && 268 return !client_->DidLastPageLoadEncounterSSLErrors() &&
280 !client_->IsOffTheRecord(); 269 !client_->IsOffTheRecord();
281 } 270 }
282 271
283 } // namespace password_manager 272 } // namespace password_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698