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 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
11 #include <functional> | 11 #include <functional> |
12 #include <utility> | 12 #include <utility> |
13 | 13 |
14 #include "base/bind.h" | 14 #include "base/bind.h" |
15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/memory/ptr_util.h" |
16 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
17 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
18 #include "build/build_config.h" | 19 #include "build/build_config.h" |
19 #include "chrome/browser/browser_process.h" | 20 #include "chrome/browser/browser_process.h" |
20 #include "chrome/browser/chrome_notification_types.h" | 21 #include "chrome/browser/chrome_notification_types.h" |
21 #include "chrome/browser/history/history_service_factory.h" | 22 #include "chrome/browser/history/history_service_factory.h" |
22 #include "chrome/browser/profiles/profile_manager.h" | 23 #include "chrome/browser/profiles/profile_manager.h" |
23 #include "chrome/browser/safe_browsing/incident_reporting/incident_reporting_ser
vice.h" | 24 #include "chrome/browser/safe_browsing/incident_reporting/incident_reporting_ser
vice.h" |
24 #include "chrome/common/pref_names.h" | 25 #include "chrome/common/pref_names.h" |
25 #include "chrome/common/safe_browsing/csd.pb.h" | 26 #include "chrome/common/safe_browsing/csd.pb.h" |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 details->set_url_spec_sha256( | 222 details->set_url_spec_sha256( |
222 crypto::SHA256HashString(download.url_chain.back().spec())); | 223 crypto::SHA256HashString(download.url_chain.back().spec())); |
223 } | 224 } |
224 | 225 |
225 } // namespace | 226 } // namespace |
226 | 227 |
227 LastDownloadFinder::~LastDownloadFinder() { | 228 LastDownloadFinder::~LastDownloadFinder() { |
228 } | 229 } |
229 | 230 |
230 // static | 231 // static |
231 scoped_ptr<LastDownloadFinder> LastDownloadFinder::Create( | 232 std::unique_ptr<LastDownloadFinder> LastDownloadFinder::Create( |
232 const DownloadDetailsGetter& download_details_getter, | 233 const DownloadDetailsGetter& download_details_getter, |
233 const LastDownloadCallback& callback) { | 234 const LastDownloadCallback& callback) { |
234 scoped_ptr<LastDownloadFinder> finder(make_scoped_ptr(new LastDownloadFinder( | 235 std::unique_ptr<LastDownloadFinder> finder( |
235 download_details_getter, | 236 base::WrapUnique(new LastDownloadFinder( |
236 g_browser_process->profile_manager()->GetLoadedProfiles(), | 237 download_details_getter, |
237 callback))); | 238 g_browser_process->profile_manager()->GetLoadedProfiles(), |
| 239 callback))); |
238 // Return NULL if there is no work to do. | 240 // Return NULL if there is no work to do. |
239 if (finder->profile_states_.empty()) | 241 if (finder->profile_states_.empty()) |
240 return scoped_ptr<LastDownloadFinder>(); | 242 return std::unique_ptr<LastDownloadFinder>(); |
241 return finder; | 243 return finder; |
242 } | 244 } |
243 | 245 |
244 LastDownloadFinder::LastDownloadFinder() | 246 LastDownloadFinder::LastDownloadFinder() |
245 : history_service_observer_(this), weak_ptr_factory_(this) { | 247 : history_service_observer_(this), weak_ptr_factory_(this) { |
246 } | 248 } |
247 | 249 |
248 LastDownloadFinder::LastDownloadFinder( | 250 LastDownloadFinder::LastDownloadFinder( |
249 const DownloadDetailsGetter& download_details_getter, | 251 const DownloadDetailsGetter& download_details_getter, |
250 const std::vector<Profile*>& profiles, | 252 const std::vector<Profile*>& profiles, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 // Initiate a metadata search. | 284 // Initiate a metadata search. |
283 profile_states_[profile] = WAITING_FOR_METADATA; | 285 profile_states_[profile] = WAITING_FOR_METADATA; |
284 download_details_getter_.Run(profile, | 286 download_details_getter_.Run(profile, |
285 base::Bind(&LastDownloadFinder::OnMetadataQuery, | 287 base::Bind(&LastDownloadFinder::OnMetadataQuery, |
286 weak_ptr_factory_.GetWeakPtr(), | 288 weak_ptr_factory_.GetWeakPtr(), |
287 profile)); | 289 profile)); |
288 } | 290 } |
289 | 291 |
290 void LastDownloadFinder::OnMetadataQuery( | 292 void LastDownloadFinder::OnMetadataQuery( |
291 Profile* profile, | 293 Profile* profile, |
292 scoped_ptr<ClientIncidentReport_DownloadDetails> details) { | 294 std::unique_ptr<ClientIncidentReport_DownloadDetails> details) { |
293 auto iter = profile_states_.find(profile); | 295 auto iter = profile_states_.find(profile); |
294 // Early-exit if the search for this profile was abandoned. | 296 // Early-exit if the search for this profile was abandoned. |
295 if (iter == profile_states_.end()) | 297 if (iter == profile_states_.end()) |
296 return; | 298 return; |
297 | 299 |
298 if (details) { | 300 if (details) { |
299 if (IsMostInterestingBinary(*details, details_.get(), | 301 if (IsMostInterestingBinary(*details, details_.get(), |
300 most_recent_binary_row_)) { | 302 most_recent_binary_row_)) { |
301 details_ = std::move(details); | 303 details_ = std::move(details); |
302 most_recent_binary_row_.end_time = base::Time(); | 304 most_recent_binary_row_.end_time = base::Time(); |
(...skipping 24 matching lines...) Expand all Loading... |
327 | 329 |
328 void LastDownloadFinder::AbandonSearchInProfile(Profile* profile) { | 330 void LastDownloadFinder::AbandonSearchInProfile(Profile* profile) { |
329 // |profile| may not be present in the set of profiles. | 331 // |profile| may not be present in the set of profiles. |
330 auto iter = profile_states_.find(profile); | 332 auto iter = profile_states_.find(profile); |
331 if (iter != profile_states_.end()) | 333 if (iter != profile_states_.end()) |
332 RemoveProfileAndReportIfDone(iter); | 334 RemoveProfileAndReportIfDone(iter); |
333 } | 335 } |
334 | 336 |
335 void LastDownloadFinder::OnDownloadQuery( | 337 void LastDownloadFinder::OnDownloadQuery( |
336 Profile* profile, | 338 Profile* profile, |
337 scoped_ptr<std::vector<history::DownloadRow> > downloads) { | 339 std::unique_ptr<std::vector<history::DownloadRow>> downloads) { |
338 // Early-exit if the history search for this profile was abandoned. | 340 // Early-exit if the history search for this profile was abandoned. |
339 auto iter = profile_states_.find(profile); | 341 auto iter = profile_states_.find(profile); |
340 if (iter == profile_states_.end()) | 342 if (iter == profile_states_.end()) |
341 return; | 343 return; |
342 | 344 |
343 // Don't overwrite the download from metadata if it came from this profile. | 345 // Don't overwrite the download from metadata if it came from this profile. |
344 if (iter->second == WAITING_FOR_HISTORY) { | 346 if (iter->second == WAITING_FOR_HISTORY) { |
345 // Find the most recent from this profile and use it if it's better than | 347 // Find the most recent from this profile and use it if it's better than |
346 // anything else found so far. | 348 // anything else found so far. |
347 const history::DownloadRow* profile_best_binary = | 349 const history::DownloadRow* profile_best_binary = |
(...skipping 24 matching lines...) Expand all Loading... |
372 | 374 |
373 // Finish processing if all results are in. | 375 // Finish processing if all results are in. |
374 if (profile_states_.empty()) | 376 if (profile_states_.empty()) |
375 ReportResults(); | 377 ReportResults(); |
376 // Do not touch this LastDownloadFinder after reporting results. | 378 // Do not touch this LastDownloadFinder after reporting results. |
377 } | 379 } |
378 | 380 |
379 void LastDownloadFinder::ReportResults() { | 381 void LastDownloadFinder::ReportResults() { |
380 DCHECK(profile_states_.empty()); | 382 DCHECK(profile_states_.empty()); |
381 | 383 |
382 scoped_ptr<ClientIncidentReport_DownloadDetails> binary_details = nullptr; | 384 std::unique_ptr<ClientIncidentReport_DownloadDetails> binary_details = |
383 scoped_ptr<ClientIncidentReport_NonBinaryDownloadDetails> non_binary_details = | |
384 nullptr; | 385 nullptr; |
| 386 std::unique_ptr<ClientIncidentReport_NonBinaryDownloadDetails> |
| 387 non_binary_details = nullptr; |
385 | 388 |
386 if (details_) { | 389 if (details_) { |
387 binary_details.reset(new ClientIncidentReport_DownloadDetails(*details_)); | 390 binary_details.reset(new ClientIncidentReport_DownloadDetails(*details_)); |
388 } else if (!most_recent_binary_row_.end_time.is_null()) { | 391 } else if (!most_recent_binary_row_.end_time.is_null()) { |
389 binary_details.reset(new ClientIncidentReport_DownloadDetails()); | 392 binary_details.reset(new ClientIncidentReport_DownloadDetails()); |
390 PopulateDetailsFromRow(most_recent_binary_row_, binary_details.get()); | 393 PopulateDetailsFromRow(most_recent_binary_row_, binary_details.get()); |
391 } | 394 } |
392 | 395 |
393 if (!most_recent_non_binary_row_.end_time.is_null()) { | 396 if (!most_recent_non_binary_row_.end_time.is_null()) { |
394 non_binary_details.reset( | 397 non_binary_details.reset( |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
436 } | 439 } |
437 } | 440 } |
438 } | 441 } |
439 | 442 |
440 void LastDownloadFinder::HistoryServiceBeingDeleted( | 443 void LastDownloadFinder::HistoryServiceBeingDeleted( |
441 history::HistoryService* history_service) { | 444 history::HistoryService* history_service) { |
442 history_service_observer_.Remove(history_service); | 445 history_service_observer_.Remove(history_service); |
443 } | 446 } |
444 | 447 |
445 } // namespace safe_browsing | 448 } // namespace safe_browsing |
OLD | NEW |