| 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 "chrome/browser/browsing_data/history_counter.h" | 5 #include "chrome/browser/browsing_data/history_counter.h" |
| 6 | 6 |
| 7 #include "base/prefs/pref_service.h" | 7 #include "base/prefs/pref_service.h" |
| 8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "chrome/browser/history/history_service_factory.h" | 10 #include "chrome/browser/history/history_service_factory.h" |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 void SetDeletionPeriodPref(BrowsingDataRemover::TimePeriod period) { | 55 void SetDeletionPeriodPref(BrowsingDataRemover::TimePeriod period) { |
| 56 browser()->profile()->GetPrefs()->SetInteger( | 56 browser()->profile()->GetPrefs()->SetInteger( |
| 57 prefs::kDeleteTimePeriod, static_cast<int>(period)); | 57 prefs::kDeleteTimePeriod, static_cast<int>(period)); |
| 58 } | 58 } |
| 59 | 59 |
| 60 void WaitForCounting() { | 60 void WaitForCounting() { |
| 61 run_loop_.reset(new base::RunLoop()); | 61 run_loop_.reset(new base::RunLoop()); |
| 62 run_loop_->Run(); | 62 run_loop_->Run(); |
| 63 } | 63 } |
| 64 | 64 |
| 65 BrowsingDataCounter::ResultInt GetResult() { | 65 BrowsingDataCounter::ResultInt GetLocalResult() { |
| 66 DCHECK(finished_); | 66 DCHECK(finished_); |
| 67 return result_; | 67 return local_result_; |
| 68 } | 68 } |
| 69 | 69 |
| 70 void Callback(bool finished, BrowsingDataCounter::ResultInt count) { | 70 bool HasSyncedVisits() { |
| 71 finished_ = finished; | 71 DCHECK(finished_); |
| 72 result_ = count; | 72 return has_synced_visits_; |
| 73 if (run_loop_ && finished) | 73 } |
| 74 |
| 75 void Callback(scoped_ptr<BrowsingDataCounter::Result> result) { |
| 76 finished_ = result->Finished(); |
| 77 |
| 78 if (finished_) { |
| 79 HistoryCounter::HistoryResult* history_result = |
| 80 static_cast<HistoryCounter::HistoryResult*>(result.get()); |
| 81 |
| 82 local_result_ = history_result->Value(); |
| 83 has_synced_visits_ = history_result->has_synced_visits(); |
| 84 } |
| 85 |
| 86 if (run_loop_ && finished_) |
| 74 run_loop_->Quit(); | 87 run_loop_->Quit(); |
| 75 } | 88 } |
| 76 | 89 |
| 77 private: | 90 private: |
| 78 scoped_ptr<base::RunLoop> run_loop_; | 91 scoped_ptr<base::RunLoop> run_loop_; |
| 79 history::HistoryService* service_; | 92 history::HistoryService* service_; |
| 80 base::Time time_; | 93 base::Time time_; |
| 81 | 94 |
| 82 bool finished_; | 95 bool finished_; |
| 83 BrowsingDataCounter::ResultInt result_; | 96 BrowsingDataCounter::ResultInt local_result_; |
| 97 bool has_synced_visits_; |
| 84 }; | 98 }; |
| 85 | 99 |
| 86 // Tests that the counter considers duplicate visits from the same day | 100 // Tests that the counter considers duplicate visits from the same day |
| 87 // to be a single item. | 101 // to be a single item. |
| 88 IN_PROC_BROWSER_TEST_F(HistoryCounterTest, DuplicateVisits) { | 102 IN_PROC_BROWSER_TEST_F(HistoryCounterTest, DuplicateVisits) { |
| 89 AddVisit("https://www.google.com"); // 1 item | 103 AddVisit("https://www.google.com"); // 1 item |
| 90 AddVisit("https://www.google.com"); | 104 AddVisit("https://www.google.com"); |
| 91 AddVisit("https://www.chrome.com"); // 2 items | 105 AddVisit("https://www.chrome.com"); // 2 items |
| 92 AddVisit("https://www.chrome.com"); | 106 AddVisit("https://www.chrome.com"); |
| 93 AddVisit("https://www.chrome.com"); | 107 AddVisit("https://www.chrome.com"); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 107 AddVisit("https://www.google.com"); | 121 AddVisit("https://www.google.com"); |
| 108 AddVisit("https://www.chrome.com"); | 122 AddVisit("https://www.chrome.com"); |
| 109 | 123 |
| 110 HistoryCounter counter; | 124 HistoryCounter counter; |
| 111 counter.Init(browser()->profile(), | 125 counter.Init(browser()->profile(), |
| 112 base::Bind(&HistoryCounterTest::Callback, | 126 base::Bind(&HistoryCounterTest::Callback, |
| 113 base::Unretained(this))); | 127 base::Unretained(this))); |
| 114 counter.Restart(); | 128 counter.Restart(); |
| 115 | 129 |
| 116 WaitForCounting(); | 130 WaitForCounting(); |
| 117 EXPECT_EQ(7u, GetResult()); | 131 EXPECT_EQ(7u, GetLocalResult()); |
| 118 } | 132 } |
| 119 | 133 |
| 120 // Tests that the counter starts counting automatically when the deletion | 134 // Tests that the counter starts counting automatically when the deletion |
| 121 // pref changes to true. | 135 // pref changes to true. |
| 122 IN_PROC_BROWSER_TEST_F(HistoryCounterTest, PrefChanged) { | 136 IN_PROC_BROWSER_TEST_F(HistoryCounterTest, PrefChanged) { |
| 123 SetHistoryDeletionPref(false); | 137 SetHistoryDeletionPref(false); |
| 124 AddVisit("https://www.google.com"); | 138 AddVisit("https://www.google.com"); |
| 125 AddVisit("https://www.chrome.com"); | 139 AddVisit("https://www.chrome.com"); |
| 126 | 140 |
| 127 HistoryCounter counter; | 141 HistoryCounter counter; |
| 128 counter.Init(browser()->profile(), | 142 counter.Init(browser()->profile(), |
| 129 base::Bind(&HistoryCounterTest::Callback, | 143 base::Bind(&HistoryCounterTest::Callback, |
| 130 base::Unretained(this))); | 144 base::Unretained(this))); |
| 131 SetHistoryDeletionPref(true); | 145 SetHistoryDeletionPref(true); |
| 132 | 146 |
| 133 WaitForCounting(); | 147 WaitForCounting(); |
| 134 EXPECT_EQ(2u, GetResult()); | 148 EXPECT_EQ(2u, GetLocalResult()); |
| 135 } | 149 } |
| 136 | 150 |
| 137 // Tests that the counter does not count history if the deletion | 151 // Tests that the counter does not count history if the deletion |
| 138 // preference is false. | 152 // preference is false. |
| 139 IN_PROC_BROWSER_TEST_F(HistoryCounterTest, PrefIsFalse) { | 153 IN_PROC_BROWSER_TEST_F(HistoryCounterTest, PrefIsFalse) { |
| 140 SetHistoryDeletionPref(false); | 154 SetHistoryDeletionPref(false); |
| 141 AddVisit("https://www.google.com"); | 155 AddVisit("https://www.google.com"); |
| 142 | 156 |
| 143 HistoryCounter counter; | 157 HistoryCounter counter; |
| 144 counter.Init(browser()->profile(), | 158 counter.Init(browser()->profile(), |
| (...skipping 28 matching lines...) Expand all Loading... |
| 173 AddVisit("https://www.example.com"); | 187 AddVisit("https://www.example.com"); |
| 174 AddVisit("https://www.example.com"); | 188 AddVisit("https://www.example.com"); |
| 175 | 189 |
| 176 HistoryCounter counter; | 190 HistoryCounter counter; |
| 177 counter.Init(browser()->profile(), | 191 counter.Init(browser()->profile(), |
| 178 base::Bind(&HistoryCounterTest::Callback, | 192 base::Bind(&HistoryCounterTest::Callback, |
| 179 base::Unretained(this))); | 193 base::Unretained(this))); |
| 180 | 194 |
| 181 SetDeletionPeriodPref(BrowsingDataRemover::LAST_HOUR); | 195 SetDeletionPeriodPref(BrowsingDataRemover::LAST_HOUR); |
| 182 WaitForCounting(); | 196 WaitForCounting(); |
| 183 EXPECT_EQ(1u, GetResult()); | 197 EXPECT_EQ(1u, GetLocalResult()); |
| 184 | 198 |
| 185 SetDeletionPeriodPref(BrowsingDataRemover::LAST_DAY); | 199 SetDeletionPeriodPref(BrowsingDataRemover::LAST_DAY); |
| 186 WaitForCounting(); | 200 WaitForCounting(); |
| 187 EXPECT_EQ(1u, GetResult()); | 201 EXPECT_EQ(1u, GetLocalResult()); |
| 188 | 202 |
| 189 SetDeletionPeriodPref(BrowsingDataRemover::LAST_WEEK); | 203 SetDeletionPeriodPref(BrowsingDataRemover::LAST_WEEK); |
| 190 WaitForCounting(); | 204 WaitForCounting(); |
| 191 EXPECT_EQ(5u, GetResult()); | 205 EXPECT_EQ(5u, GetLocalResult()); |
| 192 | 206 |
| 193 SetDeletionPeriodPref(BrowsingDataRemover::FOUR_WEEKS); | 207 SetDeletionPeriodPref(BrowsingDataRemover::FOUR_WEEKS); |
| 194 WaitForCounting(); | 208 WaitForCounting(); |
| 195 EXPECT_EQ(8u, GetResult()); | 209 EXPECT_EQ(8u, GetLocalResult()); |
| 196 | 210 |
| 197 SetDeletionPeriodPref(BrowsingDataRemover::EVERYTHING); | 211 SetDeletionPeriodPref(BrowsingDataRemover::EVERYTHING); |
| 198 WaitForCounting(); | 212 WaitForCounting(); |
| 199 EXPECT_EQ(9u, GetResult()); | 213 EXPECT_EQ(9u, GetLocalResult()); |
| 200 } | 214 } |
| 201 | 215 |
| 202 class FakeWebHistoryService : public history::WebHistoryService { | 216 class FakeWebHistoryService : public history::WebHistoryService { |
| 203 public: | 217 public: |
| 204 | 218 |
| 205 class Request : public history::WebHistoryService::Request { | 219 class Request : public history::WebHistoryService::Request { |
| 206 public: | 220 public: |
| 207 Request(FakeWebHistoryService* service, | 221 Request(FakeWebHistoryService* service, |
| 208 bool emulate_success, | 222 bool emulate_success, |
| 209 int emulate_response_code, | 223 int emulate_response_code, |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 331 // for testing. | 345 // for testing. |
| 332 scoped_ptr<FakeWebHistoryService> service( | 346 scoped_ptr<FakeWebHistoryService> service( |
| 333 new FakeWebHistoryService(browser()->profile())); | 347 new FakeWebHistoryService(browser()->profile())); |
| 334 | 348 |
| 335 HistoryCounter counter; | 349 HistoryCounter counter; |
| 336 counter.SetWebHistoryServiceForTesting(service.get()); | 350 counter.SetWebHistoryServiceForTesting(service.get()); |
| 337 counter.Init(browser()->profile(), | 351 counter.Init(browser()->profile(), |
| 338 base::Bind(&HistoryCounterTest::Callback, | 352 base::Bind(&HistoryCounterTest::Callback, |
| 339 base::Unretained(this))); | 353 base::Unretained(this))); |
| 340 | 354 |
| 341 // No entries locally and no entries in Sync - the result should be zero. | 355 // No entries locally and no entries in Sync. |
| 342 service->SetRequestOptions(true /* success */, net::HTTP_OK); | 356 service->SetRequestOptions(true /* success */, net::HTTP_OK); |
| 343 counter.Restart(); | 357 counter.Restart(); |
| 344 WaitForCounting(); | 358 WaitForCounting(); |
| 345 EXPECT_EQ(0u, GetResult()); | 359 EXPECT_EQ(0u, GetLocalResult()); |
| 360 EXPECT_FALSE(HasSyncedVisits()); |
| 346 | 361 |
| 347 // No entries locally. There are some entries in Sync, but they are out of the | 362 // No entries locally. There are some entries in Sync, but they are out of the |
| 348 // time range. The result should still be zero. | 363 // time range. |
| 349 SetDeletionPeriodPref(BrowsingDataRemover::LAST_HOUR); | 364 SetDeletionPeriodPref(BrowsingDataRemover::LAST_HOUR); |
| 350 service->AddSyncedVisit( | 365 service->AddSyncedVisit( |
| 351 "www.google.com", GetCurrentTime() - base::TimeDelta::FromHours(2)); | 366 "www.google.com", GetCurrentTime() - base::TimeDelta::FromHours(2)); |
| 352 service->AddSyncedVisit( | 367 service->AddSyncedVisit( |
| 353 "www.chrome.com", GetCurrentTime() - base::TimeDelta::FromHours(2)); | 368 "www.chrome.com", GetCurrentTime() - base::TimeDelta::FromHours(2)); |
| 354 service->SetRequestOptions(true /* success */, net::HTTP_OK); | 369 service->SetRequestOptions(true /* success */, net::HTTP_OK); |
| 355 counter.Restart(); | 370 counter.Restart(); |
| 356 WaitForCounting(); | 371 WaitForCounting(); |
| 357 EXPECT_EQ(0u, GetResult()); | 372 EXPECT_EQ(0u, GetLocalResult()); |
| 373 EXPECT_FALSE(HasSyncedVisits()); |
| 358 | 374 |
| 359 // No entries locally, but some entries in Sync - the result should be | 375 // No entries locally, but some entries in Sync. |
| 360 // kOnlySyncedHistory. | |
| 361 service->AddSyncedVisit("www.google.com", GetCurrentTime()); | 376 service->AddSyncedVisit("www.google.com", GetCurrentTime()); |
| 362 service->SetRequestOptions(true /* success */, net::HTTP_OK); | 377 service->SetRequestOptions(true /* success */, net::HTTP_OK); |
| 363 counter.Restart(); | 378 counter.Restart(); |
| 364 WaitForCounting(); | 379 WaitForCounting(); |
| 365 EXPECT_EQ(HistoryCounter::kOnlySyncedHistory, GetResult()); | 380 EXPECT_EQ(0u, GetLocalResult()); |
| 381 EXPECT_TRUE(HasSyncedVisits()); |
| 366 | 382 |
| 367 // To err on the safe side, if the server request fails, we assume that there | 383 // To err on the safe side, if the server request fails, we assume that there |
| 368 // might be some items on the server. | 384 // might be some items on the server. |
| 369 service->SetRequestOptions(true /* success */, | 385 service->SetRequestOptions(true /* success */, |
| 370 net::HTTP_INTERNAL_SERVER_ERROR); | 386 net::HTTP_INTERNAL_SERVER_ERROR); |
| 371 counter.Restart(); | 387 counter.Restart(); |
| 372 WaitForCounting(); | 388 WaitForCounting(); |
| 373 EXPECT_EQ(HistoryCounter::kOnlySyncedHistory, GetResult()); | 389 EXPECT_EQ(0u, GetLocalResult()); |
| 390 EXPECT_TRUE(HasSyncedVisits()); |
| 374 | 391 |
| 375 // Same when the entire query fails. | 392 // Same when the entire query fails. |
| 376 service->SetRequestOptions(false /* success */, | 393 service->SetRequestOptions(false /* success */, |
| 377 net::HTTP_INTERNAL_SERVER_ERROR); | 394 net::HTTP_INTERNAL_SERVER_ERROR); |
| 378 counter.Restart(); | 395 counter.Restart(); |
| 379 WaitForCounting(); | 396 WaitForCounting(); |
| 380 EXPECT_EQ(HistoryCounter::kOnlySyncedHistory, GetResult()); | 397 EXPECT_EQ(0u, GetLocalResult()); |
| 398 EXPECT_TRUE(HasSyncedVisits()); |
| 381 | 399 |
| 382 // If the local count is nonzero, the server result has no influence on the | 400 // Nonzero local count, nonempty sync. |
| 383 // output, whether its nonempty... | |
| 384 AddVisit("https://www.google.com"); | 401 AddVisit("https://www.google.com"); |
| 385 AddVisit("https://www.chrome.com"); | 402 AddVisit("https://www.chrome.com"); |
| 386 service->SetRequestOptions(true /* success */, net::HTTP_OK); | 403 service->SetRequestOptions(true /* success */, net::HTTP_OK); |
| 387 counter.Restart(); | 404 counter.Restart(); |
| 388 WaitForCounting(); | 405 WaitForCounting(); |
| 389 EXPECT_EQ(2u, GetResult()); | 406 EXPECT_EQ(2u, GetLocalResult()); |
| 407 EXPECT_TRUE(HasSyncedVisits()); |
| 390 | 408 |
| 391 // ...or empty. | 409 // Nonzero local count, empty sync. |
| 392 service->ClearSyncedVisits(); | 410 service->ClearSyncedVisits(); |
| 393 service->SetRequestOptions(true /* success */, net::HTTP_OK); | 411 service->SetRequestOptions(true /* success */, net::HTTP_OK); |
| 394 counter.Restart(); | 412 counter.Restart(); |
| 395 WaitForCounting(); | 413 WaitForCounting(); |
| 396 EXPECT_EQ(2u, GetResult()); | 414 EXPECT_EQ(2u, GetLocalResult()); |
| 415 EXPECT_FALSE(HasSyncedVisits()); |
| 397 } | 416 } |
| 398 | 417 |
| 399 } // namespace | 418 } // namespace |
| OLD | NEW |