| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "components/password_manager/content/browser/credential_manager_impl.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/strings/string16.h" | |
| 11 #include "base/strings/utf_string_conversions.h" | |
| 12 #include "components/autofill/core/common/password_form.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" | |
| 15 #include "components/password_manager/content/public/cpp/type_converters.h" | |
| 16 #include "components/password_manager/core/browser/affiliated_match_helper.h" | |
| 17 #include "components/password_manager/core/browser/password_manager_client.h" | |
| 18 #include "components/password_manager/core/browser/password_store.h" | |
| 19 #include "components/password_manager/core/common/credential_manager_types.h" | |
| 20 #include "components/password_manager/core/common/password_manager_pref_names.h" | |
| 21 #include "content/public/browser/web_contents.h" | |
| 22 #include "mojo/common/url_type_converters.h" | |
| 23 | |
| 24 namespace password_manager { | |
| 25 | |
| 26 namespace { | |
| 27 | |
| 28 void RunMojoGetCallback(const mojom::CredentialManager::GetCallback& callback, | |
| 29 const CredentialInfo& info) { | |
| 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) | |
| 40 : WebContentsObserver(web_contents), client_(client), weak_factory_(this) { | |
| 41 DCHECK(web_contents); | |
| 42 auto_signin_enabled_.Init(prefs::kCredentialsEnableAutosignin, | |
| 43 client_->GetPrefs()); | |
| 44 } | |
| 45 | |
| 46 CredentialManagerImpl::~CredentialManagerImpl() {} | |
| 47 | |
| 48 void CredentialManagerImpl::BindRequest( | |
| 49 mojom::CredentialManagerRequest request) { | |
| 50 bindings_.AddBinding(this, std::move(request)); | |
| 51 } | |
| 52 | |
| 53 void CredentialManagerImpl::Store(mojom::CredentialInfoPtr credential, | |
| 54 const StoreCallback& callback) { | |
| 55 CredentialInfo info = credential.To<CredentialInfo>(); | |
| 56 DCHECK_NE(CredentialType::CREDENTIAL_TYPE_EMPTY, info.type); | |
| 57 | |
| 58 // Send acknowledge response back. | |
| 59 callback.Run(); | |
| 60 | |
| 61 if (!client_->IsSavingAndFillingEnabledForCurrentPage()) | |
| 62 return; | |
| 63 | |
| 64 std::unique_ptr<autofill::PasswordForm> form( | |
| 65 CreatePasswordFormFromCredentialInfo( | |
| 66 info, web_contents()->GetLastCommittedURL().GetOrigin())); | |
| 67 form->skip_zero_click = !IsZeroClickAllowed(); | |
| 68 | |
| 69 form_manager_.reset(new CredentialManagerPasswordFormManager( | |
| 70 client_, GetDriver(), *form, this)); | |
| 71 } | |
| 72 | |
| 73 void CredentialManagerImpl::OnProvisionalSaveComplete() { | |
| 74 DCHECK(form_manager_); | |
| 75 DCHECK(client_->IsSavingAndFillingEnabledForCurrentPage()); | |
| 76 const autofill::PasswordForm& form = form_manager_->pending_credentials(); | |
| 77 | |
| 78 if (!form.federation_origin.unique()) { | |
| 79 // If this is a federated credential, check it against the federated matches | |
| 80 // produced by the PasswordFormManager. If a match is found, update it and | |
| 81 // return. | |
| 82 for (const auto& match : form_manager_->federated_matches()) { | |
| 83 if (match->username_value == form.username_value && | |
| 84 match->federation_origin.IsSameOriginWith(form.federation_origin)) { | |
| 85 form_manager_->Update(*match); | |
| 86 return; | |
| 87 } | |
| 88 } | |
| 89 } else if (!form_manager_->IsNewLogin()) { | |
| 90 // Otherwise, if this is not a new password credential, update the existing | |
| 91 // credential without prompting the user. This will also update the | |
| 92 // 'skip_zero_click' state, as we've gotten an explicit signal that the page | |
| 93 // understands the credential management API and so can be trusted to notify | |
| 94 // us when they sign the user out. | |
| 95 form_manager_->Update(*form_manager_->preferred_match()); | |
| 96 return; | |
| 97 } | |
| 98 | |
| 99 // Otherwise, this is a new form, so as the user if they'd like to save. | |
| 100 client_->PromptUserToSaveOrUpdatePassword( | |
| 101 std::move(form_manager_), CredentialSourceType::CREDENTIAL_SOURCE_API, | |
| 102 false); | |
| 103 } | |
| 104 | |
| 105 void CredentialManagerImpl::RequireUserMediation( | |
| 106 const RequireUserMediationCallback& callback) { | |
| 107 PasswordStore* store = GetPasswordStore(); | |
| 108 if (!store || !IsUpdatingCredentialAllowed()) { | |
| 109 callback.Run(); | |
| 110 return; | |
| 111 } | |
| 112 | |
| 113 if (store->affiliated_match_helper()) { | |
| 114 store->affiliated_match_helper()->GetAffiliatedAndroidRealms( | |
| 115 GetSynthesizedFormForOrigin(), | |
| 116 base::Bind(&CredentialManagerImpl::ScheduleRequireMediationTask, | |
| 117 weak_factory_.GetWeakPtr(), callback)); | |
| 118 } else { | |
| 119 std::vector<std::string> no_affiliated_realms; | |
| 120 ScheduleRequireMediationTask(callback, no_affiliated_realms); | |
| 121 } | |
| 122 } | |
| 123 | |
| 124 void CredentialManagerImpl::ScheduleRequireMediationTask( | |
| 125 const RequireUserMediationCallback& callback, | |
| 126 const std::vector<std::string>& android_realms) { | |
| 127 DCHECK(GetPasswordStore()); | |
| 128 if (!pending_require_user_mediation_) { | |
| 129 pending_require_user_mediation_.reset( | |
| 130 new CredentialManagerPendingRequireUserMediationTask( | |
| 131 this, web_contents()->GetLastCommittedURL().GetOrigin(), | |
| 132 android_realms)); | |
| 133 | |
| 134 // This will result in a callback to | |
| 135 // CredentialManagerPendingRequireUserMediationTask:: | |
| 136 // OnGetPasswordStoreResults(). | |
| 137 GetPasswordStore()->GetAutofillableLogins( | |
| 138 pending_require_user_mediation_.get()); | |
| 139 } else { | |
| 140 pending_require_user_mediation_->AddOrigin( | |
| 141 web_contents()->GetLastCommittedURL().GetOrigin()); | |
| 142 } | |
| 143 | |
| 144 // Send acknowledge response back. | |
| 145 callback.Run(); | |
| 146 } | |
| 147 | |
| 148 void CredentialManagerImpl::Get(bool zero_click_only, | |
| 149 bool include_passwords, | |
| 150 mojo::Array<mojo::String> federations, | |
| 151 const GetCallback& callback) { | |
| 152 PasswordStore* store = GetPasswordStore(); | |
| 153 if (pending_request_ || !store) { | |
| 154 // Callback error. | |
| 155 callback.Run(pending_request_ | |
| 156 ? mojom::CredentialManagerError::PENDINGREQUEST | |
| 157 : mojom::CredentialManagerError::PASSWORDSTOREUNAVAILABLE, | |
| 158 nullptr); | |
| 159 return; | |
| 160 } | |
| 161 | |
| 162 // Return an empty credential if zero-click is required but disabled, or if | |
| 163 // the current page has TLS errors. | |
| 164 if ((zero_click_only && !IsZeroClickAllowed()) || | |
| 165 client_->DidLastPageLoadEncounterSSLErrors()) { | |
| 166 // Callback with empty credential info. | |
| 167 callback.Run(mojom::CredentialManagerError::SUCCESS, | |
| 168 mojom::CredentialInfo::New()); | |
| 169 return; | |
| 170 } | |
| 171 | |
| 172 if (store->affiliated_match_helper()) { | |
| 173 store->affiliated_match_helper()->GetAffiliatedAndroidRealms( | |
| 174 GetSynthesizedFormForOrigin(), | |
| 175 base::Bind(&CredentialManagerImpl::ScheduleRequestTask, | |
| 176 weak_factory_.GetWeakPtr(), callback, zero_click_only, | |
| 177 include_passwords, federations.To<std::vector<GURL>>())); | |
| 178 } else { | |
| 179 std::vector<std::string> no_affiliated_realms; | |
| 180 ScheduleRequestTask(callback, zero_click_only, include_passwords, | |
| 181 federations.To<std::vector<GURL>>(), | |
| 182 no_affiliated_realms); | |
| 183 } | |
| 184 } | |
| 185 | |
| 186 void CredentialManagerImpl::ScheduleRequestTask( | |
| 187 const GetCallback& callback, | |
| 188 bool zero_click_only, | |
| 189 bool include_passwords, | |
| 190 const std::vector<GURL>& federations, | |
| 191 const std::vector<std::string>& android_realms) { | |
| 192 DCHECK(GetPasswordStore()); | |
| 193 pending_request_.reset(new CredentialManagerPendingRequestTask( | |
| 194 this, base::Bind(&RunMojoGetCallback, callback), zero_click_only, | |
| 195 web_contents()->GetLastCommittedURL().GetOrigin(), include_passwords, | |
| 196 federations, android_realms)); | |
| 197 | |
| 198 // This will result in a callback to | |
| 199 // PendingRequestTask::OnGetPasswordStoreResults(). | |
| 200 GetPasswordStore()->GetAutofillableLogins(pending_request_.get()); | |
| 201 } | |
| 202 | |
| 203 PasswordStore* CredentialManagerImpl::GetPasswordStore() { | |
| 204 return client_ ? client_->GetPasswordStore() : nullptr; | |
| 205 } | |
| 206 | |
| 207 bool CredentialManagerImpl::IsZeroClickAllowed() const { | |
| 208 return *auto_signin_enabled_ && !client_->IsOffTheRecord(); | |
| 209 } | |
| 210 | |
| 211 GURL CredentialManagerImpl::GetOrigin() const { | |
| 212 return web_contents()->GetLastCommittedURL().GetOrigin(); | |
| 213 } | |
| 214 | |
| 215 base::WeakPtr<PasswordManagerDriver> CredentialManagerImpl::GetDriver() { | |
| 216 ContentPasswordManagerDriverFactory* driver_factory = | |
| 217 ContentPasswordManagerDriverFactory::FromWebContents(web_contents()); | |
| 218 DCHECK(driver_factory); | |
| 219 PasswordManagerDriver* driver = | |
| 220 driver_factory->GetDriverForFrame(web_contents()->GetMainFrame()); | |
| 221 return driver->AsWeakPtr(); | |
| 222 } | |
| 223 | |
| 224 void CredentialManagerImpl::SendCredential( | |
| 225 const SendCredentialCallback& send_callback, | |
| 226 const CredentialInfo& info) { | |
| 227 DCHECK(pending_request_); | |
| 228 DCHECK(send_callback.Equals(pending_request_->send_callback())); | |
| 229 | |
| 230 send_callback.Run(info); | |
| 231 pending_request_.reset(); | |
| 232 } | |
| 233 | |
| 234 void CredentialManagerImpl::SendPasswordForm( | |
| 235 const SendCredentialCallback& send_callback, | |
| 236 const autofill::PasswordForm* form) { | |
| 237 CredentialInfo info; | |
| 238 if (form) { | |
| 239 password_manager::CredentialType type_to_return = | |
| 240 form->federation_origin.unique() | |
| 241 ? CredentialType::CREDENTIAL_TYPE_PASSWORD | |
| 242 : CredentialType::CREDENTIAL_TYPE_FEDERATED; | |
| 243 info = CredentialInfo(*form, type_to_return); | |
| 244 if (PasswordStore* store = GetPasswordStore()) { | |
| 245 if (form->skip_zero_click && IsZeroClickAllowed()) { | |
| 246 DCHECK(IsUpdatingCredentialAllowed()); | |
| 247 autofill::PasswordForm update_form = *form; | |
| 248 update_form.skip_zero_click = false; | |
| 249 store->UpdateLogin(update_form); | |
| 250 } | |
| 251 } | |
| 252 } | |
| 253 SendCredential(send_callback, info); | |
| 254 } | |
| 255 | |
| 256 PasswordManagerClient* CredentialManagerImpl::client() const { | |
| 257 return client_; | |
| 258 } | |
| 259 | |
| 260 autofill::PasswordForm CredentialManagerImpl::GetSynthesizedFormForOrigin() | |
| 261 const { | |
| 262 autofill::PasswordForm synthetic_form; | |
| 263 synthetic_form.origin = web_contents()->GetLastCommittedURL().GetOrigin(); | |
| 264 synthetic_form.signon_realm = synthetic_form.origin.spec(); | |
| 265 synthetic_form.scheme = autofill::PasswordForm::SCHEME_HTML; | |
| 266 synthetic_form.ssl_valid = synthetic_form.origin.SchemeIsCryptographic() && | |
| 267 !client_->DidLastPageLoadEncounterSSLErrors(); | |
| 268 return synthetic_form; | |
| 269 } | |
| 270 | |
| 271 void CredentialManagerImpl::DoneRequiringUserMediation() { | |
| 272 DCHECK(pending_require_user_mediation_); | |
| 273 pending_require_user_mediation_.reset(); | |
| 274 } | |
| 275 | |
| 276 bool CredentialManagerImpl::IsUpdatingCredentialAllowed() const { | |
| 277 return !client_->DidLastPageLoadEncounterSSLErrors() && | |
| 278 !client_->IsOffTheRecord(); | |
| 279 } | |
| 280 | |
| 281 } // namespace password_manager | |
| OLD | NEW |