Refactor WebsiteSettings to operate on a SecurityInfo
[chromium-blink-merge.git] / chrome / browser / safe_browsing / incident_reporting / last_download_finder.h
blob588b04c826a0a388ddc7b5c090ed762781113947
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_
8 #include <map>
9 #include <vector>
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"
23 class Profile;
25 namespace content {
26 class NotificationDetails;
27 class NotificationSource;
30 namespace history {
31 class HistoryService;
34 namespace safe_browsing {
36 class ClientIncidentReport_DownloadDetails;
38 // Finds the most recent executable downloaded by any on-the-record profile with
39 // history that participates in safe browsing.
40 class LastDownloadFinder : public content::NotificationObserver,
41 public history::HistoryServiceObserver {
42 public:
43 typedef base::Callback<void(
44 content::BrowserContext* context,
45 const DownloadMetadataManager::GetDownloadDetailsCallback&)>
46 DownloadDetailsGetter;
48 // The type of a callback run by the finder upon completion. The argument is a
49 // protobuf containing details of the download that was found, or an empty
50 // pointer if none was found.
51 typedef base::Callback<void(scoped_ptr<ClientIncidentReport_DownloadDetails>)>
52 LastDownloadCallback;
54 ~LastDownloadFinder() override;
56 // Initiates an asynchronous search for the most recent download. |callback|
57 // will be run when the search is complete. The returned instance can be
58 // deleted to terminate the search, in which case |callback| is not invoked.
59 // Returns NULL without running |callback| if there are no eligible profiles
60 // to search.
61 static scoped_ptr<LastDownloadFinder> Create(
62 const DownloadDetailsGetter& download_details_getter,
63 const LastDownloadCallback& callback);
65 protected:
66 // Protected constructor so that unit tests can create a fake finder.
67 LastDownloadFinder();
69 private:
70 enum ProfileWaitState {
71 WAITING_FOR_METADATA,
72 WAITING_FOR_HISTORY,
75 LastDownloadFinder(const DownloadDetailsGetter& download_details_getter,
76 const std::vector<Profile*>& profiles,
77 const LastDownloadCallback& callback);
79 // Adds |profile| to the set of profiles to be searched if it is an
80 // on-the-record profile with history that participates in safe browsing. A
81 // search for metadata is initiated immediately.
82 void SearchInProfile(Profile* profile);
84 // DownloadMetadataManager::GetDownloadDetailsCallback. If |details| are
85 // provided, retrieves them if they are the most relevant results. Otherwise
86 // begins a search in history. Reports results if there are no more pending
87 // queries.
88 void OnMetadataQuery(
89 Profile* profile,
90 scoped_ptr<ClientIncidentReport_DownloadDetails> details);
92 // Abandons the search for downloads in |profile|, reporting results if there
93 // are no more pending queries.
94 void AbandonSearchInProfile(Profile* profile);
96 // HistoryService::DownloadQueryCallback. Retrieves the most recent completed
97 // executable download from |downloads| and reports results if there are no
98 // more pending queries.
99 void OnDownloadQuery(
100 Profile* profile,
101 scoped_ptr<std::vector<history::DownloadRow> > downloads);
103 // Removes the profile pointed to by |it| from profile_states_ and reports
104 // results if there are no more pending queries.
105 void RemoveProfileAndReportIfDone(
106 std::map<Profile*, ProfileWaitState>::iterator iter);
108 // Invokes the caller-supplied callback with the download found.
109 void ReportResults();
111 // content::NotificationObserver methods.
112 void Observe(int type,
113 const content::NotificationSource& source,
114 const content::NotificationDetails& details) override;
116 // history::HistoryServiceObserver:
117 void OnHistoryServiceLoaded(history::HistoryService* service) override;
118 void HistoryServiceBeingDeleted(
119 history::HistoryService* history_service) override;
121 // Caller-supplied callback to make an asynchronous request for a profile's
122 // persistent download details.
123 DownloadDetailsGetter download_details_getter_;
125 // Caller-supplied callback to be invoked when the most recent download is
126 // found.
127 LastDownloadCallback callback_;
129 // A mapping of profiles for which a download query is pending to their
130 // respective states.
131 std::map<Profile*, ProfileWaitState> profile_states_;
133 // Registrar for observing profile lifecycle notifications.
134 content::NotificationRegistrar notification_registrar_;
136 // The most interesting download details retrieved from download metadata.
137 scoped_ptr<ClientIncidentReport_DownloadDetails> details_;
139 // The most recent download, updated progressively as query results arrive.
140 history::DownloadRow most_recent_row_;
142 // HistoryServiceObserver
143 ScopedObserver<history::HistoryService, history::HistoryServiceObserver>
144 history_service_observer_;
146 // A factory for asynchronous operations on profiles' HistoryService.
147 base::WeakPtrFactory<LastDownloadFinder> weak_ptr_factory_;
149 DISALLOW_COPY_AND_ASSIGN(LastDownloadFinder);
152 } // namespace safe_browsing
154 #endif // CHROME_BROWSER_SAFE_BROWSING_INCIDENT_REPORTING_LAST_DOWNLOAD_FINDER_H_