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 <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 if (history_service->Init(profile->GetPath())) | 54 if (history_service->Init(profile->GetPath())) |
55 return history_service; | 55 return history_service; |
56 | 56 |
57 ADD_FAILURE() << "failed to initialize history service"; | 57 ADD_FAILURE() << "failed to initialize history service"; |
58 delete history_service; | 58 delete history_service; |
59 return NULL; | 59 return NULL; |
60 } | 60 } |
61 | 61 |
62 } // namespace | 62 } // namespace |
63 | 63 |
| 64 namespace safe_browsing { |
| 65 |
64 class LastDownloadFinderTest : public testing::Test { | 66 class LastDownloadFinderTest : public testing::Test { |
65 public: | 67 public: |
66 void NeverCalled(scoped_ptr< | 68 void NeverCalled(scoped_ptr<ClientIncidentReport_DownloadDetails> download) { |
67 safe_browsing::ClientIncidentReport_DownloadDetails> download) { | |
68 FAIL(); | 69 FAIL(); |
69 } | 70 } |
70 | 71 |
71 // Creates a new profile that participates in safe browsing and adds a | 72 // Creates a new profile that participates in safe browsing and adds a |
72 // download to its history. | 73 // download to its history. |
73 void CreateProfileWithDownload() { | 74 void CreateProfileWithDownload() { |
74 TestingProfile* profile = CreateProfile(SAFE_BROWSING_OPT_IN); | 75 TestingProfile* profile = CreateProfile(SAFE_BROWSING_OPT_IN); |
75 HistoryService* history_service = | 76 HistoryService* history_service = |
76 HistoryServiceFactory::GetForProfile(profile, Profile::EXPLICIT_ACCESS); | 77 HistoryServiceFactory::GetForProfile(profile, Profile::EXPLICIT_ACCESS); |
77 history_service->CreateDownload( | 78 history_service->CreateDownload( |
78 CreateTestDownloadRow(), | 79 CreateTestDownloadRow(), |
79 base::Bind(&LastDownloadFinderTest::OnDownloadCreated, | 80 base::Bind(&LastDownloadFinderTest::OnDownloadCreated, |
80 base::Unretained(this))); | 81 base::Unretained(this))); |
81 } | 82 } |
82 | 83 |
83 // safe_browsing::LastDownloadFinder::LastDownloadCallback implementation that | 84 // LastDownloadFinder::LastDownloadCallback implementation that |
84 // passes the found download to |result| and then runs a closure. | 85 // passes the found download to |result| and then runs a closure. |
85 void OnLastDownload( | 86 void OnLastDownload( |
86 scoped_ptr<safe_browsing::ClientIncidentReport_DownloadDetails>* result, | 87 scoped_ptr<ClientIncidentReport_DownloadDetails>* result, |
87 const base::Closure& quit_closure, | 88 const base::Closure& quit_closure, |
88 scoped_ptr<safe_browsing::ClientIncidentReport_DownloadDetails> | 89 scoped_ptr<ClientIncidentReport_DownloadDetails> download) { |
89 download) { | |
90 *result = download.Pass(); | 90 *result = download.Pass(); |
91 quit_closure.Run(); | 91 quit_closure.Run(); |
92 } | 92 } |
93 | 93 |
94 protected: | 94 protected: |
95 // A type for specifying whether or not a profile created by CreateProfile | 95 // A type for specifying whether or not a profile created by CreateProfile |
96 // participates in safe browsing. | 96 // participates in safe browsing. |
97 enum SafeBrowsingDisposition { | 97 enum SafeBrowsingDisposition { |
98 SAFE_BROWSING_OPT_OUT, | 98 SAFE_BROWSING_OPT_OUT, |
99 SAFE_BROWSING_OPT_IN, | 99 SAFE_BROWSING_OPT_IN, |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 profile_name, | 146 profile_name, |
147 prefs.Pass(), | 147 prefs.Pass(), |
148 base::UTF8ToUTF16(profile_name), // user_name | 148 base::UTF8ToUTF16(profile_name), // user_name |
149 0, // avatar_id | 149 0, // avatar_id |
150 std::string(), // supervised_user_id | 150 std::string(), // supervised_user_id |
151 factories); | 151 factories); |
152 | 152 |
153 return profile; | 153 return profile; |
154 } | 154 } |
155 | 155 |
| 156 LastDownloadFinder::DownloadDetailsGetter GetDownloadDetailsGetter() { |
| 157 return base::Bind(&LastDownloadFinderTest::GetDownloadDetails, |
| 158 base::Unretained(this)); |
| 159 } |
| 160 |
156 void AddDownload(Profile* profile, const history::DownloadRow& download) { | 161 void AddDownload(Profile* profile, const history::DownloadRow& download) { |
157 base::RunLoop run_loop; | 162 base::RunLoop run_loop; |
158 | 163 |
159 HistoryService* history_service = | 164 HistoryService* history_service = |
160 HistoryServiceFactory::GetForProfile(profile, Profile::EXPLICIT_ACCESS); | 165 HistoryServiceFactory::GetForProfile(profile, Profile::EXPLICIT_ACCESS); |
161 history_service->CreateDownload( | 166 history_service->CreateDownload( |
162 download, | 167 download, |
163 base::Bind(&LastDownloadFinderTest::ContinueOnDownloadCreated, | 168 base::Bind(&LastDownloadFinderTest::ContinueOnDownloadCreated, |
164 base::Unretained(this), | 169 base::Unretained(this), |
165 run_loop.QuitClosure())); | 170 run_loop.QuitClosure())); |
(...skipping 13 matching lines...) Expand all Loading... |
179 base::RunLoop run_loop; | 184 base::RunLoop run_loop; |
180 HistoryServiceFactory::GetForProfile(profile, Profile::EXPLICIT_ACCESS) | 185 HistoryServiceFactory::GetForProfile(profile, Profile::EXPLICIT_ACCESS) |
181 ->FlushForTest(run_loop.QuitClosure()); | 186 ->FlushForTest(run_loop.QuitClosure()); |
182 run_loop.Run(); | 187 run_loop.Run(); |
183 // Then make sure anything bounced back to the main thread has been handled. | 188 // Then make sure anything bounced back to the main thread has been handled. |
184 base::RunLoop().RunUntilIdle(); | 189 base::RunLoop().RunUntilIdle(); |
185 } | 190 } |
186 | 191 |
187 // Runs the last download finder on all loaded profiles, returning the found | 192 // Runs the last download finder on all loaded profiles, returning the found |
188 // download or an empty pointer if none was found. | 193 // download or an empty pointer if none was found. |
189 scoped_ptr<safe_browsing::ClientIncidentReport_DownloadDetails> | 194 scoped_ptr<ClientIncidentReport_DownloadDetails> RunLastDownloadFinder() { |
190 RunLastDownloadFinder() { | |
191 base::RunLoop run_loop; | 195 base::RunLoop run_loop; |
192 | 196 |
193 scoped_ptr<safe_browsing::ClientIncidentReport_DownloadDetails> | 197 scoped_ptr<ClientIncidentReport_DownloadDetails> last_download; |
194 last_download; | |
195 | 198 |
196 scoped_ptr<safe_browsing::LastDownloadFinder> finder( | 199 scoped_ptr<LastDownloadFinder> finder(LastDownloadFinder::Create( |
197 safe_browsing::LastDownloadFinder::Create( | 200 GetDownloadDetailsGetter(), |
198 base::Bind(&LastDownloadFinderTest::OnLastDownload, | 201 base::Bind(&LastDownloadFinderTest::OnLastDownload, |
199 base::Unretained(this), | 202 base::Unretained(this), |
200 &last_download, | 203 &last_download, |
201 run_loop.QuitClosure()))); | 204 run_loop.QuitClosure()))); |
202 | 205 |
203 if (finder) | 206 if (finder) |
204 run_loop.Run(); | 207 run_loop.Run(); |
205 | 208 |
206 return last_download.Pass(); | 209 return last_download.Pass(); |
207 } | 210 } |
208 | 211 |
209 history::DownloadRow CreateTestDownloadRow() { | 212 history::DownloadRow CreateTestDownloadRow() { |
210 base::Time now(base::Time::Now()); | 213 base::Time now(base::Time::Now()); |
211 return history::DownloadRow( | 214 return history::DownloadRow( |
(...skipping 11 matching lines...) Expand all Loading... |
223 47LL, // total | 226 47LL, // total |
224 content::DownloadItem::COMPLETE, // download_state | 227 content::DownloadItem::COMPLETE, // download_state |
225 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, // danger_type | 228 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, // danger_type |
226 content::DOWNLOAD_INTERRUPT_REASON_NONE, // interrupt_reason, | 229 content::DOWNLOAD_INTERRUPT_REASON_NONE, // interrupt_reason, |
227 1, // id | 230 1, // id |
228 false, // download_opened | 231 false, // download_opened |
229 std::string(), // ext_id | 232 std::string(), // ext_id |
230 std::string()); // ext_name | 233 std::string()); // ext_name |
231 } | 234 } |
232 | 235 |
233 void ExpectNoDownloadFound(scoped_ptr< | 236 void ExpectNoDownloadFound( |
234 safe_browsing::ClientIncidentReport_DownloadDetails> download) { | 237 scoped_ptr<ClientIncidentReport_DownloadDetails> download) { |
235 EXPECT_FALSE(download); | 238 EXPECT_FALSE(download); |
236 } | 239 } |
237 | 240 |
238 void ExpectFoundTestDownload(scoped_ptr< | 241 void ExpectFoundTestDownload( |
239 safe_browsing::ClientIncidentReport_DownloadDetails> download) { | 242 scoped_ptr<ClientIncidentReport_DownloadDetails> download) { |
240 ASSERT_TRUE(download); | 243 ASSERT_TRUE(download); |
241 } | 244 } |
242 | 245 |
243 content::TestBrowserThreadBundle browser_thread_bundle_; | 246 content::TestBrowserThreadBundle browser_thread_bundle_; |
244 scoped_ptr<TestingProfileManager> profile_manager_; | 247 scoped_ptr<TestingProfileManager> profile_manager_; |
245 | 248 |
246 private: | 249 private: |
247 // A HistoryService::DownloadCreateCallback that asserts that the download was | 250 // A HistoryService::DownloadCreateCallback that asserts that the download was |
248 // created and runs |closure|. | 251 // created and runs |closure|. |
249 void ContinueOnDownloadCreated(const base::Closure& closure, bool created) { | 252 void ContinueOnDownloadCreated(const base::Closure& closure, bool created) { |
250 ASSERT_TRUE(created); | 253 ASSERT_TRUE(created); |
251 closure.Run(); | 254 closure.Run(); |
252 } | 255 } |
253 | 256 |
254 // A HistoryService::DownloadCreateCallback that asserts that the download was | 257 // A HistoryService::DownloadCreateCallback that asserts that the download was |
255 // created. | 258 // created. |
256 void OnDownloadCreated(bool created) { ASSERT_TRUE(created); } | 259 void OnDownloadCreated(bool created) { ASSERT_TRUE(created); } |
257 | 260 |
| 261 void GetDownloadDetails( |
| 262 content::BrowserContext* context, |
| 263 const DownloadMetadataManager::GetDownloadDetailsCallback& callback) { |
| 264 callback.Run(scoped_ptr<ClientIncidentReport_DownloadDetails>()); |
| 265 } |
| 266 |
258 int profile_number_; | 267 int profile_number_; |
259 }; | 268 }; |
260 | 269 |
261 // Tests that nothing happens if there are no profiles at all. | 270 // Tests that nothing happens if there are no profiles at all. |
262 TEST_F(LastDownloadFinderTest, NoProfiles) { | 271 TEST_F(LastDownloadFinderTest, NoProfiles) { |
263 ExpectNoDownloadFound(RunLastDownloadFinder()); | 272 ExpectNoDownloadFound(RunLastDownloadFinder()); |
264 } | 273 } |
265 | 274 |
266 // Tests that nothing happens other than the callback being invoked if there are | 275 // Tests that nothing happens other than the callback being invoked if there are |
267 // no profiles participating in safe browsing. | 276 // no profiles participating in safe browsing. |
(...skipping 20 matching lines...) Expand all Loading... |
288 | 297 |
289 // Tests that there is no crash if the finder is deleted before results arrive. | 298 // Tests that there is no crash if the finder is deleted before results arrive. |
290 TEST_F(LastDownloadFinderTest, DeleteBeforeResults) { | 299 TEST_F(LastDownloadFinderTest, DeleteBeforeResults) { |
291 // Create a profile with a history service that is opted-in. | 300 // Create a profile with a history service that is opted-in. |
292 TestingProfile* profile = CreateProfile(SAFE_BROWSING_OPT_IN); | 301 TestingProfile* profile = CreateProfile(SAFE_BROWSING_OPT_IN); |
293 | 302 |
294 // Add a download. | 303 // Add a download. |
295 AddDownload(profile, CreateTestDownloadRow()); | 304 AddDownload(profile, CreateTestDownloadRow()); |
296 | 305 |
297 // Start a finder and kill it before the search completes. | 306 // Start a finder and kill it before the search completes. |
298 safe_browsing::LastDownloadFinder::Create( | 307 LastDownloadFinder::Create(GetDownloadDetailsGetter(), |
299 base::Bind(&LastDownloadFinderTest::NeverCalled, base::Unretained(this))) | 308 base::Bind(&LastDownloadFinderTest::NeverCalled, |
300 .reset(); | 309 base::Unretained(this))).reset(); |
301 | 310 |
302 // Flush tasks on the history backend thread. | 311 // Flush tasks on the history backend thread. |
303 FlushHistoryBackend(profile); | 312 FlushHistoryBackend(profile); |
304 } | 313 } |
305 | 314 |
306 // Tests that a download in profile added after the search is begun is found. | 315 // Tests that a download in profile added after the search is begun is found. |
307 TEST_F(LastDownloadFinderTest, AddProfileAfterStarting) { | 316 TEST_F(LastDownloadFinderTest, AddProfileAfterStarting) { |
308 // Create a profile with a history service that is opted-in. | 317 // Create a profile with a history service that is opted-in. |
309 CreateProfile(SAFE_BROWSING_OPT_IN); | 318 CreateProfile(SAFE_BROWSING_OPT_IN); |
310 | 319 |
311 scoped_ptr<safe_browsing::ClientIncidentReport_DownloadDetails> last_download; | 320 scoped_ptr<ClientIncidentReport_DownloadDetails> last_download; |
312 base::RunLoop run_loop; | 321 base::RunLoop run_loop; |
313 | 322 |
314 // Post a task that will create a second profile once the main loop is run. | 323 // Post a task that will create a second profile once the main loop is run. |
315 base::MessageLoop::current()->PostTask( | 324 base::MessageLoop::current()->PostTask( |
316 FROM_HERE, | 325 FROM_HERE, |
317 base::Bind(&LastDownloadFinderTest::CreateProfileWithDownload, | 326 base::Bind(&LastDownloadFinderTest::CreateProfileWithDownload, |
318 base::Unretained(this))); | 327 base::Unretained(this))); |
319 | 328 |
320 // Create a finder that we expect will find a download in the second profile. | 329 // Create a finder that we expect will find a download in the second profile. |
321 scoped_ptr<safe_browsing::LastDownloadFinder> finder( | 330 scoped_ptr<LastDownloadFinder> finder(LastDownloadFinder::Create( |
322 safe_browsing::LastDownloadFinder::Create( | 331 GetDownloadDetailsGetter(), |
323 base::Bind(&LastDownloadFinderTest::OnLastDownload, | 332 base::Bind(&LastDownloadFinderTest::OnLastDownload, |
324 base::Unretained(this), | 333 base::Unretained(this), |
325 &last_download, | 334 &last_download, |
326 run_loop.QuitClosure()))); | 335 run_loop.QuitClosure()))); |
327 | 336 |
328 run_loop.Run(); | 337 run_loop.Run(); |
329 | 338 |
330 ExpectFoundTestDownload(last_download.Pass()); | 339 ExpectFoundTestDownload(last_download.Pass()); |
331 } | 340 } |
| 341 |
| 342 } // namespace safe_browsing |
OLD | NEW |