| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/browsing_data/core/counters/passwords_counter.h" | 5 #include "components/browsing_data/core/counters/passwords_counter.h" |
| 6 | 6 |
| 7 #include "components/browsing_data/core/pref_names.h" | 7 #include "components/browsing_data/core/pref_names.h" |
| 8 #include "components/password_manager/core/browser/password_manager_util.h" |
| 8 #include "components/password_manager/core/browser/password_store.h" | 9 #include "components/password_manager/core/browser/password_store.h" |
| 10 #include "components/sync/driver/sync_service.h" |
| 11 |
| 12 namespace { |
| 13 |
| 14 bool IsPasswordSyncEnabled(const syncer::SyncService* sync_service) { |
| 15 if (!sync_service) |
| 16 return false; |
| 17 return password_manager_util::GetPasswordSyncState(sync_service) != |
| 18 password_manager::PasswordSyncState::NOT_SYNCING_PASSWORDS; |
| 19 } |
| 20 |
| 21 } // namespace |
| 9 | 22 |
| 10 namespace browsing_data { | 23 namespace browsing_data { |
| 11 | 24 |
| 12 PasswordsCounter::PasswordsCounter( | 25 PasswordsCounter::PasswordsCounter( |
| 13 scoped_refptr<password_manager::PasswordStore> store) : store_(store) {} | 26 scoped_refptr<password_manager::PasswordStore> store, |
| 27 syncer::SyncService* sync_service) |
| 28 : store_(store), sync_service_(sync_service), password_sync_enabled_() { |
| 29 DCHECK(store_); |
| 30 } |
| 14 | 31 |
| 15 PasswordsCounter::~PasswordsCounter() { | 32 PasswordsCounter::~PasswordsCounter() { |
| 16 store_->RemoveObserver(this); | 33 store_->RemoveObserver(this); |
| 34 if (sync_service_) |
| 35 sync_service_->RemoveObserver(this); |
| 17 } | 36 } |
| 18 | 37 |
| 19 void PasswordsCounter::OnInitialized() { | 38 void PasswordsCounter::OnInitialized() { |
| 20 DCHECK(store_); | |
| 21 store_->AddObserver(this); | 39 store_->AddObserver(this); |
| 40 if (sync_service_) |
| 41 sync_service_->AddObserver(this); |
| 42 password_sync_enabled_ = IsPasswordSyncEnabled(sync_service_); |
| 22 } | 43 } |
| 23 | 44 |
| 24 const char* PasswordsCounter::GetPrefName() const { | 45 const char* PasswordsCounter::GetPrefName() const { |
| 25 return browsing_data::prefs::kDeletePasswords; | 46 return browsing_data::prefs::kDeletePasswords; |
| 26 } | 47 } |
| 27 | 48 |
| 28 void PasswordsCounter::Count() { | 49 void PasswordsCounter::Count() { |
| 29 cancelable_task_tracker()->TryCancelAll(); | 50 cancelable_task_tracker()->TryCancelAll(); |
| 30 // TODO(msramek): We don't actually need the logins themselves, just their | 51 // TODO(msramek): We don't actually need the logins themselves, just their |
| 31 // count. Consider implementing |PasswordStore::CountAutofillableLogins|. | 52 // count. Consider implementing |PasswordStore::CountAutofillableLogins|. |
| 32 // This custom request should also allow us to specify the time range, so that | 53 // This custom request should also allow us to specify the time range, so that |
| 33 // we can use it to filter the login creation date in the database. | 54 // we can use it to filter the login creation date in the database. |
| 34 store_->GetAutofillableLogins(this); | 55 store_->GetAutofillableLogins(this); |
| 35 } | 56 } |
| 36 | 57 |
| 37 void PasswordsCounter::OnGetPasswordStoreResults( | 58 void PasswordsCounter::OnGetPasswordStoreResults( |
| 38 std::vector<std::unique_ptr<autofill::PasswordForm>> results) { | 59 std::vector<std::unique_ptr<autofill::PasswordForm>> results) { |
| 39 base::Time start = GetPeriodStart(); | 60 base::Time start = GetPeriodStart(); |
| 40 ReportResult(std::count_if( | 61 int num_passwords = std::count_if( |
| 41 results.begin(), results.end(), | 62 results.begin(), results.end(), |
| 42 [start](const std::unique_ptr<autofill::PasswordForm>& form) { | 63 [start](const std::unique_ptr<autofill::PasswordForm>& form) { |
| 43 return form->date_created >= start; | 64 return form->date_created >= start; |
| 44 })); | 65 }); |
| 66 ReportResult(base::MakeUnique<PasswordResult>(this, num_passwords, |
| 67 password_sync_enabled_)); |
| 45 } | 68 } |
| 46 | 69 |
| 47 void PasswordsCounter::OnLoginsChanged( | 70 void PasswordsCounter::OnLoginsChanged( |
| 48 const password_manager::PasswordStoreChangeList& changes) { | 71 const password_manager::PasswordStoreChangeList& changes) { |
| 49 Restart(); | 72 Restart(); |
| 50 } | 73 } |
| 51 | 74 |
| 75 void PasswordsCounter::OnStateChanged(syncer::SyncService* sync) { |
| 76 bool sync_enabled_new = IsPasswordSyncEnabled(sync_service_); |
| 77 |
| 78 if (password_sync_enabled_ != sync_enabled_new) { |
| 79 password_sync_enabled_ = sync_enabled_new; |
| 80 Restart(); |
| 81 } |
| 82 } |
| 83 |
| 84 // PasswordsCounter::PasswordResult |
| 85 |
| 86 PasswordsCounter::PasswordResult::PasswordResult(const PasswordsCounter* source, |
| 87 ResultInt value, |
| 88 bool password_sync_enabled) |
| 89 : FinishedResult(source, value), |
| 90 password_sync_enabled_(password_sync_enabled) {} |
| 91 |
| 92 PasswordsCounter::PasswordResult::~PasswordResult() {} |
| 93 |
| 52 } // namespace browsing_data | 94 } // namespace browsing_data |
| OLD | NEW |