Home | History | Annotate | Download | only in download
      1 // Copyright (c) 2011 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_DOWNLOAD_DOWNLOAD_SAFE_BROWSING_CLIENT_H_
      6 #define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_SAFE_BROWSING_CLIENT_H_
      7 #pragma once
      8 
      9 #include "base/callback.h"
     10 #include "base/memory/ref_counted.h"
     11 #include "base/time.h"
     12 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
     13 
     14 struct DownloadCreateInfo;
     15 
     16 // This is a helper class used by DownloadManager to check a download URL with
     17 // SafeBrowsingService. The client is refcounted and will be  released once
     18 // there is no reference to it.
     19 // Usage:
     20 // {
     21 //    scoped_refptr<DownloadSBClient> client_ = new DownloadSBClient(...);
     22 //    client_->CheckDownloadUrl(..., NewCallback(this,
     23 //                              &DownloadManager::UrlCallBack));
     24 //    or
     25 //    client_->CheckDownloadHash(..., NewCallback(this,
     26 //                               &DownloadManager::HashCallBack));
     27 // }
     28 // DownloadManager::UrlCallBack(...) or HashCallCall {
     29 //    // After this, the |client_| is gone.
     30 // }
     31 class DownloadSBClient
     32     : public SafeBrowsingService::Client,
     33       public base::RefCountedThreadSafe<DownloadSBClient> {
     34  public:
     35   typedef Callback2<DownloadCreateInfo*, bool>::Type UrlDoneCallback;
     36   typedef Callback2<int32, bool>::Type HashDoneCallback;
     37 
     38   DownloadSBClient(int32 download_id,
     39                    const std::vector<GURL>& url_chain,
     40                    const GURL& referrer_url);
     41 
     42   // Call safebrowsing service to verifiy the download.
     43   // For each DownloadSBClient instance, either CheckDownloadUrl or
     44   // CheckDownloadHash can be called, and be called only once.
     45   // DownloadSBClient instance.
     46   void CheckDownloadUrl(DownloadCreateInfo* info, UrlDoneCallback* callback);
     47   void CheckDownloadHash(const std::string& hash, HashDoneCallback* callback);
     48 
     49  private:
     50   // Call SafeBrowsingService on IO thread to verify the download URL or
     51   // hash of downloaded file.
     52   void CheckDownloadUrlOnIOThread(const std::vector<GURL>& url_chain);
     53   void CheckDownloadHashOnIOThread(const std::string& hash);
     54 
     55   // Callback interfaces for SafeBrowsingService::Client.
     56   virtual void OnDownloadUrlCheckResult(
     57       const std::vector<GURL>& url_chain,
     58       SafeBrowsingService::UrlCheckResult result);
     59   virtual void OnDownloadHashCheckResult(
     60       const std::string& hash, SafeBrowsingService::UrlCheckResult result);
     61 
     62   // Enumerate for histogramming purposes.
     63   // DO NOT CHANGE THE ORDERING OF THESE VALUES (different histogram data will
     64   // be mixed together based on their values).
     65   enum SBStatsType {
     66     DOWNLOAD_URL_CHECKS_TOTAL,
     67     DOWNLOAD_URL_CHECKS_CANCELED,
     68     DOWNLOAD_URL_CHECKS_MALWARE,
     69 
     70     DOWNLOAD_HASH_CHECKS_TOTAL,
     71     DOWNLOAD_HASH_CHECKS_MALWARE,
     72 
     73     // Memory space for histograms is determined by the max.
     74     // ALWAYS ADD NEW VALUES BEFORE THIS ONE.
     75     DOWNLOAD_CHECKS_MAX
     76   };
     77 
     78   friend class base::RefCountedThreadSafe<DownloadSBClient>;
     79   virtual ~DownloadSBClient();
     80 
     81   // Call DownloadManager on UI thread for download URL or hash check.
     82   void SafeBrowsingCheckUrlDone(SafeBrowsingService::UrlCheckResult result);
     83   void SafeBrowsingCheckHashDone(SafeBrowsingService::UrlCheckResult result);
     84 
     85   // Report malware hits to safebrowsing service.
     86   void ReportMalware(SafeBrowsingService::UrlCheckResult result);
     87 
     88   // Update the UMA stats.
     89   void UpdateDownloadCheckStats(SBStatsType stat_type);
     90 
     91   scoped_ptr<UrlDoneCallback> url_done_callback_;
     92   scoped_ptr<HashDoneCallback> hash_done_callback_;
     93 
     94   // Not owned by this class.
     95   DownloadCreateInfo* info_;
     96 
     97   int32 download_id_;
     98   scoped_refptr<SafeBrowsingService> sb_service_;
     99 
    100   // These URLs are used to report malware to safe browsing service.
    101   std::vector<GURL> url_chain_;
    102   GURL referrer_url_;
    103 
    104   // When a safebrowsing check starts, for stats purpose.
    105   base::TimeTicks start_time_;
    106 
    107   DISALLOW_COPY_AND_ASSIGN(DownloadSBClient);
    108 };
    109 
    110 #endif  // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_SAFE_BROWSING_CLIENT_H_
    111