Home | History | Annotate | Download | only in safe_browsing
      1 // Copyright (c) 2012 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 // The Safe Browsing service is responsible for downloading anti-phishing and
      6 // anti-malware tables and checking urls against them.
      7 
      8 #ifndef CHROME_BROWSER_SAFE_BROWSING_UI_MANAGER_H_
      9 #define CHROME_BROWSER_SAFE_BROWSING_UI_MANAGER_H_
     10 
     11 #include <string>
     12 #include <vector>
     13 
     14 #include "base/callback.h"
     15 #include "base/memory/ref_counted.h"
     16 #include "base/memory/scoped_ptr.h"
     17 #include "base/observer_list.h"
     18 #include "base/time/time.h"
     19 #include "chrome/browser/safe_browsing/safe_browsing_util.h"
     20 #include "content/public/browser/notification_observer.h"
     21 #include "url/gurl.h"
     22 
     23 class SafeBrowsingService;
     24 
     25 namespace base {
     26 class Thread;
     27 }
     28 
     29 // Construction needs to happen on the main thread.
     30 class SafeBrowsingUIManager
     31     : public base::RefCountedThreadSafe<SafeBrowsingUIManager> {
     32  public:
     33   // Passed a boolean indicating whether or not it is OK to proceed with
     34   // loading an URL.
     35   typedef base::Callback<void(bool /*proceed*/)> UrlCheckCallback;
     36 
     37   // Structure used to pass parameters between the IO and UI thread when
     38   // interacting with the blocking page.
     39   struct UnsafeResource {
     40     UnsafeResource();
     41     ~UnsafeResource();
     42 
     43     GURL url;
     44     GURL original_url;
     45     std::vector<GURL> redirect_urls;
     46     bool is_subresource;
     47     bool is_subframe;
     48     SBThreatType threat_type;
     49     std::string threat_metadata;
     50     UrlCheckCallback callback;  // This is called back on the IO thread.
     51     int render_process_host_id;
     52     int render_view_id;
     53   };
     54 
     55   // Observer class can be used to get notified when a SafeBrowsing hit
     56   // was found.
     57   class Observer {
     58    public:
     59     // The |resource| was classified as unsafe by SafeBrowsing.
     60     // This method will be called every time an unsafe resource is
     61     // loaded, even if it has already been whitelisted by the user.
     62     // The |resource| must not be accessed after OnSafeBrowsingHit returns.
     63     // This method will be called on the UI thread.
     64     virtual void OnSafeBrowsingMatch(const UnsafeResource& resource) = 0;
     65 
     66     // The |resource| was classified as unsafe by SafeBrowsing, and is
     67     // not whitelisted.
     68     // The |resource| must not be accessed after OnSafeBrowsingHit returns.
     69     // This method will be called on the UI thread.
     70     virtual void OnSafeBrowsingHit(const UnsafeResource& resource) = 0;
     71 
     72    protected:
     73     Observer() {}
     74     virtual ~Observer() {}
     75 
     76    private:
     77     DISALLOW_COPY_AND_ASSIGN(Observer);
     78   };
     79 
     80   explicit SafeBrowsingUIManager(
     81       const scoped_refptr<SafeBrowsingService>& service);
     82 
     83   // Called to stop or shutdown operations on the io_thread. This may be called
     84   // multiple times during the life of the UIManager. Should be called
     85   // on IO thread. If shutdown is true, the manager is disabled permanently.
     86   void StopOnIOThread(bool shutdown);
     87 
     88   // Called on UI thread to decide if safe browsing related stats
     89   // could be reported.
     90   virtual bool CanReportStats() const;
     91 
     92   // Called on the UI thread to display an interstitial page.
     93   // |url| is the url of the resource that matches a safe browsing list.
     94   // If the request contained a chain of redirects, |url| is the last url
     95   // in the chain, and |original_url| is the first one (the root of the
     96   // chain). Otherwise, |original_url| = |url|.
     97   virtual void DisplayBlockingPage(const UnsafeResource& resource);
     98 
     99   // Returns true if we already displayed an interstitial for that resource.
    100   // Called on the UI thread.
    101   bool IsWhitelisted(const UnsafeResource& resource);
    102 
    103   // The blocking page on the UI thread has completed.
    104   void OnBlockingPageDone(const std::vector<UnsafeResource>& resources,
    105                           bool proceed);
    106 
    107   // Log the user perceived delay caused by SafeBrowsing. This delay is the time
    108   // delta starting from when we would have started reading data from the
    109   // network, and ending when the SafeBrowsing check completes indicating that
    110   // the current page is 'safe'.
    111   void LogPauseDelay(base::TimeDelta time);
    112 
    113   // Called on the IO thread by the MalwareDetails with the serialized
    114   // protocol buffer, so the service can send it over.
    115   virtual void SendSerializedMalwareDetails(const std::string& serialized);
    116 
    117   // Report hits to the unsafe contents (malware, phishing, unsafe download URL)
    118   // to the server. Can only be called on UI thread.  If |post_data| is
    119   // non-empty, the request will be sent as a POST instead of a GET.
    120   virtual void ReportSafeBrowsingHit(const GURL& malicious_url,
    121                                      const GURL& page_url,
    122                                      const GURL& referrer_url,
    123                                      bool is_subresource,
    124                                      SBThreatType threat_type,
    125                                      const std::string& post_data);
    126 
    127   // Add and remove observers.  These methods must be invoked on the UI thread.
    128   void AddObserver(Observer* observer);
    129   void RemoveObserver(Observer* remove);
    130 
    131  protected:
    132   virtual ~SafeBrowsingUIManager();
    133 
    134  private:
    135   friend class base::RefCountedThreadSafe<SafeBrowsingUIManager>;
    136 
    137   // Used for whitelisting a render view when the user ignores our warning.
    138   struct WhiteListedEntry;
    139 
    140   // Call protocol manager on IO thread to report hits of unsafe contents.
    141   void ReportSafeBrowsingHitOnIOThread(const GURL& malicious_url,
    142                                        const GURL& page_url,
    143                                        const GURL& referrer_url,
    144                                        bool is_subresource,
    145                                        SBThreatType threat_type,
    146                                        const std::string& post_data);
    147 
    148   // Adds the given entry to the whitelist.  Called on the UI thread.
    149   void UpdateWhitelist(const UnsafeResource& resource);
    150 
    151   // Safebrowsing service.
    152   scoped_refptr<SafeBrowsingService> sb_service_;
    153 
    154   // Only access this whitelist from the UI thread.
    155   std::vector<WhiteListedEntry> white_listed_entries_;
    156 
    157   ObserverList<Observer> observer_list_;
    158 
    159   DISALLOW_COPY_AND_ASSIGN(SafeBrowsingUIManager);
    160 };
    161 
    162 #endif  // CHROME_BROWSER_SAFE_BROWSING_UI_MANAGER_H_
    163