| 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/credential_manager_dispatc
her.h" | 5 #include "components/password_manager/content/browser/credential_manager_dispatc
her.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/scoped_vector.h" | 8 #include "base/memory/scoped_vector.h" |
| 9 #include "base/strings/string16.h" | 9 #include "base/strings/string16.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
| 22 #include "ipc/ipc_message_macros.h" | 22 #include "ipc/ipc_message_macros.h" |
| 23 | 23 |
| 24 namespace password_manager { | 24 namespace password_manager { |
| 25 | 25 |
| 26 // CredentialManagerDispatcher::PendingRequestTask ----------------------------- | 26 // CredentialManagerDispatcher::PendingRequestTask ----------------------------- |
| 27 | 27 |
| 28 class CredentialManagerDispatcher::PendingRequestTask | 28 class CredentialManagerDispatcher::PendingRequestTask |
| 29 : public PasswordStoreConsumer { | 29 : public PasswordStoreConsumer { |
| 30 public: | 30 public: |
| 31 // TODO(mkwst): De-inline the methods in this class. http://goo.gl/RmFwKd |
| 31 PendingRequestTask(CredentialManagerDispatcher* const dispatcher, | 32 PendingRequestTask(CredentialManagerDispatcher* const dispatcher, |
| 32 int request_id, | 33 int request_id, |
| 33 bool request_zero_click_only, | 34 bool request_zero_click_only, |
| 34 const GURL& request_origin, | 35 const GURL& request_origin, |
| 35 const std::vector<GURL>& request_federations) | 36 const std::vector<GURL>& request_federations) |
| 36 : dispatcher_(dispatcher), | 37 : dispatcher_(dispatcher), |
| 37 id_(request_id), | 38 id_(request_id), |
| 38 zero_click_only_(request_zero_click_only), | 39 zero_click_only_(request_zero_click_only), |
| 39 origin_(request_origin) { | 40 origin_(request_origin) { |
| 40 for (const GURL& origin : request_federations) | 41 for (const GURL& origin : request_federations) |
| 41 federations_.insert(origin.spec()); | 42 federations_.insert(origin.spec()); |
| 42 } | 43 } |
| 43 | 44 |
| 44 int id() const { return id_; } | 45 int id() const { return id_; } |
| 45 | 46 |
| 46 // PasswordStoreConsumer implementation. | 47 // PasswordStoreConsumer implementation. |
| 47 void OnGetPasswordStoreResults( | 48 void OnGetPasswordStoreResults( |
| 48 const std::vector<autofill::PasswordForm*>& results) override { | 49 ScopedVector<autofill::PasswordForm> results) override; |
| 49 // We own the PasswordForm instances, so we're responsible for cleaning | |
| 50 // up the instances we don't add to |local_results| or |federated_results|. | |
| 51 // | |
| 52 // TODO(mkwst): Switch this and PromptUserToChooseCredentials() to use | |
| 53 // ScopedVector. | |
| 54 std::vector<autofill::PasswordForm*> local_results; | |
| 55 std::vector<autofill::PasswordForm*> federated_results; | |
| 56 for (autofill::PasswordForm* form : results) { | |
| 57 if (form->origin == origin_) | |
| 58 local_results.push_back(form); | |
| 59 else if (federations_.count(form->origin.spec())) | |
| 60 federated_results.push_back(form); | |
| 61 else | |
| 62 delete form; | |
| 63 } | |
| 64 | |
| 65 if ((local_results.empty() && federated_results.empty()) || | |
| 66 dispatcher_->web_contents()->GetLastCommittedURL().GetOrigin() != | |
| 67 origin_) { | |
| 68 dispatcher_->SendCredential(id_, CredentialInfo()); | |
| 69 return; | |
| 70 } | |
| 71 if (local_results.size() == 1 && dispatcher_->IsZeroClickAllowed()) { | |
| 72 // TODO(mkwst): Use the `one_time_disable_zero_click` flag on the result | |
| 73 // to prevent auto-sign-in, once that flag is implemented. | |
| 74 CredentialInfo info(*local_results[0], | |
| 75 local_results[0]->federation_url.is_empty() | |
| 76 ? CredentialType::CREDENTIAL_TYPE_LOCAL | |
| 77 : CredentialType::CREDENTIAL_TYPE_FEDERATED); | |
| 78 STLDeleteElements(&local_results); | |
| 79 STLDeleteElements(&federated_results); | |
| 80 dispatcher_->SendCredential(id_, info); | |
| 81 return; | |
| 82 } | |
| 83 | |
| 84 if (zero_click_only_ || | |
| 85 !dispatcher_->client()->PromptUserToChooseCredentials( | |
| 86 local_results, federated_results, | |
| 87 base::Bind(&CredentialManagerDispatcher::SendCredential, | |
| 88 base::Unretained(dispatcher_), id_))) { | |
| 89 STLDeleteElements(&local_results); | |
| 90 STLDeleteElements(&federated_results); | |
| 91 dispatcher_->SendCredential(id_, CredentialInfo()); | |
| 92 } | |
| 93 } | |
| 94 | 50 |
| 95 private: | 51 private: |
| 96 // Backlink to the CredentialManagerDispatcher that owns this object. | 52 // Backlink to the CredentialManagerDispatcher that owns this object. |
| 97 CredentialManagerDispatcher* const dispatcher_; | 53 CredentialManagerDispatcher* const dispatcher_; |
| 98 | 54 |
| 99 const int id_; | 55 const int id_; |
| 100 const bool zero_click_only_; | 56 const bool zero_click_only_; |
| 101 const GURL origin_; | 57 const GURL origin_; |
| 102 std::set<std::string> federations_; | 58 std::set<std::string> federations_; |
| 103 | 59 |
| 104 DISALLOW_COPY_AND_ASSIGN(PendingRequestTask); | 60 DISALLOW_COPY_AND_ASSIGN(PendingRequestTask); |
| 105 }; | 61 }; |
| 106 | 62 |
| 63 void CredentialManagerDispatcher::PendingRequestTask::OnGetPasswordStoreResults( |
| 64 ScopedVector<autofill::PasswordForm> results) { |
| 65 ScopedVector<autofill::PasswordForm> local_results; |
| 66 ScopedVector<autofill::PasswordForm> federated_results; |
| 67 for (auto& form : results) { |
| 68 if (form->origin == origin_) { |
| 69 local_results.push_back(form); |
| 70 form = nullptr; |
| 71 } else if (federations_.count(form->origin.spec())) { |
| 72 federated_results.push_back(form); |
| 73 form = nullptr; |
| 74 } |
| 75 } |
| 76 |
| 77 if ((local_results.empty() && federated_results.empty()) || |
| 78 dispatcher_->web_contents()->GetLastCommittedURL().GetOrigin() != |
| 79 origin_) { |
| 80 dispatcher_->SendCredential(id_, CredentialInfo()); |
| 81 return; |
| 82 } |
| 83 if (local_results.size() == 1 && dispatcher_->IsZeroClickAllowed()) { |
| 84 // TODO(mkwst): Use the `skip_zero_click` flag on the result to prevent |
| 85 // auto-sign-in. |
| 86 CredentialInfo info(*local_results[0], |
| 87 local_results[0]->federation_url.is_empty() |
| 88 ? CredentialType::CREDENTIAL_TYPE_LOCAL |
| 89 : CredentialType::CREDENTIAL_TYPE_FEDERATED); |
| 90 dispatcher_->SendCredential(id_, info); |
| 91 return; |
| 92 } |
| 93 |
| 94 if (zero_click_only_ || |
| 95 !dispatcher_->client()->PromptUserToChooseCredentials( |
| 96 local_results.Pass(), federated_results.Pass(), |
| 97 base::Bind(&CredentialManagerDispatcher::SendCredential, |
| 98 base::Unretained(dispatcher_), id_))) { |
| 99 dispatcher_->SendCredential(id_, CredentialInfo()); |
| 100 } |
| 101 } |
| 102 |
| 107 // CredentialManagerDispatcher::PendingSignedOutTask --------------------------- | 103 // CredentialManagerDispatcher::PendingSignedOutTask --------------------------- |
| 108 | 104 |
| 109 class CredentialManagerDispatcher::PendingSignedOutTask | 105 class CredentialManagerDispatcher::PendingSignedOutTask |
| 110 : public PasswordStoreConsumer { | 106 : public PasswordStoreConsumer { |
| 111 public: | 107 public: |
| 112 PendingSignedOutTask(CredentialManagerDispatcher* const dispatcher, | 108 PendingSignedOutTask(CredentialManagerDispatcher* const dispatcher, |
| 113 const GURL& origin); | 109 const GURL& origin); |
| 114 | 110 |
| 115 void AddOrigin(const GURL& origin); | 111 void AddOrigin(const GURL& origin); |
| 116 | 112 |
| 117 // PasswordStoreConsumer implementation. | 113 // PasswordStoreConsumer implementation. |
| 118 void OnGetPasswordStoreResults( | 114 void OnGetPasswordStoreResults( |
| 119 const std::vector<autofill::PasswordForm*>& results) override; | 115 ScopedVector<autofill::PasswordForm> results) override; |
| 120 | 116 |
| 121 private: | 117 private: |
| 122 // Backlink to the CredentialManagerDispatcher that owns this object. | 118 // Backlink to the CredentialManagerDispatcher that owns this object. |
| 123 CredentialManagerDispatcher* const dispatcher_; | 119 CredentialManagerDispatcher* const dispatcher_; |
| 124 std::set<std::string> origins_; | 120 std::set<std::string> origins_; |
| 125 | 121 |
| 126 DISALLOW_COPY_AND_ASSIGN(PendingSignedOutTask); | 122 DISALLOW_COPY_AND_ASSIGN(PendingSignedOutTask); |
| 127 }; | 123 }; |
| 128 | 124 |
| 129 CredentialManagerDispatcher::PendingSignedOutTask::PendingSignedOutTask( | 125 CredentialManagerDispatcher::PendingSignedOutTask::PendingSignedOutTask( |
| 130 CredentialManagerDispatcher* const dispatcher, | 126 CredentialManagerDispatcher* const dispatcher, |
| 131 const GURL& origin) | 127 const GURL& origin) |
| 132 : dispatcher_(dispatcher) { | 128 : dispatcher_(dispatcher) { |
| 133 origins_.insert(origin.spec()); | 129 origins_.insert(origin.spec()); |
| 134 } | 130 } |
| 135 | 131 |
| 136 void CredentialManagerDispatcher::PendingSignedOutTask::AddOrigin( | 132 void CredentialManagerDispatcher::PendingSignedOutTask::AddOrigin( |
| 137 const GURL& origin) { | 133 const GURL& origin) { |
| 138 origins_.insert(origin.spec()); | 134 origins_.insert(origin.spec()); |
| 139 } | 135 } |
| 140 | 136 |
| 141 void CredentialManagerDispatcher::PendingSignedOutTask:: | 137 void CredentialManagerDispatcher::PendingSignedOutTask:: |
| 142 OnGetPasswordStoreResults( | 138 OnGetPasswordStoreResults(ScopedVector<autofill::PasswordForm> results) { |
| 143 const std::vector<autofill::PasswordForm*>& results) { | |
| 144 PasswordStore* store = dispatcher_->GetPasswordStore(); | 139 PasswordStore* store = dispatcher_->GetPasswordStore(); |
| 145 for (autofill::PasswordForm* form : results) { | 140 for (autofill::PasswordForm* form : results) { |
| 146 if (origins_.count(form->origin.spec())) { | 141 if (origins_.count(form->origin.spec())) { |
| 147 form->skip_zero_click = true; | 142 form->skip_zero_click = true; |
| 143 // Note that UpdateLogin ends up copying the form while posting a task to |
| 144 // update the PasswordStore, so it's fine to let |results| delete the |
| 145 // original at the end of this method. |
| 148 store->UpdateLogin(*form); | 146 store->UpdateLogin(*form); |
| 149 } | 147 } |
| 150 // We own the PasswordForms, so we need to dispose of them. This is safe to | |
| 151 // do, as ::UpdateLogin ends up copying the form while posting a task to | |
| 152 // update the PasswordStore. | |
| 153 delete form; | |
| 154 } | 148 } |
| 155 | 149 |
| 156 dispatcher_->DoneSigningOut(); | 150 dispatcher_->DoneSigningOut(); |
| 157 } | 151 } |
| 158 | 152 |
| 159 // CredentialManagerDispatcher ------------------------------------------------- | 153 // CredentialManagerDispatcher ------------------------------------------------- |
| 160 | 154 |
| 161 CredentialManagerDispatcher::CredentialManagerDispatcher( | 155 CredentialManagerDispatcher::CredentialManagerDispatcher( |
| 162 content::WebContents* web_contents, | 156 content::WebContents* web_contents, |
| 163 PasswordManagerClient* client) | 157 PasswordManagerClient* client) |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 315 pending_request_->id(), info)); | 309 pending_request_->id(), info)); |
| 316 pending_request_.reset(); | 310 pending_request_.reset(); |
| 317 } | 311 } |
| 318 | 312 |
| 319 void CredentialManagerDispatcher::DoneSigningOut() { | 313 void CredentialManagerDispatcher::DoneSigningOut() { |
| 320 DCHECK(pending_sign_out_); | 314 DCHECK(pending_sign_out_); |
| 321 pending_sign_out_.reset(); | 315 pending_sign_out_.reset(); |
| 322 } | 316 } |
| 323 | 317 |
| 324 } // namespace password_manager | 318 } // namespace password_manager |
| OLD | NEW |