OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/incident_reporting/last_download_finder.h
" | 5 #include "chrome/browser/safe_browsing/incident_reporting/last_download_finder.h
" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 |
| 9 #include <memory> |
8 #include <string> | 10 #include <string> |
9 #include <utility> | 11 #include <utility> |
10 #include <vector> | 12 #include <vector> |
11 | 13 |
12 #include "base/bind.h" | 14 #include "base/bind.h" |
13 #include "base/callback.h" | 15 #include "base/callback.h" |
14 #include "base/files/file_path.h" | 16 #include "base/files/file_path.h" |
15 #include "base/files/file_util.h" | 17 #include "base/files/file_util.h" |
16 #include "base/guid.h" | 18 #include "base/guid.h" |
17 #include "base/location.h" | 19 #include "base/location.h" |
18 #include "base/memory/scoped_ptr.h" | 20 #include "base/memory/ptr_util.h" |
19 #include "base/metrics/field_trial.h" | 21 #include "base/metrics/field_trial.h" |
20 #include "base/run_loop.h" | 22 #include "base/run_loop.h" |
21 #include "base/single_thread_task_runner.h" | 23 #include "base/single_thread_task_runner.h" |
22 #include "base/strings/string_number_conversions.h" | 24 #include "base/strings/string_number_conversions.h" |
23 #include "base/strings/utf_string_conversions.h" | 25 #include "base/strings/utf_string_conversions.h" |
24 #include "base/test/mock_entropy_provider.h" | 26 #include "base/test/mock_entropy_provider.h" |
25 #include "base/thread_task_runner_handle.h" | 27 #include "base/thread_task_runner_handle.h" |
26 #include "build/build_config.h" | 28 #include "build/build_config.h" |
27 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 29 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
28 #include "chrome/browser/history/chrome_history_client.h" | 30 #include "chrome/browser/history/chrome_history_client.h" |
(...skipping 17 matching lines...) Expand all Loading... |
46 #include "components/history/core/browser/history_service.h" | 48 #include "components/history/core/browser/history_service.h" |
47 #include "components/syncable_prefs/testing_pref_service_syncable.h" | 49 #include "components/syncable_prefs/testing_pref_service_syncable.h" |
48 #include "content/public/test/test_browser_thread_bundle.h" | 50 #include "content/public/test/test_browser_thread_bundle.h" |
49 #include "content/public/test/test_utils.h" | 51 #include "content/public/test/test_utils.h" |
50 #include "testing/gtest/include/gtest/gtest.h" | 52 #include "testing/gtest/include/gtest/gtest.h" |
51 | 53 |
52 namespace { | 54 namespace { |
53 | 55 |
54 // A BrowserContextKeyedServiceFactory::TestingFactoryFunction that creates a | 56 // A BrowserContextKeyedServiceFactory::TestingFactoryFunction that creates a |
55 // HistoryService for a TestingProfile. | 57 // HistoryService for a TestingProfile. |
56 scoped_ptr<KeyedService> BuildHistoryService(content::BrowserContext* context) { | 58 std::unique_ptr<KeyedService> BuildHistoryService( |
| 59 content::BrowserContext* context) { |
57 TestingProfile* profile = static_cast<TestingProfile*>(context); | 60 TestingProfile* profile = static_cast<TestingProfile*>(context); |
58 | 61 |
59 // Delete the file before creating the service. | 62 // Delete the file before creating the service. |
60 base::FilePath history_path( | 63 base::FilePath history_path( |
61 profile->GetPath().Append(history::kHistoryFilename)); | 64 profile->GetPath().Append(history::kHistoryFilename)); |
62 if (!base::DeleteFile(history_path, false) || | 65 if (!base::DeleteFile(history_path, false) || |
63 base::PathExists(history_path)) { | 66 base::PathExists(history_path)) { |
64 ADD_FAILURE() << "failed to delete history db file " | 67 ADD_FAILURE() << "failed to delete history db file " |
65 << history_path.value(); | 68 << history_path.value(); |
66 return nullptr; | 69 return nullptr; |
67 } | 70 } |
68 | 71 |
69 scoped_ptr<history::HistoryService> history_service( | 72 std::unique_ptr<history::HistoryService> history_service( |
70 new history::HistoryService( | 73 new history::HistoryService( |
71 make_scoped_ptr(new ChromeHistoryClient( | 74 base::WrapUnique(new ChromeHistoryClient( |
72 BookmarkModelFactory::GetForProfile(profile))), | 75 BookmarkModelFactory::GetForProfile(profile))), |
73 scoped_ptr<history::VisitDelegate>())); | 76 std::unique_ptr<history::VisitDelegate>())); |
74 if (history_service->Init( | 77 if (history_service->Init( |
75 history::HistoryDatabaseParamsForPath(profile->GetPath()))) { | 78 history::HistoryDatabaseParamsForPath(profile->GetPath()))) { |
76 return std::move(history_service); | 79 return std::move(history_service); |
77 } | 80 } |
78 | 81 |
79 ADD_FAILURE() << "failed to initialize history service"; | 82 ADD_FAILURE() << "failed to initialize history service"; |
80 return nullptr; | 83 return nullptr; |
81 } | 84 } |
82 | 85 |
83 #if defined(OS_WIN) | 86 #if defined(OS_WIN) |
(...skipping 18 matching lines...) Expand all Loading... |
102 | 105 |
103 static const base::FilePath::CharType kPDFFileName[] = | 106 static const base::FilePath::CharType kPDFFileName[] = |
104 FILE_PATH_LITERAL("download.pdf"); | 107 FILE_PATH_LITERAL("download.pdf"); |
105 | 108 |
106 } // namespace | 109 } // namespace |
107 | 110 |
108 namespace safe_browsing { | 111 namespace safe_browsing { |
109 | 112 |
110 class LastDownloadFinderTest : public testing::Test { | 113 class LastDownloadFinderTest : public testing::Test { |
111 public: | 114 public: |
112 void NeverCalled(scoped_ptr<ClientIncidentReport_DownloadDetails> download, | 115 void NeverCalled( |
113 scoped_ptr<ClientIncidentReport_NonBinaryDownloadDetails> | 116 std::unique_ptr<ClientIncidentReport_DownloadDetails> download, |
114 non_binary_download) { | 117 std::unique_ptr<ClientIncidentReport_NonBinaryDownloadDetails> |
| 118 non_binary_download) { |
115 FAIL(); | 119 FAIL(); |
116 } | 120 } |
117 | 121 |
118 // Creates a new profile that participates in safe browsing and adds a | 122 // Creates a new profile that participates in safe browsing and adds a |
119 // download to its history. | 123 // download to its history. |
120 void CreateProfileWithDownload() { | 124 void CreateProfileWithDownload() { |
121 TestingProfile* profile = CreateProfile(EXTENDED_REPORTING_OPT_IN); | 125 TestingProfile* profile = CreateProfile(EXTENDED_REPORTING_OPT_IN); |
122 history::HistoryService* history_service = | 126 history::HistoryService* history_service = |
123 HistoryServiceFactory::GetForProfile( | 127 HistoryServiceFactory::GetForProfile( |
124 profile, ServiceAccessType::EXPLICIT_ACCESS); | 128 profile, ServiceAccessType::EXPLICIT_ACCESS); |
125 history_service->CreateDownload( | 129 history_service->CreateDownload( |
126 CreateTestDownloadRow(kBinaryFileName), | 130 CreateTestDownloadRow(kBinaryFileName), |
127 base::Bind(&LastDownloadFinderTest::OnDownloadCreated, | 131 base::Bind(&LastDownloadFinderTest::OnDownloadCreated, |
128 base::Unretained(this))); | 132 base::Unretained(this))); |
129 } | 133 } |
130 | 134 |
131 // LastDownloadFinder::LastDownloadCallback implementation that | 135 // LastDownloadFinder::LastDownloadCallback implementation that |
132 // passes the found download to |result| and then runs a closure. | 136 // passes the found download to |result| and then runs a closure. |
133 void OnLastDownload(scoped_ptr<ClientIncidentReport_DownloadDetails>* result, | 137 void OnLastDownload( |
134 scoped_ptr<ClientIncidentReport_NonBinaryDownloadDetails>* | 138 std::unique_ptr<ClientIncidentReport_DownloadDetails>* result, |
135 non_binary_result, | 139 std::unique_ptr<ClientIncidentReport_NonBinaryDownloadDetails>* |
136 const base::Closure& quit_closure, | 140 non_binary_result, |
137 scoped_ptr<ClientIncidentReport_DownloadDetails> download, | 141 const base::Closure& quit_closure, |
138 scoped_ptr<ClientIncidentReport_NonBinaryDownloadDetails> | 142 std::unique_ptr<ClientIncidentReport_DownloadDetails> download, |
139 non_binary_download) { | 143 std::unique_ptr<ClientIncidentReport_NonBinaryDownloadDetails> |
| 144 non_binary_download) { |
140 *result = std::move(download); | 145 *result = std::move(download); |
141 *non_binary_result = std::move(non_binary_download); | 146 *non_binary_result = std::move(non_binary_download); |
142 quit_closure.Run(); | 147 quit_closure.Run(); |
143 } | 148 } |
144 | 149 |
145 protected: | 150 protected: |
146 // A type for specifying whether or not a profile created by CreateProfile | 151 // A type for specifying whether or not a profile created by CreateProfile |
147 // participates in safe browsing. | 152 // participates in safe browsing. |
148 enum SafeBrowsingDisposition { | 153 enum SafeBrowsingDisposition { |
149 SAFE_BROWSING_OPT_OUT, | 154 SAFE_BROWSING_OPT_OUT, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 // Build up a custom history service. | 186 // Build up a custom history service. |
182 factories.push_back(std::make_pair(HistoryServiceFactory::GetInstance(), | 187 factories.push_back(std::make_pair(HistoryServiceFactory::GetInstance(), |
183 &BuildHistoryService)); | 188 &BuildHistoryService)); |
184 // Suppress WebHistoryService since it makes network requests. | 189 // Suppress WebHistoryService since it makes network requests. |
185 factories.push_back(std::make_pair( | 190 factories.push_back(std::make_pair( |
186 WebHistoryServiceFactory::GetInstance(), | 191 WebHistoryServiceFactory::GetInstance(), |
187 static_cast<BrowserContextKeyedServiceFactory::TestingFactoryFunction>( | 192 static_cast<BrowserContextKeyedServiceFactory::TestingFactoryFunction>( |
188 NULL))); | 193 NULL))); |
189 | 194 |
190 // Create prefs for the profile with safe browsing enabled or not. | 195 // Create prefs for the profile with safe browsing enabled or not. |
191 scoped_ptr<syncable_prefs::TestingPrefServiceSyncable> prefs( | 196 std::unique_ptr<syncable_prefs::TestingPrefServiceSyncable> prefs( |
192 new syncable_prefs::TestingPrefServiceSyncable); | 197 new syncable_prefs::TestingPrefServiceSyncable); |
193 chrome::RegisterUserProfilePrefs(prefs->registry()); | 198 chrome::RegisterUserProfilePrefs(prefs->registry()); |
194 prefs->SetBoolean(prefs::kSafeBrowsingEnabled, | 199 prefs->SetBoolean(prefs::kSafeBrowsingEnabled, |
195 safe_browsing_opt_in != SAFE_BROWSING_OPT_OUT); | 200 safe_browsing_opt_in != SAFE_BROWSING_OPT_OUT); |
196 prefs->SetBoolean(prefs::kSafeBrowsingExtendedReportingEnabled, | 201 prefs->SetBoolean(prefs::kSafeBrowsingExtendedReportingEnabled, |
197 safe_browsing_opt_in == EXTENDED_REPORTING_OPT_IN); | 202 safe_browsing_opt_in == EXTENDED_REPORTING_OPT_IN); |
198 | 203 |
199 TestingProfile* profile = profile_manager_->CreateTestingProfile( | 204 TestingProfile* profile = profile_manager_->CreateTestingProfile( |
200 profile_name, std::move(prefs), | 205 profile_name, std::move(prefs), |
201 base::UTF8ToUTF16(profile_name), // user_name | 206 base::UTF8ToUTF16(profile_name), // user_name |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 HistoryServiceFactory::GetForProfile(profile, | 244 HistoryServiceFactory::GetForProfile(profile, |
240 ServiceAccessType::EXPLICIT_ACCESS) | 245 ServiceAccessType::EXPLICIT_ACCESS) |
241 ->FlushForTest(run_loop.QuitClosure()); | 246 ->FlushForTest(run_loop.QuitClosure()); |
242 run_loop.Run(); | 247 run_loop.Run(); |
243 // Then make sure anything bounced back to the main thread has been handled. | 248 // Then make sure anything bounced back to the main thread has been handled. |
244 base::RunLoop().RunUntilIdle(); | 249 base::RunLoop().RunUntilIdle(); |
245 } | 250 } |
246 | 251 |
247 // Runs the last download finder on all loaded profiles. | 252 // Runs the last download finder on all loaded profiles. |
248 void RunLastDownloadFinder( | 253 void RunLastDownloadFinder( |
249 scoped_ptr<ClientIncidentReport_DownloadDetails>* last_binary_download, | 254 std::unique_ptr<ClientIncidentReport_DownloadDetails>* |
250 scoped_ptr<ClientIncidentReport_NonBinaryDownloadDetails>* | 255 last_binary_download, |
| 256 std::unique_ptr<ClientIncidentReport_NonBinaryDownloadDetails>* |
251 last_non_binary_download) { | 257 last_non_binary_download) { |
252 base::RunLoop run_loop; | 258 base::RunLoop run_loop; |
253 | 259 |
254 scoped_ptr<LastDownloadFinder> finder(LastDownloadFinder::Create( | 260 std::unique_ptr<LastDownloadFinder> finder(LastDownloadFinder::Create( |
255 GetDownloadDetailsGetter(), | 261 GetDownloadDetailsGetter(), |
256 base::Bind(&LastDownloadFinderTest::OnLastDownload, | 262 base::Bind(&LastDownloadFinderTest::OnLastDownload, |
257 base::Unretained(this), last_binary_download, | 263 base::Unretained(this), last_binary_download, |
258 last_non_binary_download, run_loop.QuitClosure()))); | 264 last_non_binary_download, run_loop.QuitClosure()))); |
259 | 265 |
260 if (finder) | 266 if (finder) |
261 run_loop.Run(); | 267 run_loop.Run(); |
262 } | 268 } |
263 | 269 |
264 history::DownloadRow CreateTestDownloadRow( | 270 history::DownloadRow CreateTestDownloadRow( |
(...skipping 19 matching lines...) Expand all Loading... |
284 content::DOWNLOAD_INTERRUPT_REASON_NONE), // interrupt_reason, | 290 content::DOWNLOAD_INTERRUPT_REASON_NONE), // interrupt_reason, |
285 std::string(), // hash | 291 std::string(), // hash |
286 download_id_++, // id | 292 download_id_++, // id |
287 base::GenerateGUID(), | 293 base::GenerateGUID(), |
288 false, // download_opened | 294 false, // download_opened |
289 std::string(), // ext_id | 295 std::string(), // ext_id |
290 std::string()); // ext_name | 296 std::string()); // ext_name |
291 } | 297 } |
292 | 298 |
293 content::TestBrowserThreadBundle browser_thread_bundle_; | 299 content::TestBrowserThreadBundle browser_thread_bundle_; |
294 scoped_ptr<TestingProfileManager> profile_manager_; | 300 std::unique_ptr<TestingProfileManager> profile_manager_; |
295 | 301 |
296 private: | 302 private: |
297 // A HistoryService::DownloadCreateCallback that asserts that the download was | 303 // A HistoryService::DownloadCreateCallback that asserts that the download was |
298 // created and runs |closure|. | 304 // created and runs |closure|. |
299 void ContinueOnDownloadCreated(const base::Closure& closure, bool created) { | 305 void ContinueOnDownloadCreated(const base::Closure& closure, bool created) { |
300 ASSERT_TRUE(created); | 306 ASSERT_TRUE(created); |
301 closure.Run(); | 307 closure.Run(); |
302 } | 308 } |
303 | 309 |
304 // A HistoryService::DownloadCreateCallback that asserts that the download was | 310 // A HistoryService::DownloadCreateCallback that asserts that the download was |
305 // created. | 311 // created. |
306 void OnDownloadCreated(bool created) { ASSERT_TRUE(created); } | 312 void OnDownloadCreated(bool created) { ASSERT_TRUE(created); } |
307 | 313 |
308 void GetDownloadDetails( | 314 void GetDownloadDetails( |
309 content::BrowserContext* context, | 315 content::BrowserContext* context, |
310 const DownloadMetadataManager::GetDownloadDetailsCallback& callback) { | 316 const DownloadMetadataManager::GetDownloadDetailsCallback& callback) { |
311 callback.Run(scoped_ptr<ClientIncidentReport_DownloadDetails>()); | 317 callback.Run(std::unique_ptr<ClientIncidentReport_DownloadDetails>()); |
312 } | 318 } |
313 | 319 |
314 int profile_number_; | 320 int profile_number_; |
315 | 321 |
316 // Incremented on every download addition to avoid downloads with the same id. | 322 // Incremented on every download addition to avoid downloads with the same id. |
317 int download_id_; | 323 int download_id_; |
318 }; | 324 }; |
319 | 325 |
320 // Tests that nothing happens if there are no profiles at all. | 326 // Tests that nothing happens if there are no profiles at all. |
321 TEST_F(LastDownloadFinderTest, NoProfiles) { | 327 TEST_F(LastDownloadFinderTest, NoProfiles) { |
322 scoped_ptr<ClientIncidentReport_DownloadDetails> last_binary_download; | 328 std::unique_ptr<ClientIncidentReport_DownloadDetails> last_binary_download; |
323 scoped_ptr<ClientIncidentReport_NonBinaryDownloadDetails> | 329 std::unique_ptr<ClientIncidentReport_NonBinaryDownloadDetails> |
324 last_non_binary_download; | 330 last_non_binary_download; |
325 RunLastDownloadFinder(&last_binary_download, &last_non_binary_download); | 331 RunLastDownloadFinder(&last_binary_download, &last_non_binary_download); |
326 EXPECT_FALSE(last_binary_download); | 332 EXPECT_FALSE(last_binary_download); |
327 EXPECT_FALSE(last_non_binary_download); | 333 EXPECT_FALSE(last_non_binary_download); |
328 } | 334 } |
329 | 335 |
330 // Tests that nothing happens other than the callback being invoked if there are | 336 // Tests that nothing happens other than the callback being invoked if there are |
331 // no profiles participating in safe browsing. | 337 // no profiles participating in safe browsing. |
332 TEST_F(LastDownloadFinderTest, NoParticipatingProfiles) { | 338 TEST_F(LastDownloadFinderTest, NoParticipatingProfiles) { |
333 // Create a profile with a history service that is opted-out | 339 // Create a profile with a history service that is opted-out |
334 TestingProfile* profile = CreateProfile(SAFE_BROWSING_OPT_OUT); | 340 TestingProfile* profile = CreateProfile(SAFE_BROWSING_OPT_OUT); |
335 | 341 |
336 // Add a download. | 342 // Add a download. |
337 AddDownload(profile, CreateTestDownloadRow(kBinaryFileName)); | 343 AddDownload(profile, CreateTestDownloadRow(kBinaryFileName)); |
338 | 344 |
339 scoped_ptr<ClientIncidentReport_DownloadDetails> last_binary_download; | 345 std::unique_ptr<ClientIncidentReport_DownloadDetails> last_binary_download; |
340 scoped_ptr<ClientIncidentReport_NonBinaryDownloadDetails> | 346 std::unique_ptr<ClientIncidentReport_NonBinaryDownloadDetails> |
341 last_non_binary_download; | 347 last_non_binary_download; |
342 RunLastDownloadFinder(&last_binary_download, &last_non_binary_download); | 348 RunLastDownloadFinder(&last_binary_download, &last_non_binary_download); |
343 EXPECT_FALSE(last_binary_download); | 349 EXPECT_FALSE(last_binary_download); |
344 EXPECT_FALSE(last_non_binary_download); | 350 EXPECT_FALSE(last_non_binary_download); |
345 } | 351 } |
346 | 352 |
347 // Tests that a download is found from a single profile. | 353 // Tests that a download is found from a single profile. |
348 TEST_F(LastDownloadFinderTest, SimpleEndToEnd) { | 354 TEST_F(LastDownloadFinderTest, SimpleEndToEnd) { |
349 // Create a profile with a history service that is opted-in. | 355 // Create a profile with a history service that is opted-in. |
350 TestingProfile* profile = CreateProfile(EXTENDED_REPORTING_OPT_IN); | 356 TestingProfile* profile = CreateProfile(EXTENDED_REPORTING_OPT_IN); |
351 | 357 |
352 // Add a binary and non-binary download. | 358 // Add a binary and non-binary download. |
353 AddDownload(profile, CreateTestDownloadRow(kBinaryFileName)); | 359 AddDownload(profile, CreateTestDownloadRow(kBinaryFileName)); |
354 AddDownload(profile, CreateTestDownloadRow(kPDFFileName)); | 360 AddDownload(profile, CreateTestDownloadRow(kPDFFileName)); |
355 | 361 |
356 scoped_ptr<ClientIncidentReport_DownloadDetails> last_binary_download; | 362 std::unique_ptr<ClientIncidentReport_DownloadDetails> last_binary_download; |
357 scoped_ptr<ClientIncidentReport_NonBinaryDownloadDetails> | 363 std::unique_ptr<ClientIncidentReport_NonBinaryDownloadDetails> |
358 last_non_binary_download; | 364 last_non_binary_download; |
359 RunLastDownloadFinder(&last_binary_download, &last_non_binary_download); | 365 RunLastDownloadFinder(&last_binary_download, &last_non_binary_download); |
360 EXPECT_TRUE(last_binary_download); | 366 EXPECT_TRUE(last_binary_download); |
361 EXPECT_TRUE(last_non_binary_download); | 367 EXPECT_TRUE(last_non_binary_download); |
362 } | 368 } |
363 | 369 |
364 // Tests that a non-binary download is found | 370 // Tests that a non-binary download is found |
365 TEST_F(LastDownloadFinderTest, NonBinaryOnly) { | 371 TEST_F(LastDownloadFinderTest, NonBinaryOnly) { |
366 // Create a profile with a history service that is opted-in. | 372 // Create a profile with a history service that is opted-in. |
367 TestingProfile* profile = CreateProfile(EXTENDED_REPORTING_OPT_IN); | 373 TestingProfile* profile = CreateProfile(EXTENDED_REPORTING_OPT_IN); |
368 | 374 |
369 // Add a non-binary download. | 375 // Add a non-binary download. |
370 AddDownload(profile, CreateTestDownloadRow(kPDFFileName)); | 376 AddDownload(profile, CreateTestDownloadRow(kPDFFileName)); |
371 | 377 |
372 scoped_ptr<ClientIncidentReport_DownloadDetails> last_binary_download; | 378 std::unique_ptr<ClientIncidentReport_DownloadDetails> last_binary_download; |
373 scoped_ptr<ClientIncidentReport_NonBinaryDownloadDetails> | 379 std::unique_ptr<ClientIncidentReport_NonBinaryDownloadDetails> |
374 last_non_binary_download; | 380 last_non_binary_download; |
375 RunLastDownloadFinder(&last_binary_download, &last_non_binary_download); | 381 RunLastDownloadFinder(&last_binary_download, &last_non_binary_download); |
376 EXPECT_FALSE(last_binary_download); | 382 EXPECT_FALSE(last_binary_download); |
377 EXPECT_TRUE(last_non_binary_download); | 383 EXPECT_TRUE(last_non_binary_download); |
378 } | 384 } |
379 | 385 |
380 // Tests that a download is found from a single profile when the field trial is | 386 // Tests that a download is found from a single profile when the field trial is |
381 // enabled. | 387 // enabled. |
382 TEST_F(LastDownloadFinderTest, SimpleEndToEndFieldTrial) { | 388 TEST_F(LastDownloadFinderTest, SimpleEndToEndFieldTrial) { |
383 // Set up a field trial | 389 // Set up a field trial |
384 base::FieldTrialList field_trial_list(new base::MockEntropyProvider()); | 390 base::FieldTrialList field_trial_list(new base::MockEntropyProvider()); |
385 base::FieldTrialList::CreateFieldTrial("SafeBrowsingIncidentReportingService", | 391 base::FieldTrialList::CreateFieldTrial("SafeBrowsingIncidentReportingService", |
386 "Enabled"); | 392 "Enabled"); |
387 // Create a profile with a history service that is opted-in to Safe Browsing | 393 // Create a profile with a history service that is opted-in to Safe Browsing |
388 // only. | 394 // only. |
389 TestingProfile* profile = CreateProfile(SAFE_BROWSING_OPT_IN); | 395 TestingProfile* profile = CreateProfile(SAFE_BROWSING_OPT_IN); |
390 | 396 |
391 // Add a download. | 397 // Add a download. |
392 AddDownload(profile, CreateTestDownloadRow(kBinaryFileName)); | 398 AddDownload(profile, CreateTestDownloadRow(kBinaryFileName)); |
393 | 399 |
394 scoped_ptr<ClientIncidentReport_DownloadDetails> last_binary_download; | 400 std::unique_ptr<ClientIncidentReport_DownloadDetails> last_binary_download; |
395 scoped_ptr<ClientIncidentReport_NonBinaryDownloadDetails> | 401 std::unique_ptr<ClientIncidentReport_NonBinaryDownloadDetails> |
396 last_non_binary_download; | 402 last_non_binary_download; |
397 RunLastDownloadFinder(&last_binary_download, &last_non_binary_download); | 403 RunLastDownloadFinder(&last_binary_download, &last_non_binary_download); |
398 EXPECT_FALSE(last_non_binary_download); | 404 EXPECT_FALSE(last_non_binary_download); |
399 EXPECT_TRUE(last_binary_download); | 405 EXPECT_TRUE(last_binary_download); |
400 } | 406 } |
401 | 407 |
402 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_ANDROID) | 408 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_ANDROID) |
403 // Tests that nothing happens if the binary is an executable for a different OS. | 409 // Tests that nothing happens if the binary is an executable for a different OS. |
404 TEST_F(LastDownloadFinderTest, DownloadForDifferentOs) { | 410 TEST_F(LastDownloadFinderTest, DownloadForDifferentOs) { |
405 // Create a profile with a history service that is opted-in. | 411 // Create a profile with a history service that is opted-in. |
406 TestingProfile* profile = CreateProfile(EXTENDED_REPORTING_OPT_IN); | 412 TestingProfile* profile = CreateProfile(EXTENDED_REPORTING_OPT_IN); |
407 | 413 |
408 // Add a download. | 414 // Add a download. |
409 AddDownload(profile, CreateTestDownloadRow(kBinaryFileNameForOtherOS)); | 415 AddDownload(profile, CreateTestDownloadRow(kBinaryFileNameForOtherOS)); |
410 | 416 |
411 scoped_ptr<ClientIncidentReport_DownloadDetails> last_binary_download; | 417 std::unique_ptr<ClientIncidentReport_DownloadDetails> last_binary_download; |
412 scoped_ptr<ClientIncidentReport_NonBinaryDownloadDetails> | 418 std::unique_ptr<ClientIncidentReport_NonBinaryDownloadDetails> |
413 last_non_binary_download; | 419 last_non_binary_download; |
414 RunLastDownloadFinder(&last_binary_download, &last_non_binary_download); | 420 RunLastDownloadFinder(&last_binary_download, &last_non_binary_download); |
415 EXPECT_FALSE(last_binary_download); | 421 EXPECT_FALSE(last_binary_download); |
416 EXPECT_FALSE(last_non_binary_download); | 422 EXPECT_FALSE(last_non_binary_download); |
417 } | 423 } |
418 #endif | 424 #endif |
419 | 425 |
420 // Tests that there is no crash if the finder is deleted before results arrive. | 426 // Tests that there is no crash if the finder is deleted before results arrive. |
421 TEST_F(LastDownloadFinderTest, DeleteBeforeResults) { | 427 TEST_F(LastDownloadFinderTest, DeleteBeforeResults) { |
422 // Create a profile with a history service that is opted-in. | 428 // Create a profile with a history service that is opted-in. |
423 TestingProfile* profile = CreateProfile(EXTENDED_REPORTING_OPT_IN); | 429 TestingProfile* profile = CreateProfile(EXTENDED_REPORTING_OPT_IN); |
424 | 430 |
425 // Add a download. | 431 // Add a download. |
426 AddDownload(profile, CreateTestDownloadRow(kBinaryFileName)); | 432 AddDownload(profile, CreateTestDownloadRow(kBinaryFileName)); |
427 | 433 |
428 // Start a finder and kill it before the search completes. | 434 // Start a finder and kill it before the search completes. |
429 LastDownloadFinder::Create(GetDownloadDetailsGetter(), | 435 LastDownloadFinder::Create(GetDownloadDetailsGetter(), |
430 base::Bind(&LastDownloadFinderTest::NeverCalled, | 436 base::Bind(&LastDownloadFinderTest::NeverCalled, |
431 base::Unretained(this))).reset(); | 437 base::Unretained(this))).reset(); |
432 | 438 |
433 // Flush tasks on the history backend thread. | 439 // Flush tasks on the history backend thread. |
434 FlushHistoryBackend(profile); | 440 FlushHistoryBackend(profile); |
435 } | 441 } |
436 | 442 |
437 // Tests that a download in profile added after the search is begun is found. | 443 // Tests that a download in profile added after the search is begun is found. |
438 TEST_F(LastDownloadFinderTest, AddProfileAfterStarting) { | 444 TEST_F(LastDownloadFinderTest, AddProfileAfterStarting) { |
439 // Create a profile with a history service that is opted-in. | 445 // Create a profile with a history service that is opted-in. |
440 CreateProfile(EXTENDED_REPORTING_OPT_IN); | 446 CreateProfile(EXTENDED_REPORTING_OPT_IN); |
441 | 447 |
442 scoped_ptr<ClientIncidentReport_DownloadDetails> last_binary_download; | 448 std::unique_ptr<ClientIncidentReport_DownloadDetails> last_binary_download; |
443 scoped_ptr<ClientIncidentReport_NonBinaryDownloadDetails> | 449 std::unique_ptr<ClientIncidentReport_NonBinaryDownloadDetails> |
444 last_non_binary_download; | 450 last_non_binary_download; |
445 base::RunLoop run_loop; | 451 base::RunLoop run_loop; |
446 | 452 |
447 // Post a task that will create a second profile once the main loop is run. | 453 // Post a task that will create a second profile once the main loop is run. |
448 base::ThreadTaskRunnerHandle::Get()->PostTask( | 454 base::ThreadTaskRunnerHandle::Get()->PostTask( |
449 FROM_HERE, base::Bind(&LastDownloadFinderTest::CreateProfileWithDownload, | 455 FROM_HERE, base::Bind(&LastDownloadFinderTest::CreateProfileWithDownload, |
450 base::Unretained(this))); | 456 base::Unretained(this))); |
451 | 457 |
452 // Create a finder that we expect will find a download in the second profile. | 458 // Create a finder that we expect will find a download in the second profile. |
453 scoped_ptr<LastDownloadFinder> finder(LastDownloadFinder::Create( | 459 std::unique_ptr<LastDownloadFinder> finder(LastDownloadFinder::Create( |
454 GetDownloadDetailsGetter(), | 460 GetDownloadDetailsGetter(), |
455 base::Bind(&LastDownloadFinderTest::OnLastDownload, | 461 base::Bind(&LastDownloadFinderTest::OnLastDownload, |
456 base::Unretained(this), &last_binary_download, | 462 base::Unretained(this), &last_binary_download, |
457 &last_non_binary_download, run_loop.QuitClosure()))); | 463 &last_non_binary_download, run_loop.QuitClosure()))); |
458 | 464 |
459 run_loop.Run(); | 465 run_loop.Run(); |
460 | 466 |
461 ASSERT_TRUE(last_binary_download); | 467 ASSERT_TRUE(last_binary_download); |
462 } | 468 } |
463 | 469 |
464 } // namespace safe_browsing | 470 } // namespace safe_browsing |
OLD | NEW |