1116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Copyright 2014 The Chromium Authors. All rights reserved.
2116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Use of this source code is governed by a BSD-style license that can be
3116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// found in the LICENSE file.
4116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
56e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#ifndef CHROME_BROWSER_SAFE_BROWSING_INCIDENT_REPORTING_LAST_DOWNLOAD_FINDER_H_
66e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#define CHROME_BROWSER_SAFE_BROWSING_INCIDENT_REPORTING_LAST_DOWNLOAD_FINDER_H_
7116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
8116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include <vector>
9116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
10116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/callback.h"
11116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/compiler_specific.h"
12116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/macros.h"
13116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/memory/scoped_ptr.h"
14116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/memory/weak_ptr.h"
15116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chrome/browser/history/download_row.h"
16116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "content/public/browser/notification_observer.h"
17116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "content/public/browser/notification_registrar.h"
18116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
19116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass HistoryService;
20116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass Profile;
21116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
22116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace content {
23116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass NotificationDetails;
24116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass NotificationSource;
25116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
26116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
27116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace history {
28116680a4aac90f2aa7413d9095a592090648e557Ben Murdochstruct DownloadRow;
29116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
30116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
31116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace safe_browsing {
32116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
33116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass ClientIncidentReport_DownloadDetails;
34116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
35116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Finds the most recent executable downloaded by any on-the-record profile with
36116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// history that participates in safe browsing.
37116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass LastDownloadFinder : public content::NotificationObserver {
38116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch public:
39116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // The type of a callback run by the finder upon completion. The argument is a
40116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // protobuf containing details of the download that was found, or an empty
41116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // pointer if none was found.
42116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  typedef base::Callback<void(scoped_ptr<ClientIncidentReport_DownloadDetails>)>
43116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      LastDownloadCallback;
44116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
45116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  virtual ~LastDownloadFinder();
46116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
47116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Initiates an asynchronous search for the most recent download. |callback|
48116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // will be run when the search is complete. The returned instance can be
49116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // deleted to terminate the search, in which case |callback| is not invoked.
50116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Returns NULL without running |callback| if there are no eligible profiles
51116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // to search.
52116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  static scoped_ptr<LastDownloadFinder> Create(
53116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      const LastDownloadCallback& callback);
54116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
55116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch protected:
56116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Protected constructor so that unit tests can create a fake finder.
57116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  LastDownloadFinder();
58116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
59116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch private:
60116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  LastDownloadFinder(const std::vector<Profile*>& profiles,
61116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                     const LastDownloadCallback& callback);
62116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
63116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Adds |profile| to the set of profiles to be searched if it is an
64116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // on-the-record profile with history that participates in safe browsing. The
65116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // search is initiated if the profile has already loaded history.
66116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void SearchInProfile(Profile* profile);
67116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
68116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Initiates a search in |profile| if it is in the set of profiles to be
69116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // searched.
70116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void OnProfileHistoryLoaded(Profile* profile,
71116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                              HistoryService* history_service);
72116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
73116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Abandons the search for downloads in |profile|, reporting results if there
74116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // are no more pending queries.
75116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void AbandonSearchInProfile(Profile* profile);
76116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
77116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // HistoryService::DownloadQueryCallback. Retrieves the most recent completed
78116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // executable download from |downloads| and reports results if there are no
79116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // more pending queries.
80116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void OnDownloadQuery(
81116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      Profile* profile,
82116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      scoped_ptr<std::vector<history::DownloadRow> > downloads);
83116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
84116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Removes the profile pointed to by |it| from profiles_ and reports results
85116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // if there are no more pending queries.
86116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void RemoveProfileAndReportIfDone(std::vector<Profile*>::iterator it);
87116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
88116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Invokes the caller-supplied callback with the download found.
89116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void ReportResults();
90116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
91116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // content::NotificationObserver methods.
92116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  virtual void Observe(int type,
93116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                       const content::NotificationSource& source,
94116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                       const content::NotificationDetails& details) OVERRIDE;
95116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
96116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Caller-supplied callback to be invoked when the most recent download is
97116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // found.
98116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  LastDownloadCallback callback_;
99116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
100116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // The profiles for which a download query is pending.
101116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  std::vector<Profile*> profiles_;
102116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
103116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Registrar for observing profile lifecycle notifications.
104116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  content::NotificationRegistrar notification_registrar_;
105116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
106116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // The most recent download, updated progressively as query results arrive.
107116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  history::DownloadRow most_recent_row_;
108116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
109116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // A factory for asynchronous operations on profiles' HistoryService.
110116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  base::WeakPtrFactory<LastDownloadFinder> weak_ptr_factory_;
111116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
112116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  DISALLOW_COPY_AND_ASSIGN(LastDownloadFinder);
113116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch};
114116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
115116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}  // namespace safe_browsing
116116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
1176e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#endif  // CHROME_BROWSER_SAFE_BROWSING_INCIDENT_REPORTING_LAST_DOWNLOAD_FINDER_H_
118