| OLD | NEW |
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 "base/run_loop.h" | 7 #include "base/run_loop.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
| 10 #include "chrome/browser/password_manager/password_store_factory.h" | 10 #include "chrome/browser/password_manager/password_store_factory.h" |
| 11 #include "chrome/browser/sync/profile_sync_service_factory.h" | 11 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 12 #include "chrome/browser/sync/test/integration/passwords_helper.h" | 12 #include "chrome/browser/sync/test/integration/passwords_helper.h" |
| 13 #include "chrome/browser/sync/test/integration/sync_test.h" | |
| 14 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
| 15 #include "chrome/test/base/in_process_browser_test.h" | 14 #include "chrome/test/base/in_process_browser_test.h" |
| 16 #include "components/autofill/core/common/password_form.h" | 15 #include "components/autofill/core/common/password_form.h" |
| 17 #include "components/browser_sync/profile_sync_service.h" | 16 #include "components/browser_sync/profile_sync_service.h" |
| 18 #include "components/browser_sync/profile_sync_service_mock.h" | |
| 19 #include "components/browsing_data/core/browsing_data_utils.h" | 17 #include "components/browsing_data/core/browsing_data_utils.h" |
| 20 #include "components/browsing_data/core/pref_names.h" | 18 #include "components/browsing_data/core/pref_names.h" |
| 21 #include "components/prefs/pref_service.h" | 19 #include "components/prefs/pref_service.h" |
| 22 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
| 23 | 21 |
| 24 namespace { | 22 namespace { |
| 25 | 23 |
| 26 using autofill::PasswordForm; | 24 using autofill::PasswordForm; |
| 25 using browsing_data::BrowsingDataCounter; |
| 27 | 26 |
| 28 // TODO(crbug.com/553421): Only RestartOnSyncChange is a SyncTest. | 27 class PasswordsCounterTest : public InProcessBrowserTest { |
| 29 // Extract it together with HistoryCounterTest.RestartOnSyncChange. | |
| 30 class PasswordsCounterTest : public SyncTest { | |
| 31 public: | 28 public: |
| 32 PasswordsCounterTest() : SyncTest(SINGLE_CLIENT) {} | 29 PasswordsCounterTest() {} |
| 33 | 30 |
| 34 void SetUpOnMainThread() override { | 31 void SetUpOnMainThread() override { |
| 35 finished_ = false; | 32 finished_ = false; |
| 36 time_ = base::Time::Now(); | 33 time_ = base::Time::Now(); |
| 37 store_ = PasswordStoreFactory::GetForProfile( | 34 store_ = PasswordStoreFactory::GetForProfile( |
| 38 browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS); | 35 browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS); |
| 39 SetPasswordsDeletionPref(true); | 36 SetPasswordsDeletionPref(true); |
| 40 SetDeletionPeriodPref(browsing_data::TimePeriod::ALL_TIME); | 37 SetDeletionPeriodPref(browsing_data::TimePeriod::ALL_TIME); |
| 41 } | 38 } |
| 42 | 39 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 base::Unretained(&waitable_event))); | 78 base::Unretained(&waitable_event))); |
| 82 waitable_event.Wait(); | 79 waitable_event.Wait(); |
| 83 | 80 |
| 84 // At this point, the calculation on DB thread should have finished, and | 81 // At this point, the calculation on DB thread should have finished, and |
| 85 // a callback should be scheduled on the UI thread. Process the tasks until | 82 // a callback should be scheduled on the UI thread. Process the tasks until |
| 86 // we get a finished result. | 83 // we get a finished result. |
| 87 run_loop_.reset(new base::RunLoop()); | 84 run_loop_.reset(new base::RunLoop()); |
| 88 run_loop_->Run(); | 85 run_loop_->Run(); |
| 89 } | 86 } |
| 90 | 87 |
| 91 bool CountingFinishedSinceLastAsked() { | 88 BrowsingDataCounter::ResultInt GetResult() { |
| 92 bool result = finished_; | |
| 93 finished_ = false; | |
| 94 return result; | |
| 95 } | |
| 96 | |
| 97 void WaitForCountingOrConfirmFinished() { | |
| 98 if (CountingFinishedSinceLastAsked()) | |
| 99 return; | |
| 100 | |
| 101 WaitForCounting(); | |
| 102 CountingFinishedSinceLastAsked(); | |
| 103 } | |
| 104 | |
| 105 browsing_data::BrowsingDataCounter::ResultInt GetResult() { | |
| 106 DCHECK(finished_); | 89 DCHECK(finished_); |
| 107 return result_; | 90 return result_; |
| 108 } | 91 } |
| 109 | 92 |
| 110 bool PasswordSyncEnabled() { return password_sync_enabled_; } | 93 void Callback(std::unique_ptr<BrowsingDataCounter::Result> result) { |
| 111 | |
| 112 void Callback( | |
| 113 std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) { | |
| 114 DCHECK(result); | 94 DCHECK(result); |
| 115 finished_ = result->Finished(); | 95 finished_ = result->Finished(); |
| 116 | 96 |
| 117 if (finished_) { | 97 if (finished_) { |
| 118 auto* password_result = | 98 auto* password_result = |
| 119 static_cast<browsing_data::PasswordsCounter::PasswordResult*>( | 99 static_cast<BrowsingDataCounter::SyncResult*>(result.get()); |
| 120 result.get()); | |
| 121 result_ = password_result->Value(); | 100 result_ = password_result->Value(); |
| 122 | |
| 123 password_sync_enabled_ = password_result->password_sync_enabled(); | |
| 124 } | 101 } |
| 125 if (run_loop_ && finished_) | 102 if (run_loop_ && finished_) |
| 126 run_loop_->Quit(); | 103 run_loop_->Quit(); |
| 127 } | 104 } |
| 128 | 105 |
| 129 void WaitForUICallbacksFromAddingLogins() { | 106 void WaitForUICallbacksFromAddingLogins() { |
| 130 base::RunLoop loop; | 107 base::RunLoop loop; |
| 131 loop.RunUntilIdle(); | 108 loop.RunUntilIdle(); |
| 132 } | 109 } |
| 133 | 110 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 144 result.date_created = time_; | 121 result.date_created = time_; |
| 145 return result; | 122 return result; |
| 146 } | 123 } |
| 147 | 124 |
| 148 scoped_refptr<password_manager::PasswordStore> store_; | 125 scoped_refptr<password_manager::PasswordStore> store_; |
| 149 | 126 |
| 150 std::unique_ptr<base::RunLoop> run_loop_; | 127 std::unique_ptr<base::RunLoop> run_loop_; |
| 151 base::Time time_; | 128 base::Time time_; |
| 152 | 129 |
| 153 bool finished_; | 130 bool finished_; |
| 154 browsing_data::BrowsingDataCounter::ResultInt result_; | 131 BrowsingDataCounter::ResultInt result_; |
| 155 bool password_sync_enabled_; | |
| 156 }; | 132 }; |
| 157 | 133 |
| 158 // Tests that the counter correctly counts each individual credential on | 134 // Tests that the counter correctly counts each individual credential on |
| 159 // the same domain. | 135 // the same domain. |
| 160 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, SameDomain) { | 136 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, SameDomain) { |
| 161 AddLogin("https://www.google.com", "user1", false); | 137 AddLogin("https://www.google.com", "user1", false); |
| 162 AddLogin("https://www.google.com", "user2", false); | 138 AddLogin("https://www.google.com", "user2", false); |
| 163 AddLogin("https://www.google.com", "user3", false); | 139 AddLogin("https://www.google.com", "user3", false); |
| 164 AddLogin("https://www.chrome.com", "user1", false); | 140 AddLogin("https://www.chrome.com", "user1", false); |
| 165 AddLogin("https://www.chrome.com", "user2", false); | 141 AddLogin("https://www.chrome.com", "user2", false); |
| 166 WaitForUICallbacksFromAddingLogins(); | 142 WaitForUICallbacksFromAddingLogins(); |
| 167 | 143 |
| 168 Profile* profile = browser()->profile(); | 144 Profile* profile = browser()->profile(); |
| 169 browsing_data::PasswordsCounter counter( | 145 browsing_data::PasswordsCounter counter( |
| 170 PasswordStoreFactory::GetForProfile(profile, | 146 PasswordStoreFactory::GetForProfile(profile, |
| 171 ServiceAccessType::EXPLICIT_ACCESS), | 147 ServiceAccessType::EXPLICIT_ACCESS), |
| 172 ProfileSyncServiceFactory::GetForProfile(profile)); | 148 ProfileSyncServiceFactory::GetForProfile(profile)); |
| 173 counter.Init( | 149 counter.Init( |
| 174 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED, | 150 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED, |
| 175 base::Bind(&PasswordsCounterTest::Callback, base::Unretained(this))); | 151 base::Bind(&PasswordsCounterTest::Callback, base::Unretained(this))); |
| 176 counter.Restart(); | 152 counter.Restart(); |
| 177 | 153 |
| 178 WaitForCounting(); | 154 WaitForCounting(); |
| 179 EXPECT_EQ(5u, GetResult()); | 155 EXPECT_EQ(5u, GetResult()); |
| 180 EXPECT_FALSE(PasswordSyncEnabled()); | |
| 181 } | 156 } |
| 182 | 157 |
| 183 // Tests that the counter doesn't count blacklisted entries. | 158 // Tests that the counter doesn't count blacklisted entries. |
| 184 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, Blacklisted) { | 159 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, Blacklisted) { |
| 185 AddLogin("https://www.google.com", "user1", false); | 160 AddLogin("https://www.google.com", "user1", false); |
| 186 AddLogin("https://www.google.com", "user2", true); | 161 AddLogin("https://www.google.com", "user2", true); |
| 187 AddLogin("https://www.chrome.com", "user3", true); | 162 AddLogin("https://www.chrome.com", "user3", true); |
| 188 WaitForUICallbacksFromAddingLogins(); | 163 WaitForUICallbacksFromAddingLogins(); |
| 189 | 164 |
| 190 Profile* profile = browser()->profile(); | 165 Profile* profile = browser()->profile(); |
| 191 browsing_data::PasswordsCounter counter( | 166 browsing_data::PasswordsCounter counter( |
| 192 PasswordStoreFactory::GetForProfile(profile, | 167 PasswordStoreFactory::GetForProfile(profile, |
| 193 ServiceAccessType::EXPLICIT_ACCESS), | 168 ServiceAccessType::EXPLICIT_ACCESS), |
| 194 ProfileSyncServiceFactory::GetForProfile(profile)); | 169 ProfileSyncServiceFactory::GetForProfile(profile)); |
| 195 | 170 |
| 196 counter.Init( | 171 counter.Init( |
| 197 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED, | 172 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED, |
| 198 base::Bind(&PasswordsCounterTest::Callback, base::Unretained(this))); | 173 base::Bind(&PasswordsCounterTest::Callback, base::Unretained(this))); |
| 199 counter.Restart(); | 174 counter.Restart(); |
| 200 | 175 |
| 201 WaitForCounting(); | 176 WaitForCounting(); |
| 202 EXPECT_EQ(1u, GetResult()); | 177 EXPECT_EQ(1u, GetResult()); |
| 203 EXPECT_FALSE(PasswordSyncEnabled()); | |
| 204 } | 178 } |
| 205 | 179 |
| 206 // Tests that the counter starts counting automatically when the deletion | 180 // Tests that the counter starts counting automatically when the deletion |
| 207 // pref changes to true. | 181 // pref changes to true. |
| 208 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, PrefChanged) { | 182 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, PrefChanged) { |
| 209 SetPasswordsDeletionPref(false); | 183 SetPasswordsDeletionPref(false); |
| 210 AddLogin("https://www.google.com", "user", false); | 184 AddLogin("https://www.google.com", "user", false); |
| 211 AddLogin("https://www.chrome.com", "user", false); | 185 AddLogin("https://www.chrome.com", "user", false); |
| 212 WaitForUICallbacksFromAddingLogins(); | 186 WaitForUICallbacksFromAddingLogins(); |
| 213 | 187 |
| 214 Profile* profile = browser()->profile(); | 188 Profile* profile = browser()->profile(); |
| 215 browsing_data::PasswordsCounter counter( | 189 browsing_data::PasswordsCounter counter( |
| 216 PasswordStoreFactory::GetForProfile(profile, | 190 PasswordStoreFactory::GetForProfile(profile, |
| 217 ServiceAccessType::EXPLICIT_ACCESS), | 191 ServiceAccessType::EXPLICIT_ACCESS), |
| 218 ProfileSyncServiceFactory::GetForProfile(profile)); | 192 ProfileSyncServiceFactory::GetForProfile(profile)); |
| 219 counter.Init( | 193 counter.Init( |
| 220 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED, | 194 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED, |
| 221 base::Bind(&PasswordsCounterTest::Callback, base::Unretained(this))); | 195 base::Bind(&PasswordsCounterTest::Callback, base::Unretained(this))); |
| 222 SetPasswordsDeletionPref(true); | 196 SetPasswordsDeletionPref(true); |
| 223 | 197 |
| 224 WaitForCounting(); | 198 WaitForCounting(); |
| 225 EXPECT_EQ(2u, GetResult()); | 199 EXPECT_EQ(2u, GetResult()); |
| 226 EXPECT_FALSE(PasswordSyncEnabled()); | |
| 227 } | 200 } |
| 228 | 201 |
| 229 // Tests that the counter starts counting automatically when | 202 // Tests that the counter starts counting automatically when |
| 230 // the password store changes. | 203 // the password store changes. |
| 231 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, StoreChanged) { | 204 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, StoreChanged) { |
| 232 AddLogin("https://www.google.com", "user", false); | 205 AddLogin("https://www.google.com", "user", false); |
| 233 WaitForUICallbacksFromAddingLogins(); | 206 WaitForUICallbacksFromAddingLogins(); |
| 234 | 207 |
| 235 Profile* profile = browser()->profile(); | 208 Profile* profile = browser()->profile(); |
| 236 browsing_data::PasswordsCounter counter( | 209 browsing_data::PasswordsCounter counter( |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 | 262 |
| 290 SetDeletionPeriodPref(browsing_data::TimePeriod::FOUR_WEEKS); | 263 SetDeletionPeriodPref(browsing_data::TimePeriod::FOUR_WEEKS); |
| 291 WaitForCounting(); | 264 WaitForCounting(); |
| 292 EXPECT_EQ(3u, GetResult()); | 265 EXPECT_EQ(3u, GetResult()); |
| 293 | 266 |
| 294 SetDeletionPeriodPref(browsing_data::TimePeriod::ALL_TIME); | 267 SetDeletionPeriodPref(browsing_data::TimePeriod::ALL_TIME); |
| 295 WaitForCounting(); | 268 WaitForCounting(); |
| 296 EXPECT_EQ(4u, GetResult()); | 269 EXPECT_EQ(4u, GetResult()); |
| 297 } | 270 } |
| 298 | 271 |
| 299 // Test that the counting restarts when password sync state changes. | |
| 300 // TODO(crbug.com/553421): Move this to the sync/test/integration directory? | |
| 301 IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, RestartOnSyncChange) { | |
| 302 // Set up the Sync client. | |
| 303 ASSERT_TRUE(SetupClients()); | |
| 304 static const int kFirstProfileIndex = 0; | |
| 305 browser_sync::ProfileSyncService* sync_service = | |
| 306 GetSyncService(kFirstProfileIndex); | |
| 307 Profile* profile = GetProfile(kFirstProfileIndex); | |
| 308 // Set up the counter. | |
| 309 browsing_data::PasswordsCounter counter( | |
| 310 PasswordStoreFactory::GetForProfile(profile, | |
| 311 ServiceAccessType::EXPLICIT_ACCESS), | |
| 312 sync_service); | |
| 313 | |
| 314 counter.Init( | |
| 315 profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED, | |
| 316 base::Bind(&PasswordsCounterTest::Callback, base::Unretained(this))); | |
| 317 | |
| 318 // Note that some Sync operations notify observers immediately (and thus there | |
| 319 // is no need to call |WaitForCounting()|; in fact, it would block the test), | |
| 320 // while other operations only post the task on UI thread's message loop | |
| 321 // (which requires calling |WaitForCounting()| for them to run). Therefore, | |
| 322 // this test always checks if the callback has already run and only waits | |
| 323 // if it has not. | |
| 324 | |
| 325 // We sync all datatypes by default, so starting Sync means that we start | |
| 326 // syncing passwords, and this should restart the counter. | |
| 327 ASSERT_TRUE(SetupSync()); | |
| 328 ASSERT_TRUE(sync_service->IsSyncActive()); | |
| 329 ASSERT_TRUE(sync_service->GetPreferredDataTypes().Has(syncer::PASSWORDS)); | |
| 330 WaitForCountingOrConfirmFinished(); | |
| 331 EXPECT_TRUE(PasswordSyncEnabled()); | |
| 332 | |
| 333 // We stop syncing passwords in particular. This restarts the counter. | |
| 334 syncer::ModelTypeSet everything_except_passwords = | |
| 335 syncer::UserSelectableTypes(); | |
| 336 everything_except_passwords.Remove(syncer::PASSWORDS); | |
| 337 auto sync_blocker = sync_service->GetSetupInProgressHandle(); | |
| 338 sync_service->OnUserChoseDatatypes(/*sync_everything=*/false, | |
| 339 everything_except_passwords); | |
| 340 ASSERT_FALSE(sync_service->GetPreferredDataTypes().Has(syncer::PASSWORDS)); | |
| 341 sync_blocker.reset(); | |
| 342 WaitForCountingOrConfirmFinished(); | |
| 343 ASSERT_FALSE(sync_service->GetPreferredDataTypes().Has(syncer::PASSWORDS)); | |
| 344 EXPECT_FALSE(PasswordSyncEnabled()); | |
| 345 | |
| 346 // If password sync is not affected, the counter is not restarted. | |
| 347 syncer::ModelTypeSet only_history(syncer::HISTORY_DELETE_DIRECTIVES); | |
| 348 sync_service->ChangePreferredDataTypes(only_history); | |
| 349 sync_blocker = sync_service->GetSetupInProgressHandle(); | |
| 350 sync_service->ChangePreferredDataTypes(only_history); | |
| 351 sync_blocker.reset(); | |
| 352 EXPECT_FALSE(CountingFinishedSinceLastAsked()); | |
| 353 | |
| 354 // We start syncing passwords again. This restarts the counter. | |
| 355 sync_blocker = sync_service->GetSetupInProgressHandle(); | |
| 356 sync_service->ChangePreferredDataTypes(syncer::ModelTypeSet::All()); | |
| 357 sync_blocker.reset(); | |
| 358 WaitForCountingOrConfirmFinished(); | |
| 359 EXPECT_TRUE(PasswordSyncEnabled()); | |
| 360 | |
| 361 // Stopping the Sync service triggers a restart. | |
| 362 sync_service->RequestStop(syncer::SyncService::CLEAR_DATA); | |
| 363 WaitForCountingOrConfirmFinished(); | |
| 364 EXPECT_FALSE(PasswordSyncEnabled()); | |
| 365 } | |
| 366 | |
| 367 } // namespace | 272 } // namespace |
| OLD | NEW |