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

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 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 callback.Run();
104 new CredentialManagerMsg_AcknowledgeRequireUserMediation(
105 web_contents()->GetRenderViewHost()->GetRoutingID(), request_id));
106 return; 100 return;
107 } 101 }
108 102
109 if (store->affiliated_match_helper()) { 103 if (store->affiliated_match_helper()) {
110 store->affiliated_match_helper()->GetAffiliatedAndroidRealms( 104 store->affiliated_match_helper()->GetAffiliatedAndroidRealms(
111 GetSynthesizedFormForOrigin(), 105 GetSynthesizedFormForOrigin(),
112 base::Bind(&CredentialManagerDispatcher::ScheduleRequireMediationTask, 106 base::Bind(&CredentialManagerImpl::ScheduleRequireMediationTask,
113 weak_factory_.GetWeakPtr(), request_id)); 107 weak_factory_.GetWeakPtr(), callback));
114 } else { 108 } else {
115 std::vector<std::string> no_affiliated_realms; 109 std::vector<std::string> no_affiliated_realms;
116 ScheduleRequireMediationTask(request_id, no_affiliated_realms); 110 ScheduleRequireMediationTask(callback, no_affiliated_realms);
117 } 111 }
118 } 112 }
119 113
120 void CredentialManagerDispatcher::ScheduleRequireMediationTask( 114 void CredentialManagerImpl::ScheduleRequireMediationTask(
121 int request_id, 115 const RequireUserMediationCallback& callback,
122 const std::vector<std::string>& android_realms) { 116 const std::vector<std::string>& android_realms) {
123 DCHECK(GetPasswordStore()); 117 DCHECK(GetPasswordStore());
124 if (!pending_require_user_mediation_) { 118 if (!pending_require_user_mediation_) {
125 pending_require_user_mediation_.reset( 119 pending_require_user_mediation_.reset(
126 new CredentialManagerPendingRequireUserMediationTask( 120 new CredentialManagerPendingRequireUserMediationTask(
127 this, web_contents()->GetLastCommittedURL().GetOrigin(), 121 this, web_contents()->GetLastCommittedURL().GetOrigin(),
128 android_realms)); 122 android_realms));
129 123
130 // This will result in a callback to 124 // This will result in a callback to
131 // CredentialManagerPendingRequireUserMediationTask::OnGetPasswordStoreResul ts(). 125 // CredentialManagerPendingRequireUserMediationTask::OnGetPasswordStoreResul ts().
132 GetPasswordStore()->GetAutofillableLogins( 126 GetPasswordStore()->GetAutofillableLogins(
133 pending_require_user_mediation_.get()); 127 pending_require_user_mediation_.get());
134 } else { 128 } else {
135 pending_require_user_mediation_->AddOrigin( 129 pending_require_user_mediation_->AddOrigin(
136 web_contents()->GetLastCommittedURL().GetOrigin()); 130 web_contents()->GetLastCommittedURL().GetOrigin());
137 } 131 }
138 132
139 web_contents()->GetRenderViewHost()->Send( 133 // Send acknowledge response back.
140 new CredentialManagerMsg_AcknowledgeRequireUserMediation( 134 callback.Run();
141 web_contents()->GetRenderViewHost()->GetRoutingID(), request_id));
142 } 135 }
143 136
144 void CredentialManagerDispatcher::OnRequestCredential( 137 void CredentialManagerImpl::Get(bool zero_click_only,
145 int request_id, 138 bool include_passwords,
146 bool zero_click_only, 139 mojo::Array<mojo::String> federations,
147 bool include_passwords, 140 const GetCallback& callback) {
148 const std::vector<GURL>& federations) {
149 DCHECK(request_id);
150 PasswordStore* store = GetPasswordStore(); 141 PasswordStore* store = GetPasswordStore();
151 if (pending_request_ || !store) { 142 if (pending_request_ || !store) {
152 web_contents()->GetRenderViewHost()->Send( 143 // Callback error.
153 new CredentialManagerMsg_RejectCredentialRequest( 144 callback.Run(pending_request_
154 web_contents()->GetRenderViewHost()->GetRoutingID(), request_id, 145 ? mojom::CredentialManagerError::PENDINGREQUEST
155 pending_request_ 146 : mojom::CredentialManagerError::PASSWORDSTOREUNAVAILABLE,
156 ? blink::WebCredentialManagerPendingRequestError 147 nullptr);
157 : blink::WebCredentialManagerPasswordStoreUnavailableError));
158 return; 148 return;
159 } 149 }
160 150
161 // Return an empty credential if zero-click is required but disabled, or if 151 // Return an empty credential if zero-click is required but disabled, or if
162 // the current page has TLS errors. 152 // the current page has TLS errors.
163 if ((zero_click_only && !IsZeroClickAllowed()) || 153 if ((zero_click_only && !IsZeroClickAllowed()) ||
164 client_->DidLastPageLoadEncounterSSLErrors()) { 154 client_->DidLastPageLoadEncounterSSLErrors()) {
165 web_contents()->GetRenderViewHost()->Send( 155 // Callback with empty credential info.
166 new CredentialManagerMsg_SendCredential( 156 callback.Run(mojom::CredentialManagerError::SUCCESS,
167 web_contents()->GetRenderViewHost()->GetRoutingID(), request_id, 157 mojom::CredentialInfo::New());
168 CredentialInfo()));
169 return; 158 return;
170 } 159 }
171 160
172 if (store->affiliated_match_helper()) { 161 if (store->affiliated_match_helper()) {
173 store->affiliated_match_helper()->GetAffiliatedAndroidRealms( 162 store->affiliated_match_helper()->GetAffiliatedAndroidRealms(
174 GetSynthesizedFormForOrigin(), 163 GetSynthesizedFormForOrigin(),
175 base::Bind(&CredentialManagerDispatcher::ScheduleRequestTask, 164 base::Bind(&CredentialManagerImpl::ScheduleRequestTask,
176 weak_factory_.GetWeakPtr(), request_id, zero_click_only, 165 weak_factory_.GetWeakPtr(), callback, zero_click_only,
177 include_passwords, federations)); 166 include_passwords, federations.To<std::vector<GURL>>()));
178 } else { 167 } else {
179 std::vector<std::string> no_affiliated_realms; 168 std::vector<std::string> no_affiliated_realms;
180 ScheduleRequestTask(request_id, zero_click_only, include_passwords, 169 ScheduleRequestTask(callback, zero_click_only, include_passwords,
181 federations, no_affiliated_realms); 170 federations.To<std::vector<GURL>>(),
171 no_affiliated_realms);
182 } 172 }
183 } 173 }
184 174
185 void CredentialManagerDispatcher::ScheduleRequestTask( 175 void CredentialManagerImpl::ScheduleRequestTask(
186 int request_id, 176 const GetCallback& callback,
187 bool zero_click_only, 177 bool zero_click_only,
188 bool include_passwords, 178 bool include_passwords,
189 const std::vector<GURL>& federations, 179 const std::vector<GURL>& federations,
190 const std::vector<std::string>& android_realms) { 180 const std::vector<std::string>& android_realms) {
191 DCHECK(GetPasswordStore()); 181 DCHECK(GetPasswordStore());
192 pending_request_.reset(new CredentialManagerPendingRequestTask( 182 pending_request_.reset(new CredentialManagerPendingRequestTask(
193 this, request_id, zero_click_only, 183 this, base::Bind(&RunMojoGetCallback, callback), zero_click_only,
194 web_contents()->GetLastCommittedURL().GetOrigin(), include_passwords, 184 web_contents()->GetLastCommittedURL().GetOrigin(), include_passwords,
195 federations, android_realms)); 185 federations, android_realms));
196 186
197 // This will result in a callback to 187 // This will result in a callback to
198 // PendingRequestTask::OnGetPasswordStoreResults(). 188 // PendingRequestTask::OnGetPasswordStoreResults().
199 GetPasswordStore()->GetAutofillableLogins(pending_request_.get()); 189 GetPasswordStore()->GetAutofillableLogins(pending_request_.get());
200 } 190 }
201 191
202 PasswordStore* CredentialManagerDispatcher::GetPasswordStore() { 192 PasswordStore* CredentialManagerImpl::GetPasswordStore() {
203 return client_ ? client_->GetPasswordStore() : nullptr; 193 return client_ ? client_->GetPasswordStore() : nullptr;
204 } 194 }
205 195
206 bool CredentialManagerDispatcher::IsZeroClickAllowed() const { 196 bool CredentialManagerImpl::IsZeroClickAllowed() const {
207 return *auto_signin_enabled_ && !client_->IsOffTheRecord(); 197 return *auto_signin_enabled_ && !client_->IsOffTheRecord();
208 } 198 }
209 199
210 GURL CredentialManagerDispatcher::GetOrigin() const { 200 GURL CredentialManagerImpl::GetOrigin() const {
211 return web_contents()->GetLastCommittedURL().GetOrigin(); 201 return web_contents()->GetLastCommittedURL().GetOrigin();
212 } 202 }
213 203
214 base::WeakPtr<PasswordManagerDriver> CredentialManagerDispatcher::GetDriver() { 204 base::WeakPtr<PasswordManagerDriver> CredentialManagerImpl::GetDriver() {
215 ContentPasswordManagerDriverFactory* driver_factory = 205 ContentPasswordManagerDriverFactory* driver_factory =
216 ContentPasswordManagerDriverFactory::FromWebContents(web_contents()); 206 ContentPasswordManagerDriverFactory::FromWebContents(web_contents());
217 DCHECK(driver_factory); 207 DCHECK(driver_factory);
218 PasswordManagerDriver* driver = 208 PasswordManagerDriver* driver =
219 driver_factory->GetDriverForFrame(web_contents()->GetMainFrame()); 209 driver_factory->GetDriverForFrame(web_contents()->GetMainFrame());
220 return driver->AsWeakPtr(); 210 return driver->AsWeakPtr();
221 } 211 }
222 212
223 void CredentialManagerDispatcher::SendCredential(int request_id, 213 void CredentialManagerImpl::SendCredential(
224 const CredentialInfo& info) { 214 const SendCredentialCallback& send_callback,
215 const CredentialInfo& info) {
225 DCHECK(pending_request_); 216 DCHECK(pending_request_);
226 DCHECK_EQ(pending_request_->id(), request_id); 217 DCHECK(send_callback.Equals(pending_request_->send_callback()));
227 218
228 if (PasswordStore* store = GetPasswordStore()) { 219 if (PasswordStore* store = GetPasswordStore()) {
229 if (info.type != CredentialType::CREDENTIAL_TYPE_EMPTY && 220 if (info.type != CredentialType::CREDENTIAL_TYPE_EMPTY &&
230 IsZeroClickAllowed()) { 221 IsZeroClickAllowed()) {
231 DCHECK(IsUpdatingCredentialAllowed()); 222 DCHECK(IsUpdatingCredentialAllowed());
232 scoped_ptr<autofill::PasswordForm> form( 223 scoped_ptr<autofill::PasswordForm> form(
233 CreatePasswordFormFromCredentialInfo(info, 224 CreatePasswordFormFromCredentialInfo(info,
234 pending_request_->origin())); 225 pending_request_->origin()));
235 form->skip_zero_click = false; 226 form->skip_zero_click = false;
236 store->UpdateLogin(*form); 227 store->UpdateLogin(*form);
237 } 228 }
238 } 229 }
239 230
240 web_contents()->GetRenderViewHost()->Send( 231 send_callback.Run(info);
241 new CredentialManagerMsg_SendCredential(
242 web_contents()->GetRenderViewHost()->GetRoutingID(),
243 pending_request_->id(), info));
244 pending_request_.reset(); 232 pending_request_.reset();
245 } 233 }
246 234
247 PasswordManagerClient* CredentialManagerDispatcher::client() const { 235 PasswordManagerClient* CredentialManagerImpl::client() const {
248 return client_; 236 return client_;
249 } 237 }
250 238
251 autofill::PasswordForm 239 autofill::PasswordForm CredentialManagerImpl::GetSynthesizedFormForOrigin()
252 CredentialManagerDispatcher::GetSynthesizedFormForOrigin() const { 240 const {
253 autofill::PasswordForm synthetic_form; 241 autofill::PasswordForm synthetic_form;
254 synthetic_form.origin = web_contents()->GetLastCommittedURL().GetOrigin(); 242 synthetic_form.origin = web_contents()->GetLastCommittedURL().GetOrigin();
255 synthetic_form.signon_realm = synthetic_form.origin.spec(); 243 synthetic_form.signon_realm = synthetic_form.origin.spec();
256 synthetic_form.scheme = autofill::PasswordForm::SCHEME_HTML; 244 synthetic_form.scheme = autofill::PasswordForm::SCHEME_HTML;
257 synthetic_form.ssl_valid = synthetic_form.origin.SchemeIsCryptographic() && 245 synthetic_form.ssl_valid = synthetic_form.origin.SchemeIsCryptographic() &&
258 !client_->DidLastPageLoadEncounterSSLErrors(); 246 !client_->DidLastPageLoadEncounterSSLErrors();
259 return synthetic_form; 247 return synthetic_form;
260 } 248 }
261 249
262 void CredentialManagerDispatcher::DoneRequiringUserMediation() { 250 void CredentialManagerImpl::DoneRequiringUserMediation() {
263 DCHECK(pending_require_user_mediation_); 251 DCHECK(pending_require_user_mediation_);
264 pending_require_user_mediation_.reset(); 252 pending_require_user_mediation_.reset();
265 } 253 }
266 254
267 bool CredentialManagerDispatcher::IsUpdatingCredentialAllowed() const { 255 bool CredentialManagerImpl::IsUpdatingCredentialAllowed() const {
268 return !client_->DidLastPageLoadEncounterSSLErrors() && 256 return !client_->DidLastPageLoadEncounterSSLErrors() &&
269 !client_->IsOffTheRecord(); 257 !client_->IsOffTheRecord();
270 } 258 }
271 259
272 } // namespace password_manager 260 } // namespace password_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698