base::Time multiplicative operator overloading
[chromium-blink-merge.git] / chrome / browser / password_manager / password_store_win.cc
blob7dcd18e2d5f384fd1c70da988de7ad1e57aad392
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 typedef base::Callback<void(ScopedVector<PasswordForm>)> ResultCallback;
28 DBHandler(const scoped_refptr<PasswordWebDataService>& web_data_service,
29 PasswordStoreWin* password_store)
30 : web_data_service_(web_data_service), password_store_(password_store) {}
32 ~DBHandler();
34 // Requests the IE7 login for |form|. This is async. |result_callback| will be
35 // run when complete.
36 void GetIE7Login(const PasswordForm& form,
37 const ResultCallback& result_callback);
39 private:
40 struct RequestInfo {
41 RequestInfo() {}
43 RequestInfo(PasswordForm* request_form,
44 const ResultCallback& result_callback)
45 : form(request_form), result_callback(result_callback) {}
47 PasswordForm* form;
48 ResultCallback result_callback;
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 ScopedVector<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 ResultCallback& result_callback) {
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), result_callback);
100 ScopedVector<autofill::PasswordForm> PasswordStoreWin::DBHandler::GetIE7Results(
101 const WDTypedResult* result,
102 const PasswordForm& form) {
103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
104 ScopedVector<autofill::PasswordForm> matched_forms;
105 const WDResult<IE7PasswordInfo>* r =
106 static_cast<const WDResult<IE7PasswordInfo>*>(result);
107 IE7PasswordInfo info = r->GetValue();
109 if (!info.encrypted_data.empty()) {
110 // We got a result.
111 // Delete the entry. If it's good we will add it to the real saved password
112 // table.
113 web_data_service_->RemoveIE7Login(info);
114 std::vector<ie7_password::DecryptedCredentials> credentials;
115 base::string16 url = base::ASCIIToUTF16(form.origin.spec());
116 if (ie7_password::DecryptPasswords(url,
117 info.encrypted_data,
118 &credentials)) {
119 for (size_t i = 0; i < credentials.size(); ++i) {
120 PasswordForm* autofill = new PasswordForm();
121 autofill->username_value = credentials[i].username;
122 autofill->password_value = credentials[i].password;
123 autofill->signon_realm = form.signon_realm;
124 autofill->origin = form.origin;
125 autofill->preferred = true;
126 autofill->ssl_valid = form.origin.SchemeIsSecure();
127 autofill->date_created = info.date_created;
129 matched_forms.push_back(autofill);
130 // Add this PasswordForm to the saved password table. We're on the DB
131 // thread already, so we use AddLoginImpl.
132 password_store_->AddLoginImpl(*autofill);
136 return matched_forms.Pass();
139 void PasswordStoreWin::DBHandler::OnWebDataServiceRequestDone(
140 PasswordWebDataService::Handle handle,
141 const WDTypedResult* result) {
142 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
144 PendingRequestMap::iterator i = pending_requests_.find(handle);
145 DCHECK(i != pending_requests_.end());
147 scoped_ptr<PasswordForm> form(i->second.form);
148 ResultCallback result_callback(i->second.result_callback);
149 pending_requests_.erase(i);
151 if (!result) {
152 // The WDS returns NULL if it is shutting down. Run callback with empty
153 // result.
154 result_callback.Run(ScopedVector<autofill::PasswordForm>());
155 return;
158 DCHECK_EQ(PASSWORD_IE7_RESULT, result->GetType());
159 result_callback.Run(GetIE7Results(result, *form));
162 PasswordStoreWin::PasswordStoreWin(
163 scoped_refptr<base::SingleThreadTaskRunner> main_thread_runner,
164 scoped_refptr<base::SingleThreadTaskRunner> db_thread_runner,
165 scoped_ptr<password_manager::LoginDatabase> login_db,
166 const scoped_refptr<PasswordWebDataService>& web_data_service)
167 : PasswordStoreDefault(main_thread_runner,
168 db_thread_runner,
169 login_db.Pass()) {
170 db_handler_.reset(new DBHandler(web_data_service, this));
173 PasswordStoreWin::~PasswordStoreWin() {
176 void PasswordStoreWin::ShutdownOnDBThread() {
177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
178 db_handler_.reset();
181 void PasswordStoreWin::Shutdown() {
182 BrowserThread::PostTask(
183 BrowserThread::DB, FROM_HERE,
184 base::Bind(&PasswordStoreWin::ShutdownOnDBThread, this));
185 PasswordStoreDefault::Shutdown();
188 void PasswordStoreWin::GetLoginsImpl(const PasswordForm& form,
189 AuthorizationPromptPolicy prompt_policy,
190 scoped_ptr<GetLoginsRequest> request) {
191 // When importing from IE7, the credentials are first stored into a temporary
192 // Web SQL database. Then, after each GetLogins() request that does not yield
193 // any matches from the LoginDatabase, the matching credentials in the Web SQL
194 // database, if any, are returned as results instead, and simultaneously get
195 // moved to the LoginDatabase, so next time they will be found immediately.
196 // TODO(engedy): Make the IE7-specific code synchronous, so FillMatchingLogins
197 // can be overridden instead. See: https://crbug.com/78830.
198 // TODO(engedy): Credentials should be imported into the LoginDatabase in the
199 // first place. See: https://crbug.com/456119.
200 ScopedVector<autofill::PasswordForm> matched_forms(
201 FillMatchingLogins(form, prompt_policy));
202 if (matched_forms.empty() && db_handler_) {
203 db_handler_->GetIE7Login(
204 form, base::Bind(&GetLoginsRequest::NotifyConsumerWithResults,
205 base::Owned(request.release())));
206 } else {
207 request->NotifyConsumerWithResults(matched_forms.Pass());