| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/password_manager/password_store_default.h" | 5 #include "chrome/browser/password_manager/password_store_default.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 11 #include "chrome/browser/password_manager/password_store_change.h" | 11 #include "chrome/browser/password_manager/password_store_change.h" |
| 12 #include "chrome/browser/prefs/pref_service.h" | 12 #include "chrome/browser/prefs/pref_service.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/webdata/web_data_service.h" | |
| 15 #include "chrome/common/chrome_constants.h" | 14 #include "chrome/common/chrome_constants.h" |
| 16 #include "chrome/common/chrome_notification_types.h" | 15 #include "chrome/common/chrome_notification_types.h" |
| 17 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
| 18 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 19 #include "content/public/browser/notification_service.h" | 18 #include "content/public/browser/notification_service.h" |
| 20 | 19 |
| 21 using content::BrowserThread; | 20 using content::BrowserThread; |
| 22 using webkit::forms::PasswordForm; | 21 using webkit::forms::PasswordForm; |
| 23 | 22 |
| 24 // MigrateHelper handles migration from WebDB to PasswordStore. It runs | |
| 25 // entirely on the UI thread and is owned by PasswordStoreDefault. | |
| 26 class PasswordStoreDefault::MigrateHelper : public WebDataServiceConsumer { | |
| 27 public: | |
| 28 MigrateHelper(Profile* profile, | |
| 29 WebDataService* web_data_service, | |
| 30 PasswordStore* password_store) | |
| 31 : profile_(profile), | |
| 32 web_data_service_(web_data_service), | |
| 33 password_store_(password_store) { | |
| 34 } | |
| 35 ~MigrateHelper(); | |
| 36 | |
| 37 void Init(); | |
| 38 | |
| 39 // WebDataServiceConsumer: | |
| 40 virtual void OnWebDataServiceRequestDone( | |
| 41 WebDataService::Handle handle, | |
| 42 const WDTypedResult *result) OVERRIDE; | |
| 43 | |
| 44 private: | |
| 45 typedef std::set<WebDataService::Handle> Handles; | |
| 46 | |
| 47 Profile* profile_; | |
| 48 | |
| 49 scoped_refptr<WebDataService> web_data_service_; | |
| 50 | |
| 51 // This creates a cycle between us and PasswordStore. The cycle is broken | |
| 52 // from PasswordStoreDefault::Shutdown, which deletes us. | |
| 53 scoped_refptr<PasswordStore> password_store_; | |
| 54 | |
| 55 // Set of handles from requesting data from the WebDB. | |
| 56 Handles handles_; | |
| 57 | |
| 58 DISALLOW_COPY_AND_ASSIGN(MigrateHelper); | |
| 59 }; | |
| 60 | |
| 61 PasswordStoreDefault::MigrateHelper::~MigrateHelper() { | |
| 62 for (Handles::const_iterator i = handles_.begin(); i != handles_.end(); ++i) | |
| 63 web_data_service_->CancelRequest(*i); | |
| 64 handles_.clear(); | |
| 65 } | |
| 66 | |
| 67 void PasswordStoreDefault::MigrateHelper::Init() { | |
| 68 handles_.insert(web_data_service_->GetAutofillableLogins(this)); | |
| 69 handles_.insert(web_data_service_->GetBlacklistLogins(this)); | |
| 70 } | |
| 71 | |
| 72 void PasswordStoreDefault::MigrateHelper::OnWebDataServiceRequestDone( | |
| 73 WebDataService::Handle handle, | |
| 74 const WDTypedResult* result) { | |
| 75 typedef std::vector<const PasswordForm*> PasswordForms; | |
| 76 | |
| 77 DCHECK(handles_.end() != handles_.find(handle)); | |
| 78 DCHECK(password_store_); | |
| 79 | |
| 80 handles_.erase(handle); | |
| 81 if (!result) | |
| 82 return; | |
| 83 | |
| 84 if (PASSWORD_RESULT != result->GetType()) { | |
| 85 NOTREACHED(); | |
| 86 return; | |
| 87 } | |
| 88 | |
| 89 const PasswordForms& forms = | |
| 90 static_cast<const WDResult<PasswordForms>*>(result)->GetValue(); | |
| 91 for (PasswordForms::const_iterator it = forms.begin(); | |
| 92 it != forms.end(); ++it) { | |
| 93 password_store_->AddLogin(**it); | |
| 94 web_data_service_->RemoveLogin(**it); | |
| 95 delete *it; | |
| 96 } | |
| 97 if (handles_.empty()) { | |
| 98 profile_->GetPrefs()->RegisterBooleanPref(prefs::kLoginDatabaseMigrated, | |
| 99 true, | |
| 100 PrefService::UNSYNCABLE_PREF); | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 PasswordStoreDefault::PasswordStoreDefault(LoginDatabase* login_db, | 23 PasswordStoreDefault::PasswordStoreDefault(LoginDatabase* login_db, |
| 105 Profile* profile, | 24 Profile* profile) |
| 106 WebDataService* web_data_service) | 25 : login_db_(login_db), profile_(profile) { |
| 107 : web_data_service_(web_data_service), | |
| 108 login_db_(login_db), profile_(profile) { | |
| 109 DCHECK(login_db); | 26 DCHECK(login_db); |
| 110 DCHECK(profile); | 27 DCHECK(profile); |
| 111 DCHECK(web_data_service); | |
| 112 MigrateIfNecessary(); | |
| 113 } | 28 } |
| 114 | 29 |
| 115 PasswordStoreDefault::~PasswordStoreDefault() { | 30 PasswordStoreDefault::~PasswordStoreDefault() { |
| 116 // MigrateHelper should always be NULL as Shutdown should be invoked before | |
| 117 // the destructor. | |
| 118 DCHECK(!migrate_helper_.get()); | |
| 119 } | 31 } |
| 120 | 32 |
| 121 void PasswordStoreDefault::ShutdownOnUIThread() { | 33 void PasswordStoreDefault::ShutdownOnUIThread() { |
| 122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 123 migrate_helper_.reset(); | |
| 124 profile_ = NULL; | 35 profile_ = NULL; |
| 125 } | 36 } |
| 126 | 37 |
| 127 void PasswordStoreDefault::ReportMetricsImpl() { | 38 void PasswordStoreDefault::ReportMetricsImpl() { |
| 128 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 39 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 129 login_db_->ReportMetrics(); | 40 login_db_->ReportMetrics(); |
| 130 } | 41 } |
| 131 | 42 |
| 132 void PasswordStoreDefault::AddLoginImpl(const PasswordForm& form) { | 43 void PasswordStoreDefault::AddLoginImpl(const PasswordForm& form) { |
| 133 if (login_db_->AddLogin(form)) { | 44 if (login_db_->AddLogin(form)) { |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 std::vector<PasswordForm*>* forms) { | 115 std::vector<PasswordForm*>* forms) { |
| 205 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 116 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 206 return login_db_->GetAutofillableLogins(forms); | 117 return login_db_->GetAutofillableLogins(forms); |
| 207 } | 118 } |
| 208 | 119 |
| 209 bool PasswordStoreDefault::FillBlacklistLogins( | 120 bool PasswordStoreDefault::FillBlacklistLogins( |
| 210 std::vector<PasswordForm*>* forms) { | 121 std::vector<PasswordForm*>* forms) { |
| 211 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 212 return login_db_->GetBlacklistLogins(forms); | 123 return login_db_->GetBlacklistLogins(forms); |
| 213 } | 124 } |
| 214 | |
| 215 void PasswordStoreDefault::MigrateIfNecessary() { | |
| 216 PrefService* prefs = profile_->GetPrefs(); | |
| 217 if (prefs->FindPreference(prefs::kLoginDatabaseMigrated)) | |
| 218 return; | |
| 219 DCHECK(!migrate_helper_.get()); | |
| 220 migrate_helper_.reset(new MigrateHelper(profile_, web_data_service_, this)); | |
| 221 migrate_helper_->Init(); | |
| 222 } | |
| OLD | NEW |