| 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/safe_browsing/client_side_detection_service.h" | 5 #include "chrome/browser/safe_browsing/client_side_detection_service.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <memory> | 10 #include <memory> |
| 11 #include <queue> | 11 #include <queue> |
| 12 #include <string> | 12 #include <string> |
| 13 | 13 |
| 14 #include "base/bind.h" | 14 #include "base/bind.h" |
| 15 #include "base/callback.h" | 15 #include "base/callback.h" |
| 16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/macros.h" | 17 #include "base/macros.h" |
| 18 #include "base/memory/ptr_util.h" |
| 18 #include "base/message_loop/message_loop.h" | 19 #include "base/message_loop/message_loop.h" |
| 19 #include "base/metrics/field_trial.h" | 20 #include "base/metrics/field_trial.h" |
| 20 #include "base/run_loop.h" | 21 #include "base/run_loop.h" |
| 21 #include "base/strings/string_number_conversions.h" | 22 #include "base/strings/string_number_conversions.h" |
| 22 #include "base/time/time.h" | 23 #include "base/time/time.h" |
| 23 #include "chrome/common/safe_browsing/client_model.pb.h" | 24 #include "chrome/common/safe_browsing/client_model.pb.h" |
| 24 #include "chrome/common/safe_browsing/csd.pb.h" | 25 #include "chrome/common/safe_browsing/csd.pb.h" |
| 25 #include "components/variations/variations_associated_data.h" | 26 #include "components/variations/variations_associated_data.h" |
| 26 #include "content/public/test/test_browser_thread.h" | 27 #include "content/public/test/test_browser_thread.h" |
| 27 #include "crypto/sha2.h" | 28 #include "crypto/sha2.h" |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 std::queue<base::Time>& GetPhishingReportTimes() { | 149 std::queue<base::Time>& GetPhishingReportTimes() { |
| 149 return csd_service_->phishing_report_times_; | 150 return csd_service_->phishing_report_times_; |
| 150 } | 151 } |
| 151 | 152 |
| 152 std::queue<base::Time>& GetMalwareReportTimes() { | 153 std::queue<base::Time>& GetMalwareReportTimes() { |
| 153 return csd_service_->malware_report_times_; | 154 return csd_service_->malware_report_times_; |
| 154 } | 155 } |
| 155 | 156 |
| 156 void SetCache(const GURL& gurl, bool is_phishing, base::Time time) { | 157 void SetCache(const GURL& gurl, bool is_phishing, base::Time time) { |
| 157 csd_service_->cache_[gurl] = | 158 csd_service_->cache_[gurl] = |
| 158 make_linked_ptr(new ClientSideDetectionService::CacheState(is_phishing, | 159 base::MakeUnique<ClientSideDetectionService::CacheState>( |
| 159 time)); | 160 is_phishing, time); |
| 160 } | 161 } |
| 161 | 162 |
| 162 void TestCache() { | 163 void TestCache() { |
| 163 ClientSideDetectionService::PhishingCache& cache = csd_service_->cache_; | 164 auto& cache = csd_service_->cache_; |
| 164 base::Time now = base::Time::Now(); | 165 base::Time now = base::Time::Now(); |
| 165 base::Time time = | 166 base::Time time = |
| 166 now - base::TimeDelta::FromDays( | 167 now - base::TimeDelta::FromDays( |
| 167 ClientSideDetectionService::kNegativeCacheIntervalDays) + | 168 ClientSideDetectionService::kNegativeCacheIntervalDays) + |
| 168 base::TimeDelta::FromMinutes(5); | 169 base::TimeDelta::FromMinutes(5); |
| 169 cache[GURL("http://first.url.com/")] = | 170 cache[GURL("http://first.url.com/")] = |
| 170 make_linked_ptr(new ClientSideDetectionService::CacheState(false, | 171 base::MakeUnique<ClientSideDetectionService::CacheState>(false, time); |
| 171 time)); | |
| 172 | 172 |
| 173 time = | 173 time = |
| 174 now - base::TimeDelta::FromDays( | 174 now - base::TimeDelta::FromDays( |
| 175 ClientSideDetectionService::kNegativeCacheIntervalDays) - | 175 ClientSideDetectionService::kNegativeCacheIntervalDays) - |
| 176 base::TimeDelta::FromHours(1); | 176 base::TimeDelta::FromHours(1); |
| 177 cache[GURL("http://second.url.com/")] = | 177 cache[GURL("http://second.url.com/")] |
| 178 make_linked_ptr(new ClientSideDetectionService::CacheState(false, | 178 = base::MakeUnique<ClientSideDetectionService::CacheState>(false, time); |
| 179 time)); | |
| 180 | 179 |
| 181 time = | 180 time = |
| 182 now - base::TimeDelta::FromMinutes( | 181 now - base::TimeDelta::FromMinutes( |
| 183 ClientSideDetectionService::kPositiveCacheIntervalMinutes) - | 182 ClientSideDetectionService::kPositiveCacheIntervalMinutes) - |
| 184 base::TimeDelta::FromMinutes(5); | 183 base::TimeDelta::FromMinutes(5); |
| 185 cache[GURL("http://third.url.com/")] = | 184 cache[GURL("http://third.url.com/")] |
| 186 make_linked_ptr(new ClientSideDetectionService::CacheState(true, time)); | 185 = base::MakeUnique<ClientSideDetectionService::CacheState>(true, time); |
| 187 | 186 |
| 188 time = | 187 time = |
| 189 now - base::TimeDelta::FromMinutes( | 188 now - base::TimeDelta::FromMinutes( |
| 190 ClientSideDetectionService::kPositiveCacheIntervalMinutes) + | 189 ClientSideDetectionService::kPositiveCacheIntervalMinutes) + |
| 191 base::TimeDelta::FromMinutes(5); | 190 base::TimeDelta::FromMinutes(5); |
| 192 cache[GURL("http://fourth.url.com/")] = | 191 cache[GURL("http://fourth.url.com/")] = |
| 193 make_linked_ptr(new ClientSideDetectionService::CacheState(true, time)); | 192 base::MakeUnique<ClientSideDetectionService::CacheState>(true, time); |
| 194 | 193 |
| 195 csd_service_->UpdateCache(); | 194 csd_service_->UpdateCache(); |
| 196 | 195 |
| 197 // 3 elements should be in the cache, the first, third, and fourth. | 196 // 3 elements should be in the cache, the first, third, and fourth. |
| 198 EXPECT_EQ(3U, cache.size()); | 197 EXPECT_EQ(3U, cache.size()); |
| 199 EXPECT_TRUE(cache.find(GURL("http://first.url.com/")) != cache.end()); | 198 EXPECT_TRUE(cache.find(GURL("http://first.url.com/")) != cache.end()); |
| 200 EXPECT_TRUE(cache.find(GURL("http://third.url.com/")) != cache.end()); | 199 EXPECT_TRUE(cache.find(GURL("http://third.url.com/")) != cache.end()); |
| 201 EXPECT_TRUE(cache.find(GURL("http://fourth.url.com/")) != cache.end()); | 200 EXPECT_TRUE(cache.find(GURL("http://fourth.url.com/")) != cache.end()); |
| 202 | 201 |
| 203 // While 3 elements remain, only the first and the fourth are actually | 202 // While 3 elements remain, only the first and the fourth are actually |
| (...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 482 | 481 |
| 483 // Check that disabling again doesn't request the model. | 482 // Check that disabling again doesn't request the model. |
| 484 csd_service_->SetEnabledAndRefreshState(false); | 483 csd_service_->SetEnabledAndRefreshState(false); |
| 485 // No calls expected. | 484 // No calls expected. |
| 486 base::RunLoop().RunUntilIdle(); | 485 base::RunLoop().RunUntilIdle(); |
| 487 Mock::VerifyAndClearExpectations(service); | 486 Mock::VerifyAndClearExpectations(service); |
| 488 Mock::VerifyAndClearExpectations(loader_1); | 487 Mock::VerifyAndClearExpectations(loader_1); |
| 489 Mock::VerifyAndClearExpectations(loader_2); | 488 Mock::VerifyAndClearExpectations(loader_2); |
| 490 } | 489 } |
| 491 } // namespace safe_browsing | 490 } // namespace safe_browsing |
| OLD | NEW |