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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_SAFE_BROWSING_INCIDENT_REPORTING_LAST_DOWNLOAD_FINDER_H_
6 #define CHROME_BROWSER_SAFE_BROWSING_INCIDENT_REPORTING_LAST_DOWNLOAD_FINDER_H_
11 #include "base/callback.h"
12 #include "base/compiler_specific.h"
13 #include "base/macros.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "chrome/browser/history/download_row.h"
17 #include "chrome/browser/safe_browsing/incident_reporting/download_metadata_manager.h"
18 #include "content/public/browser/notification_observer.h"
19 #include "content/public/browser/notification_registrar.h"
25 class NotificationDetails;
26 class NotificationSource;
33 namespace safe_browsing {
35 class ClientIncidentReport_DownloadDetails;
37 // Finds the most recent executable downloaded by any on-the-record profile with
38 // history that participates in safe browsing.
39 class LastDownloadFinder : public content::NotificationObserver {
41 typedef base::Callback<void(
42 content::BrowserContext* context,
43 const DownloadMetadataManager::GetDownloadDetailsCallback&)>
44 DownloadDetailsGetter;
46 // The type of a callback run by the finder upon completion. The argument is a
47 // protobuf containing details of the download that was found, or an empty
48 // pointer if none was found.
49 typedef base::Callback<void(scoped_ptr<ClientIncidentReport_DownloadDetails>)>
52 ~LastDownloadFinder() override;
54 // Initiates an asynchronous search for the most recent download. |callback|
55 // will be run when the search is complete. The returned instance can be
56 // deleted to terminate the search, in which case |callback| is not invoked.
57 // Returns NULL without running |callback| if there are no eligible profiles
59 static scoped_ptr<LastDownloadFinder> Create(
60 const DownloadDetailsGetter& download_details_getter,
61 const LastDownloadCallback& callback);
64 // Protected constructor so that unit tests can create a fake finder.
68 enum ProfileWaitState {
73 LastDownloadFinder(const DownloadDetailsGetter& download_details_getter,
74 const std::vector<Profile*>& profiles,
75 const LastDownloadCallback& callback);
77 // Adds |profile| to the set of profiles to be searched if it is an
78 // on-the-record profile with history that participates in safe browsing. A
79 // search for metadata is initiated immediately.
80 void SearchInProfile(Profile* profile);
82 // DownloadMetadataManager::GetDownloadDetailsCallback. If |details| are
83 // provided, retrieves them if they are the most relevant results. Otherwise
84 // begins a search in history. Reports results if there are no more pending
88 scoped_ptr<ClientIncidentReport_DownloadDetails> details);
90 // Initiates a search in |profile| if it is in the set of profiles to be
92 void OnProfileHistoryLoaded(Profile* profile,
93 HistoryService* history_service);
95 // Abandons the search for downloads in |profile|, reporting results if there
96 // are no more pending queries.
97 void AbandonSearchInProfile(Profile* profile);
99 // HistoryService::DownloadQueryCallback. Retrieves the most recent completed
100 // executable download from |downloads| and reports results if there are no
101 // more pending queries.
102 void OnDownloadQuery(
104 scoped_ptr<std::vector<history::DownloadRow> > downloads);
106 // Removes the profile pointed to by |it| from profile_states_ and reports
107 // results if there are no more pending queries.
108 void RemoveProfileAndReportIfDone(
109 std::map<Profile*, ProfileWaitState>::iterator iter);
111 // Invokes the caller-supplied callback with the download found.
112 void ReportResults();
114 // content::NotificationObserver methods.
115 void Observe(int type,
116 const content::NotificationSource& source,
117 const content::NotificationDetails& details) override;
119 // Caller-supplied callback to make an asynchronous request for a profile's
120 // persistent download details.
121 DownloadDetailsGetter download_details_getter_;
123 // Caller-supplied callback to be invoked when the most recent download is
125 LastDownloadCallback callback_;
127 // A mapping of profiles for which a download query is pending to their
128 // respective states.
129 std::map<Profile*, ProfileWaitState> profile_states_;
131 // Registrar for observing profile lifecycle notifications.
132 content::NotificationRegistrar notification_registrar_;
134 // The most interesting download details retrieved from download metadata.
135 scoped_ptr<ClientIncidentReport_DownloadDetails> details_;
137 // The most recent download, updated progressively as query results arrive.
138 history::DownloadRow most_recent_row_;
140 // A factory for asynchronous operations on profiles' HistoryService.
141 base::WeakPtrFactory<LastDownloadFinder> weak_ptr_factory_;
143 DISALLOW_COPY_AND_ASSIGN(LastDownloadFinder);
146 } // namespace safe_browsing
148 #endif // CHROME_BROWSER_SAFE_BROWSING_INCIDENT_REPORTING_LAST_DOWNLOAD_FINDER_H_