Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / safe_browsing / incident_reporting / last_download_finder.h
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.
4
5 #ifndef CHROME_BROWSER_SAFE_BROWSING_INCIDENT_REPORTING_LAST_DOWNLOAD_FINDER_H_
6 #define CHROME_BROWSER_SAFE_BROWSING_INCIDENT_REPORTING_LAST_DOWNLOAD_FINDER_H_
7
8 #include <map>
9 #include <vector>
10
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"
20
21 class HistoryService;
22 class Profile;
23
24 namespace content {
25 class NotificationDetails;
26 class NotificationSource;
27 }
28
29 namespace history {
30 struct DownloadRow;
31 }
32
33 namespace safe_browsing {
34
35 class ClientIncidentReport_DownloadDetails;
36
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 {
40  public:
41   typedef base::Callback<void(
42       content::BrowserContext* context,
43       const DownloadMetadataManager::GetDownloadDetailsCallback&)>
44       DownloadDetailsGetter;
45
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>)>
50       LastDownloadCallback;
51
52   ~LastDownloadFinder() override;
53
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
58   // to search.
59   static scoped_ptr<LastDownloadFinder> Create(
60       const DownloadDetailsGetter& download_details_getter,
61       const LastDownloadCallback& callback);
62
63  protected:
64   // Protected constructor so that unit tests can create a fake finder.
65   LastDownloadFinder();
66
67  private:
68   enum ProfileWaitState {
69     WAITING_FOR_METADATA,
70     WAITING_FOR_HISTORY,
71   };
72
73   LastDownloadFinder(const DownloadDetailsGetter& download_details_getter,
74                      const std::vector<Profile*>& profiles,
75                      const LastDownloadCallback& callback);
76
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);
81
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
85   // queries.
86   void OnMetadataQuery(
87       Profile* profile,
88       scoped_ptr<ClientIncidentReport_DownloadDetails> details);
89
90   // Initiates a search in |profile| if it is in the set of profiles to be
91   // searched.
92   void OnProfileHistoryLoaded(Profile* profile,
93                               HistoryService* history_service);
94
95   // Abandons the search for downloads in |profile|, reporting results if there
96   // are no more pending queries.
97   void AbandonSearchInProfile(Profile* profile);
98
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(
103       Profile* profile,
104       scoped_ptr<std::vector<history::DownloadRow> > downloads);
105
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);
110
111   // Invokes the caller-supplied callback with the download found.
112   void ReportResults();
113
114   // content::NotificationObserver methods.
115   void Observe(int type,
116                const content::NotificationSource& source,
117                const content::NotificationDetails& details) override;
118
119   // Caller-supplied callback to make an asynchronous request for a profile's
120   // persistent download details.
121   DownloadDetailsGetter download_details_getter_;
122
123   // Caller-supplied callback to be invoked when the most recent download is
124   // found.
125   LastDownloadCallback callback_;
126
127   // A mapping of profiles for which a download query is pending to their
128   // respective states.
129   std::map<Profile*, ProfileWaitState> profile_states_;
130
131   // Registrar for observing profile lifecycle notifications.
132   content::NotificationRegistrar notification_registrar_;
133
134   // The most interesting download details retrieved from download metadata.
135   scoped_ptr<ClientIncidentReport_DownloadDetails> details_;
136
137   // The most recent download, updated progressively as query results arrive.
138   history::DownloadRow most_recent_row_;
139
140   // A factory for asynchronous operations on profiles' HistoryService.
141   base::WeakPtrFactory<LastDownloadFinder> weak_ptr_factory_;
142
143   DISALLOW_COPY_AND_ASSIGN(LastDownloadFinder);
144 };
145
146 }  // namespace safe_browsing
147
148 #endif  // CHROME_BROWSER_SAFE_BROWSING_INCIDENT_REPORTING_LAST_DOWNLOAD_FINDER_H_