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

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: Impl done, unit_tests and browser_tests not ready yet Created 4 years, 9 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/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(info.type != CredentialType::CREDENTIAL_TYPE_EMPTY);
vabr (Chromium) 2016/03/09 16:26:29 nit: Use DCHECK_NE(CredentialType::CREDENTIAL_TYPE
leonhsl(Using Gerrit) 2016/03/10 06:42:41 Build will fail because seems DCHECK_NE can't find
vabr (Chromium) 2016/03/10 12:57:11 The comparison is OK (otherwise != would not work
leonhsl(Using Gerrit) 2016/03/11 09:52:26 Thank you~ Yeah it is! I'd like to get the bonus p
vabr (Chromium) 2016/03/14 15:40:26 Thanks, I appreciate the care you put into your co
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 scoped_ptr<autofill::PasswordForm> form(CreatePasswordFormFromCredentialInfo( 64 scoped_ptr<autofill::PasswordForm> form(CreatePasswordFormFromCredentialInfo(
68 credential, web_contents()->GetLastCommittedURL().GetOrigin())); 65 info, web_contents()->GetLastCommittedURL().GetOrigin()));
69 form->skip_zero_click = !IsZeroClickAllowed(); 66 form->skip_zero_click = !IsZeroClickAllowed();
70 67
71 form_manager_.reset(new CredentialManagerPasswordFormManager( 68 form_manager_.reset(new CredentialManagerPasswordFormManager(
72 client_, GetDriver(), *form, this)); 69 client_, GetDriver(), *form, this));
73 } 70 }
74 71
75 void CredentialManagerDispatcher::OnProvisionalSaveComplete() { 72 void CredentialManagerImpl::OnProvisionalSaveComplete() {
76 DCHECK(form_manager_); 73 DCHECK(form_manager_);
77 DCHECK(client_->IsSavingAndFillingEnabledForCurrentPage()); 74 DCHECK(client_->IsSavingAndFillingEnabledForCurrentPage());
78 75
79 if (form_manager_->IsNewLogin()) { 76 if (form_manager_->IsNewLogin()) {
80 // If the PasswordForm we were given does not match an existing 77 // If the PasswordForm we were given does not match an existing
81 // PasswordForm, ask the user if they'd like to save. 78 // PasswordForm, ask the user if they'd like to save.
82 client_->PromptUserToSaveOrUpdatePassword( 79 client_->PromptUserToSaveOrUpdatePassword(
83 std::move(form_manager_), CredentialSourceType::CREDENTIAL_SOURCE_API, 80 std::move(form_manager_), CredentialSourceType::CREDENTIAL_SOURCE_API,
84 false); 81 false);
85 } else { 82 } else {
86 // Otherwise, update the existing form, as we've been told by the site 83 // Otherwise, update the existing form, as we've been told by the site
87 // that the new PasswordForm is a functioning credential for the user. 84 // that the new PasswordForm is a functioning credential for the user.
88 // We use 'PasswordFormManager::Update(PasswordForm&)' here rather than 85 // We use 'PasswordFormManager::Update(PasswordForm&)' here rather than
89 // 'PasswordFormManager::UpdateLogin', as we need to port over the 86 // 'PasswordFormManager::UpdateLogin', as we need to port over the
90 // 'skip_zero_click' state to ensure that we don't accidentally start 87 // 'skip_zero_click' state to ensure that we don't accidentally start
91 // signing users in just because the site asks us to. The simplest way 88 // signing users in just because the site asks us to. The simplest way
92 // to do so is simply to update the password field of the existing 89 // to do so is simply to update the password field of the existing
93 // credential. 90 // credential.
94 form_manager_->Update(*form_manager_->preferred_match()); 91 form_manager_->Update(*form_manager_->preferred_match());
95 } 92 }
96 } 93 }
97 94
98 void CredentialManagerDispatcher::OnRequireUserMediation(int request_id) { 95 void CredentialManagerImpl::RequireUserMediation(
99 DCHECK(request_id); 96 const RequireUserMediationCallback& callback) {
100
101 PasswordStore* store = GetPasswordStore(); 97 PasswordStore* store = GetPasswordStore();
102 if (!store || !IsUpdatingCredentialAllowed()) { 98 if (!store || !IsUpdatingCredentialAllowed()) {
103 web_contents()->GetRenderViewHost()->Send( 99 // Send acknowledge response back.
104 new CredentialManagerMsg_AcknowledgeRequireUserMediation( 100 callback.Run();
105 web_contents()->GetRenderViewHost()->GetRoutingID(), request_id));
106 return; 101 return;
107 } 102 }
108 103
109 if (store->affiliated_match_helper()) { 104 if (store->affiliated_match_helper()) {
110 store->affiliated_match_helper()->GetAffiliatedAndroidRealms( 105 store->affiliated_match_helper()->GetAffiliatedAndroidRealms(
111 GetSynthesizedFormForOrigin(), 106 GetSynthesizedFormForOrigin(),
112 base::Bind(&CredentialManagerDispatcher::ScheduleRequireMediationTask, 107 base::Bind(&CredentialManagerImpl::ScheduleRequireMediationTask,
113 weak_factory_.GetWeakPtr(), request_id)); 108 weak_factory_.GetWeakPtr(), callback));
114 } else { 109 } else {
115 std::vector<std::string> no_affiliated_realms; 110 std::vector<std::string> no_affiliated_realms;
116 ScheduleRequireMediationTask(request_id, no_affiliated_realms); 111 ScheduleRequireMediationTask(callback, no_affiliated_realms);
117 } 112 }
118 } 113 }
119 114
120 void CredentialManagerDispatcher::ScheduleRequireMediationTask( 115 void CredentialManagerImpl::ScheduleRequireMediationTask(
121 int request_id, 116 const RequireUserMediationCallback& callback,
122 const std::vector<std::string>& android_realms) { 117 const std::vector<std::string>& android_realms) {
123 DCHECK(GetPasswordStore()); 118 DCHECK(GetPasswordStore());
124 if (!pending_require_user_mediation_) { 119 if (!pending_require_user_mediation_) {
125 pending_require_user_mediation_.reset( 120 pending_require_user_mediation_.reset(
126 new CredentialManagerPendingRequireUserMediationTask( 121 new CredentialManagerPendingRequireUserMediationTask(
127 this, web_contents()->GetLastCommittedURL().GetOrigin(), 122 this, web_contents()->GetLastCommittedURL().GetOrigin(),
128 android_realms)); 123 android_realms));
129 124
130 // This will result in a callback to 125 // This will result in a callback to
131 // CredentialManagerPendingRequireUserMediationTask::OnGetPasswordStoreResul ts(). 126 // CredentialManagerPendingRequireUserMediationTask::OnGetPasswordStoreResul ts().
132 GetPasswordStore()->GetAutofillableLogins( 127 GetPasswordStore()->GetAutofillableLogins(
133 pending_require_user_mediation_.get()); 128 pending_require_user_mediation_.get());
134 } else { 129 } else {
135 pending_require_user_mediation_->AddOrigin( 130 pending_require_user_mediation_->AddOrigin(
136 web_contents()->GetLastCommittedURL().GetOrigin()); 131 web_contents()->GetLastCommittedURL().GetOrigin());
137 } 132 }
138 133
139 web_contents()->GetRenderViewHost()->Send( 134 // Send acknowledge response back.
140 new CredentialManagerMsg_AcknowledgeRequireUserMediation( 135 callback.Run();
141 web_contents()->GetRenderViewHost()->GetRoutingID(), request_id));
142 } 136 }
143 137
144 void CredentialManagerDispatcher::OnRequestCredential( 138 void CredentialManagerImpl::Get(bool zero_click_only,
145 int request_id, 139 bool include_passwords,
146 bool zero_click_only, 140 mojo::Array<mojo::String> federations,
147 bool include_passwords, 141 const GetCallback& callback) {
148 const std::vector<GURL>& federations) {
149 DCHECK(request_id);
150 PasswordStore* store = GetPasswordStore(); 142 PasswordStore* store = GetPasswordStore();
151 if (pending_request_ || !store) { 143 if (pending_request_ || !store) {
152 web_contents()->GetRenderViewHost()->Send( 144 // Callback error.
153 new CredentialManagerMsg_RejectCredentialRequest( 145 callback.Run(pending_request_
154 web_contents()->GetRenderViewHost()->GetRoutingID(), request_id, 146 ? mojom::CredentialManagerError::PENDINGREQUEST
155 pending_request_ 147 : mojom::CredentialManagerError::PASSWORDSTOREUNAVAILABLE,
156 ? blink::WebCredentialManagerPendingRequestError 148 nullptr);
157 : blink::WebCredentialManagerPasswordStoreUnavailableError));
158 return; 149 return;
159 } 150 }
160 151
161 // Return an empty credential if zero-click is required but disabled, or if 152 // Return an empty credential if zero-click is required but disabled, or if
162 // the current page has TLS errors. 153 // the current page has TLS errors.
163 if ((zero_click_only && !IsZeroClickAllowed()) || 154 if ((zero_click_only && !IsZeroClickAllowed()) ||
164 client_->DidLastPageLoadEncounterSSLErrors()) { 155 client_->DidLastPageLoadEncounterSSLErrors()) {
165 web_contents()->GetRenderViewHost()->Send( 156 // Callback with empty credential info.
166 new CredentialManagerMsg_SendCredential( 157 callback.Run(mojom::CredentialManagerError::SUCCESS,
167 web_contents()->GetRenderViewHost()->GetRoutingID(), request_id, 158 mojom::CredentialInfo::New());
168 CredentialInfo()));
169 return; 159 return;
170 } 160 }
171 161
172 if (store->affiliated_match_helper()) { 162 if (store->affiliated_match_helper()) {
173 store->affiliated_match_helper()->GetAffiliatedAndroidRealms( 163 store->affiliated_match_helper()->GetAffiliatedAndroidRealms(
174 GetSynthesizedFormForOrigin(), 164 GetSynthesizedFormForOrigin(),
175 base::Bind(&CredentialManagerDispatcher::ScheduleRequestTask, 165 base::Bind(&CredentialManagerImpl::ScheduleRequestTask,
176 weak_factory_.GetWeakPtr(), request_id, zero_click_only, 166 weak_factory_.GetWeakPtr(), callback, zero_click_only,
177 include_passwords, federations)); 167 include_passwords, federations.To<std::vector<GURL>>()));
178 } else { 168 } else {
179 std::vector<std::string> no_affiliated_realms; 169 std::vector<std::string> no_affiliated_realms;
180 ScheduleRequestTask(request_id, zero_click_only, include_passwords, 170 ScheduleRequestTask(callback, zero_click_only, include_passwords,
181 federations, no_affiliated_realms); 171 federations.To<std::vector<GURL>>(),
172 no_affiliated_realms);
182 } 173 }
183 } 174 }
184 175
185 void CredentialManagerDispatcher::ScheduleRequestTask( 176 void CredentialManagerImpl::ScheduleRequestTask(
186 int request_id, 177 const GetCallback& callback,
187 bool zero_click_only, 178 bool zero_click_only,
188 bool include_passwords, 179 bool include_passwords,
189 const std::vector<GURL>& federations, 180 const std::vector<GURL>& federations,
190 const std::vector<std::string>& android_realms) { 181 const std::vector<std::string>& android_realms) {
191 DCHECK(GetPasswordStore()); 182 DCHECK(GetPasswordStore());
192 pending_request_.reset(new CredentialManagerPendingRequestTask( 183 pending_request_.reset(new CredentialManagerPendingRequestTask(
193 this, request_id, zero_click_only, 184 this, base::Bind(&RunMojoGetCallback, callback), zero_click_only,
194 web_contents()->GetLastCommittedURL().GetOrigin(), include_passwords, 185 web_contents()->GetLastCommittedURL().GetOrigin(), include_passwords,
195 federations, android_realms)); 186 federations, android_realms));
196 187
197 // This will result in a callback to 188 // This will result in a callback to
198 // PendingRequestTask::OnGetPasswordStoreResults(). 189 // PendingRequestTask::OnGetPasswordStoreResults().
199 GetPasswordStore()->GetAutofillableLogins(pending_request_.get()); 190 GetPasswordStore()->GetAutofillableLogins(pending_request_.get());
200 } 191 }
201 192
202 PasswordStore* CredentialManagerDispatcher::GetPasswordStore() { 193 PasswordStore* CredentialManagerImpl::GetPasswordStore() {
203 return client_ ? client_->GetPasswordStore() : nullptr; 194 return client_ ? client_->GetPasswordStore() : nullptr;
204 } 195 }
205 196
206 bool CredentialManagerDispatcher::IsZeroClickAllowed() const { 197 bool CredentialManagerImpl::IsZeroClickAllowed() const {
207 return *auto_signin_enabled_ && !client_->IsOffTheRecord(); 198 return *auto_signin_enabled_ && !client_->IsOffTheRecord();
208 } 199 }
209 200
210 GURL CredentialManagerDispatcher::GetOrigin() const { 201 GURL CredentialManagerImpl::GetOrigin() const {
211 return web_contents()->GetLastCommittedURL().GetOrigin(); 202 return web_contents()->GetLastCommittedURL().GetOrigin();
212 } 203 }
213 204
214 base::WeakPtr<PasswordManagerDriver> CredentialManagerDispatcher::GetDriver() { 205 base::WeakPtr<PasswordManagerDriver> CredentialManagerImpl::GetDriver() {
215 ContentPasswordManagerDriverFactory* driver_factory = 206 ContentPasswordManagerDriverFactory* driver_factory =
216 ContentPasswordManagerDriverFactory::FromWebContents(web_contents()); 207 ContentPasswordManagerDriverFactory::FromWebContents(web_contents());
217 DCHECK(driver_factory); 208 DCHECK(driver_factory);
218 PasswordManagerDriver* driver = 209 PasswordManagerDriver* driver =
219 driver_factory->GetDriverForFrame(web_contents()->GetMainFrame()); 210 driver_factory->GetDriverForFrame(web_contents()->GetMainFrame());
220 return driver->AsWeakPtr(); 211 return driver->AsWeakPtr();
221 } 212 }
222 213
223 void CredentialManagerDispatcher::SendCredential(int request_id, 214 void CredentialManagerImpl::SendCredential(
224 const CredentialInfo& info) { 215 const SendCredentialCallback& send_callback,
216 const CredentialInfo& info) {
225 DCHECK(pending_request_); 217 DCHECK(pending_request_);
226 DCHECK_EQ(pending_request_->id(), request_id); 218 DCHECK(send_callback.Equals(pending_request_->send_callback()));
227 219
228 if (PasswordStore* store = GetPasswordStore()) { 220 if (PasswordStore* store = GetPasswordStore()) {
229 if (info.type != CredentialType::CREDENTIAL_TYPE_EMPTY && 221 if (info.type != CredentialType::CREDENTIAL_TYPE_EMPTY &&
230 IsZeroClickAllowed()) { 222 IsZeroClickAllowed()) {
231 DCHECK(IsUpdatingCredentialAllowed()); 223 DCHECK(IsUpdatingCredentialAllowed());
232 scoped_ptr<autofill::PasswordForm> form( 224 scoped_ptr<autofill::PasswordForm> form(
233 CreatePasswordFormFromCredentialInfo(info, 225 CreatePasswordFormFromCredentialInfo(info,
234 pending_request_->origin())); 226 pending_request_->origin()));
235 form->skip_zero_click = false; 227 form->skip_zero_click = false;
236 store->UpdateLogin(*form); 228 store->UpdateLogin(*form);
237 } 229 }
238 } 230 }
239 231
240 web_contents()->GetRenderViewHost()->Send( 232 // Run callback to send credential info back.
241 new CredentialManagerMsg_SendCredential( 233 send_callback.Run(info);
242 web_contents()->GetRenderViewHost()->GetRoutingID(),
243 pending_request_->id(), info));
244 pending_request_.reset(); 234 pending_request_.reset();
245 } 235 }
246 236
247 PasswordManagerClient* CredentialManagerDispatcher::client() const { 237 PasswordManagerClient* CredentialManagerImpl::client() const {
248 return client_; 238 return client_;
249 } 239 }
250 240
251 autofill::PasswordForm 241 autofill::PasswordForm CredentialManagerImpl::GetSynthesizedFormForOrigin()
252 CredentialManagerDispatcher::GetSynthesizedFormForOrigin() const { 242 const {
253 autofill::PasswordForm synthetic_form; 243 autofill::PasswordForm synthetic_form;
254 synthetic_form.origin = web_contents()->GetLastCommittedURL().GetOrigin(); 244 synthetic_form.origin = web_contents()->GetLastCommittedURL().GetOrigin();
255 synthetic_form.signon_realm = synthetic_form.origin.spec(); 245 synthetic_form.signon_realm = synthetic_form.origin.spec();
256 synthetic_form.scheme = autofill::PasswordForm::SCHEME_HTML; 246 synthetic_form.scheme = autofill::PasswordForm::SCHEME_HTML;
257 synthetic_form.ssl_valid = synthetic_form.origin.SchemeIsCryptographic() && 247 synthetic_form.ssl_valid = synthetic_form.origin.SchemeIsCryptographic() &&
258 !client_->DidLastPageLoadEncounterSSLErrors(); 248 !client_->DidLastPageLoadEncounterSSLErrors();
259 return synthetic_form; 249 return synthetic_form;
260 } 250 }
261 251
262 void CredentialManagerDispatcher::DoneRequiringUserMediation() { 252 void CredentialManagerImpl::DoneRequiringUserMediation() {
263 DCHECK(pending_require_user_mediation_); 253 DCHECK(pending_require_user_mediation_);
264 pending_require_user_mediation_.reset(); 254 pending_require_user_mediation_.reset();
265 } 255 }
266 256
267 bool CredentialManagerDispatcher::IsUpdatingCredentialAllowed() const { 257 bool CredentialManagerImpl::IsUpdatingCredentialAllowed() const {
268 return !client_->DidLastPageLoadEncounterSSLErrors() && 258 return !client_->DidLastPageLoadEncounterSSLErrors() &&
269 !client_->IsOffTheRecord(); 259 !client_->IsOffTheRecord();
270 } 260 }
271 261
272 } // namespace password_manager 262 } // namespace password_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698