summaryrefslogtreecommitdiffstats
path: root/chrome/browser/safe_browsing/last_download_finder.h
blob: 4fef70cfb36e61fbbda325868acd9031d1dfaad1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_SAFE_BROWSING_LAST_DOWNLOAD_FINDER_H_
#define CHROME_BROWSER_SAFE_BROWSING_LAST_DOWNLOAD_FINDER_H_

#include <vector>

#include "base/callback.h"
#include "base/compiler_specific.h"
#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/history/download_row.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"

class HistoryService;
class Profile;

namespace content {
class NotificationDetails;
class NotificationSource;
}

namespace history {
struct DownloadRow;
}

namespace safe_browsing {

class ClientIncidentReport_DownloadDetails;

// Finds the most recent executable downloaded by any on-the-record profile with
// history that participates in safe browsing.
class LastDownloadFinder : public content::NotificationObserver {
 public:
  // The type of a callback run by the finder upon completion. The argument is a
  // protobuf containing details of the download that was found, or an empty
  // pointer if none was found.
  typedef base::Callback<void(scoped_ptr<ClientIncidentReport_DownloadDetails>)>
      LastDownloadCallback;

  virtual ~LastDownloadFinder();

  // Initiates an asynchronous search for the most recent download. |callback|
  // will be run when the search is complete. The returned instance can be
  // deleted to terminate the search, in which case |callback| is not invoked.
  // Returns NULL without running |callback| if there are no eligible profiles
  // to search.
  static scoped_ptr<LastDownloadFinder> Create(
      const LastDownloadCallback& callback);

 protected:
  // Protected constructor so that unit tests can create a fake finder.
  LastDownloadFinder();

 private:
  LastDownloadFinder(const std::vector<Profile*>& profiles,
                     const LastDownloadCallback& callback);

  // Adds |profile| to the set of profiles to be searched if it is an
  // on-the-record profile with history that participates in safe browsing. The
  // search is initiated if the profile has already loaded history.
  void SearchInProfile(Profile* profile);

  // Initiates a search in |profile| if it is in the set of profiles to be
  // searched.
  void OnProfileHistoryLoaded(Profile* profile,
                              HistoryService* history_service);

  // Abandons the search for downloads in |profile|, reporting results if there
  // are no more pending queries.
  void AbandonSearchInProfile(Profile* profile);

  // HistoryService::DownloadQueryCallback. Retrieves the most recent completed
  // executable download from |downloads| and reports results if there are no
  // more pending queries.
  void OnDownloadQuery(
      Profile* profile,
      scoped_ptr<std::vector<history::DownloadRow> > downloads);

  // Removes the profile pointed to by |it| from profiles_ and reports results
  // if there are no more pending queries.
  void RemoveProfileAndReportIfDone(std::vector<Profile*>::iterator it);

  // Invokes the caller-supplied callback with the download found.
  void ReportResults();

  // content::NotificationObserver methods.
  virtual void Observe(int type,
                       const content::NotificationSource& source,
                       const content::NotificationDetails& details) OVERRIDE;

  // Caller-supplied callback to be invoked when the most recent download is
  // found.
  LastDownloadCallback callback_;

  // The profiles for which a download query is pending.
  std::vector<Profile*> profiles_;

  // Registrar for observing profile lifecycle notifications.
  content::NotificationRegistrar notification_registrar_;

  // The most recent download, updated progressively as query results arrive.
  history::DownloadRow most_recent_row_;

  // A factory for asynchronous operations on profiles' HistoryService.
  base::WeakPtrFactory<LastDownloadFinder> weak_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(LastDownloadFinder);
};

}  // namespace safe_browsing

#endif  // CHROME_BROWSER_SAFE_BROWSING_LAST_DOWNLOAD_FINDER_H_