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 autofill::PasswordForm* zero_click_form_to_return = nullptr; | |
57 bool found_zero_clickable_credential = false; | |
58 for (autofill::PasswordForm* form : results) { | |
59 if (form->origin == origin_) { | |
60 local_results.push_back(form); | |
61 | |
62 // If this is a zero-clickable PasswordForm, and we haven't found any | |
63 // other zero-clickable PasswordForms, then store this one for later. | |
64 // If we have found other zero-clickable PasswordForms, then clear | |
65 // the stored form (we return zero-click forms iff there is a single, | |
66 // unambigious choice). | |
67 if (!form->skip_zero_click) { | |
68 zero_click_form_to_return = | |
69 found_zero_clickable_credential ? nullptr : form; | |
70 found_zero_clickable_credential = true; | |
71 } | |
72 } else if (federations_.count(form->origin.spec())) | |
73 federated_results.push_back(form); | |
74 else | |
75 delete form; | |
76 } | |
77 | |
78 if ((local_results.empty() && federated_results.empty()) || | |
79 dispatcher_->web_contents()->GetLastCommittedURL().GetOrigin() != | |
80 origin_) { | |
81 dispatcher_->SendCredential(id_, CredentialInfo()); | |
82 return; | |
83 } | |
84 if (zero_click_form_to_return && dispatcher_->IsZeroClickAllowed()) { | |
85 CredentialInfo info(*zero_click_form_to_return, | |
86 zero_click_form_to_return->federation_url.is_empty() | |
87 ? CredentialType::CREDENTIAL_TYPE_LOCAL | |
88 : CredentialType::CREDENTIAL_TYPE_FEDERATED); | |
89 STLDeleteElements(&local_results); | |
90 STLDeleteElements(&federated_results); | |
91 dispatcher_->SendCredential(id_, info); | |
92 return; | |
93 } | |
94 | |
95 if (zero_click_only_ || | |
96 !dispatcher_->client()->PromptUserToChooseCredentials( | |
97 local_results, federated_results, | |
98 base::Bind(&CredentialManagerDispatcher::SendCredential, | |
99 base::Unretained(dispatcher_), id_))) { | |
100 STLDeleteElements(&local_results); | |
101 STLDeleteElements(&federated_results); | |
102 dispatcher_->SendCredential(id_, CredentialInfo()); | |
103 } | |
104 } | |
105 | 50 |
106 private: | 51 private: |
107 // Backlink to the CredentialManagerDispatcher that owns this object. | 52 // Backlink to the CredentialManagerDispatcher that owns this object. |
108 CredentialManagerDispatcher* const dispatcher_; | 53 CredentialManagerDispatcher* const dispatcher_; |
109 | 54 |
110 const int id_; | 55 const int id_; |
111 const bool zero_click_only_; | 56 const bool zero_click_only_; |
112 const GURL origin_; | 57 const GURL origin_; |
113 std::set<std::string> federations_; | 58 std::set<std::string> federations_; |
114 | 59 |
115 DISALLOW_COPY_AND_ASSIGN(PendingRequestTask); | 60 DISALLOW_COPY_AND_ASSIGN(PendingRequestTask); |
116 }; | 61 }; |
117 | 62 |
| 63 void CredentialManagerDispatcher::PendingRequestTask::OnGetPasswordStoreResults( |
| 64 ScopedVector<autofill::PasswordForm> results) { |
| 65 ScopedVector<autofill::PasswordForm> local_results; |
| 66 ScopedVector<autofill::PasswordForm> federated_results; |
| 67 autofill::PasswordForm* zero_click_form_to_return = nullptr; |
| 68 bool found_zero_clickable_credential = false; |
| 69 for (auto& form : results) { |
| 70 if (form->origin == origin_) { |
| 71 local_results.push_back(form); |
| 72 |
| 73 // If this is a zero-clickable PasswordForm, and we haven't found any |
| 74 // other zero-clickable PasswordForms, then store this one for later. |
| 75 // If we have found other zero-clickable PasswordForms, then clear |
| 76 // the stored form (we return zero-click forms iff there is a single, |
| 77 // unambigious choice). |
| 78 if (!form->skip_zero_click) { |
| 79 zero_click_form_to_return = |
| 80 found_zero_clickable_credential ? nullptr : form; |
| 81 found_zero_clickable_credential = true; |
| 82 } |
| 83 form = nullptr; |
| 84 } else if (federations_.count(form->origin.spec())) { |
| 85 federated_results.push_back(form); |
| 86 form = nullptr; |
| 87 } |
| 88 } |
| 89 |
| 90 if ((local_results.empty() && federated_results.empty()) || |
| 91 dispatcher_->web_contents()->GetLastCommittedURL().GetOrigin() != |
| 92 origin_) { |
| 93 dispatcher_->SendCredential(id_, CredentialInfo()); |
| 94 return; |
| 95 } |
| 96 if (zero_click_form_to_return && dispatcher_->IsZeroClickAllowed()) { |
| 97 CredentialInfo info(*zero_click_form_to_return, |
| 98 zero_click_form_to_return->federation_url.is_empty() |
| 99 ? CredentialType::CREDENTIAL_TYPE_LOCAL |
| 100 : CredentialType::CREDENTIAL_TYPE_FEDERATED); |
| 101 dispatcher_->SendCredential(id_, info); |
| 102 return; |
| 103 } |
| 104 |
| 105 if (zero_click_only_ || |
| 106 !dispatcher_->client()->PromptUserToChooseCredentials( |
| 107 local_results.Pass(), federated_results.Pass(), |
| 108 base::Bind(&CredentialManagerDispatcher::SendCredential, |
| 109 base::Unretained(dispatcher_), id_))) { |
| 110 dispatcher_->SendCredential(id_, CredentialInfo()); |
| 111 } |
| 112 } |
| 113 |
118 // CredentialManagerDispatcher::PendingSignedOutTask --------------------------- | 114 // CredentialManagerDispatcher::PendingSignedOutTask --------------------------- |
119 | 115 |
120 class CredentialManagerDispatcher::PendingSignedOutTask | 116 class CredentialManagerDispatcher::PendingSignedOutTask |
121 : public PasswordStoreConsumer { | 117 : public PasswordStoreConsumer { |
122 public: | 118 public: |
123 PendingSignedOutTask(CredentialManagerDispatcher* const dispatcher, | 119 PendingSignedOutTask(CredentialManagerDispatcher* const dispatcher, |
124 const GURL& origin); | 120 const GURL& origin); |
125 | 121 |
126 void AddOrigin(const GURL& origin); | 122 void AddOrigin(const GURL& origin); |
127 | 123 |
128 // PasswordStoreConsumer implementation. | 124 // PasswordStoreConsumer implementation. |
129 void OnGetPasswordStoreResults( | 125 void OnGetPasswordStoreResults( |
130 const std::vector<autofill::PasswordForm*>& results) override; | 126 ScopedVector<autofill::PasswordForm> results) override; |
131 | 127 |
132 private: | 128 private: |
133 // Backlink to the CredentialManagerDispatcher that owns this object. | 129 // Backlink to the CredentialManagerDispatcher that owns this object. |
134 CredentialManagerDispatcher* const dispatcher_; | 130 CredentialManagerDispatcher* const dispatcher_; |
135 std::set<std::string> origins_; | 131 std::set<std::string> origins_; |
136 | 132 |
137 DISALLOW_COPY_AND_ASSIGN(PendingSignedOutTask); | 133 DISALLOW_COPY_AND_ASSIGN(PendingSignedOutTask); |
138 }; | 134 }; |
139 | 135 |
140 CredentialManagerDispatcher::PendingSignedOutTask::PendingSignedOutTask( | 136 CredentialManagerDispatcher::PendingSignedOutTask::PendingSignedOutTask( |
141 CredentialManagerDispatcher* const dispatcher, | 137 CredentialManagerDispatcher* const dispatcher, |
142 const GURL& origin) | 138 const GURL& origin) |
143 : dispatcher_(dispatcher) { | 139 : dispatcher_(dispatcher) { |
144 origins_.insert(origin.spec()); | 140 origins_.insert(origin.spec()); |
145 } | 141 } |
146 | 142 |
147 void CredentialManagerDispatcher::PendingSignedOutTask::AddOrigin( | 143 void CredentialManagerDispatcher::PendingSignedOutTask::AddOrigin( |
148 const GURL& origin) { | 144 const GURL& origin) { |
149 origins_.insert(origin.spec()); | 145 origins_.insert(origin.spec()); |
150 } | 146 } |
151 | 147 |
152 void CredentialManagerDispatcher::PendingSignedOutTask:: | 148 void CredentialManagerDispatcher::PendingSignedOutTask:: |
153 OnGetPasswordStoreResults( | 149 OnGetPasswordStoreResults(ScopedVector<autofill::PasswordForm> results) { |
154 const std::vector<autofill::PasswordForm*>& results) { | |
155 PasswordStore* store = dispatcher_->GetPasswordStore(); | 150 PasswordStore* store = dispatcher_->GetPasswordStore(); |
156 for (autofill::PasswordForm* form : results) { | 151 for (autofill::PasswordForm* form : results) { |
157 if (origins_.count(form->origin.spec())) { | 152 if (origins_.count(form->origin.spec())) { |
158 form->skip_zero_click = true; | 153 form->skip_zero_click = true; |
| 154 // Note that UpdateLogin ends up copying the form while posting a task to |
| 155 // update the PasswordStore, so it's fine to let |results| delete the |
| 156 // original at the end of this method. |
159 store->UpdateLogin(*form); | 157 store->UpdateLogin(*form); |
160 } | 158 } |
161 // We own the PasswordForms, so we need to dispose of them. This is safe to | |
162 // do, as ::UpdateLogin ends up copying the form while posting a task to | |
163 // update the PasswordStore. | |
164 delete form; | |
165 } | 159 } |
166 | 160 |
167 dispatcher_->DoneSigningOut(); | 161 dispatcher_->DoneSigningOut(); |
168 } | 162 } |
169 | 163 |
170 // CredentialManagerDispatcher ------------------------------------------------- | 164 // CredentialManagerDispatcher ------------------------------------------------- |
171 | 165 |
172 CredentialManagerDispatcher::CredentialManagerDispatcher( | 166 CredentialManagerDispatcher::CredentialManagerDispatcher( |
173 content::WebContents* web_contents, | 167 content::WebContents* web_contents, |
174 PasswordManagerClient* client) | 168 PasswordManagerClient* client) |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 pending_request_->id(), info)); | 320 pending_request_->id(), info)); |
327 pending_request_.reset(); | 321 pending_request_.reset(); |
328 } | 322 } |
329 | 323 |
330 void CredentialManagerDispatcher::DoneSigningOut() { | 324 void CredentialManagerDispatcher::DoneSigningOut() { |
331 DCHECK(pending_sign_out_); | 325 DCHECK(pending_sign_out_); |
332 pending_sign_out_.reset(); | 326 pending_sign_out_.reset(); |
333 } | 327 } |
334 | 328 |
335 } // namespace password_manager | 329 } // namespace password_manager |
OLD | NEW |