app_list: Re-enable people search.
[chromium-blink-merge.git] / chrome / browser / password_manager / password_store_win.cc
blob8f706d93db9f9d812b8eea3a34facc7cf1041f2b
1 // Copyright (c) 2012 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.
5 #include "chrome/browser/password_manager/password_store_win.h"
7 #include <map>
9 #include "base/bind.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "components/os_crypt/ie7_password_win.h"
15 #include "components/password_manager/core/browser/password_manager.h"
16 #include "components/password_manager/core/browser/webdata/password_web_data_service_win.h"
17 #include "content/public/browser/browser_thread.h"
19 using autofill::PasswordForm;
20 using content::BrowserThread;
21 using password_manager::PasswordStoreDefault;
23 // Handles requests to PasswordWebDataService.
24 class PasswordStoreWin::DBHandler : public WebDataServiceConsumer {
25 public:
26 DBHandler(const scoped_refptr<PasswordWebDataService>& web_data_service,
27 PasswordStoreWin* password_store)
28 : web_data_service_(web_data_service), password_store_(password_store) {}
30 ~DBHandler();
32 // Requests the IE7 login for |form|. This is async. |callback_runner| will be
33 // run when complete.
34 void GetIE7Login(
35 const PasswordForm& form,
36 const PasswordStoreWin::ConsumerCallbackRunner& callback_runner);
38 private:
39 struct RequestInfo {
40 RequestInfo() {}
42 RequestInfo(PasswordForm* request_form,
43 const PasswordStoreWin::ConsumerCallbackRunner& runner)
44 : form(request_form),
45 callback_runner(runner) {}
47 PasswordForm* form;
48 PasswordStoreWin::ConsumerCallbackRunner callback_runner;
51 // Holds info associated with in-flight GetIE7Login requests.
52 typedef std::map<PasswordWebDataService::Handle, RequestInfo>
53 PendingRequestMap;
55 // Gets logins from IE7 if no others are found. Also copies them into
56 // Chrome's WebDatabase so we don't need to look next time.
57 std::vector<autofill::PasswordForm*> GetIE7Results(
58 const WDTypedResult* result,
59 const PasswordForm& form);
61 // WebDataServiceConsumer implementation.
62 virtual void OnWebDataServiceRequestDone(
63 PasswordWebDataService::Handle handle,
64 const WDTypedResult* result) override;
66 scoped_refptr<PasswordWebDataService> web_data_service_;
68 // This creates a cycle between us and PasswordStore. The cycle is broken
69 // from PasswordStoreWin::Shutdown, which deletes us.
70 scoped_refptr<PasswordStoreWin> password_store_;
72 PendingRequestMap pending_requests_;
74 DISALLOW_COPY_AND_ASSIGN(DBHandler);
77 PasswordStoreWin::DBHandler::~DBHandler() {
78 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
79 for (PendingRequestMap::const_iterator i = pending_requests_.begin();
80 i != pending_requests_.end();
81 ++i) {
82 web_data_service_->CancelRequest(i->first);
83 delete i->second.form;
87 void PasswordStoreWin::DBHandler::GetIE7Login(
88 const PasswordForm& form,
89 const PasswordStoreWin::ConsumerCallbackRunner& callback_runner) {
90 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
91 IE7PasswordInfo info;
92 info.url_hash =
93 ie7_password::GetUrlHash(base::UTF8ToWide(form.origin.spec()));
94 PasswordWebDataService::Handle handle =
95 web_data_service_->GetIE7Login(info, this);
96 pending_requests_[handle] =
97 RequestInfo(new PasswordForm(form), callback_runner);
100 std::vector<PasswordForm*> PasswordStoreWin::DBHandler::GetIE7Results(
101 const WDTypedResult *result,
102 const PasswordForm& form) {
103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
104 std::vector<PasswordForm*> matching_forms;
106 const WDResult<IE7PasswordInfo>* r =
107 static_cast<const WDResult<IE7PasswordInfo>*>(result);
108 IE7PasswordInfo info = r->GetValue();
110 if (!info.encrypted_data.empty()) {
111 // We got a result.
112 // Delete the entry. If it's good we will add it to the real saved password
113 // table.
114 web_data_service_->RemoveIE7Login(info);
115 std::vector<ie7_password::DecryptedCredentials> credentials;
116 base::string16 url = base::ASCIIToUTF16(form.origin.spec());
117 if (ie7_password::DecryptPasswords(url,
118 info.encrypted_data,
119 &credentials)) {
120 for (size_t i = 0; i < credentials.size(); ++i) {
121 PasswordForm* autofill = new PasswordForm();
122 autofill->username_value = credentials[i].username;
123 autofill->password_value = credentials[i].password;
124 autofill->signon_realm = form.signon_realm;
125 autofill->origin = form.origin;
126 autofill->preferred = true;
127 autofill->ssl_valid = form.origin.SchemeIsSecure();
128 autofill->date_created = info.date_created;
130 matching_forms.push_back(autofill);
131 // Add this PasswordForm to the saved password table. We're on the DB
132 // thread already, so we use AddLoginImpl.
133 password_store_->AddLoginImpl(*autofill);
137 return matching_forms;
140 void PasswordStoreWin::DBHandler::OnWebDataServiceRequestDone(
141 PasswordWebDataService::Handle handle,
142 const WDTypedResult* result) {
143 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
145 PendingRequestMap::iterator i = pending_requests_.find(handle);
146 DCHECK(i != pending_requests_.end());
148 scoped_ptr<PasswordForm> form(i->second.form);
149 PasswordStoreWin::ConsumerCallbackRunner callback_runner(
150 i->second.callback_runner);
151 pending_requests_.erase(i);
153 if (!result) {
154 // The WDS returns NULL if it is shutting down. Run callback with empty
155 // result.
156 callback_runner.Run(std::vector<autofill::PasswordForm*>());
157 return;
160 DCHECK_EQ(PASSWORD_IE7_RESULT, result->GetType());
161 std::vector<autofill::PasswordForm*> matched_forms =
162 GetIE7Results(result, *form);
164 callback_runner.Run(matched_forms);
167 PasswordStoreWin::PasswordStoreWin(
168 scoped_refptr<base::SingleThreadTaskRunner> main_thread_runner,
169 scoped_refptr<base::SingleThreadTaskRunner> db_thread_runner,
170 password_manager::LoginDatabase* login_database,
171 const scoped_refptr<PasswordWebDataService>& web_data_service)
172 : PasswordStoreDefault(main_thread_runner,
173 db_thread_runner,
174 login_database) {
175 db_handler_.reset(new DBHandler(web_data_service, this));
178 PasswordStoreWin::~PasswordStoreWin() {
181 void PasswordStoreWin::ShutdownOnDBThread() {
182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
183 db_handler_.reset();
186 void PasswordStoreWin::Shutdown() {
187 BrowserThread::PostTask(
188 BrowserThread::DB, FROM_HERE,
189 base::Bind(&PasswordStoreWin::ShutdownOnDBThread, this));
190 PasswordStoreDefault::Shutdown();
193 void PasswordStoreWin::GetIE7LoginIfNecessary(
194 const PasswordForm& form,
195 const ConsumerCallbackRunner& callback_runner,
196 const std::vector<autofill::PasswordForm*>& matched_forms) {
197 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
198 if (matched_forms.empty() && db_handler_.get()) {
199 db_handler_->GetIE7Login(form, callback_runner);
200 } else {
201 // No need to get IE7 login.
202 callback_runner.Run(matched_forms);
206 void PasswordStoreWin::GetLoginsImpl(
207 const PasswordForm& form,
208 AuthorizationPromptPolicy prompt_policy,
209 const ConsumerCallbackRunner& callback_runner) {
210 ConsumerCallbackRunner get_ie7_login =
211 base::Bind(&PasswordStoreWin::GetIE7LoginIfNecessary,
212 this, form, callback_runner);
213 PasswordStoreDefault::GetLoginsImpl(form, prompt_policy, get_ie7_login);