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 "base/scoped_observer.h"
17 #include "chrome/browser/safe_browsing/incident_reporting/download_metadata_manager.h"
18 #include "components/history/core/browser/download_row.h"
19 #include "components/history/core/browser/history_service_observer.h"
20 #include "content/public/browser/notification_observer.h"
21 #include "content/public/browser/notification_registrar.h"
27 class NotificationDetails
;
28 class NotificationSource
;
31 namespace safe_browsing
{
33 class ClientIncidentReport_DownloadDetails
;
35 // Finds the most recent executable downloaded by any on-the-record profile with
36 // history that participates in safe browsing.
37 class LastDownloadFinder
: public content::NotificationObserver
,
38 public history::HistoryServiceObserver
{
40 typedef base::Callback
<void(
41 content::BrowserContext
* context
,
42 const DownloadMetadataManager::GetDownloadDetailsCallback
&)>
43 DownloadDetailsGetter
;
45 // The type of a callback run by the finder upon completion. The argument is a
46 // protobuf containing details of the download that was found, or an empty
47 // pointer if none was found.
48 typedef base::Callback
<void(scoped_ptr
<ClientIncidentReport_DownloadDetails
>)>
51 ~LastDownloadFinder() override
;
53 // Initiates an asynchronous search for the most recent download. |callback|
54 // will be run when the search is complete. The returned instance can be
55 // deleted to terminate the search, in which case |callback| is not invoked.
56 // Returns NULL without running |callback| if there are no eligible profiles
58 static scoped_ptr
<LastDownloadFinder
> Create(
59 const DownloadDetailsGetter
& download_details_getter
,
60 const LastDownloadCallback
& callback
);
63 // Protected constructor so that unit tests can create a fake finder.
67 enum ProfileWaitState
{
72 LastDownloadFinder(const DownloadDetailsGetter
& download_details_getter
,
73 const std::vector
<Profile
*>& profiles
,
74 const LastDownloadCallback
& callback
);
76 // Adds |profile| to the set of profiles to be searched if it is an
77 // on-the-record profile with history that participates in safe browsing. A
78 // search for metadata is initiated immediately.
79 void SearchInProfile(Profile
* profile
);
81 // DownloadMetadataManager::GetDownloadDetailsCallback. If |details| are
82 // provided, retrieves them if they are the most relevant results. Otherwise
83 // begins a search in history. Reports results if there are no more pending
87 scoped_ptr
<ClientIncidentReport_DownloadDetails
> details
);
89 // Abandons the search for downloads in |profile|, reporting results if there
90 // are no more pending queries.
91 void AbandonSearchInProfile(Profile
* profile
);
93 // HistoryService::DownloadQueryCallback. Retrieves the most recent completed
94 // executable download from |downloads| and reports results if there are no
95 // more pending queries.
98 scoped_ptr
<std::vector
<history::DownloadRow
> > downloads
);
100 // Removes the profile pointed to by |it| from profile_states_ and reports
101 // results if there are no more pending queries.
102 void RemoveProfileAndReportIfDone(
103 std::map
<Profile
*, ProfileWaitState
>::iterator iter
);
105 // Invokes the caller-supplied callback with the download found.
106 void ReportResults();
108 // content::NotificationObserver methods.
109 void Observe(int type
,
110 const content::NotificationSource
& source
,
111 const content::NotificationDetails
& details
) override
;
113 // history::HistoryServiceObserver:
114 void OnHistoryServiceLoaded(HistoryService
* service
) override
;
115 void HistoryServiceBeingDeleted(HistoryService
* history_service
) override
;
117 // Caller-supplied callback to make an asynchronous request for a profile's
118 // persistent download details.
119 DownloadDetailsGetter download_details_getter_
;
121 // Caller-supplied callback to be invoked when the most recent download is
123 LastDownloadCallback callback_
;
125 // A mapping of profiles for which a download query is pending to their
126 // respective states.
127 std::map
<Profile
*, ProfileWaitState
> profile_states_
;
129 // Registrar for observing profile lifecycle notifications.
130 content::NotificationRegistrar notification_registrar_
;
132 // The most interesting download details retrieved from download metadata.
133 scoped_ptr
<ClientIncidentReport_DownloadDetails
> details_
;
135 // The most recent download, updated progressively as query results arrive.
136 history::DownloadRow most_recent_row_
;
138 // HistoryServiceObserver
139 ScopedObserver
<HistoryService
, HistoryServiceObserver
>
140 history_service_observer_
;
142 // A factory for asynchronous operations on profiles' HistoryService.
143 base::WeakPtrFactory
<LastDownloadFinder
> weak_ptr_factory_
;
145 DISALLOW_COPY_AND_ASSIGN(LastDownloadFinder
);
148 } // namespace safe_browsing
150 #endif // CHROME_BROWSER_SAFE_BROWSING_INCIDENT_REPORTING_LAST_DOWNLOAD_FINDER_H_