Home | History | Annotate | Download | only in incident_reporting
      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 <vector>
      9 
     10 #include "base/callback.h"
     11 #include "base/compiler_specific.h"
     12 #include "base/macros.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "base/memory/weak_ptr.h"
     15 #include "chrome/browser/history/download_row.h"
     16 #include "content/public/browser/notification_observer.h"
     17 #include "content/public/browser/notification_registrar.h"
     18 
     19 class HistoryService;
     20 class Profile;
     21 
     22 namespace content {
     23 class NotificationDetails;
     24 class NotificationSource;
     25 }
     26 
     27 namespace history {
     28 struct DownloadRow;
     29 }
     30 
     31 namespace safe_browsing {
     32 
     33 class ClientIncidentReport_DownloadDetails;
     34 
     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:
     39   // The type of a callback run by the finder upon completion. The argument is a
     40   // protobuf containing details of the download that was found, or an empty
     41   // pointer if none was found.
     42   typedef base::Callback<void(scoped_ptr<ClientIncidentReport_DownloadDetails>)>
     43       LastDownloadCallback;
     44 
     45   virtual ~LastDownloadFinder();
     46 
     47   // Initiates an asynchronous search for the most recent download. |callback|
     48   // will be run when the search is complete. The returned instance can be
     49   // deleted to terminate the search, in which case |callback| is not invoked.
     50   // Returns NULL without running |callback| if there are no eligible profiles
     51   // to search.
     52   static scoped_ptr<LastDownloadFinder> Create(
     53       const LastDownloadCallback& callback);
     54 
     55  protected:
     56   // Protected constructor so that unit tests can create a fake finder.
     57   LastDownloadFinder();
     58 
     59  private:
     60   LastDownloadFinder(const std::vector<Profile*>& profiles,
     61                      const LastDownloadCallback& callback);
     62 
     63   // Adds |profile| to the set of profiles to be searched if it is an
     64   // on-the-record profile with history that participates in safe browsing. The
     65   // search is initiated if the profile has already loaded history.
     66   void SearchInProfile(Profile* profile);
     67 
     68   // Initiates a search in |profile| if it is in the set of profiles to be
     69   // searched.
     70   void OnProfileHistoryLoaded(Profile* profile,
     71                               HistoryService* history_service);
     72 
     73   // Abandons the search for downloads in |profile|, reporting results if there
     74   // are no more pending queries.
     75   void AbandonSearchInProfile(Profile* profile);
     76 
     77   // HistoryService::DownloadQueryCallback. Retrieves the most recent completed
     78   // executable download from |downloads| and reports results if there are no
     79   // more pending queries.
     80   void OnDownloadQuery(
     81       Profile* profile,
     82       scoped_ptr<std::vector<history::DownloadRow> > downloads);
     83 
     84   // Removes the profile pointed to by |it| from profiles_ and reports results
     85   // if there are no more pending queries.
     86   void RemoveProfileAndReportIfDone(std::vector<Profile*>::iterator it);
     87 
     88   // Invokes the caller-supplied callback with the download found.
     89   void ReportResults();
     90 
     91   // content::NotificationObserver methods.
     92   virtual void Observe(int type,
     93                        const content::NotificationSource& source,
     94                        const content::NotificationDetails& details) OVERRIDE;
     95 
     96   // Caller-supplied callback to be invoked when the most recent download is
     97   // found.
     98   LastDownloadCallback callback_;
     99 
    100   // The profiles for which a download query is pending.
    101   std::vector<Profile*> profiles_;
    102 
    103   // Registrar for observing profile lifecycle notifications.
    104   content::NotificationRegistrar notification_registrar_;
    105 
    106   // The most recent download, updated progressively as query results arrive.
    107   history::DownloadRow most_recent_row_;
    108 
    109   // A factory for asynchronous operations on profiles' HistoryService.
    110   base::WeakPtrFactory<LastDownloadFinder> weak_ptr_factory_;
    111 
    112   DISALLOW_COPY_AND_ASSIGN(LastDownloadFinder);
    113 };
    114 
    115 }  // namespace safe_browsing
    116 
    117 #endif  // CHROME_BROWSER_SAFE_BROWSING_INCIDENT_REPORTING_LAST_DOWNLOAD_FINDER_H_
    118