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

Side by Side Diff: components/password_manager/content/browser/credential_manager_dispatcher.cc

Issue 866983003: GetLoginsRequest: Use ScopedVector to express ownership of forms (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@324291_scopedvector
Patch Set: Fix Mac unittest Created 5 years, 10 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_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"
11 #include "components/autofill/core/common/password_form.h" 11 #include "components/autofill/core/common/password_form.h"
12 #include "components/password_manager/content/browser/content_password_manager_d river.h" 12 #include "components/password_manager/content/browser/content_password_manager_d river.h"
13 #include "components/password_manager/content/browser/content_password_manager_d river_factory.h" 13 #include "components/password_manager/content/browser/content_password_manager_d river_factory.h"
14 #include "components/password_manager/content/browser/credential_manager_passwor d_form_manager.h" 14 #include "components/password_manager/content/browser/credential_manager_passwor d_form_manager.h"
15 #include "components/password_manager/content/common/credential_manager_messages .h" 15 #include "components/password_manager/content/common/credential_manager_messages .h"
16 #include "components/password_manager/content/common/credential_manager_types.h" 16 #include "components/password_manager/content/common/credential_manager_types.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/password_manager_pref_names.h" 19 #include "components/password_manager/core/common/password_manager_pref_names.h"
20 #include "content/public/browser/render_view_host.h" 20 #include "content/public/browser/render_view_host.h"
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 class CredentialManagerDispatcher::PendingRequestTask 26 class CredentialManagerDispatcher::PendingRequestTask
27 : public PasswordStoreConsumer { 27 : public PasswordStoreConsumer {
28 public: 28 public:
29 // TODO(mkwst): De-inline the methods in this class. http://goo.gl/RmFwKd
29 PendingRequestTask(CredentialManagerDispatcher* const dispatcher, 30 PendingRequestTask(CredentialManagerDispatcher* const dispatcher,
30 int request_id, 31 int request_id,
31 bool request_zero_click_only, 32 bool request_zero_click_only,
32 const GURL& request_origin, 33 const GURL& request_origin,
33 const std::vector<GURL>& request_federations) 34 const std::vector<GURL>& request_federations)
34 : dispatcher_(dispatcher), 35 : dispatcher_(dispatcher),
35 id_(request_id), 36 id_(request_id),
36 zero_click_only_(request_zero_click_only), 37 zero_click_only_(request_zero_click_only),
37 origin_(request_origin) { 38 origin_(request_origin) {
38 for (const GURL& origin : request_federations) 39 for (const GURL& origin : request_federations)
39 federations_.insert(origin.spec()); 40 federations_.insert(origin.spec());
40 } 41 }
41 42
42 int id() const { return id_; } 43 int id() const { return id_; }
43 44
44 // PasswordStoreConsumer implementation. 45 // PasswordStoreConsumer implementation.
45 void OnGetPasswordStoreResults( 46 void OnGetPasswordStoreResults(
46 const std::vector<autofill::PasswordForm*>& results) override { 47 ScopedVector<autofill::PasswordForm> results) override;
47 // We own the PasswordForm instances, so we're responsible for cleaning
48 // up the instances we don't add to |local_results| or |federated_results|.
49 //
50 // TODO(mkwst): Switch this and PromptUserToChooseCredentials() to use
51 // ScopedVector.
52 std::vector<autofill::PasswordForm*> local_results;
53 std::vector<autofill::PasswordForm*> federated_results;
54 for (autofill::PasswordForm* form : results) {
55 if (form->origin == origin_)
56 local_results.push_back(form);
57 else if (federations_.count(form->origin.spec()))
58 federated_results.push_back(form);
59 else
60 delete form;
61 }
62
63 if ((local_results.empty() && federated_results.empty()) ||
64 dispatcher_->web_contents()->GetLastCommittedURL().GetOrigin() !=
65 origin_) {
66 dispatcher_->SendCredential(id_, CredentialInfo());
67 return;
68 }
69 if (local_results.size() == 1 && dispatcher_->IsZeroClickAllowed()) {
70 // TODO(mkwst): Use the `one_time_disable_zero_click` flag on the result
71 // to prevent auto-sign-in, once that flag is implemented.
72 CredentialInfo info(*local_results[0],
73 local_results[0]->federation_url.is_empty()
74 ? CredentialType::CREDENTIAL_TYPE_LOCAL
75 : CredentialType::CREDENTIAL_TYPE_FEDERATED);
76 STLDeleteElements(&local_results);
77 STLDeleteElements(&federated_results);
78 dispatcher_->SendCredential(id_, info);
79 return;
80 }
81
82 if (zero_click_only_ ||
83 !dispatcher_->client()->PromptUserToChooseCredentials(
84 local_results, federated_results,
85 base::Bind(&CredentialManagerDispatcher::SendCredential,
86 base::Unretained(dispatcher_), id_))) {
87 STLDeleteElements(&local_results);
88 STLDeleteElements(&federated_results);
89 dispatcher_->SendCredential(id_, CredentialInfo());
90 }
91 }
92 48
93 private: 49 private:
94 // Backlink to the CredentialManagerDispatcher that owns this object. 50 // Backlink to the CredentialManagerDispatcher that owns this object.
95 CredentialManagerDispatcher* const dispatcher_; 51 CredentialManagerDispatcher* const dispatcher_;
96 52
97 const int id_; 53 const int id_;
98 const bool zero_click_only_; 54 const bool zero_click_only_;
99 const GURL origin_; 55 const GURL origin_;
100 std::set<std::string> federations_; 56 std::set<std::string> federations_;
101 57
102 DISALLOW_COPY_AND_ASSIGN(PendingRequestTask); 58 DISALLOW_COPY_AND_ASSIGN(PendingRequestTask);
103 }; 59 };
104 60
61 void CredentialManagerDispatcher::PendingRequestTask::OnGetPasswordStoreResults(
62 ScopedVector<autofill::PasswordForm> results) {
63 ScopedVector<autofill::PasswordForm> local_results;
64 ScopedVector<autofill::PasswordForm> federated_results;
65 for (auto& form : results) {
66 if (form->origin == origin_) {
67 local_results.push_back(form);
68 form = nullptr;
69 } else if (federations_.count(form->origin.spec())) {
70 federated_results.push_back(form);
71 form = nullptr;
72 }
73 }
74
75 if ((local_results.empty() && federated_results.empty()) ||
76 dispatcher_->web_contents()->GetLastCommittedURL().GetOrigin() !=
77 origin_) {
78 dispatcher_->SendCredential(id_, CredentialInfo());
79 return;
80 }
81 if (local_results.size() == 1 && dispatcher_->IsZeroClickAllowed()) {
82 // TODO(mkwst): Use the `one_time_disable_zero_click` flag on the result
vasilii 2015/02/05 19:23:27 The flag is 'skip_zero_click' and it's already imp
vabr (Chromium) 2015/02/06 14:16:05 I checked with Mike, and corrected the comment bas
83 // to prevent auto-sign-in, once that flag is implemented.
84 CredentialInfo info(*local_results[0],
85 local_results[0]->federation_url.is_empty()
86 ? CredentialType::CREDENTIAL_TYPE_LOCAL
87 : CredentialType::CREDENTIAL_TYPE_FEDERATED);
88 dispatcher_->SendCredential(id_, info);
89 return;
90 }
91
92 if (zero_click_only_ ||
93 !dispatcher_->client()->PromptUserToChooseCredentials(
94 local_results.Pass(), federated_results.Pass(),
95 base::Bind(&CredentialManagerDispatcher::SendCredential,
96 base::Unretained(dispatcher_), id_))) {
97 dispatcher_->SendCredential(id_, CredentialInfo());
98 }
99 }
100
105 CredentialManagerDispatcher::CredentialManagerDispatcher( 101 CredentialManagerDispatcher::CredentialManagerDispatcher(
106 content::WebContents* web_contents, 102 content::WebContents* web_contents,
107 PasswordManagerClient* client) 103 PasswordManagerClient* client)
108 : WebContentsObserver(web_contents), client_(client) { 104 : WebContentsObserver(web_contents), client_(client) {
109 DCHECK(web_contents); 105 DCHECK(web_contents);
110 auto_signin_enabled_.Init(prefs::kPasswordManagerAutoSignin, 106 auto_signin_enabled_.Init(prefs::kPasswordManagerAutoSignin,
111 client_->GetPrefs()); 107 client_->GetPrefs());
112 } 108 }
113 109
114 CredentialManagerDispatcher::~CredentialManagerDispatcher() { 110 CredentialManagerDispatcher::~CredentialManagerDispatcher() {
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
235 DCHECK(pending_request_); 231 DCHECK(pending_request_);
236 DCHECK_EQ(pending_request_->id(), request_id); 232 DCHECK_EQ(pending_request_->id(), request_id);
237 web_contents()->GetRenderViewHost()->Send( 233 web_contents()->GetRenderViewHost()->Send(
238 new CredentialManagerMsg_SendCredential( 234 new CredentialManagerMsg_SendCredential(
239 web_contents()->GetRenderViewHost()->GetRoutingID(), 235 web_contents()->GetRenderViewHost()->GetRoutingID(),
240 pending_request_->id(), info)); 236 pending_request_->id(), info));
241 pending_request_.reset(); 237 pending_request_.reset();
242 } 238 }
243 239
244 } // namespace password_manager 240 } // namespace password_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698