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_SAFE_BROWSING_SERVICE_H_
      9 #define CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_SERVICE_H_
     10 
     11 #include <map>
     12 #include <string>
     13 
     14 #include "base/callback.h"
     15 #include "base/files/file_path.h"
     16 #include "base/memory/ref_counted.h"
     17 #include "base/memory/scoped_ptr.h"
     18 #include "base/observer_list.h"
     19 #include "base/sequenced_task_runner_helpers.h"
     20 #include "chrome/browser/safe_browsing/safe_browsing_util.h"
     21 #include "content/public/browser/browser_thread.h"
     22 #include "content/public/browser/notification_observer.h"
     23 #include "content/public/browser/notification_registrar.h"
     24 
     25 class PrefChangeRegistrar;
     26 class PrefService;
     27 class Profile;
     28 struct SafeBrowsingProtocolConfig;
     29 class SafeBrowsingDatabaseManager;
     30 class SafeBrowsingPingManager;
     31 class SafeBrowsingProtocolManager;
     32 class SafeBrowsingServiceFactory;
     33 class SafeBrowsingUIManager;
     34 class SafeBrowsingURLRequestContextGetter;
     35 class TrackedPreferenceValidationDelegate;
     36 
     37 namespace base {
     38 class Thread;
     39 }
     40 
     41 namespace net {
     42 class URLRequestContext;
     43 class URLRequestContextGetter;
     44 }
     45 
     46 namespace safe_browsing {
     47 class ClientSideDetectionService;
     48 class DownloadProtectionService;
     49 class IncidentReportingService;
     50 }
     51 
     52 // Construction needs to happen on the main thread.
     53 // The SafeBrowsingService owns both the UI and Database managers which do
     54 // the heavylifting of safebrowsing service. Both of these managers stay
     55 // alive until SafeBrowsingService is destroyed, however, they are disabled
     56 // permanently when Shutdown method is called.
     57 class SafeBrowsingService
     58     : public base::RefCountedThreadSafe<
     59           SafeBrowsingService,
     60           content::BrowserThread::DeleteOnUIThread>,
     61       public content::NotificationObserver {
     62  public:
     63   // Makes the passed |factory| the factory used to instanciate
     64   // a SafeBrowsingService. Useful for tests.
     65   static void RegisterFactory(SafeBrowsingServiceFactory* factory) {
     66     factory_ = factory;
     67   }
     68 
     69   static base::FilePath GetCookieFilePathForTesting();
     70 
     71   static base::FilePath GetBaseFilename();
     72 
     73   // Create an instance of the safe browsing service.
     74   static SafeBrowsingService* CreateSafeBrowsingService();
     75 
     76 #if defined(OS_ANDROID) && defined(FULL_SAFE_BROWSING)
     77   // Return whether the user is in mobile safe browsing
     78   // field trial enabled group.
     79   static bool IsEnabledByFieldTrial();
     80 #endif
     81 
     82   // Called on the UI thread to initialize the service.
     83   void Initialize();
     84 
     85   // Called on the main thread to let us know that the io_thread is going away.
     86   void ShutDown();
     87 
     88   // Called on UI thread to decide if the download file's sha256 hash
     89   // should be calculated for safebrowsing.
     90   bool DownloadBinHashNeeded() const;
     91 
     92   // Create a protocol config struct.
     93   virtual SafeBrowsingProtocolConfig GetProtocolConfig() const;
     94 
     95   bool enabled() const { return enabled_; }
     96 
     97   safe_browsing::ClientSideDetectionService*
     98       safe_browsing_detection_service() const {
     99     DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
    100     return csd_service_.get();
    101   }
    102 
    103   // The DownloadProtectionService is not valid after the SafeBrowsingService
    104   // is destroyed.
    105   safe_browsing::DownloadProtectionService*
    106       download_protection_service() const {
    107     DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
    108     return download_service_.get();
    109   }
    110 
    111   net::URLRequestContextGetter* url_request_context();
    112 
    113   const scoped_refptr<SafeBrowsingUIManager>& ui_manager() const;
    114 
    115   const scoped_refptr<SafeBrowsingDatabaseManager>& database_manager() const;
    116 
    117   SafeBrowsingProtocolManager* protocol_manager() const;
    118 
    119   SafeBrowsingPingManager* ping_manager() const;
    120 
    121   // Returns a preference validation delegate that adds incidents to the
    122   // incident reporting service for validation failures. Returns NULL if the
    123   // service is not applicable for the given profile.
    124   scoped_ptr<TrackedPreferenceValidationDelegate>
    125       CreatePreferenceValidationDelegate(Profile* profile) const;
    126 
    127  protected:
    128   // Creates the safe browsing service.  Need to initialize before using.
    129   SafeBrowsingService();
    130 
    131   virtual ~SafeBrowsingService();
    132 
    133   virtual SafeBrowsingDatabaseManager* CreateDatabaseManager();
    134 
    135   virtual SafeBrowsingUIManager* CreateUIManager();
    136 
    137  private:
    138   friend class SafeBrowsingServiceFactoryImpl;
    139   friend struct content::BrowserThread::DeleteOnThread<
    140       content::BrowserThread::UI>;
    141   friend class base::DeleteHelper<SafeBrowsingService>;
    142   friend class SafeBrowsingServerTest;
    143   friend class SafeBrowsingServiceTest;
    144   friend class SafeBrowsingURLRequestContextGetter;
    145 
    146   void InitURLRequestContextOnIOThread(
    147       net::URLRequestContextGetter* system_url_request_context_getter);
    148 
    149   void DestroyURLRequestContextOnIOThread();
    150 
    151   // Called to initialize objects that are used on the io_thread.  This may be
    152   // called multiple times during the life of the SafeBrowsingService.
    153   void StartOnIOThread(
    154       net::URLRequestContextGetter* url_request_context_getter);
    155 
    156   // Called to stop or shutdown operations on the io_thread. This may be called
    157   // multiple times to stop during the life of the SafeBrowsingService. If
    158   // shutdown is true, then the operations on the io thread are shutdown
    159   // permanently and cannot be restarted.
    160   void StopOnIOThread(bool shutdown);
    161 
    162   // Start up SafeBrowsing objects. This can be called at browser start, or when
    163   // the user checks the "Enable SafeBrowsing" option in the Advanced options
    164   // UI.
    165   void Start();
    166 
    167   // Stops the SafeBrowsingService. This can be called when the safe browsing
    168   // preference is disabled. When shutdown is true, operation is permanently
    169   // shutdown and cannot be restarted.
    170   void Stop(bool shutdown);
    171 
    172   // content::NotificationObserver override
    173   virtual void Observe(int type,
    174                        const content::NotificationSource& source,
    175                        const content::NotificationDetails& details) OVERRIDE;
    176 
    177   // Starts following the safe browsing preference on |pref_service|.
    178   void AddPrefService(PrefService* pref_service);
    179 
    180   // Stop following the safe browsing preference on |pref_service|.
    181   void RemovePrefService(PrefService* pref_service);
    182 
    183   // Checks if any profile is currently using the safe browsing service, and
    184   // starts or stops the service accordingly.
    185   void RefreshState();
    186 
    187   // The factory used to instanciate a SafeBrowsingService object.
    188   // Useful for tests, so they can provide their own implementation of
    189   // SafeBrowsingService.
    190   static SafeBrowsingServiceFactory* factory_;
    191 
    192   // The SafeBrowsingURLRequestContextGetter used to access
    193   // |url_request_context_|. Accessed on UI thread.
    194   scoped_refptr<net::URLRequestContextGetter>
    195       url_request_context_getter_;
    196 
    197   // The SafeBrowsingURLRequestContext. Accessed on IO thread.
    198   scoped_ptr<net::URLRequestContext> url_request_context_;
    199 
    200   // Handles interaction with SafeBrowsing servers. Accessed on IO thread.
    201   SafeBrowsingProtocolManager* protocol_manager_;
    202 
    203   // Provides phishing and malware statistics. Accessed on IO thread.
    204   SafeBrowsingPingManager* ping_manager_;
    205 
    206   // Whether the service is running. 'enabled_' is used by SafeBrowsingService
    207   // on the IO thread during normal operations.
    208   bool enabled_;
    209 
    210   // Tracks existing PrefServices, and the safe browsing preference on each.
    211   // This is used to determine if any profile is currently using the safe
    212   // browsing service, and to start it up or shut it down accordingly.
    213   // Accessed on UI thread.
    214   std::map<PrefService*, PrefChangeRegistrar*> prefs_map_;
    215 
    216   // Used to track creation and destruction of profiles on the UI thread.
    217   content::NotificationRegistrar prefs_registrar_;
    218 
    219   // The ClientSideDetectionService is managed by the SafeBrowsingService,
    220   // since its running state and lifecycle depends on SafeBrowsingService's.
    221   // Accessed on UI thread.
    222   scoped_ptr<safe_browsing::ClientSideDetectionService> csd_service_;
    223 
    224   // The DownloadProtectionService is managed by the SafeBrowsingService,
    225   // since its running state and lifecycle depends on SafeBrowsingService's.
    226   // Accessed on UI thread.
    227   scoped_ptr<safe_browsing::DownloadProtectionService> download_service_;
    228 
    229   scoped_ptr<safe_browsing::IncidentReportingService> incident_service_;
    230 
    231   // The UI manager handles showing interstitials.  Accessed on both UI and IO
    232   // thread.
    233   scoped_refptr<SafeBrowsingUIManager> ui_manager_;
    234 
    235   // The database manager handles the database and download logic.  Accessed on
    236   // both UI and IO thread.
    237   scoped_refptr<SafeBrowsingDatabaseManager> database_manager_;
    238 
    239   DISALLOW_COPY_AND_ASSIGN(SafeBrowsingService);
    240 };
    241 
    242 // Factory for creating SafeBrowsingService.  Useful for tests.
    243 class SafeBrowsingServiceFactory {
    244  public:
    245   SafeBrowsingServiceFactory() { }
    246   virtual ~SafeBrowsingServiceFactory() { }
    247   virtual SafeBrowsingService* CreateSafeBrowsingService() = 0;
    248  private:
    249   DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceFactory);
    250 };
    251 
    252 #endif  // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_SERVICE_H_
    253